Skip to content

Commit

Permalink
refactor!: Reduce dependencies of core, remove image from URL (#4919)
Browse files Browse the repository at this point in the history
* Reduce dependencies of core

* Update lock

* add package to test deps

* Update lock

* aio console dep

* Update lock
  • Loading branch information
jackgerrits authored Jan 7, 2025
1 parent 725d573 commit d4b406b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 35 deletions.
1 change: 1 addition & 0 deletions python/packages/autogen-agentchat/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ classifiers = [
]
dependencies = [
"autogen-core==0.4.0.dev13",
"aioconsole>=0.8.1"
]

[tool.ruff]
Expand Down
16 changes: 6 additions & 10 deletions python/packages/autogen-core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,20 @@ classifiers = [
"Operating System :: OS Independent",
]
dependencies = [
"openai>=1.3",
"pillow>=11.0.0",
"aioconsole>=0.8.1",
"aiohttp>=3.10.10",
"typing-extensions",
"typing-extensions>=4.0.0",
"pydantic<3.0.0,>=2.10.0",
"protobuf~=4.25.1",
"tiktoken>=0.8.0",
"opentelemetry-api~=1.27.0",
"asyncio_atexit",
"opentelemetry-api>=1.27.0",
"jsonref~=1.1.0",
]


[dependency-groups]
dev = [
"autogen_test_utils",
"aiofiles",
"asyncio_atexit",
"autogen_test_utils",
"azure-identity",
"chess",
"colorama",
Expand All @@ -46,6 +42,7 @@ dev = [
"llama-index",
"markdownify",
"nbqa",
"opentelemetry-sdk>=1.27.0",
"pip",
"polars",
"python-dotenv",
Expand All @@ -55,12 +52,11 @@ dev = [
"textual-imageview",
"textual",
"types-aiofiles",
"types-docker",
"types-pillow",
"types-protobuf",
"types-requests",
"types-docker",
"wikipedia",
"opentelemetry-sdk>=1.27.0",

# Documentation
"myst-nb==1.1.2",
Expand Down
41 changes: 30 additions & 11 deletions python/packages/autogen-core/src/autogen_core/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,41 @@
import re
from io import BytesIO
from pathlib import Path
from typing import Any, cast
from typing import Any, Dict, cast

import aiohttp
from openai.types.chat import ChatCompletionContentPartImageParam
from PIL import Image as PILImage
from pydantic import GetCoreSchemaHandler, ValidationInfo
from pydantic_core import core_schema
from typing_extensions import Literal


class Image:
"""Represents an image.
Example:
Loading an image from a URL:
.. code-block:: python
from autogen_core import Image
from PIL import Image as PILImage
import aiohttp
import asyncio
async def from_url(url: str) -> Image:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
content = await response.read()
return Image.from_pil(PILImage.open(content))
image = asyncio.run(from_url("https://example.com/image"))
"""

def __init__(self, image: PILImage.Image):
self.image: PILImage.Image = image.convert("RGB")

Expand All @@ -31,13 +55,6 @@ def from_uri(cls, uri: str) -> Image:
base64_data = re.sub(r"data:image/(?:png|jpeg);base64,", "", uri)
return cls.from_base64(base64_data)

@classmethod
async def from_url(cls, url: str) -> Image:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
content = await response.read()
return cls(PILImage.open(content))

@classmethod
def from_base64(cls, base64_str: str) -> Image:
return cls(PILImage.open(BytesIO(base64.b64decode(base64_str))))
Expand All @@ -60,7 +77,9 @@ def _repr_html_(self) -> str:
def data_uri(self) -> str:
return _convert_base64_to_data_uri(self.to_base64())

def to_openai_format(self, detail: Literal["auto", "low", "high"] = "auto") -> ChatCompletionContentPartImageParam:
# Returns openai.types.chat.ChatCompletionContentPartImageParam, which is a TypedDict
# We don't use the explicit type annotation so that we can avoid a dependency on the OpenAI Python SDK in this package.
def to_openai_format(self, detail: Literal["auto", "low", "high"] = "auto") -> Dict[str, Any]:
return {"type": "image_url", "image_url": {"url": self.data_uri, "detail": detail}}

@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from openai.types.chat import (
ChatCompletion,
ChatCompletionAssistantMessageParam,
ChatCompletionContentPartImageParam,
ChatCompletionContentPartParam,
ChatCompletionContentPartTextParam,
ChatCompletionMessageParam,
Expand Down Expand Up @@ -154,7 +155,7 @@ def user_message_to_oai(message: UserMessage) -> ChatCompletionUserMessageParam:
elif isinstance(part, Image):
# TODO: support url based images
# TODO: support specifying details
parts.append(part.to_openai_format())
parts.append(cast(ChatCompletionContentPartImageParam, part.to_openai_format()))
else:
raise ValueError(f"Unknown content type: {part}")
return ChatCompletionUserMessageParam(
Expand Down
22 changes: 9 additions & 13 deletions python/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d4b406b

Please sign in to comment.