Skip to content

Commit

Permalink
Added linting rules to ruff check and formatting with ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
davorrunje committed Jan 9, 2025
1 parent 325c488 commit 8fd96bf
Show file tree
Hide file tree
Showing 69 changed files with 740 additions and 707 deletions.
23 changes: 9 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ repos:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: no-commit-to-branch
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.8
- repo: local
hooks:
- id: ruff
types_or: [ python, pyi, jupyter ]
args: ["--fix", "--ignore=E402"]
exclude: notebook/agentchat_databricks_dbrx.ipynb
- id: lint
name: linting and formatting
entry: "scripts/pre-commit-lint.sh"
language: python
# language_version: python3.9
types: [python]
require_serial: true
verbose: true
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
hooks:
Expand Down Expand Up @@ -80,7 +79,3 @@ repos:
notebook/.* |
website/.*
)$
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.8.5
hooks:
- id: nbqa-black
2 changes: 1 addition & 1 deletion autogen/agentchat/contrib/capabilities/generate_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import re
from typing import Any, Dict, List, Literal, Optional, Protocol, Tuple, Union

from openai import OpenAI
from PIL.Image import Image
from openai import OpenAI

from autogen import Agent, ConversableAgent, code_utils
from autogen.agentchat.contrib import img_utils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def process_last_received_message(self, content: Union[str, list[dict]]) -> str:
```python
content = [
{"type": "text", "text": "What's weather in this cool photo:"},
{"type": "image_url", "image_url": {"url": "http://example.com/photo.jpg"}}
{"type": "image_url", "image_url": {"url": "http://example.com/photo.jpg"}},
]
```
Output: "What's weather in this cool photo: `<img http://example.com/photo.jpg>` in case you can not see, the caption of this image is:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def count_distinct_permutations(sequence):
int: The number of distinct permutations.
Example:
>>> count_distinct_permutations('aab')
>>> count_distinct_permutations("aab")
3
>>> count_distinct_permutations([1, 2, 2])
3
Expand Down
2 changes: 1 addition & 1 deletion autogen/agentchat/contrib/img_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def num_tokens_from_gpt_image(
Examples:
--------
>>> from PIL import Image
>>> img = Image.new('RGB', (2500, 2500), color = 'red')
>>> img = Image.new("RGB", (2500, 2500), color="red")
>>> num_tokens_from_gpt_image(img, model="gpt-4-vision")
765
"""
Expand Down
4 changes: 2 additions & 2 deletions autogen/agentchat/contrib/reasoning_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@


class ThinkNode:

def __init__(self, content: str, parent: Optional["ThinkNode"] = None) -> None:
"""A node in a tree structure representing a step in the reasoning process.
Expand Down Expand Up @@ -623,7 +622,8 @@ def _mtcs_reply(self, prompt, ground_truth=""):
# More intensive analysis is needed in the future.
choices_weights = [
# exploitation term +
(child.value / (child.visits + EPSILON)) +
(child.value / (child.visits + EPSILON))
+
# exploration term
self._exploration_constant
* math.sqrt(2 * math.log(node.visits + EPSILON) / (child.visits + EPSILON))
Expand Down
5 changes: 0 additions & 5 deletions autogen/agentchat/contrib/swarm_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,6 @@ def register_update_agent_state_before_reply(self, functions: Optional[Union[lis

for func in functions:
if isinstance(func, UPDATE_SYSTEM_MESSAGE):

# Wrapper function that allows this to be used in the update_agent_state hook
# Its primary purpose, however, is just to update the agent's system message
# Outer function to create a closure with the update function
Expand Down Expand Up @@ -732,7 +731,6 @@ def transfer_to_agent_name() -> SwarmAgent:
), "Invalid After Work value"
self.after_work = transit
elif isinstance(transit, ON_CONDITION):

if isinstance(transit.target, SwarmAgent):
# Transition to agent

Expand Down Expand Up @@ -809,15 +807,13 @@ def generate_swarm_tool_reply(

message = messages[-1]
if "tool_calls" in message:

tool_call_count = len(message["tool_calls"])

# Loop through tool calls individually (so context can be updated after each function call)
next_agent = None
tool_responses_inner = []
contents = []
for index in range(tool_call_count):

# Deep copy to ensure no changes to messages when we insert the context variables
message_copy = copy.deepcopy(message)

Expand All @@ -834,7 +830,6 @@ def generate_swarm_tool_reply(
# Inject the context variables into the tool call if it has the parameter
sig = signature(func)
if __CONTEXT_VARIABLES_PARAM_NAME__ in sig.parameters:

current_args = json.loads(tool_call["function"]["arguments"])
current_args[__CONTEXT_VARIABLES_PARAM_NAME__] = self._context_variables
tool_call["function"]["arguments"] = json.dumps(current_args)
Expand Down
6 changes: 3 additions & 3 deletions autogen/agentchat/contrib/tool_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class LocalExecutorWithTools(CodeExecutor):
ag2_tool = interop.convert_tool(tool=langchain_tool, type="langchain")
# `ag2_tool.name` is wikipedia
local_executor = LocalExecutorWithTools(tools=[ag2_tool], work_dir='./')
local_executor = LocalExecutorWithTools(tools=[ag2_tool], work_dir="./")
code = '''
result = wikipedia(tool_input={"query":"Christmas"})
Expand All @@ -161,13 +161,13 @@ def code_extractor(self) -> CodeExtractor:
"""(Experimental) Export a code extractor that can be used by an agent."""
return MarkdownCodeExtractor()

def __init__(self, tools: Optional[List[Tool]] = None, work_dir: Union[Path, str] = Path(".")):
def __init__(self, tools: Optional[list[Tool]] = None, work_dir: Union[Path, str] = Path(".")):
self.tools = tools if tools is not None else []
self.work_dir = work_dir
if not os.path.exists(work_dir):
os.makedirs(work_dir, exist_ok=True)

def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CodeResult:
def execute_code_blocks(self, code_blocks: list[CodeBlock]) -> CodeResult:
"""Execute code blocks and return the result.
Args:
Expand Down
13 changes: 4 additions & 9 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
ConversableAgentUsageSummaryMessage,
ConversableAgentUsageSummaryNoCostIncurredMessage,
ExecuteCodeBlockMessage,
ExecutedFunctionMessage,
ExecuteFunctionMessage,
ExecutedFunctionMessage,
GenerateCodeExecutionReplyMessage,
TerminationAndHumanReplyMessage,
UsingAutoReplyMessage,
Expand Down Expand Up @@ -435,7 +435,7 @@ def _get_chats_to_run(
message = last_msg
if callable(message):
message = message(recipient, messages, sender, config)
# We only run chat that has a valid message. NOTE: This is prone to change dependin on applications.
# We only run chat that has a valid message. NOTE: This is prone to change depending on applications.
if message:
current_c["message"] = message
chat_to_run.append(current_c)
Expand Down Expand Up @@ -783,9 +783,7 @@ def send(
```python
{
"content": lambda context: context["use_tool_msg"],
"context": {
"use_tool_msg": "Use tool X if they are relevant."
}
"context": {"use_tool_msg": "Use tool X if they are relevant."},
}
```
Next time, one agent can send a message B with a different "use_tool_msg".
Expand Down Expand Up @@ -833,9 +831,7 @@ async def a_send(
```python
{
"content": lambda context: context["use_tool_msg"],
"context": {
"use_tool_msg": "Use tool X if they are relevant."
}
"context": {"use_tool_msg": "Use tool X if they are relevant."},
}
```
Next time, one agent can send a message B with a different "use_tool_msg".
Expand Down Expand Up @@ -2721,7 +2717,6 @@ def _decorator(func_or_tool: Union[F, Tool]) -> Tool:
return _decorator

def _register_for_llm(self, tool: Tool, api_style: Literal["tool", "function"]) -> None:

# register the function to the agent if there is LLM config, raise an exception otherwise
if self.llm_config is None:
raise RuntimeError("LLM config must be setup before registering a function for LLM.")
Expand Down
12 changes: 3 additions & 9 deletions autogen/agentchat/groupchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,9 +1107,7 @@ def last_speaker(self) -> Agent:
def print_messages(recipient, messages, sender, config):
# Print the message immediately
print(
f"Sender: {sender.name} | Recipient: {recipient.name} | Message: {messages[-1].get('content')}"
)
print(f"Sender: {sender.name} | Recipient: {recipient.name} | Message: {messages[-1].get('content')}")
print(f"Real Sender: {sender.last_speaker.name}")
assert sender.last_speaker.name in messages[-1].get("content")
return False, None # Required to ensure the agent communication flow continues
Expand All @@ -1119,9 +1117,7 @@ def print_messages(recipient, messages, sender, config):
agent_b = ConversableAgent("agent B", default_auto_reply="I'm agent B.")
agent_c = ConversableAgent("agent C", default_auto_reply="I'm agent C.")
for agent in [agent_a, agent_b, agent_c]:
agent.register_reply(
[ConversableAgent, None], reply_func=print_messages, config=None
)
agent.register_reply([ConversableAgent, None], reply_func=print_messages, config=None)
group_chat = GroupChat(
[agent_a, agent_b, agent_c],
messages=[],
Expand All @@ -1130,9 +1126,7 @@ def print_messages(recipient, messages, sender, config):
allow_repeat_speaker=True,
)
chat_manager = GroupChatManager(group_chat)
groupchat_result = agent_a.initiate_chat(
chat_manager, message="Hi, there, I'm agent A."
)
groupchat_result = agent_a.initiate_chat(chat_manager, message="Hi, there, I'm agent A.")
```
"""
return self._last_speaker
Expand Down
3 changes: 2 additions & 1 deletion autogen/agentchat/realtime_agent/oai_realtime_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import asyncio
import json
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from logging import Logger, getLogger
from typing import TYPE_CHECKING, Any, AsyncGenerator, Optional
from typing import TYPE_CHECKING, Any, Optional

import httpx
from openai import DEFAULT_MAX_RETRIES, NOT_GIVEN, AsyncOpenAI
Expand Down
2 changes: 0 additions & 2 deletions autogen/agentchat/realtime_agent/realtime_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,8 @@ async def run(self) -> None:
"""Run the agent."""
# everything is run in the same task group to enable easy cancellation using self._tg.cancel_scope.cancel()
async with create_task_group() as self._tg:

# connect with the client first (establishes a connection and initializes a session)
async with self._realtime_client.connect():

# start the observers
for observer in self._observers:
self._tg.soonify(observer.run)(self)
Expand Down
3 changes: 2 additions & 1 deletion autogen/agentchat/realtime_agent/realtime_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#
# SPDX-License-Identifier: Apache-2.0

from typing import Any, AsyncContextManager, AsyncGenerator, Literal, Protocol, runtime_checkable
from collections.abc import AsyncGenerator
from typing import Any, AsyncContextManager, Literal, Protocol, runtime_checkable

__all__ = ["RealtimeClientProtocol", "Role"]

Expand Down
23 changes: 11 additions & 12 deletions autogen/agentchat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,24 @@ def gather_usage_summary(agents: list[Agent]) -> dict[dict[str, dict], dict[str,
```python
{
"usage_including_cached_inference" : {
"usage_including_cached_inference": {
"total_cost": 0.0006090000000000001,
"gpt-35-turbo": {
"cost": 0.0006090000000000001,
"prompt_tokens": 242,
"completion_tokens": 123,
"total_tokens": 365
"cost": 0.0006090000000000001,
"prompt_tokens": 242,
"completion_tokens": 123,
"total_tokens": 365,
},
},
"usage_excluding_cached_inference" : {
"usage_excluding_cached_inference": {
"total_cost": 0.0006090000000000001,
"gpt-35-turbo": {
"cost": 0.0006090000000000001,
"prompt_tokens": 242,
"completion_tokens": 123,
"total_tokens": 365
"cost": 0.0006090000000000001,
"prompt_tokens": 242,
"completion_tokens": 123,
"total_tokens": 365,
},
}
},
}
```
Expand Down
9 changes: 6 additions & 3 deletions autogen/cache/cache_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ def cache_factory(
Creating a Cosmos DB cache:
```python
cosmos_cache = cache_factory("myseed", cosmosdb_config={
cosmos_cache = cache_factory(
"myseed",
cosmosdb_config={
"connection_string": "your_connection_string",
"database_id": "your_database_id",
"container_id": "your_container_id"}
)
"container_id": "your_container_id",
},
)
```
"""
Expand Down
1 change: 0 additions & 1 deletion autogen/cache/in_memory_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@


class InMemoryCache(AbstractCache):

def __init__(self, seed: Union[str, int] = ""):
self._seed = str(seed)
self._cache: dict[str, Any] = {}
Expand Down
4 changes: 3 additions & 1 deletion autogen/code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,9 @@ def execute_code(
image_list = (
["python:3-slim", "python:3", "python:3-windowsservercore"]
if use_docker is True
else [use_docker] if isinstance(use_docker, str) else use_docker
else [use_docker]
if isinstance(use_docker, str)
else use_docker
)
for image in image_list:
# check if the image exists
Expand Down
2 changes: 1 addition & 1 deletion autogen/coding/func_with_reqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def wrapper(func: Callable[P, T]) -> FunctionWithRequirements[T, P]:


def _build_python_functions_file(
funcs: list[FunctionWithRequirements[Any, P] | Callable[..., Any] | FunctionWithRequirementsStr]
funcs: list[FunctionWithRequirements[Any, P] | Callable[..., Any] | FunctionWithRequirementsStr],
) -> str:
# First collect all global imports
global_imports: set[str] = set()
Expand Down
10 changes: 6 additions & 4 deletions autogen/coding/local_commandline_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,9 @@ def new(cls, *args, **kwargs): # type: ignore[no-untyped-def]

if alias is not None:
warnings.warn(
"{} has been renamed to {}, the alias will be "
"removed in the future".format(cls.__name__, alias.__name__),
"{} has been renamed to {}, the alias will be " "removed in the future".format(
cls.__name__, alias.__name__
),
DeprecationWarning,
stacklevel=2,
)
Expand All @@ -373,8 +374,9 @@ def new(cls, *args, **kwargs): # type: ignore[no-untyped-def]

if alias is not None:
warnings.warn(
"{} has been renamed to {}, the alias will be "
"removed in the future".format(b.__name__, alias.__name__),
"{} has been renamed to {}, the alias will be " "removed in the future".format(
b.__name__, alias.__name__
),
DeprecationWarning,
stacklevel=2,
)
Expand Down
2 changes: 1 addition & 1 deletion autogen/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# Portions derived from https://github.com/microsoft/autogen are under the MIT License.
# SPDX-License-Identifier: MIT
from .base import InputStream, IOStream, OutputStream
from .base import IOStream, InputStream, OutputStream
from .console import IOConsole
from .websockets import IOWebsockets

Expand Down
1 change: 0 additions & 1 deletion autogen/logger/sqlite_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ def log_new_wrapper(self, wrapper: OpenAIWrapper, init_args: dict[str, LLMConfig
self._run_query(query=query, args=args)

def log_function_use(self, source: str | Agent, function: F, args: dict[str, Any], returns: Any) -> None:

if self.con is None:
return

Expand Down
Loading

0 comments on commit 8fd96bf

Please sign in to comment.