Skip to content

Commit

Permalink
Fixing Pyright Errors (#1267)
Browse files Browse the repository at this point in the history
This fixes the pyright errors introduced in a previous commit
  • Loading branch information
ivanleomk authored Dec 18, 2024
1 parent a953b99 commit 46ac59c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
6 changes: 3 additions & 3 deletions instructor/batch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any, Union, TypeVar, Optional
from collections.abc import Iterable
from pydantic import BaseModel, Field # type: ignore
from pydantic import BaseModel, Field
from instructor.process_response import handle_response_model
import instructor
import uuid
Expand Down Expand Up @@ -139,7 +139,7 @@ def create_from_messages(
"temperature": temperature,
"messages": messages,
**kwargs,
}
},
}
file.write(json.dumps(request) + "\n")
else:
Expand All @@ -159,6 +159,6 @@ def create_from_messages(
**kwargs,
),
method="POST",
url="/v1/chat/completions"
url="/v1/chat/completions",
)
file.write(batch_model.model_dump_json() + "\n")
2 changes: 1 addition & 1 deletion instructor/dsl/citation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydantic import BaseModel, Field, model_validator, ValidationInfo # type: ignore
from pydantic import BaseModel, Field, model_validator, ValidationInfo
from collections.abc import Generator


Expand Down
16 changes: 13 additions & 3 deletions instructor/dsl/iterable.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, Optional, cast, ClassVar
from collections.abc import AsyncGenerator, Generator, Iterable

from pydantic import BaseModel, Field, create_model # type: ignore
from pydantic import BaseModel, Field, create_model

from instructor.function_calls import OpenAISchema
from instructor.mode import Mode
Expand Down Expand Up @@ -109,7 +109,12 @@ def extract_json(
}:
if json_chunk := chunk.choices[0].delta.content:
yield json_chunk
elif mode in {Mode.TOOLS, Mode.TOOLS_STRICT, Mode.FIREWORKS_TOOLS, Mode.WRITER_TOOLS}:
elif mode in {
Mode.TOOLS,
Mode.TOOLS_STRICT,
Mode.FIREWORKS_TOOLS,
Mode.WRITER_TOOLS,
}:
if json_chunk := chunk.choices[0].delta.tool_calls:
if json_chunk[0].function.arguments is not None:
yield json_chunk[0].function.arguments
Expand Down Expand Up @@ -145,7 +150,12 @@ async def extract_json_async(
}:
if json_chunk := chunk.choices[0].delta.content:
yield json_chunk
elif mode in {Mode.TOOLS, Mode.TOOLS_STRICT, Mode.FIREWORKS_TOOLS, Mode.WRITER_TOOLS}:
elif mode in {
Mode.TOOLS,
Mode.TOOLS_STRICT,
Mode.FIREWORKS_TOOLS,
Mode.WRITER_TOOLS,
}:
if json_chunk := chunk.choices[0].delta.tool_calls:
if json_chunk[0].function.arguments is not None:
yield json_chunk[0].function.arguments
Expand Down
2 changes: 1 addition & 1 deletion instructor/dsl/maybe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydantic import BaseModel, Field, create_model # type: ignore
from pydantic import BaseModel, Field, create_model
from typing import Generic, Optional, TypeVar

T = TypeVar("T", bound=BaseModel)
Expand Down
2 changes: 1 addition & 1 deletion instructor/dsl/validators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Callable, Optional

from openai import OpenAI
from pydantic import Field # type: ignore
from pydantic import Field

from instructor.function_calls import OpenAISchema
from instructor.client import Instructor
Expand Down
22 changes: 10 additions & 12 deletions instructor/multimodal.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from urllib.parse import urlparse
import mimetypes
import requests
from pydantic import BaseModel, Field # type:ignore
from pydantic import BaseModel, Field
from .mode import Mode

F = TypeVar("F", bound=Callable[..., Any])
Expand Down Expand Up @@ -76,9 +76,7 @@ def autodetect(cls, source: Union[str, Path]) -> Image: # noqa: UP007
raise ValueError("Unable to determine image type or unsupported image format")

@classmethod
def autodetect_safely(
cls, source: str | Path
) -> Union[Image, str]: # noqa: UP007
def autodetect_safely(cls, source: str | Path) -> Union[Image, str]: # noqa: UP007
"""Safely attempt to autodetect an image from a source string or path.
Args:
Expand Down Expand Up @@ -210,9 +208,7 @@ def to_openai(self) -> dict[str, Any]:
class Audio(BaseModel):
"""Represents an audio that can be loaded from a URL or file path."""

source: str | Path = Field(
description="URL or file path of the audio"
) # noqa: UP007
source: str | Path = Field(description="URL or file path of the audio") # noqa: UP007
data: Union[str, None] = Field( # noqa: UP007
None, description="Base64 encoded audio data", repr=False
)
Expand Down Expand Up @@ -343,9 +339,7 @@ def is_image_params(x: Any) -> bool:
}
if autodetect_images:
if isinstance(content, list):
new_content: list[str | dict[str, Any] | Image | Audio] = (
[]
) # noqa: UP007
new_content: list[str | dict[str, Any] | Image | Audio] = [] # noqa: UP007
for item in content:
if isinstance(item, str):
new_content.append(Image.autodetect_safely(item))
Expand All @@ -365,8 +359,12 @@ def is_image_params(x: Any) -> bool:
cast(ImageParams, content)
)
if isinstance(content, str):
converted_messages.append({"role": role, "content": content, **other_kwargs}) # type: ignore
converted_messages.append( # type: ignore
{"role": role, "content": content, **other_kwargs}
)
else:
converted_content = convert_contents(content, mode)
converted_messages.append({"role": role, "content": converted_content, **other_kwargs}) # type: ignore
converted_messages.append( # type: ignore
{"role": role, "content": converted_content, **other_kwargs}
)
return converted_messages # type: ignore

0 comments on commit 46ac59c

Please sign in to comment.