返回提交历史
Added
g4f/Provider/Mhystical.py
+88
-0
Modified
g4f/Provider/__init__.py
+1
-0
XFEstudio/gpt4free
Added the best AI Provider there exists
61f93923
代码差异
2 个文件
+89
-0
@@ -0,0 +1,88 @@
1
from __future__ import annotations
2
3
import json
4
import logging
5
from aiohttp import ClientSession
6
from ..typing import AsyncResult, Messages
7
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
8
from .helper import format_prompt
9
10
"""
11
Mhystical.cc
12
~~~~~~~~~~~~
13
Author: NoelP.dev
14
Last Updated: 2024-05-11
15
16
Author Site: https://noelp.dev
17
Provider Site: https://mhystical.cc
18
19
"""
20
21
class Mhystical(AsyncGeneratorProvider, ProviderModelMixin):
22
url = "https://api.mhystical.cc"
23
api_endpoint = "https://api.mhystical.cc/v1/completions"
24
working = True
25
supports_stream = False # Set to False, as streaming is not specified in ChatifyAI
26
supports_system_message = False
27
supports_message_history = True
28
29
default_model = 'gpt-4'
30
models = [default_model]
31
model_aliases = {}
32
33
@classmethod
34
def get_model(cls, model: str) -> str:
35
if model in cls.models:
36
return model
37
elif model in cls.model_aliases:
38
return cls.model_aliases.get(model, cls.default_model)
39
else:
40
return cls.default_model
41
42
@classmethod
43
async def create_async_generator(
44
cls,
45
model: str,
46
messages: Messages,
47
proxy: str = None,
48
**kwargs
49
) -> AsyncResult:
50
model = cls.get_model(model)
51
52
headers = {
53
"x-api-key": "mhystical",
54
"Content-Type": "application/json",
55
"accept": "*/*",
56
"cache-control": "no-cache",
57
"origin": cls.url,
58
"referer": f"{cls.url}/",
59
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
60
}
61
62
async with ClientSession(headers=headers) as session:
63
data = {
64
"model": model,
65
"messages": [{"role": "user", "content": format_prompt(messages)}]
66
}
67
async with session.post(cls.api_endpoint, json=data, headers=headers, proxy=proxy) as response:
68
if response.status == 400:
69
yield "Error: API key is missing"
70
elif response.status == 429:
71
yield "Error: Rate limit exceeded"
72
elif response.status == 500:
73
yield "Error: Internal server error"
74
else:
75
response.raise_for_status()
76
response_text = await response.text()
77
filtered_response = cls.filter_response(response_text)
78
yield filtered_response
79
80
@staticmethod
81
def filter_response(response_text: str) -> str:
82
try:
83
json_response = json.loads(response_text)
84
message_content = json_response["choices"][0]["message"]["content"]
85
return message_content
86
except (KeyError, IndexError, json.JSONDecodeError) as e:
87
logging.error("Error parsing response: %s", e)
88
return "Error: Failed to parse response from API."
@@ -70,6 +70,7 @@ from .TeachAnything import TeachAnything
70
70
from .Upstage import Upstage
71
71
from .WhiteRabbitNeo import WhiteRabbitNeo
72
72
from .You import You
73
from .Mhystical import Mhystical
73
74
74
75
import sys
75
76