返回提交历史
Modified
g4f/Provider/Allyfy.py
+52
-36
XFEstudio/gpt4free
Update (g4f/Provider/Allyfy.py)
c01e3b6b
代码差异
1 个文件
+52
-36
@@ -1,17 +1,28 @@
1
1
from __future__ import annotations
2
3
from aiohttp import ClientSession
2
import aiohttp
3
import asyncio
4
4
import json
5
5
import uuid
6
from aiohttp import ClientSession
6
7
from ..typing import AsyncResult, Messages
7
from .base_provider import AsyncGeneratorProvider
8
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
8
9
from .helper import format_prompt
9
10
10
11
11
class Allyfy(AsyncGeneratorProvider):
12
class Allyfy(AsyncGeneratorProvider, ProviderModelMixin):
12
13
url = "https://allyfy.chat"
13
14
api_endpoint = "https://chatbot.allyfy.chat/api/v1/message/stream/super/chat"
14
15
working = True
16
supports_stream = True
17
supports_system_message = True
18
supports_message_history = True
19
20
default_model = 'gpt-3.5-turbo'
21
models = [default_model]
22
23
@classmethod
24
def get_model(cls, model: str) -> str:
25
return cls.default_model
15
26
16
27
@classmethod
17
28
async def create_async_generator(
@@ -21,50 +32,55 @@ class Allyfy(AsyncGeneratorProvider):
21
32
proxy: str = None,
22
33
**kwargs
23
34
) -> AsyncResult:
35
model = cls.get_model(model)
36
client_id = str(uuid.uuid4())
37
24
38
headers = {
25
"accept": "text/event-stream",
26
"accept-language": "en-US,en;q=0.9",
27
"content-type": "application/json;charset=utf-8",
28
"dnt": "1",
29
"origin": "https://www.allyfy.chat",
30
"priority": "u=1, i",
31
"referer": "https://www.allyfy.chat/",
32
"referrer": "https://www.allyfy.chat",
33
'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126"',
39
'accept': 'text/event-stream',
40
'accept-language': 'en-US,en;q=0.9',
41
'cache-control': 'no-cache',
42
'content-type': 'application/json;charset=utf-8',
43
'origin': cls.url,
44
'pragma': 'no-cache',
45
'priority': 'u=1, i',
46
'referer': f"{cls.url}/",
47
'referrer': cls.url,
48
'sec-ch-ua': '"Not?A_Brand";v="99", "Chromium";v="130"',
34
49
'sec-ch-ua-mobile': '?0',
35
50
'sec-ch-ua-platform': '"Linux"',
36
"sec-fetch-dest": "empty",
37
"sec-fetch-mode": "cors",
38
"sec-fetch-site": "same-site",
39
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
51
'sec-fetch-dest': 'empty',
52
'sec-fetch-mode': 'cors',
53
'sec-fetch-site': 'same-site',
54
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'
40
55
}
56
41
57
async with ClientSession(headers=headers) as session:
42
58
prompt = format_prompt(messages)
43
59
data = {
44
"messages": [{"content": prompt, "role": "user"}],
60
"messages": messages,
45
61
"content": prompt,
46
62
"baseInfo": {
47
"clientId": "q08kdrde1115003lyedfoir6af0yy531",
63
"clientId": client_id,
48
64
"pid": "38281",
49
65
"channelId": "100000",
50
66
"locale": "en-US",
51
"localZone": 180,
67
"localZone": 120,
52
68
"packageName": "com.cch.allyfy.webh",
53
69
}
54
70
}
55
async with session.post(f"{cls.api_endpoint}", json=data, proxy=proxy) as response:
71
72
async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
56
73
response.raise_for_status()
57
full_response = []
58
async for line in response.content:
59
line = line.decode().strip()
60
if line.startswith("data:"):
61
data_content = line[5:]
62
if data_content == "[DONE]":
63
break
64
try:
65
json_data = json.loads(data_content)
66
if "content" in json_data:
67
full_response.append(json_data["content"])
68
except json.JSONDecodeError:
69
continue
70
yield "".join(full_response)
74
response_text = await response.text()
75
76
filtered_response = []
77
for line in response_text.splitlines():
78
if line.startswith('data:'):
79
content = line[5:]
80
if content and 'code' in content:
81
json_content = json.loads(content)
82
if json_content['content']:
83
filtered_response.append(json_content['content'])
84
85
final_response = ''.join(filtered_response)
86
yield final_response