diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-02-26 11:30:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-26 11:30:17 +0100 |
commit | 36e7665613c4a8ff373d90401ae804f7a334feab (patch) | |
tree | 1c5bd93c13f845b8554da3290e124116e251663f /g4f/Provider | |
parent | Merge pull request #1630 from hlohaus/flow (diff) | |
parent | Add proxy connector to GeminiPro (diff) | |
download | gpt4free-36e7665613c4a8ff373d90401ae804f7a334feab.tar gpt4free-36e7665613c4a8ff373d90401ae804f7a334feab.tar.gz gpt4free-36e7665613c4a8ff373d90401ae804f7a334feab.tar.bz2 gpt4free-36e7665613c4a8ff373d90401ae804f7a334feab.tar.lz gpt4free-36e7665613c4a8ff373d90401ae804f7a334feab.tar.xz gpt4free-36e7665613c4a8ff373d90401ae804f7a334feab.tar.zst gpt4free-36e7665613c4a8ff373d90401ae804f7a334feab.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/Provider/Bing.py | 2 | ||||
-rw-r--r-- | g4f/Provider/GeminiPro.py | 24 | ||||
-rw-r--r-- | g4f/Provider/Liaobots.py | 2 |
3 files changed, 16 insertions, 12 deletions
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/GeminiPro.py b/g4f/Provider/GeminiPro.py index 87ded3ac..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 @@ -34,18 +36,19 @@ 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: + async with ClientSession(headers=headers, connector=get_connector(connector, proxy)) as session: contents = [ { "role": "model" if message["role"] == "assistant" else message["role"], @@ -71,10 +74,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) 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: 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: |