返回提交历史
Modified
g4f/Provider/Bing.py
+9
-8
XFEstudio/gpt4free
Added timeout to Bing (#1438)
Co-authored-by: H Lohaus <hlohaus@users.noreply.github.com>
fcbe9300
代码差异
1 个文件
+9
-8
@@ -31,6 +31,7 @@ class Bing(AsyncGeneratorProvider):
31
31
model: str,
32
32
messages: Messages,
33
33
proxy: str = None,
34
timeout: int = 900,
34
35
cookies: dict = None,
35
36
tone: str = Tones.creative,
36
37
image: str = None,
@@ -53,7 +54,7 @@ class Bing(AsyncGeneratorProvider):
53
54
54
55
gpt4_turbo = True if model.startswith("gpt-4-turbo") else False
55
56
56
return stream_generate(prompt, tone, image, context, proxy, cookies, web_search, gpt4_turbo)
57
return stream_generate(prompt, tone, image, context, proxy, cookies, web_search, gpt4_turbo, timeout)
57
58
58
59
def create_context(messages: Messages):
59
60
return "".join(
@@ -247,13 +248,14 @@ async def stream_generate(
247
248
proxy: str = None,
248
249
cookies: dict = None,
249
250
web_search: bool = False,
250
gpt4_turbo: bool = False
251
gpt4_turbo: bool = False,
252
timeout = int = 900
251
253
):
252
254
headers = Defaults.headers
253
255
if cookies:
254
256
headers["Cookie"] = "; ".join(f"{k}={v}" for k, v in cookies.items())
255
257
async with ClientSession(
256
timeout=ClientTimeout(total=900),
258
timeout=ClientTimeout(total=timeout),
257
259
headers=headers
258
260
) as session:
259
261
conversation = await create_conversation(session, proxy)
@@ -268,14 +270,14 @@ async def stream_generate(
268
270
proxy=proxy
269
271
) as wss:
270
272
await wss.send_str(format_message({'protocol': 'json', 'version': 1}))
271
await wss.receive(timeout=900)
273
await wss.receive(timeout=timeout)
272
274
await wss.send_str(create_message(conversation, prompt, tone, context, image_info, web_search, gpt4_turbo))
273
275
274
276
response_txt = ''
275
277
returned_text = ''
276
278
final = False
277
279
while not final:
278
msg = await wss.receive(timeout=900)
280
msg = await wss.receive(timeout=timeout)
279
281
if not msg.data:
280
282
continue
281
283
objects = msg.data.split(Defaults.delimiter)
@@ -311,12 +313,11 @@ async def stream_generate(
311
313
if result["value"] == "CaptchaChallenge":
312
314
driver = get_browser(proxy=proxy)
313
315
try:
314
for chunk in wait_for_login(driver):
315
yield chunk
316
wait_for_login(driver)
316
317
cookies = get_driver_cookies(driver)
317
318
finally:
318
319
driver.quit()
319
async for chunk in stream_generate(prompt, tone, image, context, proxy, cookies, web_search, gpt4_turbo):
320
async for chunk in stream_generate(prompt, tone, image, context, proxy, cookies, web_search, gpt4_turbo, timeout):
320
321
yield chunk
321
322
else:
322
323
raise Exception(f"{result['value']}: {result['message']}")