diff options
Diffstat (limited to 'g4f/api/__init__.py')
-rw-r--r-- | g4f/api/__init__.py | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index 2e6f19a1..83df469a 100644 --- a/g4f/api/__init__.py +++ b/g4f/api/__init__.py @@ -18,7 +18,7 @@ from typing import Union, Optional import g4f import g4f.debug -from g4f.client import AsyncClient +from g4f.client import Client from g4f.typing import Messages from g4f.cookies import read_cookie_files @@ -77,7 +77,7 @@ class AppConfig(): class Api: def __init__(self, app: FastAPI) -> None: self.app = app - self.client = AsyncClient() + self.client = Client() self.get_g4f_api_key = APIKeyHeader(name="g4f-api-key") def register_authorization(self): @@ -164,7 +164,8 @@ class Api: auth_header = auth_header.split(None, 1)[-1] if auth_header and auth_header != "Bearer": config.api_key = auth_header - response = self.client.chat.completions.create( + # Use the asynchronous create method and await it + response = await self.client.chat.completions.async_create( **{ **AppConfig.defaults, **config.dict(exclude_none=True), @@ -172,7 +173,7 @@ class Api: ignored=AppConfig.ignored_providers ) if not config.stream: - return JSONResponse((await response).to_json()) + return JSONResponse(response.to_json()) async def streaming(): try: @@ -204,10 +205,11 @@ class Api: auth_header = auth_header.split(None, 1)[-1] if auth_header and auth_header != "Bearer": config.api_key = auth_header - response = self.client.images.generate( + # Use the asynchronous generate method and await it + response = await self.client.images.async_generate( **config.dict(exclude_none=True), ) - return JSONResponse((await response).to_json()) + return JSONResponse(response.to_json()) except Exception as e: logging.exception(e) return Response(content=format_exception(e, config), status_code=500, media_type="application/json") |