diff options
author | Tekky <98614666+xtekky@users.noreply.github.com> | 2023-11-13 10:57:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-13 10:57:50 +0100 |
commit | 82f3cdc7621f0aabfe168a823f59d9f79792533b (patch) | |
tree | 612904e83f55237fcc83b68c29a3e4cadaf04e4a /g4f/api/__init__.py | |
parent | Merge pull request #1241 from hlohaus/pi (diff) | |
parent | Refactor item_data update to handle byte keys and convert messages to dict. (diff) | |
download | gpt4free-82f3cdc7621f0aabfe168a823f59d9f79792533b.tar gpt4free-82f3cdc7621f0aabfe168a823f59d9f79792533b.tar.gz gpt4free-82f3cdc7621f0aabfe168a823f59d9f79792533b.tar.bz2 gpt4free-82f3cdc7621f0aabfe168a823f59d9f79792533b.tar.lz gpt4free-82f3cdc7621f0aabfe168a823f59d9f79792533b.tar.xz gpt4free-82f3cdc7621f0aabfe168a823f59d9f79792533b.tar.zst gpt4free-82f3cdc7621f0aabfe168a823f59d9f79792533b.zip |
Diffstat (limited to 'g4f/api/__init__.py')
-rw-r--r-- | g4f/api/__init__.py | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index ad3b0db4..de05c377 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,20 @@ 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: + 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)) |