From 126496d3cacd06a4fa8cbb4e5bde417ce6bb5b4a Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Fri, 25 Aug 2023 06:41:32 +0200 Subject: Add OpenaiChat and Hugchat Provider Add tests for providers with auth Improve async support / 2x faster Shared get_cookies by domain function --- g4f/Provider/Bard.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) (limited to 'g4f/Provider/Bard.py') diff --git a/g4f/Provider/Bard.py b/g4f/Provider/Bard.py index cbe728cd..a8c7d13f 100644 --- a/g4f/Provider/Bard.py +++ b/g4f/Provider/Bard.py @@ -2,42 +2,26 @@ import json import random import re -import browser_cookie3 from aiohttp import ClientSession import asyncio from ..typing import Any, CreateResult -from .base_provider import BaseProvider +from .base_provider import AsyncProvider, get_cookies -class Bard(BaseProvider): +class Bard(AsyncProvider): url = "https://bard.google.com" needs_auth = True working = True - @classmethod - def create_completion( - cls, - model: str, - messages: list[dict[str, str]], - stream: bool, - proxy: str = None, - cookies: dict = {}, - **kwargs: Any, - ) -> CreateResult: - yield asyncio.run(cls.create_async(str, messages, proxy, cookies)) - @classmethod async def create_async( cls, model: str, messages: list[dict[str, str]], proxy: str = None, - cookies: dict = {}, + cookies: dict = get_cookies(".google.com"), **kwargs: Any, ) -> str: - if not cookies: - for cookie in browser_cookie3.load(domain_name='.google.com'): - cookies[cookie.name] = cookie.value formatted = "\n".join( ["%s: %s" % (message["role"], message["content"]) for message in messages] -- cgit v1.2.3 From efd75a11b871d61ac31b0e274acdfb33daba361d Mon Sep 17 00:00:00 2001 From: abc <98614666+xtekky@users.noreply.github.com> Date: Sun, 27 Aug 2023 17:37:44 +0200 Subject: ~ | code styling --- g4f/Provider/Bard.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'g4f/Provider/Bard.py') diff --git a/g4f/Provider/Bard.py b/g4f/Provider/Bard.py index a8c7d13f..a516227b 100644 --- a/g4f/Provider/Bard.py +++ b/g4f/Provider/Bard.py @@ -19,9 +19,7 @@ class Bard(AsyncProvider): model: str, messages: list[dict[str, str]], proxy: str = None, - cookies: dict = get_cookies(".google.com"), - **kwargs: Any, - ) -> str: + cookies: dict = get_cookies(".google.com"), **kwargs: Any,) -> str: formatted = "\n".join( ["%s: %s" % (message["role"], message["content"]) for message in messages] -- cgit v1.2.3 From 7294abc890c377d75c6c8c932620c2e2c8b3f0f9 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Mon, 28 Aug 2023 01:43:45 +0200 Subject: Add async support for H2o Add format_prompt helper Fix create_completion in AsyncGeneratorProvider Move get_cookies from constructor to function Add ow HuggingChat implement Remove need auth form Liabots Add staic cache for access_token in OpenaiChat Add OpenAssistant provider Support stream and async in You Support async and add userId in Yqcloud Add log_time module --- g4f/Provider/Bard.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'g4f/Provider/Bard.py') diff --git a/g4f/Provider/Bard.py b/g4f/Provider/Bard.py index a516227b..978fa98e 100644 --- a/g4f/Provider/Bard.py +++ b/g4f/Provider/Bard.py @@ -1,12 +1,9 @@ import json import random import re - from aiohttp import ClientSession -import asyncio -from ..typing import Any, CreateResult -from .base_provider import AsyncProvider, get_cookies +from .base_provider import AsyncProvider, get_cookies, format_prompt class Bard(AsyncProvider): url = "https://bard.google.com" @@ -19,15 +16,14 @@ class Bard(AsyncProvider): model: str, messages: list[dict[str, str]], proxy: str = None, - cookies: dict = get_cookies(".google.com"), **kwargs: Any,) -> str: - - formatted = "\n".join( - ["%s: %s" % (message["role"], message["content"]) for message in messages] - ) - prompt = f"{formatted}\nAssistant:" - + cookies: dict = None, + **kwargs + ) -> str: + prompt = format_prompt(messages) if proxy and "://" not in proxy: proxy = f"http://{proxy}" + if not cookies: + cookies = get_cookies(".google.com") headers = { 'authority': 'bard.google.com', @@ -44,10 +40,11 @@ class Bard(AsyncProvider): ) as session: async with session.get(cls.url, proxy=proxy) as response: text = await response.text() - + match = re.search(r'SNlM0e\":\"(.*?)\"', text) - if match: - snlm0e = match.group(1) + if not match: + raise RuntimeError("No snlm0e value.") + snlm0e = match.group(1) params = { 'bl': 'boq_assistant-bard-web-server_20230326.21_p0', -- cgit v1.2.3 From 901595b10f08972ee3ac5fc08c346dbb561a7d62 Mon Sep 17 00:00:00 2001 From: msi-JunXiang Date: Sun, 3 Sep 2023 16:26:26 +0800 Subject: type hints Use `from __future__ import annotations avoid `dict` and `list` cause "TypeErro: 'type' object is not subscriptable". Refer to the following Stack Overflow discussions for more information: 1. https://stackoverflow.com/questions/75202610/typeerror-type-object-is-not-subscriptable-python 2. https://stackoverflow.com/questions/59101121/type-hint-for-a-dict-gives-typeerror-type-object-is-not-subscriptable --- g4f/Provider/Bard.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'g4f/Provider/Bard.py') diff --git a/g4f/Provider/Bard.py b/g4f/Provider/Bard.py index 978fa98e..2137d820 100644 --- a/g4f/Provider/Bard.py +++ b/g4f/Provider/Bard.py @@ -1,9 +1,13 @@ +from __future__ import annotations + import json import random import re + from aiohttp import ClientSession -from .base_provider import AsyncProvider, get_cookies, format_prompt +from .base_provider import AsyncProvider, format_prompt, get_cookies + class Bard(AsyncProvider): url = "https://bard.google.com" -- cgit v1.2.3