-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
redesign: use @easy_sync.sync_compatible to support sync & async
- Loading branch information
1 parent
17ee0aa
commit c537e7a
Showing
11 changed files
with
152 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from functools import partial | ||
from typing import Any, Mapping, Union | ||
from easy_sync import Waitable, sync_compatible | ||
import httpx | ||
import openai | ||
from openai.types.chat.chat_completion import ChatCompletion | ||
from ai_powered.utils.function_wraps import wraps_arguments_type | ||
|
||
|
||
class LlmConnection: | ||
sync_client: openai.OpenAI | ||
async_client: openai.AsyncOpenAI | ||
base_url: str | httpx.URL | None | ||
|
||
def __init__(self, | ||
api_key: str | None = None, | ||
organization: str | None = None, | ||
project: str | None = None, | ||
base_url: str | httpx.URL | None = None, | ||
timeout: Union[float, httpx.Timeout, None, openai.NotGiven] = openai.NOT_GIVEN, | ||
max_retries: int = openai.DEFAULT_MAX_RETRIES, | ||
default_headers: Mapping[str, str] | None = None, | ||
default_query: Mapping[str, object] | None = None, | ||
sync_http_client: httpx.Client | None = None, | ||
async_http_client: httpx.AsyncClient | None = None, | ||
): | ||
self.base_url = base_url | ||
|
||
self.sync_client = openai.OpenAI( | ||
api_key=api_key, | ||
organization=organization, | ||
project=project, | ||
base_url=base_url, | ||
timeout=timeout, | ||
max_retries=max_retries, | ||
default_headers=default_headers, | ||
default_query=default_query, | ||
http_client=sync_http_client, | ||
) | ||
|
||
self.async_client = openai.AsyncOpenAI( | ||
api_key=api_key, | ||
organization=organization, | ||
project=project, | ||
base_url=base_url, | ||
timeout=timeout, | ||
max_retries=max_retries, | ||
default_headers=default_headers, | ||
default_query=default_query, | ||
http_client=async_http_client, | ||
) | ||
|
||
async_fn = partial(self.async_client.chat.completions.create, stream=False) | ||
sync_fn = partial(self.sync_client.chat.completions.create, stream=False) | ||
|
||
f = sync_compatible(sync_fn=sync_fn)(async_fn) #type: ignore | ||
self._chat_completions = f | ||
|
||
@wraps_arguments_type(openai.AsyncOpenAI().chat.completions.create) | ||
def chat_completions(self, *args: list[Any], **kwargs: dict[str, Any]) -> Waitable[ChatCompletion]: | ||
return self._chat_completions(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from typing import Any, Callable, TypeVar | ||
from typing_extensions import ParamSpec | ||
|
||
|
||
P = ParamSpec('P') | ||
R = TypeVar('R') | ||
|
||
|
||
def wraps(origin_fn: Callable[P, R]) -> Callable[ [Callable[..., Any]], Callable[P, R]]: | ||
def wrapper(fn: Callable[..., Any]) -> Callable[P, R]: | ||
return fn #type: ignore | ||
return wrapper | ||
|
||
|
||
def wraps_arguments_type(origin_fn: Callable[P, Any]) -> Callable[ [Callable[..., R]], Callable[P, R]]: | ||
def wrapper(fn: Callable[..., R]) -> Callable[P, R]: | ||
return fn #type: ignore | ||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.