返回提交历史
Modified
g4f/Provider/Chatxyz.py
+14
-22
XFEstudio/gpt4free
Improve Chatxyz Provider
6d0d975c
代码差异
1 个文件
+14
-22
@@ -1,12 +1,16 @@
1
1
from __future__ import annotations
2
3
import json
2
4
from aiohttp import ClientSession
5
3
6
from ..typing import AsyncResult, Messages
4
7
from .base_provider import AsyncGeneratorProvider
5
import json
8
6
9
class Chatxyz(AsyncGeneratorProvider):
7
url = "https://chat.3211000.xyz"
8
working = True
10
url = "https://chat.3211000.xyz"
11
working = True
9
12
supports_gpt_35_turbo = True
13
supports_message_history = True
10
14
11
15
@classmethod
12
16
async def create_async_generator(
@@ -16,8 +20,6 @@ class Chatxyz(AsyncGeneratorProvider):
16
20
proxy: str = None,
17
21
**kwargs
18
22
) -> AsyncResult:
19
if not model:
20
model = "gpt-3.5-turbo"
21
23
headers = {
22
24
'Accept': 'text/event-stream',
23
25
'Accept-Encoding': 'gzip, deflate, br',
@@ -35,28 +37,17 @@ class Chatxyz(AsyncGeneratorProvider):
35
37
'x-requested-with': 'XMLHttpRequest'
36
38
}
37
39
async with ClientSession(headers=headers) as session:
38
39
system_message = ""
40
user_messages=[]
41
for message in messages:
42
if message["role"] == "system":
43
system_message+=f'{message["content"]}\n'
44
else:
45
user_messages.append(message)
46
new_message = [{'role':'system','content':system_message}]
47
for i in user_messages:
48
new_message.append(i)
49
50
40
data = {
51
"messages": new_message,
41
"messages": messages,
52
42
"stream": True,
53
43
"model": "gpt-3.5-turbo",
54
44
"temperature": 0.5,
55
45
"presence_penalty": 0,
56
46
"frequency_penalty": 0,
57
"top_p": 1
47
"top_p": 1,
48
**kwargs
58
49
}
59
async with session.post(f'{cls.url}/api/openai/v1/chat/completions',json=data) as response:
50
async with session.post(f'{cls.url}/api/openai/v1/chat/completions', json=data, proxy=proxy) as response:
60
51
response.raise_for_status()
61
52
async for chunk in response.content:
62
53
line = chunk.decode()
@@ -64,5 +55,6 @@ class Chatxyz(AsyncGeneratorProvider):
64
55
break
65
56
elif line.startswith("data: "):
66
57
line = json.loads(line[6:])
67
if(line["choices"][0]["delta"]["content"]!=None):
68
yield line["choices"][0]["delta"]["content"]
58
chunk = line["choices"][0]["delta"].get("content")
59
if(chunk):
60
yield chunk