Added timeout to Bing (#1438)

Co-authored-by: H Lohaus <hlohaus@users.noreply.github.com>
This commit is contained in:
Eikosa 2024-01-10 12:41:15 +03:00 committed by GitHub
parent bee75be8e3
commit fcbe930020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,6 +31,7 @@ class Bing(AsyncGeneratorProvider):
model: str, model: str,
messages: Messages, messages: Messages,
proxy: str = None, proxy: str = None,
timeout: int = 900,
cookies: dict = None, cookies: dict = None,
tone: str = Tones.creative, tone: str = Tones.creative,
image: str = None, image: str = None,
@ -53,7 +54,7 @@ class Bing(AsyncGeneratorProvider):
gpt4_turbo = True if model.startswith("gpt-4-turbo") else False gpt4_turbo = True if model.startswith("gpt-4-turbo") else False
return stream_generate(prompt, tone, image, context, proxy, cookies, web_search, gpt4_turbo) return stream_generate(prompt, tone, image, context, proxy, cookies, web_search, gpt4_turbo, timeout)
def create_context(messages: Messages): def create_context(messages: Messages):
return "".join( return "".join(
@ -247,13 +248,14 @@ async def stream_generate(
proxy: str = None, proxy: str = None,
cookies: dict = None, cookies: dict = None,
web_search: bool = False, web_search: bool = False,
gpt4_turbo: bool = False gpt4_turbo: bool = False,
timeout = int = 900
): ):
headers = Defaults.headers headers = Defaults.headers
if cookies: if cookies:
headers["Cookie"] = "; ".join(f"{k}={v}" for k, v in cookies.items()) headers["Cookie"] = "; ".join(f"{k}={v}" for k, v in cookies.items())
async with ClientSession( async with ClientSession(
timeout=ClientTimeout(total=900), timeout=ClientTimeout(total=timeout),
headers=headers headers=headers
) as session: ) as session:
conversation = await create_conversation(session, proxy) conversation = await create_conversation(session, proxy)
@ -268,14 +270,14 @@ async def stream_generate(
proxy=proxy proxy=proxy
) as wss: ) as wss:
await wss.send_str(format_message({'protocol': 'json', 'version': 1})) await wss.send_str(format_message({'protocol': 'json', 'version': 1}))
await wss.receive(timeout=900) await wss.receive(timeout=timeout)
await wss.send_str(create_message(conversation, prompt, tone, context, image_info, web_search, gpt4_turbo)) await wss.send_str(create_message(conversation, prompt, tone, context, image_info, web_search, gpt4_turbo))
response_txt = '' response_txt = ''
returned_text = '' returned_text = ''
final = False final = False
while not final: while not final:
msg = await wss.receive(timeout=900) msg = await wss.receive(timeout=timeout)
if not msg.data: if not msg.data:
continue continue
objects = msg.data.split(Defaults.delimiter) objects = msg.data.split(Defaults.delimiter)
@ -311,12 +313,11 @@ async def stream_generate(
if result["value"] == "CaptchaChallenge": if result["value"] == "CaptchaChallenge":
driver = get_browser(proxy=proxy) driver = get_browser(proxy=proxy)
try: try:
for chunk in wait_for_login(driver): wait_for_login(driver)
yield chunk
cookies = get_driver_cookies(driver) cookies = get_driver_cookies(driver)
finally: finally:
driver.quit() driver.quit()
async for chunk in stream_generate(prompt, tone, image, context, proxy, cookies, web_search, gpt4_turbo): async for chunk in stream_generate(prompt, tone, image, context, proxy, cookies, web_search, gpt4_turbo, timeout):
yield chunk yield chunk
else: else:
raise Exception(f"{result['value']}: {result['message']}") raise Exception(f"{result['value']}: {result['message']}")