From 8bed5cd64848ca4da4b548458e628ed1b1ae3116 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Sun, 15 Dec 2024 20:36:06 +0000 Subject: [PATCH] fix: update type annotations and remove unused model parameter Co-Authored-By: jason@jxnl.co --- instructor/multimodal.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/instructor/multimodal.py b/instructor/multimodal.py index 9ab7a3f1a..1cb444692 100644 --- a/instructor/multimodal.py +++ b/instructor/multimodal.py @@ -47,20 +47,20 @@ class ImageParams(ImageParamsBase, total=False): class Image(BaseModel): - source: Union[str, Path] = Field( # noqa: UP007 + source: str | Path = Field( description="URL, file path, or base64 data of the image" ) media_type: str = Field(description="MIME type of the image") - data: Union[str, None] = Field( # noqa: UP007 + data: str | None = Field( None, description="Base64 encoded image data", repr=False ) @classmethod - def autodetect(cls, source: Union[str, Path]) -> Image: # noqa: UP007 + def autodetect(cls, source: str | Path) -> Image: """Attempt to autodetect an image from a source string or Path. Args: - source (Union[str,path]): The source string or path. + source (str | Path): The source string or path. Returns: An Image if the source is detected to be a valid image. Raises: @@ -82,8 +82,8 @@ def autodetect(cls, source: Union[str, Path]) -> Image: # noqa: UP007 @classmethod def autodetect_safely( - cls, source: Union[str, Path] - ) -> Union[Image, str]: # noqa: UP007 + cls, source: str | Path + ) -> Image | str: """Safely attempt to autodetect an image from a source string or path. Args: @@ -154,7 +154,7 @@ def from_url(cls, url: str) -> Image: @classmethod @lru_cache - def from_path(cls, path: Union[str, Path]) -> Image: # noqa: UP007 + def from_path(cls, path: str | Path) -> Image: path = Path(path) if not path.is_file(): raise FileNotFoundError(f"Image file not found: {path}") @@ -295,10 +295,10 @@ def to_anthropic(self) -> dict[str, Any]: def convert_contents( - contents: list[Union[str, Image]], mode: Mode # noqa: UP007 -) -> list[Union[str, dict[str, Any]]]: # noqa: UP007 + contents: list[str | Image], mode: Mode +) -> list[str | dict[str, Any]]: """Convert contents to the appropriate format for the given mode.""" - converted_contents: list[Union[str, dict[str, Any]]] = [] # noqa: UP007 + converted_contents: list[str | dict[str, Any]] = [] for content in contents: if isinstance(content, str): converted_contents.append(content) @@ -319,14 +319,12 @@ def convert_contents( def convert_messages( messages: list[dict[str, Any]], mode: Mode, - model: Optional[str] = None, # Reserved for future provider-specific handling ) -> list[dict[str, Any]]: """Convert messages to the appropriate format for the given mode. Args: messages: List of message dictionaries to convert mode: The mode to convert messages for (e.g. MISTRAL_JSON) - model: Optional model name for provider-specific handling (reserved for future use) Returns: List of converted message dictionaries @@ -339,7 +337,7 @@ def convert_messages( continue content_list: list[dict[str, Any]] = [] - for item in cast(list[Union[str, Image, dict[str, Any]]], message["content"]): # noqa: UP007 + for item in cast(list[str | Image | dict[str, Any]], message["content"]): if isinstance(item, str): content_list.append({"type": "text", "text": item}) elif isinstance(item, Image):