Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PROPOSAL] Allow the client to pass a callback that receives arguments Instructor sends to LLM providers #911

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions instructor/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
max_retries: int = 3,
validation_context: dict[str, Any] | None = None,
strict: bool = True,
provider_args_callback: Callable[[list[Any], dict[str, Any]], None]
| None = None,
**kwargs: Any,
) -> Awaitable[T]:
...
Expand All @@ -76,6 +78,8 @@
max_retries: int = 3,
validation_context: dict[str, Any] | None = None,
strict: bool = True,
provider_args_callback: Callable[[list[Any], dict[str, Any]], None]
| None = None,
**kwargs: Any,
) -> T:
...
Expand All @@ -88,8 +92,11 @@
max_retries: int = 3,
validation_context: dict[str, Any] | None = None,
strict: bool = True,
provider_args_callback: Callable[[list[Any], dict[str, Any]], None]
| None = None,
**kwargs: Any,
) -> Awaitable[Any]: ...
) -> Awaitable[Any]:
...

@overload
def create(
Expand All @@ -99,8 +106,11 @@
max_retries: int = 3,
validation_context: dict[str, Any] | None = None,
strict: bool = True,
provider_args_callback: Callable[[list[Any], dict[str, Any]], None]
| None = None,
**kwargs: Any,
) -> Any: ...
) -> Any:
...

def create(
self,
Expand All @@ -109,6 +119,8 @@
max_retries: int = 3,
validation_context: dict[str, Any] | None = None,
strict: bool = True,
provider_args_callback: Callable[[list[Any], dict[str, Any]], None]
| None = None,
**kwargs: Any,
) -> T | Any | Awaitable[T] | Awaitable[Any]:
kwargs = self.handle_kwargs(kwargs)
Expand All @@ -119,6 +131,7 @@
max_retries=max_retries,
validation_context=validation_context,
strict=strict,
provider_args_callback=provider_args_callback,
**kwargs,
)

Expand Down Expand Up @@ -223,6 +236,8 @@
max_retries: int = 3,
validation_context: dict[str, Any] | None = None,
strict: bool = True,
provider_args_callback: Callable[[list[Any], dict[str, Any]], None]
| None = None,
**kwargs: Any,
) -> Awaitable[tuple[T, Any]]:
...
Expand All @@ -235,6 +250,8 @@
max_retries: int = 3,
validation_context: dict[str, Any] | None = None,
strict: bool = True,
provider_args_callback: Callable[[list[Any], dict[str, Any]], None]
| None = None,
**kwargs: Any,
) -> tuple[T, Any]:
...
Expand All @@ -246,6 +263,8 @@
max_retries: int = 3,
validation_context: dict[str, Any] | None = None,
strict: bool = True,
provider_args_callback: Callable[[list[Any], dict[str, Any]], None]
| None = None,
**kwargs: Any,
) -> tuple[T, Any] | Awaitable[tuple[T, Any]]:
kwargs = self.handle_kwargs(kwargs)
Expand All @@ -255,6 +274,7 @@
max_retries=max_retries,
validation_context=validation_context,
strict=strict,
provider_args_callback=provider_args_callback,
**kwargs,
)
return model, model._raw_response
Expand Down Expand Up @@ -287,7 +307,7 @@
self.kwargs = kwargs
self.provider = provider

async def create(

Check failure on line 310 in instructor/client.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.11)

Method "create" overrides class "Instructor" in an incompatible manner   Positional parameter count mismatch; base method has 8, but override has 7 (reportIncompatibleMethodOverride)
self,
response_model: type[T] | None,
messages: list[ChatCompletionMessageParam],
Expand All @@ -306,7 +326,7 @@
**kwargs,
)

async def create_partial(

Check failure on line 329 in instructor/client.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.11)

Method "create_partial" overrides class "Instructor" in an incompatible manner   Return type mismatch: base method returns type "Generator[T@create_partial, None, None]", override returns type "AsyncGenerator[T@create_partial, None]"     "AsyncGenerator[T@create_partial, None]" is incompatible with "Generator[T@create_partial, None, None]" (reportIncompatibleMethodOverride)
self,
response_model: type[T],
messages: list[ChatCompletionMessageParam],
Expand All @@ -327,7 +347,7 @@
):
yield item

async def create_iterable(

Check failure on line 350 in instructor/client.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.11)

