From 8864b70ee4bd405d9405263a63e3ada793b30ac1 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Tue, 23 Jan 2024 23:48:11 +0100 Subject: Add aiohttp_socks support --- g4f/Provider/Bing.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'g4f/Provider/Bing.py') diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py index b869a6ef..751b0d3a 100644 --- a/g4f/Provider/Bing.py +++ b/g4f/Provider/Bing.py @@ -6,7 +6,7 @@ import os import uuid import time from urllib import parse -from aiohttp import ClientSession, ClientTimeout +from aiohttp import ClientSession, ClientTimeout, BaseConnector from ..typing import AsyncResult, Messages, ImageType from ..image import ImageResponse @@ -39,6 +39,7 @@ class Bing(AsyncGeneratorProvider): proxy: str = None, timeout: int = 900, cookies: dict = None, + connector: BaseConnector = None, tone: str = Tones.balanced, image: ImageType = None, web_search: bool = False, @@ -67,8 +68,15 @@ class Bing(AsyncGeneratorProvider): cookies = {**Defaults.cookies, **cookies} if cookies else Defaults.cookies gpt4_turbo = True if model.startswith("gpt-4-turbo") else False + + if proxy and not connector: + try: + from aiohttp_socks import ProxyConnector + connector = ProxyConnector.from_url(proxy) + except ImportError: + raise RuntimeError('Install "aiohttp_socks" package for proxy support') - return stream_generate(prompt, tone, image, context, proxy, cookies, web_search, gpt4_turbo, timeout) + return stream_generate(prompt, tone, image, context, cookies, connector, web_search, gpt4_turbo, timeout) def create_context(messages: Messages) -> str: """ @@ -253,8 +261,8 @@ async def stream_generate( tone: str, image: ImageType = None, context: str = None, - proxy: str = None, cookies: dict = None, + connector: BaseConnector = None, web_search: bool = False, gpt4_turbo: bool = False, timeout: int = 900 @@ -266,7 +274,6 @@ async def stream_generate( :param tone: The desired tone for the response. :param image: The image type involved in the response. :param context: Additional context for the prompt. - :param proxy: Proxy settings for the request. :param cookies: Cookies for the session. :param web_search: Flag to enable web search. :param gpt4_turbo: Flag to enable GPT-4 Turbo. @@ -278,10 +285,10 @@ async def stream_generate( headers["Cookie"] = "; ".join(f"{k}={v}" for k, v in cookies.items()) async with ClientSession( - timeout=ClientTimeout(total=timeout), headers=headers + timeout=ClientTimeout(total=timeout), headers=headers, connector=connector ) as session: - conversation = await create_conversation(session, proxy) - image_response = await upload_image(session, image, tone, proxy) if image else None + conversation = await create_conversation(session) + image_response = await upload_image(session, image, tone) if image else None if image_response: yield image_response @@ -289,8 +296,7 @@ async def stream_generate( async with session.ws_connect( 'wss://sydney.bing.com/sydney/ChatHub', autoping=False, - params={'sec_access_token': conversation.conversationSignature}, - proxy=proxy + params={'sec_access_token': conversation.conversationSignature} ) as wss: await wss.send_str(format_message({'protocol': 'json', 'version': 1})) await wss.receive(timeout=timeout) @@ -322,7 +328,7 @@ async def stream_generate( elif message.get('contentType') == "IMAGE": prompt = message.get('text') try: - image_response = ImageResponse(await create_images(session, prompt, proxy), prompt) + image_response = ImageResponse(await create_images(session, prompt), prompt) except: response_txt += f"\nhttps://www.bing.com/images/create?q={parse.quote(prompt)}" final = True @@ -342,4 +348,4 @@ async def stream_generate( raise Exception(f"{result['value']}: {result['message']}") return finally: - await delete_conversation(session, conversation, proxy) + await delete_conversation(session, conversation) -- cgit v1.2.3 From 69ef224f92fcec3335a857305191678b886651e1 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Wed, 24 Jan 2024 00:46:35 +0100 Subject: Add get_connector helper --- g4f/Provider/Bing.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'g4f/Provider/Bing.py') diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py index 751b0d3a..11bb1414 100644 --- a/g4f/Provider/Bing.py +++ b/g4f/Provider/Bing.py @@ -11,6 +11,7 @@ from aiohttp import ClientSession, ClientTimeout, BaseConnector from ..typing import AsyncResult, Messages, ImageType from ..image import ImageResponse from .base_provider import AsyncGeneratorProvider +from .helper import get_connector from .bing.upload_image import upload_image from .bing.create_images import create_images from .bing.conversation import Conversation, create_conversation, delete_conversation @@ -68,15 +69,8 @@ class Bing(AsyncGeneratorProvider): cookies = {**Defaults.cookies, **cookies} if cookies else Defaults.cookies gpt4_turbo = True if model.startswith("gpt-4-turbo") else False - - if proxy and not connector: - try: - from aiohttp_socks import ProxyConnector - connector = ProxyConnector.from_url(proxy) - except ImportError: - raise RuntimeError('Install "aiohttp_socks" package for proxy support') - return stream_generate(prompt, tone, image, context, cookies, connector, web_search, gpt4_turbo, timeout) + return stream_generate(prompt, tone, image, context, cookies, get_connector(connector, proxy), web_search, gpt4_turbo, timeout) def create_context(messages: Messages) -> str: """ -- cgit v1.2.3