summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkqlio67 <kqlio67@users.noreply.github.com>2024-10-15 14:07:06 +0200
committerkqlio67 <kqlio67@users.noreply.github.com>2024-10-15 14:07:06 +0200
commitc6d11e5cef86ac0bcce1e83435f1206c5acff433 (patch)
treea9e6e87af40b174219276a979919b17fef3978fc
parentMerge branch 'xtekky:main' into main (diff)
downloadgpt4free-c6d11e5cef86ac0bcce1e83435f1206c5acff433.tar
gpt4free-c6d11e5cef86ac0bcce1e83435f1206c5acff433.tar.gz
gpt4free-c6d11e5cef86ac0bcce1e83435f1206c5acff433.tar.bz2
gpt4free-c6d11e5cef86ac0bcce1e83435f1206c5acff433.tar.lz
gpt4free-c6d11e5cef86ac0bcce1e83435f1206c5acff433.tar.xz
gpt4free-c6d11e5cef86ac0bcce1e83435f1206c5acff433.tar.zst
gpt4free-c6d11e5cef86ac0bcce1e83435f1206c5acff433.zip
-rw-r--r--g4f/api/__init__.py16
1 files changed, 9 insertions, 7 deletions
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
+ )