Skip to content

Commit

Permalink
Added timeout to Bing (#1438)
Browse files Browse the repository at this point in the history
Co-authored-by: H Lohaus <hlohaus@users.noreply.github.com>
  • Loading branch information
Eikosa and hlohaus authored Jan 10, 2024
1 parent bee75be commit fcbe930
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions g4f/Provider/Bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def create_async_generator(
model: str,
messages: Messages,
proxy: str = None,
timeout: int = 900,
cookies: dict = None,
tone: str = Tones.creative,
image: str = None,
Expand All @@ -53,7 +54,7 @@ def create_async_generator(

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):
return "".join(
Expand Down Expand Up @@ -247,13 +248,14 @@ async def stream_generate(
proxy: str = None,
cookies: dict = None,
web_search: bool = False,
gpt4_turbo: bool = False
gpt4_turbo: bool = False,
timeout = int = 900
):
headers = Defaults.headers
if cookies:
headers["Cookie"] = "; ".join(f"{k}={v}" for k, v in cookies.items())
async with ClientSession(
timeout=ClientTimeout(total=900),
timeout=ClientTimeout(total=timeout),
headers=headers
) as session:
conversation = await create_conversation(session, proxy)
Expand All @@ -268,14 +270,14 @@ async def stream_generate(
proxy=proxy
) as wss:
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))

response_txt = ''
returned_text = ''
final = False
while not final:
msg = await wss.receive(timeout=900)
msg = await wss.receive(timeout=timeout)
if not msg.data:
continue
objects = msg.data.split(Defaults.delimiter)
Expand Down Expand Up @@ -311,12 +313,11 @@ async def stream_generate(
if result["value"] == "CaptchaChallenge":
driver = get_browser(proxy=proxy)
try:
for chunk in wait_for_login(driver):
yield chunk
wait_for_login(driver)
cookies = get_driver_cookies(driver)
finally:
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
else:
raise Exception(f"{result['value']}: {result['message']}")
Expand Down

0 comments on commit fcbe930

Please sign in to comment.