diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-03-16 20:05:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-16 20:05:35 +0100 |
commit | 930ff2b888ebb5a241d415c1a88fc688b60da980 (patch) | |
tree | cc39a3df291ad30f55df34f52d31d73afcd9a827 | |
parent | Merge pull request #1691 from hlohaus/retry (diff) | |
parent | Fix GigaChat exceptions, Raise missing browser error (diff) | |
download | gpt4free-930ff2b888ebb5a241d415c1a88fc688b60da980.tar gpt4free-930ff2b888ebb5a241d415c1a88fc688b60da980.tar.gz gpt4free-930ff2b888ebb5a241d415c1a88fc688b60da980.tar.bz2 gpt4free-930ff2b888ebb5a241d415c1a88fc688b60da980.tar.lz gpt4free-930ff2b888ebb5a241d415c1a88fc688b60da980.tar.xz gpt4free-930ff2b888ebb5a241d415c1a88fc688b60da980.tar.zst gpt4free-930ff2b888ebb5a241d415c1a88fc688b60da980.zip |
Diffstat (limited to '')
-rw-r--r-- | MANIFEST.in | 3 | ||||
-rw-r--r-- | g4f/Provider/Bing.py | 2 | ||||
-rw-r--r-- | g4f/Provider/GigaChat.py | 23 | ||||
-rw-r--r-- | g4f/Provider/HuggingFace.py | 10 | ||||
-rw-r--r-- | g4f/gui/client/static/css/style.css | 10 | ||||
-rw-r--r-- | g4f/gui/client/static/js/chat.v1.js | 6 | ||||
-rw-r--r-- | g4f/providers/base_provider.py | 2 | ||||
-rw-r--r-- | g4f/webdriver.py | 8 |
8 files changed, 30 insertions, 34 deletions
diff --git a/MANIFEST.in b/MANIFEST.in index c2d2974a..5559110f 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,4 @@ recursive-include g4f/gui/server * recursive-include g4f/gui/client * -recursive-include g4f/Provider/npm *
\ No newline at end of file +recursive-include g4f/Provider/npm * +recursive-include g4f/Provider/gigachat_crt *
\ No newline at end of file diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py index 69c32775..7ff4d74b 100644 --- a/g4f/Provider/Bing.py +++ b/g4f/Provider/Bing.py @@ -311,7 +311,7 @@ def create_message( "allowedMessageTypes": Defaults.allowedMessageTypes, "sliceIds": Defaults.sliceIds[tone], "verbosity": "verbose", - "scenario": "CopilotMicrosoftCom", # "SERP", + "scenario": "CopilotMicrosoftCom" if tone == "copilot" else "SERP", "plugins": [{"id": "c310c353-b9f0-4d76-ab0d-1dd5e979cf68", "category": 1}] if web_search else [], "traceId": get_random_hex(40), "conversationHistoryOptionsSets": ["autosave","savemem","uprofupd","uprofgen"], diff --git a/g4f/Provider/GigaChat.py b/g4f/Provider/GigaChat.py index 699353b1..c1ec7f5e 100644 --- a/g4f/Provider/GigaChat.py +++ b/g4f/Provider/GigaChat.py @@ -1,35 +1,28 @@ from __future__ import annotations -import base64 import os import ssl import time import uuid import json -from aiohttp import ClientSession, BaseConnector, TCPConnector +from aiohttp import ClientSession, TCPConnector, BaseConnector from g4f.requests import raise_for_status -from ..typing import AsyncResult, Messages, ImageType +from ..typing import AsyncResult, Messages from .base_provider import AsyncGeneratorProvider, ProviderModelMixin -from ..image import to_bytes, is_accepted_format from ..errors import MissingAuthError from .helper import get_connector -access_token = '' +access_token = "" token_expires_at = 0 -ssl_ctx = ssl.create_default_context( - cafile=os.path.dirname(__file__) + '/gigachat_crt/russian_trusted_root_ca_pem.crt') - - class GigaChat(AsyncGeneratorProvider, ProviderModelMixin): url = "https://developers.sber.ru/gigachat" working = True supports_message_history = True supports_system_message = True supports_stream = True - needs_auth = True default_model = "GigaChat:latest" models = ["GigaChat:latest", "GigaChat-Plus", "GigaChat-Pro"] @@ -42,18 +35,20 @@ class GigaChat(AsyncGeneratorProvider, ProviderModelMixin): stream: bool = True, proxy: str = None, api_key: str = None, + coonector: BaseConnector = None, scope: str = "GIGACHAT_API_PERS", update_interval: float = 0, **kwargs ) -> AsyncResult: global access_token, token_expires_at model = cls.get_model(model) - if not api_key: raise MissingAuthError('Missing "api_key"') - - connector = TCPConnector(ssl_context=ssl_ctx) - + + cafile = os.path.join(os.path.dirname(__file__), "gigachat_crt/russian_trusted_root_ca_pem.crt") + ssl_context = ssl.create_default_context(cafile=cafile) if os.path.exists(cafile) else None + if connector is None and ssl_context is not None: + connector = TCPConnector(ssl_context=ssl_context) async with ClientSession(connector=get_connector(connector, proxy)) as session: if token_expires_at - int(time.time() * 1000) < 60000: async with session.post(url="https://ngw.devices.sberbank.ru:9443/api/v2/oauth", diff --git a/g4f/Provider/HuggingFace.py b/g4f/Provider/HuggingFace.py index a73411ce..647780fd 100644 --- a/g4f/Provider/HuggingFace.py +++ b/g4f/Provider/HuggingFace.py @@ -7,6 +7,7 @@ from ..typing import AsyncResult, Messages from .base_provider import AsyncGeneratorProvider, ProviderModelMixin from .helper import get_connector from ..errors import RateLimitError, ModelNotFoundError +from ..requests.raise_for_status import raise_for_status class HuggingFace(AsyncGeneratorProvider, ProviderModelMixin): url = "https://huggingface.co/chat" @@ -44,12 +45,9 @@ class HuggingFace(AsyncGeneratorProvider, ProviderModelMixin): connector=get_connector(connector, proxy) ) as session: async with session.post(f"{api_base.rstrip('/')}/models/{model}", json=payload) as response: - if response.status == 429: - raise RateLimitError("Rate limit reached. Set a api_key") - elif response.status == 404: + if response.status == 404: raise ModelNotFoundError(f"Model is not supported: {model}") - elif response.status != 200: - raise RuntimeError(f"Response {response.status}: {await response.text()}") + await raise_for_status(response) if stream: first = True async for line in response.content: @@ -68,7 +66,7 @@ def format_prompt(messages: Messages) -> str: system_messages = [message["content"] for message in messages if message["role"] == "system"] question = " ".join([messages[-1]["content"], *system_messages]) history = "".join([ - f"<s>[INST]{messages[idx-1]['content']} [/INST] {message}</s>" + f"<s>[INST]{messages[idx-1]['content']} [/INST] {message['content']}</s>" for idx, message in enumerate(messages) if message["role"] == "assistant" ]) diff --git a/g4f/gui/client/static/css/style.css b/g4f/gui/client/static/css/style.css index ba3f1187..936df0d2 100644 --- a/g4f/gui/client/static/css/style.css +++ b/g4f/gui/client/static/css/style.css @@ -653,12 +653,10 @@ select { width: 100%; } -.bottom_buttons button span { - color: var(--colour-3); - font-size: 14px; -} - -.bottom_buttons button a { +.bottom_buttons button a, +.bottom_buttons button span, +.bottom_buttons .info a, +.bottom_buttons .info i { color: var(--colour-3); font-weight: 500; } diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 5440fc4a..c2bb77a9 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -819,7 +819,8 @@ async function on_api() { if (versions["version"] != versions["latest_version"]) { let release_url = 'https://github.com/xtekky/gpt4free/releases/tag/' + versions["latest_version"]; let title = `New version: ${versions["latest_version"]}`; - text += `<a href="${release_url}" target="_blank" title="${title}">${versions["version"]} 🆕</a>`; + text += `<a href="${release_url}" target="_blank" title="${title}">${versions["version"]}</a> `; + text += `<i class="fa-solid fa-rotate"></i>` } else { text += versions["version"]; } @@ -994,8 +995,7 @@ async function load_provider_models(providerIndex=null) { providerSelect.addEventListener("change", () => load_provider_models()); function save_storage() { - let filename = new Date().toLocaleString() - filename += ".json" + let filename = `chat ${new Date().toLocaleString()}.json`.replaceAll(":", "-"); let data = {"options": {"g4f": ""}}; for (let i = 0; i < appStorage.length; i++){ let key = appStorage.key(i); diff --git a/g4f/providers/base_provider.py b/g4f/providers/base_provider.py index c8397193..ee5bcbb8 100644 --- a/g4f/providers/base_provider.py +++ b/g4f/providers/base_provider.py @@ -95,7 +95,7 @@ class AbstractProvider(BaseProvider): return annotation.__name__ if hasattr(annotation, "__name__") else str(annotation) args = "" - for name, param in cls.get_parameters(): + for name, param in cls.get_parameters().items(): if name in ("self", "kwargs") or (name == "stream" and not cls.supports_stream): continue args += f"\n {name}" diff --git a/g4f/webdriver.py b/g4f/webdriver.py index 2b7a7241..044ae03d 100644 --- a/g4f/webdriver.py +++ b/g4f/webdriver.py @@ -2,7 +2,7 @@ from __future__ import annotations try: from platformdirs import user_config_dir - from undetected_chromedriver import Chrome, ChromeOptions + from undetected_chromedriver import Chrome, ChromeOptions, find_chrome_executable from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.remote.webelement import WebElement from selenium.webdriver.common.by import By @@ -39,7 +39,7 @@ try: options = ChromeOptions() config = self._setup_backend(seleniumwire_options) options.add_argument(f"--proxy-server={config['proxy']['httpProxy']}") - options.add_argument('--proxy-bypass-list=<-loopback>') + options.add_argument("--proxy-bypass-list=<-loopback>") options.add_argument("--ignore-certificate-errors") super().__init__(*args, options=options, **kwargs) has_seleniumwire = True @@ -66,6 +66,9 @@ def get_browser( """ if not has_requirements: raise MissingRequirementsError('Install "undetected_chromedriver" and "platformdirs" package') + browser = find_chrome_executable() + if browser is None: + raise MissingRequirementsError('Install "Google Chrome" browser') if user_data_dir is None: user_data_dir = user_config_dir("g4f") if user_data_dir and debug.logging: @@ -82,6 +85,7 @@ def get_browser( options=options, user_data_dir=user_data_dir, driver_executable_path=driver, + browser_executable_path=browser, headless=headless, patcher_force_close=True ) |