summaryrefslogtreecommitdiffstats
path: root/g4f
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--g4f/__init__.py4
-rw-r--r--g4f/api/__init__.py19
2 files changed, 19 insertions, 4 deletions
diff --git a/g4f/__init__.py b/g4f/__init__.py
index 25e31833..fd8aa306 100644
--- a/g4f/__init__.py
+++ b/g4f/__init__.py
@@ -5,7 +5,7 @@ from .Provider import BaseProvider, RetryProvider
from .typing import Messages, CreateResult, Union, List
from . import debug
-version = '0.1.8.1'
+version = '0.1.8.2'
version_check = True
def check_pypi_version() -> None:
@@ -15,6 +15,8 @@ def check_pypi_version() -> None:
if version != latest_version:
print(f'New pypi version: {latest_version} (current: {version}) | pip install -U g4f')
+ return False
+ return True
except Exception as e:
print(f'Failed to check g4f pypi version: {e}')
diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py
index ad3b0db4..8eceb743 100644
--- a/g4f/api/__init__.py
+++ b/g4f/api/__init__.py
@@ -1,3 +1,6 @@
+import ast
+import logging
+
from fastapi import FastAPI, Response, Request
from fastapi.responses import StreamingResponse
from typing import List, Union, Any, Dict, AnyStr
@@ -68,14 +71,24 @@ class Api:
'stream': False,
}
- item_data.update(item or {})
+ # item contains byte keys, and dict.get suppresses error
+ item_data.update({key.decode('utf-8') if isinstance(key, bytes) else key: str(value) for key, value in (item or {}).items()})
+ # messages is str, need dict
+ if isinstance(item_data.get('messages'), str):
+ item_data['messages'] = ast.literal_eval(item_data.get('messages'))
+
model = item_data.get('model')
stream = item_data.get('stream')
messages = item_data.get('messages')
try:
- response = g4f.ChatCompletion.create(model=model, stream=stream, messages=messages)
- except:
+ response = g4f.ChatCompletion.create(
+ model=model,
+ stream=stream,
+ messages=messages,
+ list_ignored_providers=self.list_ignored_providers)
+ except Exception as e:
+ logging.exception(e)
return Response(content=json.dumps({"error": "An error occurred while generating the response."}, indent=4), media_type="application/json")
completion_id = ''.join(random.choices(string.ascii_letters + string.digits, k=28))