2024-09-29 23:38:25 +03:00
|
|
|
from __future__ import annotations
|
2024-10-11 09:33:30 +03:00
|
|
|
|
|
|
|
import json
|
2024-10-21 20:57:40 +03:00
|
|
|
import requests
|
2024-10-11 09:33:30 +03:00
|
|
|
|
2024-10-21 20:57:40 +03:00
|
|
|
from ...typing import CreateResult, Messages
|
|
|
|
from ..base_provider import ProviderModelMixin, AbstractProvider
|
2024-09-29 23:38:25 +03:00
|
|
|
from ..helper import format_prompt
|
2024-10-11 09:33:30 +03:00
|
|
|
|
2024-10-21 20:57:40 +03:00
|
|
|
class NexraChatGPT(AbstractProvider, ProviderModelMixin):
|
2024-09-29 23:38:25 +03:00
|
|
|
label = "Nexra ChatGPT"
|
2024-10-11 09:33:30 +03:00
|
|
|
url = "https://nexra.aryahcr.cc/documentation/chatgpt/en"
|
2024-09-29 23:38:25 +03:00
|
|
|
api_endpoint = "https://nexra.aryahcr.cc/api/chat/gpt"
|
2024-10-21 20:57:40 +03:00
|
|
|
working = True
|
2024-10-11 09:33:30 +03:00
|
|
|
|
|
|
|
default_model = 'gpt-3.5-turbo'
|
2024-10-21 20:57:40 +03:00
|
|
|
models = ['gpt-4', 'gpt-4-0613', 'gpt-4-0314', 'gpt-4-32k-0314', default_model, 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-16k-0613', 'gpt-3.5-turbo-0301', 'text-davinci-003', 'text-davinci-002', 'code-davinci-002', 'gpt-3', 'text-curie-001', 'text-babbage-001', 'text-ada-001', 'davinci', 'curie', 'babbage', 'ada', 'babbage-002', 'davinci-002']
|
2024-10-11 09:33:30 +03:00
|
|
|
|
|
|
|
model_aliases = {
|
|
|
|
"gpt-4": "gpt-4-0613",
|
|
|
|
"gpt-4": "gpt-4-32k",
|
|
|
|
"gpt-4": "gpt-4-0314",
|
|
|
|
"gpt-4": "gpt-4-32k-0314",
|
|
|
|
|
|
|
|
"gpt-3.5-turbo": "gpt-3.5-turbo-16k",
|
|
|
|
"gpt-3.5-turbo": "gpt-3.5-turbo-0613",
|
|
|
|
"gpt-3.5-turbo": "gpt-3.5-turbo-16k-0613",
|
|
|
|
"gpt-3.5-turbo": "gpt-3.5-turbo-0301",
|
|
|
|
|
|
|
|
"gpt-3": "text-davinci-003",
|
|
|
|
"gpt-3": "text-davinci-002",
|
|
|
|
"gpt-3": "code-davinci-002",
|
|
|
|
"gpt-3": "text-curie-001",
|
|
|
|
"gpt-3": "text-babbage-001",
|
|
|
|
"gpt-3": "text-ada-001",
|
|
|
|
"gpt-3": "text-ada-001",
|
|
|
|
"gpt-3": "davinci",
|
|
|
|
"gpt-3": "curie",
|
|
|
|
"gpt-3": "babbage",
|
|
|
|
"gpt-3": "ada",
|
|
|
|
"gpt-3": "babbage-002",
|
|
|
|
"gpt-3": "davinci-002",
|
|
|
|
}
|
2024-09-29 23:38:25 +03:00
|
|
|
|
2024-10-11 09:33:30 +03:00
|
|
|
@classmethod
|
|
|
|
def get_model(cls, model: str) -> str:
|
|
|
|
if model in cls.models:
|
|
|
|
return model
|
|
|
|
elif model in cls.model_aliases:
|
|
|
|
return cls.model_aliases[model]
|
|
|
|
else:
|
|
|
|
return cls.default_model
|
2024-10-21 20:57:40 +03:00
|
|
|
|
2024-09-29 23:38:25 +03:00
|
|
|
@classmethod
|
2024-10-21 20:57:40 +03:00
|
|
|
def create_completion(
|
2024-09-29 23:38:25 +03:00
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
2024-10-22 13:46:36 +03:00
|
|
|
proxy: str = None,
|
2024-10-21 21:45:40 +03:00
|
|
|
markdown: bool = False,
|
2024-09-29 23:38:25 +03:00
|
|
|
**kwargs
|
2024-10-21 20:57:40 +03:00
|
|
|
) -> CreateResult:
|
2024-10-11 09:33:30 +03:00
|
|
|
model = cls.get_model(model)
|
2024-10-21 20:57:40 +03:00
|
|
|
|
2024-09-29 23:38:25 +03:00
|
|
|
headers = {
|
2024-10-21 20:57:40 +03:00
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"messages": [],
|
|
|
|
"prompt": format_prompt(messages),
|
|
|
|
"model": model,
|
2024-10-21 21:45:40 +03:00
|
|
|
"markdown": markdown
|
2024-09-29 23:38:25 +03:00
|
|
|
}
|
2024-10-21 20:57:40 +03:00
|
|
|
|
|
|
|
response = requests.post(cls.api_endpoint, headers=headers, json=data)
|
|
|
|
|
|
|
|
return cls.process_response(response)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def process_response(cls, response):
|
|
|
|
if response.status_code == 200:
|
|
|
|
try:
|
|
|
|
data = response.json()
|
|
|
|
return data.get('gpt', '')
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
return "Error: Unable to decode JSON response"
|
|
|
|
else:
|
|
|
|
return f"Error: {response.status_code}"
|