summaryrefslogtreecommitdiffstats
path: root/g4f/api
diff options
context:
space:
mode:
authorTekky <98614666+xtekky@users.noreply.github.com>2024-10-17 17:56:51 +0200
committerGitHub <noreply@github.com>2024-10-17 17:56:51 +0200
commit66a305998d47e724efaea696bf352428cfcd8291 (patch)
treee372492a190abfa1254e6b05afea8d154aa48225 /g4f/api
parentMerge pull request #2275 from hansipie/setollamahost (diff)
parentUpdate (g4f/Provider/Blackbox.py) (diff)
downloadgpt4free-0.3.3.1.tar
gpt4free-0.3.3.1.tar.gz
gpt4free-0.3.3.1.tar.bz2
gpt4free-0.3.3.1.tar.lz
gpt4free-0.3.3.1.tar.xz
gpt4free-0.3.3.1.tar.zst
gpt4free-0.3.3.1.zip
Diffstat (limited to '')
-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
+ )