Method "create_iterable" overrides class "Instructor" in an incompatible manner   Return type mismatch: base method returns type "Generator[T@create_iterable, None, None]", override returns type "AsyncGenerator[T@create_iterable, None]"     "AsyncGenerator[T@create_iterable, None]" is incompatible with "Generator[T@create_iterable, None, None]" (reportIncompatibleMethodOverride)
self,
messages: list[ChatCompletionMessageParam],
response_model: type[T],
Expand All @@ -348,7 +368,7 @@
):
yield item

async def create_with_completion(

Check failure on line 371 in instructor/client.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.11)

Method "create_with_completion" overrides class "Instructor" in an incompatible manner   Positional parameter count mismatch; base method has 8, but override has 7 (reportIncompatibleMethodOverride)
self,
messages: list[ChatCompletionMessageParam],
response_model: type[T],
Expand Down Expand Up @@ -484,4 +504,4 @@
create=instructor.patch(create=completion, mode=mode),
mode=mode,
**kwargs,
)
)
32 changes: 19 additions & 13 deletions instructor/patch.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# type: ignore[all]
from functools import wraps
from typing import (
Callable,
Protocol,
TypeVar,
Union,
overload,
)
from typing import Callable, Protocol, TypeVar, Union, overload, Any
from collections.abc import Awaitable
from typing_extensions import ParamSpec

Expand Down Expand Up @@ -35,7 +29,8 @@ def __call__(
max_retries: int = 1,
*args: T_ParamSpec.args,
**kwargs: T_ParamSpec.kwargs,
) -> T_Model: ...
) -> T_Model:
...


class AsyncInstructorChatCompletionCreate(Protocol):
Expand All @@ -46,35 +41,40 @@ async def __call__(
max_retries: int = 1,
*args: T_ParamSpec.args,
**kwargs: T_ParamSpec.kwargs,
) -> T_Model: ...
) -> T_Model:
...


@overload
def patch(
client: OpenAI,
mode: Mode = Mode.TOOLS,
) -> OpenAI: ...
) -> OpenAI:
...


@overload
def patch(
client: AsyncOpenAI,
mode: Mode = Mode.TOOLS,
) -> AsyncOpenAI: ...
) -> AsyncOpenAI:
...


@overload
def patch(
create: Callable[T_ParamSpec, T_Retval],
mode: Mode = Mode.TOOLS,
) -> InstructorChatCompletionCreate: ...
) -> InstructorChatCompletionCreate:
...


@overload
def patch(
create: Awaitable[T_Retval],
mode: Mode = Mode.TOOLS,
) -> InstructorChatCompletionCreate: ...
) -> InstructorChatCompletionCreate:
...


def patch(
Expand Down Expand Up @@ -110,6 +110,9 @@ async def new_create_async(
validation_context: dict = None,
max_retries: int = 1,
strict: bool = True,
provider_args_callback: Callable[
[list[Any], dict[str, Any]], Awaitable[None]
] = None,
*args: T_ParamSpec.args,
**kwargs: T_ParamSpec.kwargs,
) -> T_Model:
Expand All @@ -124,6 +127,7 @@ async def new_create_async(
args=args,
kwargs=new_kwargs,
strict=strict,
provider_args_callback=provider_args_callback,
mode=mode,
)
return response
Expand All @@ -134,6 +138,7 @@ def new_create_sync(
validation_context: dict = None,
max_retries: int = 1,
strict: bool = True,
provider_args_callback: Callable[[list[Any], dict[str, Any]], None] = None,
*args: T_ParamSpec.args,
**kwargs: T_ParamSpec.kwargs,
) -> T_Model:
Expand All @@ -147,6 +152,7 @@ def new_create_sync(
max_retries=max_retries,
args=args,
strict=strict,
provider_args_callback=provider_args_callback,
kwargs=new_kwargs,
mode=mode,
)
Expand Down
2 changes: 2 additions & 0 deletions instructor/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def retry_sync(
max_retries: int | Retrying = 1,
strict: bool | None = None,
mode: Mode = Mode.TOOLS,
provider_args_callback: Callable[[list[Any], dict[str, Any]], None] | None = None,
) -> T_Model:
total_usage = CompletionUsage(completion_tokens=0, prompt_tokens=0, total_tokens=0)
if mode in {Mode.ANTHROPIC_TOOLS, Mode.ANTHROPIC_JSON}:
Expand All @@ -158,6 +159,7 @@ def retry_sync(
for attempt in max_retries:
with attempt:
try:
provider_args_callback(args, kwargs)
response = func(*args, **kwargs)
stream = kwargs.get("stream", False)
response = update_total_usage(response, total_usage)
Expand Down
Loading