From c6d11e5cef86ac0bcce1e83435f1206c5acff433 Mon Sep 17 00:00:00 2001 From: kqlio67 Date: Tue, 15 Oct 2024 15:07:06 +0300 Subject: refactor(g4f/api/__init__.py): use asynchronous methods in Client --- g4f/api/__init__.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'g4f/api/__init__.py') diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index 2c723978..da35319a 100644 --- a/g4f/api/__init__.py +++ b/g4f/api/__init__.py @@ -17,7 +17,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 @@ -69,7 +69,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): @@ -156,7 +156,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), @@ -164,7 +165,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: @@ -196,10 +197,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") @@ -232,4 +234,4 @@ def run_api( use_colors=use_colors, factory=True, reload=debug - ) \ No newline at end of file + ) -- cgit v1.2.3