From 5b2efa4aaf00cb5b5b169c83f2690630495a962e Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Tue, 3 Oct 2023 23:53:17 +0200 Subject: Fix fetch_access_token in openai --- g4f/Provider/OpenaiChat.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/g4f/Provider/OpenaiChat.py b/g4f/Provider/OpenaiChat.py index fbd26d7c..ca148da2 100644 --- a/g4f/Provider/OpenaiChat.py +++ b/g4f/Provider/OpenaiChat.py @@ -63,7 +63,7 @@ class OpenaiChat(AsyncGeneratorProvider): 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 @@ -85,19 +85,25 @@ class OpenaiChat(AsyncGeneratorProvider): finally: 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 -- cgit v1.2.3 From 136770e05b63fd1482665eac5f90eba5bd10e4c5 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Wed, 4 Oct 2023 03:15:17 +0200 Subject: Use custom user dir --- g4f/Provider/OpenaiChat.py | 19 +++++++++++-------- g4f/Provider/helper.py | 23 +++++------------------ 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/g4f/Provider/OpenaiChat.py b/g4f/Provider/OpenaiChat.py index ca148da2..8f183fb9 100644 --- a/g4f/Provider/OpenaiChat.py +++ b/g4f/Provider/OpenaiChat.py @@ -1,7 +1,6 @@ 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 @@ -56,8 +55,13 @@ 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 @@ -68,11 +72,9 @@ class OpenaiChat(AsyncGeneratorProvider): 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,6 +85,7 @@ 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 diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index 8f09239a..c127f241 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -2,7 +2,7 @@ from __future__ import annotations import asyncio, sys from asyncio import AbstractEventLoop - +from os import path import browser_cookie3 # Change event loop policy on windows @@ -60,24 +60,11 @@ def format_prompt(messages: list[dict[str, str]], add_special_tokens=False): def get_browser(user_data_dir: str = None): - try: - from undetected_chromedriver import Chrome - except ImportError: - return None + from undetected_chromedriver import Chrome + from platformdirs import user_config_dir - def get_user_data_dir(): - dirs = [ - '~/.config/google-chrome/Default', - '~/.var/app/com.google.Chrome/config/google-chrome/Default', - '%LOCALAPPDATA%\\Google\\Chrome\\User Data\\Default', - '~/Library/Application Support/Google/Chrome/Default', - ] - from os import path - for dir in dirs: - dir = path.expandvars(dir) - if path.exists(dir): - return dir if not user_data_dir: - user_data_dir = get_user_data_dir() + user_data_dir = user_config_dir("g4f") + user_data_dir = path.join(user_data_dir, "Default") return Chrome(user_data_dir=user_data_dir) \ No newline at end of file -- cgit v1.2.3 From 6a61cf811655fa87dbcb196025cc0b6040502293 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Wed, 4 Oct 2023 07:20:51 +0200 Subject: Sort providers in new dirs --- g4f/Provider/AItianhu.py | 2 + g4f/Provider/AItianhuSpace.py | 2 + g4f/Provider/AiService.py | 36 --------- g4f/Provider/Bard.py | 92 ----------------------- g4f/Provider/CodeLinkAva.py | 64 ---------------- g4f/Provider/DeepAi.py | 4 +- g4f/Provider/DfeHub.py | 77 ------------------- g4f/Provider/EasyChat.py | 111 --------------------------- g4f/Provider/Equing.py | 81 -------------------- g4f/Provider/FastGpt.py | 86 --------------------- g4f/Provider/Forefront.py | 40 ---------- g4f/Provider/GetGpt.py | 88 ---------------------- g4f/Provider/HuggingChat.py | 74 ------------------ g4f/Provider/Lockchat.py | 64 ---------------- g4f/Provider/Opchatgpts.py | 8 -- g4f/Provider/OpenAssistant.py | 100 ------------------------- g4f/Provider/OpenaiChat.py | 124 ------------------------------- g4f/Provider/PerplexityAi.py | 101 ------------------------- g4f/Provider/Raycast.py | 72 ------------------ g4f/Provider/Theb.py | 97 ------------------------ g4f/Provider/V50.py | 67 ----------------- g4f/Provider/Wewordle.py | 65 ---------------- g4f/Provider/Wuguokai.py | 63 ---------------- g4f/Provider/__init__.py | 22 +----- g4f/Provider/deprecated/AiService.py | 36 +++++++++ g4f/Provider/deprecated/CodeLinkAva.py | 64 ++++++++++++++++ g4f/Provider/deprecated/DfeHub.py | 77 +++++++++++++++++++ g4f/Provider/deprecated/EasyChat.py | 111 +++++++++++++++++++++++++++ g4f/Provider/deprecated/Equing.py | 81 ++++++++++++++++++++ g4f/Provider/deprecated/FastGpt.py | 87 ++++++++++++++++++++++ g4f/Provider/deprecated/Forefront.py | 40 ++++++++++ g4f/Provider/deprecated/GetGpt.py | 88 ++++++++++++++++++++++ g4f/Provider/deprecated/Lockchat.py | 64 ++++++++++++++++ g4f/Provider/deprecated/Opchatgpts.py | 8 ++ g4f/Provider/deprecated/PerplexityAi.py | 101 +++++++++++++++++++++++++ g4f/Provider/deprecated/V50.py | 67 +++++++++++++++++ g4f/Provider/deprecated/Wewordle.py | 65 ++++++++++++++++ g4f/Provider/deprecated/Wuguokai.py | 63 ++++++++++++++++ g4f/Provider/deprecated/__init__.py | 14 ++++ g4f/Provider/needs_auth/Bard.py | 92 +++++++++++++++++++++++ g4f/Provider/needs_auth/HuggingChat.py | 74 ++++++++++++++++++ g4f/Provider/needs_auth/OpenAssistant.py | 100 +++++++++++++++++++++++++ g4f/Provider/needs_auth/OpenaiChat.py | 124 +++++++++++++++++++++++++++++++ g4f/Provider/needs_auth/Raycast.py | 72 ++++++++++++++++++ g4f/Provider/needs_auth/Theb.py | 97 ++++++++++++++++++++++++ g4f/Provider/needs_auth/__init__.py | 6 ++ g4f/models.py | 4 +- testing/test_providers.py | 20 +---- 48 files changed, 1545 insertions(+), 1550 deletions(-) delete mode 100644 g4f/Provider/AiService.py delete mode 100644 g4f/Provider/Bard.py delete mode 100644 g4f/Provider/CodeLinkAva.py delete mode 100644 g4f/Provider/DfeHub.py delete mode 100644 g4f/Provider/EasyChat.py delete mode 100644 g4f/Provider/Equing.py delete mode 100644 g4f/Provider/FastGpt.py delete mode 100644 g4f/Provider/Forefront.py delete mode 100644 g4f/Provider/GetGpt.py delete mode 100644 g4f/Provider/HuggingChat.py delete mode 100644 g4f/Provider/Lockchat.py delete mode 100644 g4f/Provider/Opchatgpts.py delete mode 100644 g4f/Provider/OpenAssistant.py delete mode 100644 g4f/Provider/OpenaiChat.py delete mode 100644 g4f/Provider/PerplexityAi.py delete mode 100644 g4f/Provider/Raycast.py delete mode 100644 g4f/Provider/Theb.py delete mode 100644 g4f/Provider/V50.py delete mode 100644 g4f/Provider/Wewordle.py delete mode 100644 g4f/Provider/Wuguokai.py create mode 100644 g4f/Provider/deprecated/AiService.py create mode 100644 g4f/Provider/deprecated/CodeLinkAva.py create mode 100644 g4f/Provider/deprecated/DfeHub.py create mode 100644 g4f/Provider/deprecated/EasyChat.py create mode 100644 g4f/Provider/deprecated/Equing.py create mode 100644 g4f/Provider/deprecated/FastGpt.py create mode 100644 g4f/Provider/deprecated/Forefront.py create mode 100644 g4f/Provider/deprecated/GetGpt.py create mode 100644 g4f/Provider/deprecated/Lockchat.py create mode 100644 g4f/Provider/deprecated/Opchatgpts.py create mode 100644 g4f/Provider/deprecated/PerplexityAi.py create mode 100644 g4f/Provider/deprecated/V50.py create mode 100644 g4f/Provider/deprecated/Wewordle.py create mode 100644 g4f/Provider/deprecated/Wuguokai.py create mode 100644 g4f/Provider/deprecated/__init__.py create mode 100644 g4f/Provider/needs_auth/Bard.py create mode 100644 g4f/Provider/needs_auth/HuggingChat.py create mode 100644 g4f/Provider/needs_auth/OpenAssistant.py create mode 100644 g4f/Provider/needs_auth/OpenaiChat.py create mode 100644 g4f/Provider/needs_auth/Raycast.py create mode 100644 g4f/Provider/needs_auth/Theb.py create mode 100644 g4f/Provider/needs_auth/__init__.py diff --git a/g4f/Provider/AItianhu.py b/g4f/Provider/AItianhu.py index 42631d7e..1d8653b7 100644 --- a/g4f/Provider/AItianhu.py +++ b/g4f/Provider/AItianhu.py @@ -38,6 +38,8 @@ class AItianhu(AsyncGeneratorProvider): async with session.post(f"{cls.url}/api/chat-process", json=data) as response: response.raise_for_status() async for line in response.iter_lines(): + if line == b"