From f36e6a2e3b88dfaaee42dac8404b555cf5c27139 Mon Sep 17 00:00:00 2001 From: H Lohaus Date: Sun, 24 Nov 2024 13:30:10 +0100 Subject: [PATCH] Fix provider api response (#2408) * Update __init__.py * Update __init__.py * Update Gemini.py * Update Gemini.py * Update Gemini.py * Update Gemini.py * Update internet.py --- g4f/Provider/needs_auth/Gemini.py | 20 +++++++------------- g4f/api/__init__.py | 12 ++++++++++-- g4f/gui/server/internet.py | 1 - 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/g4f/Provider/needs_auth/Gemini.py b/g4f/Provider/needs_auth/Gemini.py index 89f6f802fc4..7b84f284c75 100644 --- a/g4f/Provider/needs_auth/Gemini.py +++ b/g4f/Provider/needs_auth/Gemini.py @@ -20,6 +20,7 @@ from ..helper import format_prompt, get_cookies from ...requests.raise_for_status import raise_for_status from ...requests.aiohttp import get_connector +from ...requests import get_nodriver from ...errors import MissingAuthError from ...image import ImageResponse, to_bytes from ... import debug @@ -68,17 +69,7 @@ async def nodriver_login(cls, proxy: str = None) -> AsyncIterator[str]: if debug.logging: print("Skip nodriver login in Gemini provider") return - try: - from platformdirs import user_config_dir - user_data_dir = user_config_dir("g4f-nodriver") - except: - user_data_dir = None - if debug.logging: - print(f"Open nodriver with user_dir: {user_data_dir}") - browser = await nodriver.start( - user_data_dir=user_data_dir, - browser_args=None if proxy is None else [f"--proxy-server={proxy}"], - ) + browser = await get_nodriver(proxy=proxy) login_url = os.environ.get("G4F_LOGIN_URL") if login_url: yield f"Please login: [Google Gemini]({login_url})\n\n" @@ -118,8 +109,11 @@ async def create_async_generator( if not cls._snlm0e: await cls.fetch_snlm0e(session, cls._cookies) if cls._cookies else None if not cls._snlm0e: - async for chunk in cls.nodriver_login(proxy): - yield chunk + try: + async for chunk in cls.nodriver_login(proxy): + yield chunk + except Exception as e: + raise MissingAuthError('Missing "__Secure-1PSID" cookie', e) if not cls._snlm0e: if cls._cookies is None or "__Secure-1PSID" not in cls._cookies: raise MissingAuthError('Missing "__Secure-1PSID" cookie') diff --git a/g4f/api/__init__.py b/g4f/api/__init__.py index f67a2aea033..d52b843160d 100644 --- a/g4f/api/__init__.py +++ b/g4f/api/__init__.py @@ -86,6 +86,14 @@ class ProviderResponseModel(BaseModel): object: str = "provider" created: int owned_by: Optional[str] + url: Optional[str] + label: Optional[str] +⅞ +class ProviderResponseModelDetail(ProviderResponseModel): + models: list[str] + image_models: list[str] + vision_models: list[str] + params: list[str] class ModelResponseModel(BaseModel): id: str @@ -299,7 +307,7 @@ async def providers() -> list[ProviderResponseModel]: } for provider in __providers__ if provider.working] @self.app.get("/v1/providers/{provider}") - async def providers_info(provider: str) -> ProviderResponseModel: + async def providers_info(provider: str) -> ProviderResponseModelDetail: if provider not in ProviderUtils.convert: return JSONResponse({"error": "The provider does not exist."}, 404) provider: ProviderType = ProviderUtils.convert[provider] @@ -396,4 +404,4 @@ def run_api( use_colors=use_colors, factory=True, reload=reload - ) \ No newline at end of file + ) diff --git a/g4f/gui/server/internet.py b/g4f/gui/server/internet.py index bafa3af74d2..96496f6552f 100644 --- a/g4f/gui/server/internet.py +++ b/g4f/gui/server/internet.py @@ -107,7 +107,6 @@ async def search(query: str, n_results: int = 5, max_words: int = 2500, add_text safesearch="moderate", timelimit="y", max_results=n_results, - backend="html" ): results.append(SearchResultEntry( result["title"],