diff options
author | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-03-15 11:46:06 +0100 |
---|---|---|
committer | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-03-15 11:46:06 +0100 |
commit | 8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0 (patch) | |
tree | ea190c7017d8e8982e63a502da0a828d5ba42f94 /g4f/Provider | |
parent | Add export / import conversations (diff) | |
download | gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.tar gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.tar.gz gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.tar.bz2 gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.tar.lz gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.tar.xz gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.tar.zst gpt4free-8cc6000ffbf4e12bf6c1d5e5878d376e36857ec0.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/Provider/Bing.py | 5 | ||||
-rw-r--r-- | g4f/Provider/You.py | 56 | ||||
-rw-r--r-- | g4f/Provider/needs_auth/OpenaiChat.py | 2 |
3 files changed, 29 insertions, 34 deletions
diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py index f8b13020..925dae0a 100644 --- a/g4f/Provider/Bing.py +++ b/g4f/Provider/Bing.py @@ -406,14 +406,13 @@ async def stream_generate( except Exception as e: if debug.logging: print(f"Bing: Failed to create images: {e}") - response_txt += f"\nhttps://www.bing.com/images/create?q={parse.quote(prompt)}" - do_read = False + image_response = f"\nhttps://www.bing.com/images/create?q={parse.quote(prompt)}" if response_txt.startswith(returned_text): new = response_txt[len(returned_text):] if new not in ("", "\n"): yield new returned_text = response_txt - if image_response: + if image_response is not None: yield image_response elif response.get('type') == 2: result = response['item']['result'] diff --git a/g4f/Provider/You.py b/g4f/Provider/You.py index 85b60452..8adc8b19 100644 --- a/g4f/Provider/You.py +++ b/g4f/Provider/You.py @@ -4,14 +4,18 @@ import re import json import base64 import uuid -from asyncio import get_running_loop -from aiohttp import ClientSession, FormData, BaseConnector, CookieJar +try: + from curl_cffi import CurlMime + has_curl_cffi = True +except ImportError: + has_curl_cffi = False from ..typing import AsyncResult, Messages, ImageType, Cookies from .base_provider import AsyncGeneratorProvider, ProviderModelMixin -from .helper import format_prompt, get_connector +from .helper import format_prompt from ..image import to_bytes, ImageResponse -from ..requests import WebDriver, raise_for_status, get_args_from_browser +from ..requests import StreamSession, raise_for_status +from ..errors import MissingRequirementsError class You(AsyncGeneratorProvider, ProviderModelMixin): url = "https://you.com" @@ -33,8 +37,6 @@ class You(AsyncGeneratorProvider, ProviderModelMixin): model_aliases = { "claude-v2": "claude-2" } - _args: dict = None - _cookie_jar: CookieJar = None _cookies = None _cookies_used = 0 @@ -45,19 +47,12 @@ class You(AsyncGeneratorProvider, ProviderModelMixin): messages: Messages, image: ImageType = None, image_name: str = None, - connector: BaseConnector = None, - webdriver: WebDriver = None, proxy: str = None, chat_mode: str = "default", **kwargs, ) -> AsyncResult: - if cls._args is None: - cls._args = get_args_from_browser(cls.url, webdriver, proxy) - cls._cookie_jar = CookieJar(loop=get_running_loop()) - else: - if "cookies" in cls._args: - del cls._args["cookies"] - cls._cookie_jar._loop = get_running_loop() + if not has_curl_cffi: + raise MissingRequirementsError('Install "curl_cffi" package') if image is not None: chat_mode = "agent" elif not model or model == cls.default_model: @@ -67,10 +62,9 @@ class You(AsyncGeneratorProvider, ProviderModelMixin): else: chat_mode = "custom" model = cls.get_model(model) - async with ClientSession( - connector=get_connector(connector, proxy), - cookie_jar=cls._cookie_jar, - **cls._args + async with StreamSession( + proxy=proxy, + impersonate="chrome" ) as session: cookies = await cls.get_cookies(session) if chat_mode != "default" else None upload = json.dumps([await cls.upload_file(session, cookies, to_bytes(image), image_name)]) if image else "" @@ -82,8 +76,8 @@ class You(AsyncGeneratorProvider, ProviderModelMixin): # and idx < len(questions) # ] headers = { - "accept": "text/event-stream", - "referer": f"{cls.url}/search?fromSearchBar=true&tbm=youchat", + "Accept": "text/event-stream", + "Referer": f"{cls.url}/search?fromSearchBar=true&tbm=youchat", } data = { "userFiles": upload, @@ -106,12 +100,12 @@ class You(AsyncGeneratorProvider, ProviderModelMixin): cookies=cookies ) as response: await raise_for_status(response) - async for line in response.content: + async for line in response.iter_lines(): if line.startswith(b'event: '): - event = line[7:-1].decode() + event = line[7:].decode() elif line.startswith(b'data: '): if event in ["youChatUpdate", "youChatToken"]: - data = json.loads(line[6:-1]) + data = json.loads(line[6:]) if event == "youChatToken" and event in data: yield data[event] elif event == "youChatUpdate" and "t" in data: @@ -122,18 +116,20 @@ class You(AsyncGeneratorProvider, ProviderModelMixin): yield data["t"] @classmethod - async def upload_file(cls, client: ClientSession, cookies: Cookies, file: bytes, filename: str = None) -> dict: + async def upload_file(cls, client: StreamSession, cookies: Cookies, file: bytes, filename: str = None) -> dict: async with client.get( f"{cls.url}/api/get_nonce", cookies=cookies, ) as response: await raise_for_status(response) upload_nonce = await response.text() - data = FormData() - data.add_field('file', file, filename=filename) + #data = FormData() + #data.add_field('file', file, filename=filename) + multipart = CurlMime() + multipart.addpart(name="file", filename=filename, data=file) async with client.post( f"{cls.url}/api/upload", - data=data, + multipart=multipart, headers={ "X-Upload-Nonce": upload_nonce, }, @@ -146,7 +142,7 @@ class You(AsyncGeneratorProvider, ProviderModelMixin): return result @classmethod - async def get_cookies(cls, client: ClientSession) -> Cookies: + async def get_cookies(cls, client: StreamSession) -> Cookies: if not cls._cookies or cls._cookies_used >= 5: cls._cookies = await cls.create_cookies(client) cls._cookies_used = 0 @@ -173,7 +169,7 @@ class You(AsyncGeneratorProvider, ProviderModelMixin): return f"Basic {auth}" @classmethod - async def create_cookies(cls, client: ClientSession) -> Cookies: + async def create_cookies(cls, client: StreamSession) -> Cookies: user_uuid = str(uuid.uuid4()) async with client.post( "https://web.stytch.com/sdk/v1/passwords", diff --git a/g4f/Provider/needs_auth/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py index b501d655..6601f500 100644 --- a/g4f/Provider/needs_auth/OpenaiChat.py +++ b/g4f/Provider/needs_auth/OpenaiChat.py @@ -572,7 +572,7 @@ this.fetch = async (url, options) => { while headers is None: headers = window.evaluate_js("this._headers") await asyncio.sleep(1) - headers["User-Agent"] = window.evaluate_js("window.navigator.userAgent") + headers["User-Agent"] = window.evaluate_js("this.navigator.userAgent") cookies = [list(*cookie.items()) for cookie in window.get_cookies()] window.destroy() cls._cookies = dict([(name, cookie.value) for name, cookie in cookies]) |