返回提交历史
Added
g4f/Provider/DarkAI.py
+87
-0
XFEstudio/gpt4free
feat(g4f/Provider/DarkAI.py): add new DarkAI provider
f63d1566
代码差异
1 个文件
+87
-0
@@ -0,0 +1,87 @@
1
from __future__ import annotations
2
3
import json
4
from aiohttp import ClientSession
5
6
from ..typing import AsyncResult, Messages
7
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
8
from .helper import format_prompt
9
10
11
class DarkAI(AsyncGeneratorProvider, ProviderModelMixin):
12
url = "https://www.aiuncensored.info"
13
api_endpoint = "https://darkai.foundation/chat"
14
working = True
15
supports_gpt_35_turbo = True
16
supports_gpt_4 = True
17
supports_stream = True
18
supports_system_message = True
19
supports_message_history = True
20
21
default_model = 'gpt-4o'
22
models = [
23
default_model, # Uncensored
24
'gpt-3.5-turbo', # Uncensored
25
'llama-3-70b', # Uncensored
26
'llama-3-405b',
27
]
28
29
model_aliases = {
30
"llama-3.1-70b": "llama-3-70b",
31
"llama-3.1-405b": "llama-3-405b",
32
}
33
34
@classmethod
35
def get_model(cls, model: str) -> str:
36
if model in cls.models:
37
return model
38
elif model in cls.model_aliases:
39
return cls.model_aliases[model]
40
else:
41
return cls.default_model
42
43
@classmethod
44
async def create_async_generator(
45
cls,
46
model: str,
47
messages: Messages,
48
proxy: str = None,
49
**kwargs
50
) -> AsyncResult:
51
model = cls.get_model(model)
52
53
headers = {
54
"accept": "text/event-stream",
55
"content-type": "application/json",
56
"origin": "https://www.aiuncensored.info",
57
"referer": "https://www.aiuncensored.info/",
58
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
59
}
60
async with ClientSession(headers=headers) as session:
61
prompt = format_prompt(messages)
62
data = {
63
"query": prompt,
64
"model": model,
65
}
66
async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
67
response.raise_for_status()
68
full_text = ""
69
async for chunk in response.content:
70
if chunk:
71
try:
72
chunk_str = chunk.decode().strip()
73
if chunk_str.startswith('data: '):
74
chunk_data = json.loads(chunk_str[6:])
75
if chunk_data['event'] == 'text-chunk':
76
full_text += chunk_data['data']['text']
77
elif chunk_data['event'] == 'stream-end':
78
if full_text:
79
yield full_text.strip()
80
return
81
except json.JSONDecodeError:
82
print(f"Failed to decode JSON: {chunk_str}")
83
except Exception as e:
84
print(f"Error processing chunk: {e}")
85
86
if full_text:
87
yield full_text.strip()