diff options
author | Tekky <98614666+xtekky@users.noreply.github.com> | 2023-10-04 10:40:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-04 10:40:18 +0200 |
commit | 6eb3fa3e5fa5675363dc09209cf77fba0542a506 (patch) | |
tree | 0ca69953cbf4820667500a195c8f97c5da75c88e /g4f/Provider/needs_auth/OpenaiChat.py | |
parent | ~ | g4f v-0.1.4.6 `pip install -U g4f` (diff) | |
parent | Sort providers in new dirs (diff) | |
download | gpt4free-6eb3fa3e5fa5675363dc09209cf77fba0542a506.tar gpt4free-6eb3fa3e5fa5675363dc09209cf77fba0542a506.tar.gz gpt4free-6eb3fa3e5fa5675363dc09209cf77fba0542a506.tar.bz2 gpt4free-6eb3fa3e5fa5675363dc09209cf77fba0542a506.tar.lz gpt4free-6eb3fa3e5fa5675363dc09209cf77fba0542a506.tar.xz gpt4free-6eb3fa3e5fa5675363dc09209cf77fba0542a506.tar.zst gpt4free-6eb3fa3e5fa5675363dc09209cf77fba0542a506.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/Provider/needs_auth/OpenaiChat.py (renamed from g4f/Provider/OpenaiChat.py) | 53 |
1 files changed, 31 insertions, 22 deletions
diff --git a/g4f/Provider/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py index fbd26d7c..c57692d3 100644 --- a/g4f/Provider/OpenaiChat.py +++ b/g4f/Provider/needs_auth/OpenaiChat.py @@ -1,12 +1,11 @@ from __future__ import annotations -import uuid -import json +import uuid, json, time -from .base_provider import AsyncGeneratorProvider -from .helper import get_browser, get_cookies, format_prompt -from ..typing import AsyncGenerator -from ..requests import StreamSession +from ..base_provider import AsyncGeneratorProvider +from ..helper import get_browser, get_cookies, format_prompt +from ...typing import AsyncGenerator +from ...requests import StreamSession class OpenaiChat(AsyncGeneratorProvider): url = "https://chat.openai.com" @@ -56,23 +55,26 @@ class OpenaiChat(AsyncGeneratorProvider): line = line[6:] if line == b"[DONE]": break - line = json.loads(line) - if "message" in line and not line["message"]["end_turn"]: + try: + line = json.loads(line) + except: + continue + if "message" not in line or "message_type" not in line["message"]["metadata"]: + continue + if line["message"]["metadata"]["message_type"] == "next": new_message = line["message"]["content"]["parts"][0] yield new_message[len(last_message):] last_message = new_message @classmethod - def fetch_access_token(cls) -> str: + def browse_access_token(cls) -> str: try: from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC - except ImportError: - return - driver = get_browser() - if not driver: + driver = get_browser() + except ImportError: return driver.get(f"{cls.url}/") @@ -83,21 +85,28 @@ class OpenaiChat(AsyncGeneratorProvider): javascript = "return (await (await fetch('/api/auth/session')).json())['accessToken']" return driver.execute_script(javascript) finally: + time.sleep(1) driver.quit() @classmethod + async def fetch_access_token(cls, cookies: dict, proxies: dict = None) -> str: + async with StreamSession(proxies=proxies, cookies=cookies, impersonate="chrome107") as session: + async with session.get(f"{cls.url}/api/auth/session") as response: + response.raise_for_status() + auth = await response.json() + if "accessToken" in auth: + return auth["accessToken"] + + @classmethod async def get_access_token(cls, cookies: dict = None, proxies: dict = None) -> str: if not cls._access_token: cookies = cookies if cookies else get_cookies("chat.openai.com") - async with StreamSession(proxies=proxies, cookies=cookies, impersonate="chrome107") as session: - async with session.get(f"{cls.url}/api/auth/session") as response: - response.raise_for_status() - auth = await response.json() - if "accessToken" in auth: - cls._access_token = auth["accessToken"] - cls._access_token = cls.fetch_access_token() - if not cls._access_token: - raise RuntimeError("Missing access token") + if cookies: + cls._access_token = await cls.fetch_access_token(cookies, proxies) + if not cls._access_token: + cls._access_token = cls.browse_access_token() + if not cls._access_token: + raise RuntimeError("Read access token failed") return cls._access_token @classmethod |