From b80ca3b7c968cc82af1a1f22815442174771446f Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Mon, 26 Feb 2024 02:31:14 +0100 Subject: Fix GeminiPro auth for normal user --- g4f/Provider/GeminiPro.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'g4f') diff --git a/g4f/Provider/GeminiPro.py b/g4f/Provider/GeminiPro.py index 87ded3ac..972855ce 100644 --- a/g4f/Provider/GeminiPro.py +++ b/g4f/Provider/GeminiPro.py @@ -34,17 +34,18 @@ class GeminiPro(AsyncGeneratorProvider, ProviderModelMixin): if not api_key: raise MissingAuthError('Missing "api_key"') + + headers = params = None + if api_base: + headers = {"Authorization": f"Bearer {api_key}"} + else: + params = {"key": api_key} + if not api_base: api_base = f"https://generativelanguage.googleapis.com/v1beta" method = "streamGenerateContent" if stream else "generateContent" url = f"{api_base.rstrip('/')}/models/{model}:{method}" - headers = None - if api_base: - headers = {f"Authorization": "Bearer {api_key}"} - else: - url += f"?key={api_key}" - async with ClientSession(headers=headers) as session: contents = [ { @@ -71,10 +72,11 @@ class GeminiPro(AsyncGeneratorProvider, ProviderModelMixin): "topK": kwargs.get("top_k"), } } - async with session.post(url, json=data, proxy=proxy) as response: + async with session.post(url, params=params, json=data, proxy=proxy) as response: if not response.ok: data = await response.json() - raise RuntimeError(data[0]["error"]["message"]) + data = data[0] if isinstance(data, list) else data + raise RuntimeError(data["error"]["message"]) if stream: lines = [] async for chunk in response.content: -- cgit v1.2.3 From 02b84fb519c853cee0a7765b475371d9eb59cf60 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Mon, 26 Feb 2024 10:59:18 +0100 Subject: Improve filter_messages in gui Add rdns suport for proxies --- g4f/gui/client/html/index.html | 1 - g4f/gui/client/js/chat.v1.js | 19 +++++++++++++++---- g4f/providers/helper.py | 6 +++++- 3 files changed, 20 insertions(+), 6 deletions(-) (limited to 'g4f') diff --git a/g4f/gui/client/html/index.html b/g4f/gui/client/html/index.html index 102a762e..9ef8a820 100644 --- a/g4f/gui/client/html/index.html +++ b/g4f/gui/client/html/index.html @@ -173,7 +173,6 @@ - diff --git a/g4f/gui/client/js/chat.v1.js b/g4f/gui/client/js/chat.v1.js index c727dbf9..edd27689 100644 --- a/g4f/gui/client/js/chat.v1.js +++ b/g4f/gui/client/js/chat.v1.js @@ -121,6 +121,20 @@ const remove_cancel_button = async () => { }; const filter_messages = (messages) => { + // Removes none user messages at end + let last_message; + while (last_message = new_messages.pop()) { + if (last_message["role"] == "user") { + new_messages.push(last_message); + break; + } + } + + // Remove history, if it is selected + if (document.getElementById('history')?.checked) { + messages = [messages[messages.length-1]]; + } + let new_messages = []; for (i in messages) { new_message = messages[i]; @@ -135,6 +149,7 @@ const filter_messages = (messages) => { new_messages.push(new_message) } } + return new_messages; } @@ -143,10 +158,6 @@ const ask_gpt = async () => { messages = await get_messages(window.conversation_id); total_messages = messages.length; - // Remove history, if it is selected - if (document.getElementById('history')?.checked) { - messages = [messages[messages.length-1]]; - } messages = filter_messages(messages); window.scrollTo(0, 0); diff --git a/g4f/providers/helper.py b/g4f/providers/helper.py index 49d033d1..6981f984 100644 --- a/g4f/providers/helper.py +++ b/g4f/providers/helper.py @@ -55,7 +55,11 @@ def get_connector(connector: BaseConnector = None, proxy: str = None) -> Optiona if proxy and not connector: try: from aiohttp_socks import ProxyConnector - connector = ProxyConnector.from_url(proxy) + rdns = False + if proxy.startswith("socks5h://"): + proxy = proxy.replace("socks5h://", "socks5://") + rdns = True + connector = ProxyConnector.from_url(proxy, rdns=rdns) except ImportError: raise MissingRequirementsError('Install "aiohttp_socks" package for proxy support') return connector \ No newline at end of file -- cgit v1.2.3 From 6bbe60572e55d7cdc72a179da3940554128c2c50 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Mon, 26 Feb 2024 11:25:07 +0100 Subject: Add default values for rdns --- g4f/Provider/Bing.py | 2 +- g4f/Provider/Liaobots.py | 2 +- g4f/providers/helper.py | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) (limited to 'g4f') diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py index 1e29c4f1..5bc89479 100644 --- a/g4f/Provider/Bing.py +++ b/g4f/Provider/Bing.py @@ -70,7 +70,7 @@ class Bing(AsyncGeneratorProvider): gpt4_turbo = True if model.startswith("gpt-4-turbo") else False - return stream_generate(prompt, tone, image, context, cookies, get_connector(connector, proxy), web_search, gpt4_turbo, timeout) + return stream_generate(prompt, tone, image, context, cookies, get_connector(connector, proxy, True), web_search, gpt4_turbo, timeout) def create_context(messages: Messages) -> str: """ diff --git a/g4f/Provider/Liaobots.py b/g4f/Provider/Liaobots.py index 54bf7f2e..159c8488 100644 --- a/g4f/Provider/Liaobots.py +++ b/g4f/Provider/Liaobots.py @@ -105,7 +105,7 @@ class Liaobots(AsyncGeneratorProvider, ProviderModelMixin): async with ClientSession( headers=headers, cookie_jar=cls._cookie_jar, - connector=get_connector(connector, proxy) + connector=get_connector(connector, proxy, True) ) as session: cls._auth_code = auth if isinstance(auth, str) else cls._auth_code if not cls._auth_code: diff --git a/g4f/providers/helper.py b/g4f/providers/helper.py index 6981f984..c027216b 100644 --- a/g4f/providers/helper.py +++ b/g4f/providers/helper.py @@ -51,11 +51,10 @@ def get_random_hex() -> str: """ return secrets.token_hex(16).zfill(32) -def get_connector(connector: BaseConnector = None, proxy: str = None) -> Optional[BaseConnector]: +def get_connector(connector: BaseConnector = None, proxy: str = None, rdns: bool = False) -> Optional[BaseConnector]: if proxy and not connector: try: from aiohttp_socks import ProxyConnector - rdns = False if proxy.startswith("socks5h://"): proxy = proxy.replace("socks5h://", "socks5://") rdns = True -- cgit v1.2.3 From 5f0592ee2928b396a170b9a90d62e28fba24e759 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Mon, 26 Feb 2024 11:28:35 +0100 Subject: Add proxy connector to GeminiPro --- g4f/Provider/GeminiPro.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'g4f') diff --git a/g4f/Provider/GeminiPro.py b/g4f/Provider/GeminiPro.py index 972855ce..a2e3538d 100644 --- a/g4f/Provider/GeminiPro.py +++ b/g4f/Provider/GeminiPro.py @@ -2,12 +2,13 @@ from __future__ import annotations import base64 import json -from aiohttp import ClientSession +from aiohttp import ClientSession, BaseConnector from ..typing import AsyncResult, Messages, ImageType from .base_provider import AsyncGeneratorProvider, ProviderModelMixin from ..image import to_bytes, is_accepted_format from ..errors import MissingAuthError +from .helper import get_connector class GeminiPro(AsyncGeneratorProvider, ProviderModelMixin): url = "https://ai.google.dev" @@ -27,6 +28,7 @@ class GeminiPro(AsyncGeneratorProvider, ProviderModelMixin): api_key: str = None, api_base: str = None, image: ImageType = None, + connector: BaseConnector = None, **kwargs ) -> AsyncResult: model = "gemini-pro-vision" if not model and image else model @@ -46,7 +48,7 @@ class GeminiPro(AsyncGeneratorProvider, ProviderModelMixin): method = "streamGenerateContent" if stream else "generateContent" url = f"{api_base.rstrip('/')}/models/{model}:{method}" - async with ClientSession(headers=headers) as session: + async with ClientSession(headers=headers, connector=get_connector(connector, proxy)) as session: contents = [ { "role": "model" if message["role"] == "assistant" else message["role"], @@ -72,7 +74,7 @@ class GeminiPro(AsyncGeneratorProvider, ProviderModelMixin): "topK": kwargs.get("top_k"), } } - async with session.post(url, params=params, json=data, proxy=proxy) as response: + async with session.post(url, params=params, json=data) as response: if not response.ok: data = await response.json() data = data[0] if isinstance(data, list) else data -- cgit v1.2.3