Restored provider (g4f/Provider/nexra/NexraChatGPT4o.py)

This commit is contained in:
kqlio67 2024-10-21 21:41:17 +03:00
parent b2f4c34fd3
commit e54e8755fa

View File

@ -1,73 +1,85 @@
from __future__ import annotations from __future__ import annotations
from aiohttp import ClientSession
from ...typing import AsyncResult, Messages
from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
from ..helper import format_prompt
import json import json
import requests
class NexraChatGPT4o(AsyncGeneratorProvider, ProviderModelMixin): from ...typing import CreateResult, Messages
from ..base_provider import ProviderModelMixin, AbstractProvider
from ..helper import format_prompt
class NexraChatGPT4o(AbstractProvider, ProviderModelMixin):
label = "Nexra ChatGPT4o" label = "Nexra ChatGPT4o"
url = "https://nexra.aryahcr.cc/documentation/chatgpt/en" url = "https://nexra.aryahcr.cc/documentation/chatgpt/en"
api_endpoint = "https://nexra.aryahcr.cc/api/chat/complements" api_endpoint = "https://nexra.aryahcr.cc/api/chat/complements"
working = False working = True
supports_stream = False supports_stream = True
default_model = 'gpt-4o' default_model = "gpt-4o"
models = [default_model] models = [default_model]
@classmethod @classmethod
def get_model(cls, model: str) -> str: def get_model(cls, model: str) -> str:
return cls.default_model return cls.default_model
@classmethod @classmethod
async def create_async_generator( def create_completion(
cls, cls,
model: str, model: str,
messages: Messages, messages: Messages,
proxy: str = None, stream: bool,
markdown: bool = False,
**kwargs **kwargs
) -> AsyncResult: ) -> CreateResult:
model = cls.get_model(model) model = cls.get_model(model)
headers = { headers = {
"Content-Type": "application/json", 'Content-Type': 'application/json'
} }
async with ClientSession(headers=headers) as session:
data = { data = {
"messages": [ "messages": [
{ {
"role": "user", "role": "user",
"content": format_prompt(messages) "content": format_prompt(messages)
} }
], ],
"stream": False, "stream": stream,
"markdown": False, "markdown": markdown,
"model": model "model": model
} }
async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
response.raise_for_status() response = requests.post(cls.api_endpoint, headers=headers, json=data, stream=stream)
buffer = ""
last_message = "" if stream:
async for chunk in response.content.iter_any(): return cls.process_streaming_response(response)
chunk_str = chunk.decode() else:
buffer += chunk_str return cls.process_non_streaming_response(response)
while '{' in buffer and '}' in buffer:
start = buffer.index('{') @classmethod
end = buffer.index('}', start) + 1 def process_non_streaming_response(cls, response):
json_str = buffer[start:end] if response.status_code == 200:
buffer = buffer[end:] try:
try: content = response.text.lstrip('')
json_obj = json.loads(json_str) data = json.loads(content)
if json_obj.get("finish"): return data.get('message', '')
if last_message: except json.JSONDecodeError:
yield last_message return "Error: Unable to decode JSON response"
return else:
elif json_obj.get("message"): return f"Error: {response.status_code}"
last_message = json_obj["message"]
except json.JSONDecodeError: @classmethod
pass def process_streaming_response(cls, response):
full_message = ""
if last_message: for line in response.iter_lines(decode_unicode=True):
yield last_message if line:
try:
line = line.lstrip('')
data = json.loads(line)
if data.get('finish'):
break
message = data.get('message', '')
if message and message != full_message:
yield message[len(full_message):]
full_message = message
except json.JSONDecodeError:
pass