summaryrefslogtreecommitdiffstats
path: root/g4f/Provider
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2023-12-06 12:02:41 +0100
committerGitHub <noreply@github.com>2023-12-06 12:02:41 +0100
commitf962993b8dac225d85f9fbb59be188e263af1c0b (patch)
treeae153b027ad5a2679b4f1fde881d61cc0e1276b4 /g4f/Provider
parent~ | g4f `v-0.1.9.1` (diff)
parentImprove docker image (diff)
downloadgpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.tar
gpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.tar.gz
gpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.tar.bz2
gpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.tar.lz
gpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.tar.xz
gpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.tar.zst
gpt4free-f962993b8dac225d85f9fbb59be188e263af1c0b.zip
Diffstat (limited to 'g4f/Provider')
-rw-r--r--g4f/Provider/Bing.py7
-rw-r--r--g4f/Provider/PerplexityAi.py9
-rw-r--r--g4f/Provider/helper.py9
-rw-r--r--g4f/Provider/needs_auth/Bard.py16
-rw-r--r--g4f/Provider/needs_auth/HuggingChat.py6
-rw-r--r--g4f/Provider/needs_auth/OpenaiChat.py37
6 files changed, 43 insertions, 41 deletions
diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py
index b790a6d2..9e3e7405 100644
--- a/g4f/Provider/Bing.py
+++ b/g4f/Provider/Bing.py
@@ -156,8 +156,11 @@ async def delete_conversation(session: ClientSession, conversation: Conversation
"optionsSets": ["autosave"]
}
async with session.post(url, json=json, proxy=proxy) as response:
- response = await response.json()
- return response["result"]["value"] == "Success"
+ try:
+ response = await response.json()
+ return response["result"]["value"] == "Success"
+ except:
+ return False
class Defaults:
delimiter = "\x1e"
diff --git a/g4f/Provider/PerplexityAi.py b/g4f/Provider/PerplexityAi.py
index 941ca6d4..ad629aa8 100644
--- a/g4f/Provider/PerplexityAi.py
+++ b/g4f/Provider/PerplexityAi.py
@@ -1,6 +1,10 @@
from __future__ import annotations
import time
+from selenium.webdriver.common.by import By
+from selenium.webdriver.support.ui import WebDriverWait
+from selenium.webdriver.support import expected_conditions as EC
+from selenium.webdriver.common.keys import Keys
from ..typing import CreateResult, Messages
from .base_provider import BaseProvider
@@ -27,11 +31,6 @@ class PerplexityAi(BaseProvider):
**kwargs
) -> CreateResult:
with WebDriverSession(webdriver, "", virtual_display=virtual_display, proxy=proxy) as driver:
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.common.keys import Keys
-
prompt = format_prompt(messages)
driver.get(f"{cls.url}/")
diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py
index 2171f0b7..61d9cb62 100644
--- a/g4f/Provider/helper.py
+++ b/g4f/Provider/helper.py
@@ -6,6 +6,7 @@ import webbrowser
import random
import string
import secrets
+import os
from os import path
from asyncio import AbstractEventLoop
from platformdirs import user_config_dir
@@ -18,7 +19,7 @@ from browser_cookie3 import (
edge,
vivaldi,
firefox,
- BrowserCookieError
+ _LinuxPasswordManager
)
from ..typing import Dict, Messages
@@ -81,6 +82,10 @@ def init_cookies():
except webbrowser.Error:
continue
+# Check for broken dbus address in docker image
+if os.environ.get('DBUS_SESSION_BUS_ADDRESS') == "/dev/null":
+ _LinuxPasswordManager.get_password = lambda a, b: b"secret"
+
# Load cookies for a domain from all supported browsers.
# Cache the results in the "_cookies" variable.
def get_cookies(domain_name=''):
@@ -100,7 +105,7 @@ def get_cookies(domain_name=''):
for cookie in cookie_jar:
if cookie.name not in cookies:
cookies[cookie.name] = cookie.value
- except BrowserCookieError as e:
+ except:
pass
_cookies[domain_name] = cookies
return _cookies[domain_name]
diff --git a/g4f/Provider/needs_auth/Bard.py b/g4f/Provider/needs_auth/Bard.py
index 877af37e..48e535dd 100644
--- a/g4f/Provider/needs_auth/Bard.py
+++ b/g4f/Provider/needs_auth/Bard.py
@@ -1,6 +1,11 @@
from __future__ import annotations
import time
+import os
+from selenium.webdriver.common.by import By
+from selenium.webdriver.support.ui import WebDriverWait
+from selenium.webdriver.support import expected_conditions as EC
+from selenium.webdriver.common.keys import Keys
from ...typing import CreateResult, Messages
from ..base_provider import BaseProvider
@@ -27,10 +32,6 @@ class Bard(BaseProvider):
prompt = format_prompt(messages)
session = WebDriverSession(webdriver, user_data_dir, headless, proxy=proxy)
with session as driver:
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
-
try:
driver.get(f"{cls.url}/chat")
wait = WebDriverWait(driver, 10 if headless else 240)
@@ -40,6 +41,9 @@ class Bard(BaseProvider):
if not webdriver:
driver = session.reopen()
driver.get(f"{cls.url}/chat")
+ login_url = os.environ.get("G4F_LOGIN_URL")
+ if login_url:
+ yield f"Please login: [Google Bard]({login_url})\n\n"
wait = WebDriverWait(driver, 240)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ql-editor.textarea")))
else:
@@ -61,8 +65,8 @@ XMLHttpRequest.prototype.open = function(method, url) {
driver.execute_script(script)
# Submit prompt
- driver.find_element(By.CSS_SELECTOR, "div.ql-editor.ql-blank.textarea").send_keys(prompt)
- driver.find_element(By.CSS_SELECTOR, "button.send-button").click()
+ driver.find_element(By.CSS_SELECTOR, "div.ql-editor.textarea").send_keys(prompt)
+ driver.find_element(By.CSS_SELECTOR, "div.ql-editor.textarea").send_keys(Keys.ENTER)
# Yield response
while True:
diff --git a/g4f/Provider/needs_auth/HuggingChat.py b/g4f/Provider/needs_auth/HuggingChat.py
index 59e2da73..530069c0 100644
--- a/g4f/Provider/needs_auth/HuggingChat.py
+++ b/g4f/Provider/needs_auth/HuggingChat.py
@@ -11,7 +11,6 @@ from ..helper import format_prompt, get_cookies
class HuggingChat(AsyncGeneratorProvider):
url = "https://huggingface.co/chat"
- needs_auth = True
working = True
model = "meta-llama/Llama-2-70b-chat-hf"
@@ -22,12 +21,11 @@ class HuggingChat(AsyncGeneratorProvider):
messages: Messages,
stream: bool = True,
proxy: str = None,
+ web_search: bool = False,
cookies: dict = None,
**kwargs
) -> AsyncResult:
model = model if model else cls.model
- if proxy and "://" not in proxy:
- proxy = f"http://{proxy}"
if not cookies:
cookies = get_cookies(".huggingface.co")
@@ -46,7 +44,7 @@ class HuggingChat(AsyncGeneratorProvider):
"inputs": format_prompt(messages),
"is_retry": False,
"response_id": str(uuid.uuid4()),
- "web_search": False
+ "web_search": web_search
}
async with session.post(f"{cls.url}/conversation/{conversation_id}", json=send, proxy=proxy) as response:
async for line in response.content:
diff --git a/g4f/Provider/needs_auth/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py
index af62382a..818c163f 100644
--- a/g4f/Provider/needs_auth/OpenaiChat.py
+++ b/g4f/Provider/needs_auth/OpenaiChat.py
@@ -1,12 +1,15 @@
from __future__ import annotations
-import uuid, json, asyncio
+import uuid, json, asyncio, os
from py_arkose_generator.arkose import get_values_for_request
from asyncstdlib.itertools import tee
from async_property import async_cached_property
-
+from selenium.webdriver.common.by import By
+from selenium.webdriver.support.ui import WebDriverWait
+from selenium.webdriver.support import expected_conditions as EC
+
from ..base_provider import AsyncGeneratorProvider
-from ..helper import get_event_loop
+from ..helper import get_event_loop, format_prompt
from ...webdriver import get_browser
from ...typing import AsyncResult, Messages
from ...requests import StreamSession
@@ -84,7 +87,12 @@ class OpenaiChat(AsyncGeneratorProvider):
if not parent_id:
parent_id = str(uuid.uuid4())
if not access_token:
- access_token = await cls.get_access_token(proxy)
+ access_token = cls._access_token
+ if not access_token:
+ login_url = os.environ.get("G4F_LOGIN_URL")
+ if login_url:
+ yield f"Please login: [ChatGPT]({login_url})\n\n"
+ access_token = cls._access_token = await cls.browse_access_token(proxy)
headers = {
"Accept": "text/event-stream",
"Authorization": f"Bearer {access_token}",
@@ -106,10 +114,11 @@ class OpenaiChat(AsyncGeneratorProvider):
"history_and_training_disabled": history_disabled and not auto_continue,
}
if action != "continue":
+ prompt = format_prompt(messages) if not conversation_id else messages[-1]["content"]
data["messages"] = [{
"id": str(uuid.uuid4()),
"author": {"role": "user"},
- "content": {"content_type": "text", "parts": [messages[-1]["content"]]},
+ "content": {"content_type": "text", "parts": [prompt]},
}]
async with session.post(f"{cls.url}/backend-api/conversation", json=data) as response:
try:
@@ -155,14 +164,7 @@ class OpenaiChat(AsyncGeneratorProvider):
@classmethod
async def browse_access_token(cls, proxy: str = None) -> str:
def browse() -> 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
-
- driver = get_browser(proxy=proxy)
- except ImportError:
- return
+ driver = get_browser(proxy=proxy)
try:
driver.get(f"{cls.url}/")
WebDriverWait(driver, 1200).until(
@@ -177,15 +179,6 @@ class OpenaiChat(AsyncGeneratorProvider):
None,
browse
)
-
- @classmethod
- async def get_access_token(cls, proxy: str = None) -> str:
- if not cls._access_token:
- cls._access_token = await cls.browse_access_token(proxy)
- if not cls._access_token:
- raise RuntimeError("Read access token failed")
- return cls._access_token
-
async def get_arkose_token(proxy: str = None, timeout: int = None) -> str:
config = {