A programming framework for agentic AI
Resolve types issues in coding (#2055)
Jack Gerrits9d33dc6f9acf
diff --git a/.github/workflows/type-check.yml b/.github/workflows/type-check.ymlindex ee790d8d5..4c96de265 100644--- a/.github/workflows/type-check.yml+++ b/.github/workflows/type-check.yml@@ -20,6 +20,7 @@ jobs:- run: pip install ".[jupyter-executor]" mypy# As more modules are type check clean, add them here- run: |- mypy --install-types --non-interactive \+ mypy \autogen/logger \- autogen/exception_utils.py+ autogen/exception_utils.py \+ autogen/codingdiff --git a/autogen/code_utils.py b/autogen/code_utils.pyindex 710af02fa..57a817855 100644--- a/autogen/code_utils.py+++ b/autogen/code_utils.py@@ -14,6 +14,8 @@ from autogen import oaiimport docker+from .types import UserMessageImageContentPart, UserMessageTextContentPart+SENTINEL = object()DEFAULT_MODEL = "gpt-4"FAST_MODEL = "gpt-3.5-turbo"@@ -37,8 +39,8 @@ PATH_SEPARATOR = WIN32 and "\\" or "/"logger = logging.getLogger(__name__)-def content_str(content: Union[str, List[Dict[str, Any]], None]) -> str:- """Converts `content` into a string format.+def content_str(content: Union[str, List[Union[UserMessageTextContentPart, UserMessageImageContentPart]], None]) -> str:+ """Converts the `content` field of an OpenAI merssage into a string format.This function processes content that may be a string, a list of mixed text and image URLs, or None,and converts it into a string. Text is directly appended to the result string, while image URLs are@@ -241,7 +243,7 @@ def get_powershell_command():raise PermissionError("No permission to run powershell.") from e-def _cmd(lang):+def _cmd(lang: str) -> str:if lang.startswith("python") or lang in ["bash", "sh"]:return langif lang in ["shell"]:diff --git a/autogen/coding/__init__.py b/autogen/coding/__init__.pyindex 6153fc514..2ba4e9b07 100644--- a/autogen/coding/__init__.py+++ b/autogen/coding/__init__.py@@ -1,7 +1,7 @@from .base import CodeBlock, CodeExecutor, CodeExtractor, CodeResultfrom .factory import CodeExecutorFactoryfrom .markdown_code_extractor import MarkdownCodeExtractor-from .local_commandline_code_executor import LocalCommandLineCodeExecutor, CommandLineCodeResult+from .local_commandline_code_executor import LocalCommandLineCodeExecutorfrom .docker_commandline_code_executor import DockerCommandLineCodeExecutor__all__ = (@@ -12,6 +12,5 @@ __all__ = ("CodeExecutorFactory","MarkdownCodeExtractor","LocalCommandLineCodeExecutor",- "CommandLineCodeResult","DockerCommandLineCodeExecutor",)diff --git a/autogen/coding/base.py b/autogen/coding/base.pyindex 3e2764bd6..f60ff0de8 100644--- a/autogen/coding/base.py+++ b/autogen/coding/base.py@@ -1,10 +1,12 @@-from typing import Any, Dict, List, Optional, Protocol, Union, runtime_checkable+from __future__ import annotations+from typing import Any, List, Literal, Mapping, Optional, Protocol, TypedDict, Union, runtime_checkablefrom pydantic import BaseModel, Fieldfrom ..agentchat.agent import LLMAgent+from ..types import UserMessageImageContentPart, UserMessageTextContentPart-__all__ = ("CodeBlock", "CodeResult", "CodeExtractor", "CodeExecutor")+__all__ = ("CodeBlock", "CodeResult", "CodeExtractor", "CodeExecutor", "CodeExecutionConfig")class CodeBlock(BaseModel):@@ -26,7 +28,9 @@ class CodeResult(BaseModel):class CodeExtractor(Protocol):"""(Experimental) A code extractor class that extracts code blocks from a message."""- def extract_code_blocks(self, message: Union[str, List[Dict[str, Any]], None]) -> List[CodeBlock]:+ def extract_code_blocks(+ self, message: Union[str, List[Union[UserMessageTextContentPart, UserMessageImageContentPart]], None]+ ) -> List[CodeBlock]:"""(Experimental) Extract code blocks from a message.Args:@@ -79,6 +83,21 @@ class IPythonCodeResult(CodeResult):)+CodeExecutionConfig = TypedDict(+ "CodeExecutionConfig",+ {+ "executor": Union[Literal["ipython-embedded", "commandline-local"], CodeExecutor],+ "last_n_messages": Union[int, Literal["auto"]],+ "timeout": int,+ "use_docker": Union[bool, str, List[str]],+ "work_dir": str,+ "ipython-embedded": Mapping[str, Any],+ "commandline-local": Mapping[str, Any],+ },+ total=False,+)++class CommandLineCodeResult(CodeResult):"""(Experimental) A code result class for command line code executor."""diff --git a/autogen/coding/docker_commandline_code_executor.py b/autogen/coding/docker_commandline_code_executor.pyindex 1758234d0..a3c26dad2 100644--- a/autogen/coding/docker_commandline_code_executor.py+++ b/autogen/coding/docker_commandline_code_executor.py@@ -6,9 +6,8 @@ from pathlib import Pathfrom time import sleepfrom types import TracebackTypeimport uuid-from typing import List, Optional, Type, Union+from typing import Any, List, Optional, Type, Unionimport docker-from docker.models.containers import Containerfrom docker.errors import ImageNotFoundfrom .utils import _get_file_name_from_content@@ -25,8 +24,8 @@ else:from typing_extensions import Self-def _wait_for_ready(container: Container, timeout: int = 60, stop_time: int = 0.1) -> None:- elapsed_time = 0+def _wait_for_ready(container: Any, timeout: int = 60, stop_time: float = 0.1) -> None:+ elapsed_time = 0.0while container.status != "running" and elapsed_time < timeout:sleep(stop_time)elapsed_time += stop_time@@ -114,7 +113,7 @@ class DockerCommandLineCodeExecutor(CodeExecutor):_wait_for_ready(self._container)- def cleanup():+ def cleanup() -> None:try:container = client.containers.get(container_name)container.stop()diff --git a/autogen/coding/factory.py b/autogen/coding/factory.pyindex c95d64460..0c2d41b89 100644--- a/autogen/coding/factory.py+++ b/autogen/coding/factory.py@@ -1,6 +1,4 @@-from typing import Any, Dict--from .base import CodeExecutor+from .base import CodeExecutor, CodeExecutionConfig__all__ = ("CodeExecutorFactory",)@@ -9,7 +7,7 @@ class CodeExecutorFactory:"""(Experimental) A factory class for creating code executors."""@staticmethod- def create(code_execution_config: Dict[str, Any]) -> CodeExecutor:+ def create(code_execution_config: CodeExecutionConfig) -> CodeExecutor:"""(Experimental) Get a code executor based on the code execution config.Args:diff --git a/autogen/coding/jupyter/docker_jupyter_server.py b/autogen/coding/jupyter/docker_jupyter_server.pyindex bf55543a4..3b9462186 100644--- a/autogen/coding/jupyter/docker_jupyter_server.py+++ b/autogen/coding/jupyter/docker_jupyter_server.py@@ -123,7 +123,7 @@ WORKDIR "${HOME}"self._port = int(container_ports["8888/tcp"][0]["HostPort"])self._container_id = container.id- def cleanup():+ def cleanup() -> None:try:inner_container = client.containers.get(container.id)inner_container.stop()@@ -142,7 +142,7 @@ WORKDIR "${HOME}"def connection_info(self) -> JupyterConnectionInfo:return JupyterConnectionInfo(host="127.0.0.1", use_https=False, port=self._port, token=self._token)- def stop(self):+ def stop(self) -> None:self._cleanup_func()def get_client(self) -> JupyterClient:diff --git a/autogen/coding/local_commandline_code_executor.py b/autogen/coding/local_commandline_code_executor.pyindex 48ec83a7f..57f91cb9c 100644--- a/autogen/coding/local_commandline_code_executor.py+++ b/autogen/coding/local_commandline_code_executor.py@@ -170,12 +170,12 @@ class LocalCommandLineCodeExecutor(CodeExecutor):# From stack overflow: https://stackoverflow.com/a/52087847/2214524class _DeprecatedClassMeta(type):- def __new__(cls, name, bases, classdict, *args, **kwargs):+ def __new__(cls, name, bases, classdict, *args, **kwargs): # type: ignore[no-untyped-def]alias = classdict.get("_DeprecatedClassMeta__alias")if alias is not None:- def new(cls, *args, **kwargs):+ def new(cls, *args, **kwargs): # type: ignore[no-untyped-def]alias = getattr(cls, "_DeprecatedClassMeta__alias")if alias is not None:@@ -209,14 +209,14 @@ class _DeprecatedClassMeta(type):if b not in fixed_bases:fixed_bases.append(b)- fixed_bases = tuple(fixed_bases)+ fixed_bases = tuple(fixed_bases) # type: ignore[assignment]- return super().__new__(cls, name, fixed_bases, classdict, *args, **kwargs)+ return super().__new__(cls, name, fixed_bases, classdict, *args, **kwargs) # type: ignore[call-overload]- def __instancecheck__(cls, instance):- return any(cls.__subclasscheck__(c) for c in {type(instance), instance.__class__})+ def __instancecheck__(cls, instance): # type: ignore[no-untyped-def]+ return any(cls.__subclasscheck__(c) for c in {type(instance), instance.__class__}) # type: ignore[no-untyped-call]- def __subclasscheck__(cls, subclass):+ def __subclasscheck__(cls, subclass): # type: ignore[no-untyped-def]if subclass is cls:return Trueelse:diff --git a/autogen/coding/markdown_code_extractor.py b/autogen/coding/markdown_code_extractor.pyindex cee107fdb..58ad4a09e 100644--- a/autogen/coding/markdown_code_extractor.py+++ b/autogen/coding/markdown_code_extractor.py@@ -2,15 +2,18 @@ import refrom typing import Any, Dict, List, Optional, Unionfrom ..code_utils import CODE_BLOCK_PATTERN, UNKNOWN, content_str, infer_lang-from .base import CodeBlock+from .base import CodeBlock, CodeExtractor+from ..types import UserMessageImageContentPart, UserMessageTextContentPart__all__ = ("MarkdownCodeExtractor",)-class MarkdownCodeExtractor:+class MarkdownCodeExtractor(CodeExtractor):"""(Experimental) A class that extracts code blocks from a message using Markdown syntax."""- def extract_code_blocks(self, message: Union[str, List[Dict[str, Any]], None]) -> List[CodeBlock]:+ def extract_code_blocks(+ self, message: Union[str, List[Union[UserMessageTextContentPart, UserMessageImageContentPart]], None]+ ) -> List[CodeBlock]:"""(Experimental) Extract code blocks from a message. If no code blocks are found,return an empty list.diff --git a/autogen/types.py b/autogen/types.pynew file mode 100644index 000000000..77ca70b70--- /dev/null+++ b/autogen/types.py@@ -0,0 +1,12 @@+from typing import Dict, List, Literal, TypedDict, Union+++class UserMessageTextContentPart(TypedDict):+ type: Literal["text"]+ text: str+++class UserMessageImageContentPart(TypedDict):+ type: Literal["image_url"]+ # Ignoring the other "detail param for now"+ image_url: Dict[Literal["url"], str]