返回提交历史
Modified
g4f/Provider/Bing.py
+45
-33
XFEstudio/gpt4free
Bing Updates: Add retry create conversation Removed get_cookies fallback Add creative tone parameter
252f595b
代码差异
1 个文件
+45
-33
@@ -6,8 +6,12 @@ import os
6
6
import urllib.parse
7
7
from aiohttp import ClientSession, ClientTimeout
8
8
from ..typing import AsyncGenerator
9
from .base_provider import AsyncGeneratorProvider, get_cookies
9
from .base_provider import AsyncGeneratorProvider
10
10
11
class Tones():
12
creative = "Creative"
13
balanced = "Balanced"
14
precise = "Precise"
11
15
12
16
class Bing(AsyncGeneratorProvider):
13
17
url = "https://bing.com/chat"
@@ -16,12 +20,13 @@ class Bing(AsyncGeneratorProvider):
16
20
17
21
@staticmethod
18
22
def create_async_generator(
19
model: str,
20
messages: list[dict[str, str]],
21
cookies: dict = None, **kwargs) -> AsyncGenerator:
23
model: str,
24
messages: list[dict[str, str]],
25
cookies: dict = None,
26
tone: str = Tones.creative,
27
**kwargs
28
) -> AsyncGenerator:
22
29
23
if not cookies:
24
cookies = get_cookies(".bing.com")
25
30
if len(messages) < 2:
26
31
prompt = messages[0]["content"]
27
32
context = None
@@ -38,7 +43,7 @@ class Bing(AsyncGeneratorProvider):
38
43
'SRCHUSR' : '',
39
44
'SRCHHPGUSR' : '',
40
45
}
41
return stream_generate(prompt, context, cookies)
46
return stream_generate(prompt, tone, context, cookies)
42
47
43
48
def create_context(messages: list[dict[str, str]]):
44
49
context = "".join(f"[{message['role']}](#message)\n{message['content']}\n\n" for message in messages)
@@ -64,6 +69,14 @@ async def create_conversation(session: ClientSession) -> Conversation:
64
69
65
70
return Conversation(conversationId, clientId, conversationSignature)
66
71
72
async def retry_conversation(session: ClientSession) -> Conversation:
73
for _ in range(5):
74
try:
75
return await create_conversation(session)
76
except:
77
session.cookie_jar.clear()
78
return await create_conversation(session)
79
67
80
async def list_conversations(session: ClientSession) -> list:
68
81
url = "https://www.bing.com/turing/conversation/chats"
69
82
async with session.get(url) as response:
@@ -162,34 +175,32 @@ class Defaults:
162
175
'x-forwarded-for': ip_address,
163
176
}
164
177
165
optionsSets = {
166
"optionsSets": [
167
'saharasugg',
168
'enablenewsfc',
169
'clgalileo',
170
'gencontentv3',
171
"nlu_direct_response_filter",
172
"deepleo",
173
"disable_emoji_spoken_text",
174
"responsible_ai_policy_235",
175
"enablemm",
176
"h3precise"
177
"dtappid",
178
"cricinfo",
179
"cricinfov2",
180
"dv3sugg",
181
"nojbfedge"
182
]
183
}
178
optionsSets = [
179
'saharasugg',
180
'enablenewsfc',
181
'clgalileo',
182
'gencontentv3',
183
"nlu_direct_response_filter",
184
"deepleo",
185
"disable_emoji_spoken_text",
186
"responsible_ai_policy_235",
187
"enablemm",
188
"h3precise"
189
"dtappid",
190
"cricinfo",
191
"cricinfov2",
192
"dv3sugg",
193
"nojbfedge"
194
]
184
195
185
196
def format_message(msg: dict) -> str:
186
197
return json.dumps(msg, ensure_ascii=False) + Defaults.delimiter
187
198
188
def create_message(conversation: Conversation, prompt: str, context: str=None) -> str:
199
def create_message(conversation: Conversation, prompt: str, tone: str, context: str=None) -> str:
189
200
struct = {
190
201
'arguments': [
191
202
{
192
**Defaults.optionsSets,
203
'optionsSets': Defaults.optionsSets,
193
204
'source': 'cib',
194
205
'allowedMessageTypes': Defaults.allowedMessageTypes,
195
206
'sliceIds': Defaults.sliceIds,
@@ -201,6 +212,7 @@ def create_message(conversation: Conversation, prompt: str, context: str=None) -
201
212
'text': prompt,
202
213
'messageType': 'Chat'
203
214
},
215
'tone': tone,
204
216
'conversationSignature': conversation.conversationSignature,
205
217
'participant': {
206
218
'id': conversation.clientId
@@ -225,15 +237,16 @@ def create_message(conversation: Conversation, prompt: str, context: str=None) -
225
237
226
238
async def stream_generate(
227
239
prompt: str,
240
tone: str,
228
241
context: str=None,
229
cookies: dict=None
242
cookies: dict=None,
230
243
):
231
244
async with ClientSession(
232
245
timeout=ClientTimeout(total=900),
233
246
cookies=cookies,
234
247
headers=Defaults.headers,
235
248
) as session:
236
conversation = await create_conversation(session)
249
conversation = await retry_conversation(session)
237
250
try:
238
251
async with session.ws_connect(
239
252
'wss://sydney.bing.com/sydney/ChatHub',
@@ -243,7 +256,7 @@ async def stream_generate(
243
256
await wss.send_str(format_message({'protocol': 'json', 'version': 1}))
244
257
msg = await wss.receive(timeout=900)
245
258
246
await wss.send_str(create_message(conversation, prompt, context))
259
await wss.send_str(create_message(conversation, prompt, tone, context))
247
260
248
261
response_txt = ''
249
262
returned_text = ''
@@ -281,7 +294,6 @@ async def stream_generate(
281
294
result = response['item']['result']
282
295
if result.get('error'):
283
296
raise Exception(f"{result['value']}: {result['message']}")
284
final = True
285
break
297
return
286
298
finally:
287
299
await delete_conversation(session, conversation)