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

XFEstudio/gpt4free

Restored providers (g4f/Provider/nexra/NexraChatGptV2.py)

eb52e0b9
kqlio67 <kqlio67@users.noreply.github.com>
提交于

代码差异

1 个文件 +57 -59
Modified g4f/Provider/nexra/NexraChatGptV2.py +57 -59
@@ -1,26 +1,22 @@
1 1 from __future__ import annotations
2 2
3 from aiohttp import ClientSession
4 3 import json
4 import requests
5 5
6 from ...typing import AsyncResult, Messages
7 from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
6 from ...typing import CreateResult, Messages
7 from ..base_provider import ProviderModelMixin, AbstractProvider
8 8 from ..helper import format_prompt
9 9
10
11 class NexraChatGptV2(AsyncGeneratorProvider, ProviderModelMixin):
10 class NexraChatGptV2(AbstractProvider, ProviderModelMixin):
12 11 label = "Nexra ChatGPT v2"
13 12 url = "https://nexra.aryahcr.cc/documentation/chatgpt/en"
14 13 api_endpoint = "https://nexra.aryahcr.cc/api/chat/complements"
15 working = False
14 working = True
16 15 supports_stream = True
17 16
18 17 default_model = 'chatgpt'
19 18 models = [default_model]
20
21 model_aliases = {
22 "gpt-4": "chatgpt",
23 }
19 model_aliases = {"gpt-4": "chatgpt"}
24 20
25 21 @classmethod
26 22 def get_model(cls, model: str) -> str:
@@ -30,63 +26,65 @@ class NexraChatGptV2(AsyncGeneratorProvider, ProviderModelMixin):
30 26 return cls.model_aliases[model]
31 27 else:
32 28 return cls.default_model
33
29
34 30 @classmethod
35 async def create_async_generator(
31 def create_completion(
36 32 cls,
37 33 model: str,
38 34 messages: Messages,
39 proxy: str = None,
40 stream: bool = False,
41 markdown: bool = False,
35 stream: bool,
42 36 **kwargs
43 ) -> AsyncResult:
37 ) -> CreateResult:
44 38 model = cls.get_model(model)
45
39
46 40 headers = {
47 "Content-Type": "application/json"
41 'Content-Type': 'application/json'
42 }
43
44 data = {
45 "messages": [
46 {
47 "role": "user",
48 "content": format_prompt(messages)
49 }
50 ],
51 "stream": stream,
52 "markdown": False,
53 "model": model
48 54 }
55
56 response = requests.post(cls.api_endpoint, headers=headers, json=data, stream=stream)
49 57
50 async with ClientSession(headers=headers) as session:
51 prompt = format_prompt(messages)
52 data = {
53 "messages": [
54 {
55 "role": "user",
56 "content": prompt
57 }
58 ],
59 "stream": stream,
60 "markdown": markdown,
61 "model": model
62 }
58 if stream:
59 return cls.process_streaming_response(response)
60 else:
61 return cls.process_non_streaming_response(response)
63 62
64 async with session.post(f"{cls.api_endpoint}", json=data, proxy=proxy) as response:
65 response.raise_for_status()
63 @classmethod
64 def process_non_streaming_response(cls, response):
65 if response.status_code == 200:
66 try:
67 content = response.text.lstrip('`')
68 data = json.loads(content)
69 return data.get('message', '')
70 except json.JSONDecodeError:
71 return "Error: Unable to decode JSON response"
72 else:
73 return f"Error: {response.status_code}"
66 74
67 if stream:
68 # Streamed response handling (stream=True)
69 collected_message = ""
70 async for chunk in response.content.iter_any():
71 if chunk:
72 decoded_chunk = chunk.decode().strip().split("\x1e")
73 for part in decoded_chunk:
74 if part:
75 message_data = json.loads(part)
76
77 # Collect messages until 'finish': true
78 if 'message' in message_data and message_data['message']:
79 collected_message = message_data['message']
80
81 # When finish is true, yield the final collected message
82 if message_data.get('finish', False):
83 yield collected_message
84 return
85 else:
86 # Non-streamed response handling (stream=False)
87 response_data = await response.json(content_type=None)
88
89 # Yield the message directly from the response
90 if 'message' in response_data and response_data['message']:
91 yield response_data['message']
92 return
75 @classmethod
76 def process_streaming_response(cls, response):
77 full_message = ""
78 for line in response.iter_lines(decode_unicode=True):
79 if line:
80 try:
81 line = line.lstrip('`')
82 data = json.loads(line)
83 if data.get('finish'):
84 break
85 message = data.get('message', '')
86 if message:
87 yield message[len(full_message):]
88 full_message = message
89 except json.JSONDecodeError:
90 pass