diff options
author | Ryan Jordan <ryjordan@gmail.com> | 2023-09-06 02:39:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-06 02:39:57 +0200 |
commit | f81e618958318a092ca4c70a1b3ea15260bda97c (patch) | |
tree | 3ce022a8d719011268da7f1a0befc97bf32a60d8 /g4f/Provider/Bard.py | |
parent | feat(docker): add Docker and Docker Compose support (diff) | |
parent | ~ | Merge pull request #869 from ahobsonsayers/add-console-script (diff) | |
download | gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.tar gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.tar.gz gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.tar.bz2 gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.tar.lz gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.tar.xz gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.tar.zst gpt4free-f81e618958318a092ca4c70a1b3ea15260bda97c.zip |
Diffstat (limited to 'g4f/Provider/Bard.py')
-rw-r--r-- | g4f/Provider/Bard.py | 45 |
1 files changed, 14 insertions, 31 deletions
diff --git a/g4f/Provider/Bard.py b/g4f/Provider/Bard.py index cbe728cd..2137d820 100644 --- a/g4f/Provider/Bard.py +++ b/g4f/Provider/Bard.py @@ -1,51 +1,33 @@ +from __future__ import annotations + 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, format_prompt, 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 = {}, - **kwargs: Any, + cookies: dict = None, + **kwargs ) -> 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] - ) - prompt = f"{formatted}\nAssistant:" - + 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', @@ -62,10 +44,11 @@ class Bard(BaseProvider): ) 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', |