From 98895e5b09ea5e3f19b2263ddca028c7b296abb2 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sun, 1 Oct 2023 06:38:11 +0200 Subject: Update HuggingChat to new api Impersonate Aivvm Provider Add ChatForAi and FreeGpt Provider Update AItianhuSpace Provider Improve StreamRequest Support Update get_event_loop Helper --- g4f/Provider/helper.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'g4f/Provider/helper.py') diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index e14ae65e..2c00a488 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -1,18 +1,12 @@ from __future__ import annotations import asyncio -import sys from asyncio import AbstractEventLoop import browser_cookie3 _cookies: dict[str, dict[str, str]] = {} -# Use own event_loop_policy with a selector event loop on windows. -if sys.platform == 'win32': - _event_loop_policy = asyncio.WindowsSelectorEventLoopPolicy() -else: - _event_loop_policy = asyncio.get_event_loop_policy() # If event loop is already running, handle nested event loops # If "nest_asyncio" is installed, patch the event loop. @@ -20,9 +14,9 @@ def get_event_loop() -> AbstractEventLoop: try: asyncio.get_running_loop() except RuntimeError: - return _event_loop_policy.get_event_loop() + return asyncio.get_event_loop() try: - event_loop = _event_loop_policy.get_event_loop() + event_loop = asyncio.get_event_loop() if not hasattr(event_loop.__class__, "_nest_patched"): import nest_asyncio nest_asyncio.apply(event_loop) -- cgit v1.2.3 From 7b9ad21de81cd5129b047d8de3ce8d9e5a53ea9a Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sun, 1 Oct 2023 20:29:57 +0200 Subject: Change event loop policy on windows Support more versions from curl_cffi --- g4f/Provider/helper.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'g4f/Provider/helper.py') diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index 2c00a488..544c5a76 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -1,13 +1,20 @@ from __future__ import annotations -import asyncio +import asyncio, sys from asyncio import AbstractEventLoop import browser_cookie3 +# Change event loop policy on windows +if sys.platform == 'win32': + if isinstance( + asyncio.get_event_loop_policy(), asyncio.WindowsProactorEventLoopPolicy + ): + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) + +# Local Cookie Storage _cookies: dict[str, dict[str, str]] = {} - # If event loop is already running, handle nested event loops # If "nest_asyncio" is installed, patch the event loop. def get_event_loop() -> AbstractEventLoop: -- cgit v1.2.3 From d116f043227f789d6582bc12f69ea4ee7a9330ea Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Mon, 2 Oct 2023 06:47:07 +0200 Subject: Fix: There is no current event loop in thread --- g4f/Provider/helper.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'g4f/Provider/helper.py') diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index 544c5a76..234cdaa1 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -21,7 +21,11 @@ def get_event_loop() -> AbstractEventLoop: try: asyncio.get_running_loop() except RuntimeError: - return asyncio.get_event_loop() + try: + return asyncio.get_event_loop() + except RuntimeError: + asyncio.set_event_loop(asyncio.new_event_loop()) + return asyncio.get_event_loop() try: event_loop = asyncio.get_event_loop() if not hasattr(event_loop.__class__, "_nest_patched"): -- cgit v1.2.3 From be9b8f796cb01483cf6dd807dfc6a71c51433c16 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Tue, 3 Oct 2023 22:12:56 +0200 Subject: Add streaming in openai chat Fetch access token with chromedriver --- g4f/Provider/helper.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'g4f/Provider/helper.py') diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index 234cdaa1..8f09239a 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -56,4 +56,28 @@ def format_prompt(messages: list[dict[str, str]], add_special_tokens=False): ) return f"{formatted}\nAssistant:" else: - return messages[0]["content"] \ No newline at end of file + return messages[0]["content"] + + +def get_browser(user_data_dir: str = None): + try: + from undetected_chromedriver import Chrome + except ImportError: + return None + + 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() + + return Chrome(user_data_dir=user_data_dir) \ No newline at end of file -- 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/helper.py | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) (limited to 'g4f/Provider/helper.py') 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 dfdb759639479da640701fe0db716d4455b7ae38 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 7 Oct 2023 19:00:45 +0200 Subject: Improve code with ai --- g4f/Provider/helper.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'g4f/Provider/helper.py') diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index c127f241..5a9a9329 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -1,8 +1,10 @@ from __future__ import annotations -import asyncio, sys +import asyncio +import sys from asyncio import AbstractEventLoop from os import path +from typing import Dict, List import browser_cookie3 # Change event loop policy on windows @@ -13,7 +15,7 @@ if sys.platform == 'win32': asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # Local Cookie Storage -_cookies: dict[str, dict[str, str]] = {} +_cookies: Dict[str, Dict[str, str]] = {} # If event loop is already running, handle nested event loops # If "nest_asyncio" is installed, patch the event loop. @@ -34,11 +36,13 @@ def get_event_loop() -> AbstractEventLoop: return event_loop except ImportError: raise RuntimeError( - 'Use "create_async" instead of "create" function in a running event loop. Or install the "nest_asyncio" package.') + 'Use "create_async" instead of "create" function in a running event loop. Or install the "nest_asyncio" package.' + ) + -# Load cookies for a domain from all supported browser. -# Cache the results in the "_cookies" variable -def get_cookies(cookie_domain: str) -> dict: +# Load cookies for a domain from all supported browsers. +# Cache the results in the "_cookies" variable. +def get_cookies(cookie_domain: str) -> Dict[str, str]: if cookie_domain not in _cookies: _cookies[cookie_domain] = {} try: @@ -49,15 +53,18 @@ def get_cookies(cookie_domain: str) -> dict: return _cookies[cookie_domain] -def format_prompt(messages: list[dict[str, str]], add_special_tokens=False): +def format_prompt(messages: List[Dict[str, str]], add_special_tokens=False) -> str: if add_special_tokens or len(messages) > 1: formatted = "\n".join( - ["%s: %s" % ((message["role"]).capitalize(), message["content"]) for message in messages] + [ + "%s: %s" % ((message["role"]).capitalize(), message["content"]) + for message in messages + ] ) return f"{formatted}\nAssistant:" else: return messages[0]["content"] - + def get_browser(user_data_dir: str = None): from undetected_chromedriver import Chrome -- cgit v1.2.3