mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-12-24 03:23:49 +03:00
Restored provider (g4f/Provider/nexra/NexraChatGPT4o.py)
This commit is contained in:
parent
b2f4c34fd3
commit
e54e8755fa
@ -1,20 +1,20 @@
|
|||||||
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
|
||||||
@ -22,19 +22,20 @@ class NexraChatGPT4o(AsyncGeneratorProvider, ProviderModelMixin):
|
|||||||
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": [
|
||||||
{
|
{
|
||||||
@ -42,32 +43,43 @@ class NexraChatGPT4o(AsyncGeneratorProvider, ProviderModelMixin):
|
|||||||
"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:
|
||||||
json_obj = json.loads(json_str)
|
content = response.text.lstrip('')
|
||||||
if json_obj.get("finish"):
|
data = json.loads(content)
|
||||||
if last_message:
|
return data.get('message', '')
|
||||||
yield last_message
|
except json.JSONDecodeError:
|
||||||
return
|
return "Error: Unable to decode JSON response"
|
||||||
elif json_obj.get("message"):
|
else:
|
||||||
last_message = json_obj["message"]
|
return f"Error: {response.status_code}"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def process_streaming_response(cls, response):
|
||||||
|
full_message = ""
|
||||||
|
for line in response.iter_lines(decode_unicode=True):
|
||||||
|
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:
|
except json.JSONDecodeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if last_message:
|
|
||||||
yield last_message
|
|
||||||
|
Loading…
Reference in New Issue
Block a user