返回提交历史
Modified
g4f/Provider/Blackbox.py
+97
-15
XFEstudio/gpt4free
feat(g4f/Provider/Blackbox.py): enhance model handling and add web search mode
bd607b14
代码差异
1 个文件
+97
-15
@@ -3,6 +3,7 @@ from __future__ import annotations
3
3
import re
4
4
import random
5
5
import string
6
import json
6
7
from aiohttp import ClientSession
7
8
8
9
from ..typing import AsyncResult, Messages, ImageType
@@ -17,40 +18,105 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
17
18
supports_system_message = True
18
19
supports_message_history = True
19
20
20
default_model = 'blackbox'
21
default_model = 'blackboxai'
21
22
models = [
22
'blackbox',
23
'gemini-1.5-flash',
23
default_model,
24
'blackboxai-pro',
25
24
26
"llama-3.1-8b",
25
27
'llama-3.1-70b',
26
28
'llama-3.1-405b',
27
'ImageGenerationLV45LJp',
29
28
30
'gpt-4o',
31
29
32
'gemini-pro',
33
'gemini-1.5-flash',
34
30
35
'claude-sonnet-3.5',
36
37
'PythonAgent',
38
'JavaAgent',
39
'JavaScriptAgent',
40
'HTMLAgent',
41
'GoogleCloudAgent',
42
'AndroidDeveloper',
43
'SwiftDeveloper',
44
'Next.jsAgent',
45
'MongoDBAgent',
46
'PyTorchAgent',
47
'ReactAgent',
48
'XcodeAgent',
49
'AngularJSAgent',
50
'ImageGeneration',
31
51
]
32
52
33
53
agentMode = {
34
'ImageGenerationLV45LJp': {'mode': True, 'id': "ImageGenerationLV45LJp", 'name': "Image Generation"},
54
'ImageGeneration': {'mode': True, 'id': "ImageGenerationLV45LJp", 'name': "Image Generation"},
35
55
}
36
56
37
57
trendingAgentMode = {
38
"blackbox": {},
58
"blackboxai": {},
39
59
"gemini-1.5-flash": {'mode': True, 'id': 'Gemini'},
40
60
"llama-3.1-8b": {'mode': True, 'id': "llama-3.1-8b"},
41
61
'llama-3.1-70b': {'mode': True, 'id': "llama-3.1-70b"},
42
62
'llama-3.1-405b': {'mode': True, 'id': "llama-3.1-405b"},
63
'blackboxai-pro': {'mode': True, 'id': "BLACKBOXAI-PRO"},
64
'PythonAgent': {'mode': True, 'id': "Python Agent"},
65
'JavaAgent': {'mode': True, 'id': "Java Agent"},
66
'JavaScriptAgent': {'mode': True, 'id': "JavaScript Agent"},
67
'HTMLAgent': {'mode': True, 'id': "HTML Agent"},
68
'GoogleCloudAgent': {'mode': True, 'id': "Google Cloud Agent"},
69
'AndroidDeveloper': {'mode': True, 'id': "Android Developer"},
70
'SwiftDeveloper': {'mode': True, 'id': "Swift Developer"},
71
'Next.jsAgent': {'mode': True, 'id': "Next.js Agent"},
72
'MongoDBAgent': {'mode': True, 'id': "MongoDB Agent"},
73
'PyTorchAgent': {'mode': True, 'id': "PyTorch Agent"},
74
'ReactAgent': {'mode': True, 'id': "React Agent"},
75
'XcodeAgent': {'mode': True, 'id': "Xcode Agent"},
76
'AngularJSAgent': {'mode': True, 'id': "AngularJS Agent"},
43
77
}
44
78
45
79
userSelectedModel = {
46
80
"gpt-4o": "gpt-4o",
47
81
"gemini-pro": "gemini-pro",
48
82
'claude-sonnet-3.5': "claude-sonnet-3.5",
83
'blackboxai-pro': "blackboxai-pro",
84
}
85
86
model_prefixes = {
87
'gpt-4o': '@GPT-4o',
88
'gemini-pro': '@Gemini-PRO',
89
'claude-sonnet-3.5': '@Claude-Sonnet-3.5',
90
91
'PythonAgent': '@Python Agent',
92
'JavaAgent': '@Java Agent',
93
'JavaScriptAgent': '@Java Agent',
94
'HTMLAgent': '@HTML Agent',
95
'GoogleCloudAgent': '@Google Cloud Agent',
96
'AndroidDeveloper': '@Android Developer',
97
'SwiftDeveloper': '@Swift Developer',
98
'Next.jsAgent': '@Next.js Agent',
99
'MongoDBAgent': '@MongoDB Agent',
100
'PyTorchAgent': '@PyTorch Agent',
101
'ReactAgent': '@React Agent',
102
'XcodeAgent': '@Xcode Agent',
103
'AngularJSAgent': '@AngularJS Agent',
104
'blackboxai-pro': '@BLACKBOXAI-PRO',
105
'ImageGeneration': '@Image Generation',
106
}
107
108
model_referers = {
109
"blackboxai": f"{url}/?model=blackboxai",
110
"blackboxai-pro": f"{url}/?model=blackboxai-pro",
111
"gpt-4o": f"{url}/?model=gpt-4o",
112
"gemini-pro": f"{url}/?model=gemini-pro",
113
"claude-sonnet-3.5": f"{url}/?model=claude-sonnet-3.5"
49
114
}
50
115
51
116
model_aliases = {
52
117
"gemini-flash": "gemini-1.5-flash",
53
"flux": "ImageGenerationLV45LJp",
118
"claude-3.5-sonnet": "claude-sonnet-3.5",
119
"flux": "ImageGeneration",
54
120
}
55
121
56
122
@classmethod
@@ -72,6 +138,7 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
72
138
proxy: str = None,
73
139
image: ImageType = None,
74
140
image_name: str = None,
141
webSearchMode: bool = False,
75
142
**kwargs
76
143
) -> AsyncResult:
77
144
model = cls.get_model(model)
@@ -83,7 +150,7 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
83
150
"content-type": "application/json",
84
151
"origin": cls.url,
85
152
"pragma": "no-cache",
86
"referer": f"{cls.url}/",
153
"referer": cls.model_referers.get(model, cls.url),
87
154
"sec-ch-ua": '"Not;A=Brand";v="24", "Chromium";v="128"',
88
155
"sec-ch-ua-mobile": "?0",
89
156
"sec-ch-ua-platform": '"Linux"',
@@ -93,8 +160,8 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
93
160
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
94
161
}
95
162
96
if model in cls.userSelectedModel:
97
prefix = f"@{cls.userSelectedModel[model]}"
163
if model in cls.model_prefixes:
164
prefix = cls.model_prefixes[model]
98
165
if not messages[0]['content'].startswith(prefix):
99
166
messages[0]['content'] = f"{prefix} {messages[0]['content']}"
100
167
@@ -115,9 +182,8 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
115
182
"codeModelMode": True,
116
183
"agentMode": {},
117
184
"trendingAgentMode": {},
118
"userSelectedModel": None,
119
"userSystemPrompt": None,
120
185
"isMicMode": False,
186
"userSystemPrompt": None,
121
187
"maxTokens": 1024,
122
188
"playgroundTopP": 0.9,
123
189
"playgroundTemperature": 0.5,
@@ -128,7 +194,8 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
128
194
"clickedForceWebSearch": False,
129
195
"visitFromDelta": False,
130
196
"mobileClient": False,
131
"webSearchMode": False,
197
"userSelectedModel": None,
198
"webSearchMode": webSearchMode,
132
199
}
133
200
134
201
if model in cls.agentMode:
@@ -140,7 +207,7 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
140
207
141
208
async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
142
209
response.raise_for_status()
143
if model == 'ImageGenerationLV45LJp':
210
if model == 'ImageGeneration':
144
211
response_text = await response.text()
145
212
url_match = re.search(r'https://storage\.googleapis\.com/[^\s\)]+', response_text)
146
213
if url_match:
@@ -149,9 +216,24 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
149
216
else:
150
217
raise Exception("Image URL not found in the response")
151
218
else:
219
full_response = ""
220
search_results_json = ""
152
221
async for chunk in response.content.iter_any():
153
222
if chunk:
154
223
decoded_chunk = chunk.decode()
155
224
decoded_chunk = re.sub(r'\$@\$v=[^$]+\$@\$', '', decoded_chunk)
156
225
if decoded_chunk.strip():
157
yield decoded_chunk
226
if '$~~~$' in decoded_chunk:
227
search_results_json += decoded_chunk
228
else:
229
full_response += decoded_chunk
230
yield decoded_chunk
231
232
if data["webSearchMode"] and search_results_json:
233
match = re.search(r'\$~~~\$(.*?)\$~~~\$', search_results_json, re.DOTALL)
234
if match:
235
search_results = json.loads(match.group(1))
236
formatted_results = "\n\n**Sources:**\n"
237
for i, result in enumerate(search_results[:5], 1):
238
formatted_results += f"{i}. [{result['title']}]({result['link']})\n"
239
yield formatted_results