XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Refactor item_data update to handle byte keys and convert messages to dict.

5a6d59e1
razrab <razrab@PimoLin>
提交于

代码差异

1 个文件 +11 -2
Modified g4f/api/__init__.py +11 -2
@@ -1,3 +1,6 @@
1 import ast
2 import logging
3
1 4 from fastapi import FastAPI, Response, Request
2 5 from fastapi.responses import StreamingResponse
3 6 from typing import List, Union, Any, Dict, AnyStr
@@ -68,14 +71,20 @@ class Api:
68 71 'stream': False,
69 72 }
70 73
71 item_data.update(item or {})
74 # item contains byte keys, and dict.get suppresses error
75 item_data.update({key.decode('utf-8') if isinstance(key, bytes) else key: str(value) for key, value in (item or {}).items()})
76 # messages is str, need dict
77 if isinstance(item_data.get('messages'), str):
78 item_data['messages'] = ast.literal_eval(item_data.get('messages'))
79
72 80 model = item_data.get('model')
73 81 stream = item_data.get('stream')
74 82 messages = item_data.get('messages')
75 83
76 84 try:
77 85 response = g4f.ChatCompletion.create(model=model, stream=stream, messages=messages)
78 except:
86 except Exception as e:
87 logging.exception(e)
79 88 return Response(content=json.dumps({"error": "An error occurred while generating the response."}, indent=4), media_type="application/json")
80 89
81 90 completion_id = ''.join(random.choices(string.ascii_letters + string.digits, k=28))