Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
trisongz committed Oct 19, 2023
1 parent 3dec050 commit 2b0ef30
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions async_openai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ def configure_routes(self, **kwargs):
if self._kwargs: kwargs.update(self._kwargs)
self._routes = ApiRoutes(
client = self.client,
name = self.name,
# headers = self.headers,
debug_enabled = self.debug_enabled,
on_error = self.on_error,
Expand Down
5 changes: 4 additions & 1 deletion async_openai/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ApiRoutes:
def __init__(
self,
client: aiohttpx.Client,
name: str,
# headers: Optional[Dict] = None,
debug_enabled: Optional[bool] = False,
on_error: Optional[Callable] = None,
Expand All @@ -49,6 +50,7 @@ def __init__(
**kwargs
):
self.client = client
self.name = name
self.settings = settings or get_settings()
# self.headers = headers or self.settings.get_headers()
self.debug_enabled = debug_enabled
Expand All @@ -74,6 +76,7 @@ def init_routes(self):
try:
setattr(self, route, route_class(
client = self.client,
name = self.name,
# headers = self.headers,
debug_enabled = self.debug_enabled,
on_error = self.on_error,
Expand All @@ -87,5 +90,5 @@ def init_routes(self):
**self.kwargs
))
except Exception as e:
logger.error(f"Failed to initialize route {route} with error: {e}")
logger.error(f"[{self.name}] Failed to initialize route {route} with error: {e}")
raise e
3 changes: 2 additions & 1 deletion async_openai/types/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ def fatal_exception(exc):
if isinstance(exc, OpenAIError):
# retry on server errors and client errors
# with 429 status code (rate limited),
# with 400, 404, 415 status codes (invalid request),
# don't retry on other client errors
return (400 <= exc.status < 500) and exc.status != 429
return (400 <= exc.status < 500) and exc.status not in [429, 400, 404, 415]
else:
# retry on all other errors (eg. network)
return False
Expand Down
9 changes: 5 additions & 4 deletions async_openai/types/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def get_retry_wrapper(

class BaseRoute(BaseModel):
client: aiohttpx.Client
name: str # Client Name
# headers: Dict[str, str] = Field(default_factory = get_default_headers)
success_codes: Optional[List[int]] = RESPONSE_SUCCESS_CODES

Expand Down Expand Up @@ -875,9 +876,9 @@ def handle_response(
is_stream = not hasattr(response, "_content")
if self.debug_enabled:
if is_stream:
logger.info(f'[Stream] [{response.status_code} - {response.request.url}] headers: {response.headers}')
logger.info(f'[{self.name} - Stream] [{response.status_code} - {response.request.url}] headers: {response.headers}')
else:
logger.info(f'[{response.status_code} - {response.request.url}] headers: {response.headers}, body: {response.text[:250]}')
logger.info(f'[{self.name} - {response.status_code} - {response.request.url}] headers: {response.headers}, body: {response.text[:250]}')

if response.status_code in self.success_codes:
if is_stream: return response
Expand Down Expand Up @@ -906,7 +907,7 @@ def _send(
Handle Sending Requests
"""
if timeout is None: timeout = self.timeout
if self.debug_enabled: logger.info(f'[{method} - {url}] headers: {headers}, params: {params}, data: {data}')
if self.debug_enabled: logger.info(f'[{self.name} - {method} - {url}] headers: {headers}, params: {params}, data: {data}')
request = self.client.build_request(
method = method,
url = url,
Expand Down Expand Up @@ -942,7 +943,7 @@ async def _async_send(
"""

if timeout is None: timeout = self.timeout
if self.debug_enabled: logger.info(f'[{method} - {url}] headers: {headers}, params: {params}, data: {data}')
if self.debug_enabled: logger.info(f'[{self.name} - {method} - {url}] headers: {headers}, params: {params}, data: {data}')
request = await self.client.async_build_request(
method = method,
url = url,
Expand Down
2 changes: 1 addition & 1 deletion async_openai/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.0.41rc0'
VERSION = '0.0.41rc1'

0 comments on commit 2b0ef30

Please sign in to comment.