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

XFEstudio/gpt4free

Added new Provider Chatxyz (#1393)

* Added fucntionality for Provider * Added Provider in init * System message if present must be the first object in message array

eb1e91d1
Debaditya Banerji <77636021+devAdityaa@users.noreply.github.com>
提交于

代码差异

2 个文件 +69 -0
Added g4f/Provider/Chatxyz.py +68 -0
@@ -0,0 +1,68 @@
1 from __future__ import annotations
2 from aiohttp import ClientSession
3 from ..typing import AsyncResult, Messages
4 from .base_provider import AsyncGeneratorProvider
5 import json
6 class Chatxyz(AsyncGeneratorProvider):
7 url = "https://chat.3211000.xyz"
8 working = True
9 supports_gpt_35_turbo = True
10
11 @classmethod
12 async def create_async_generator(
13 cls,
14 model: str,
15 messages: Messages,
16 proxy: str = None,
17 **kwargs
18 ) -> AsyncResult:
19 if not model:
20 model = "gpt-3.5-turbo"
21 headers = {
22 'Accept': 'text/event-stream',
23 'Accept-Encoding': 'gzip, deflate, br',
24 'Accept-Language': 'en-US,en;q=0.5',
25 'Alt-Used': 'chat.3211000.xyz',
26 'Content-Type': 'application/json',
27 'Host': 'chat.3211000.xyz',
28 'Origin': 'https://chat.3211000.xyz',
29 'Referer': 'https://chat.3211000.xyz/',
30 'Sec-Fetch-Dest': 'empty',
31 'Sec-Fetch-Mode': 'cors',
32 'Sec-Fetch-Site': 'same-origin',
33 'TE': 'trailers',
34 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0',
35 'x-requested-with': 'XMLHttpRequest'
36 }
37 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 data = {
51 "messages": new_message,
52 "stream": True,
53 "model": "gpt-3.5-turbo",
54 "temperature": 0.5,
55 "presence_penalty": 0,
56 "frequency_penalty": 0,
57 "top_p": 1
58 }
59 async with session.post(f'{cls.url}/api/openai/v1/chat/completions',json=data) as response:
60 response.raise_for_status()
61 async for chunk in response.content:
62 line = chunk.decode()
63 if line.startswith("data: [DONE]"):
64 break
65 elif line.startswith("data: "):
66 line = json.loads(line[6:])
67 if(line["choices"][0]["delta"]["content"]!=None):
68 yield line["choices"][0]["delta"]["content"]
Modified g4f/Provider/__init__.py +1 -0
@@ -25,6 +25,7 @@ from .ChatgptFree import ChatgptFree
25 25 from .ChatgptLogin import ChatgptLogin
26 26 from .ChatgptNext import ChatgptNext
27 27 from .ChatgptX import ChatgptX
28 from .Chatxyz import Chatxyz
28 29 from .DeepInfra import DeepInfra
29 30 from .FakeGpt import FakeGpt
30 31 from .FreeGpt import FreeGpt