From 98d3304108de3e55c18f2af8a66a501541ec658b Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Tue, 22 Aug 2023 23:27:34 +0200 Subject: Improve providers with tests --- g4f/Provider/You.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'g4f/Provider/You.py') diff --git a/g4f/Provider/You.py b/g4f/Provider/You.py index 0d8114a8..cbd741ba 100644 --- a/g4f/Provider/You.py +++ b/g4f/Provider/You.py @@ -1,5 +1,6 @@ import re import urllib.parse +import json from curl_cffi import requests @@ -28,7 +29,11 @@ class You(BaseProvider): impersonate="chrome107", ) response.raise_for_status() - yield _parse_output(response.text) + start = 'data: {"youChatToken": ' + for line in response.content.splitlines(): + line = line.decode('utf-8') + if line.startswith(start): + yield json.loads(line[len(start): -1]) def _create_url_param(messages: list[dict[str, str]]): @@ -50,10 +55,4 @@ def _create_header(): return { "accept": "text/event-stream", "referer": "https://you.com/search?fromSearchBar=true&tbm=youchat", - } - - -def _parse_output(output: str) -> str: - regex = r"^data:\s{\"youChatToken\": \"(.*)\"}$" - tokens = [token for token in re.findall(regex, output, re.MULTILINE)] - return "".join(tokens) + } \ No newline at end of file -- cgit v1.2.3 From 7222d9b7cea4db173258c1189f5069b544606eeb Mon Sep 17 00:00:00 2001 From: zengrr <47846202+zeng-rr@users.noreply.github.com> Date: Wed, 23 Aug 2023 15:26:00 +0800 Subject: "You" unicode decode and fix prompt just is last message --- g4f/Provider/You.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'g4f/Provider/You.py') diff --git a/g4f/Provider/You.py b/g4f/Provider/You.py index 0d8114a8..a8de7dec 100644 --- a/g4f/Provider/You.py +++ b/g4f/Provider/You.py @@ -19,7 +19,7 @@ class You(BaseProvider): stream: bool, **kwargs: Any, ) -> CreateResult: - url_param = _create_url_param(messages) + url_param = _create_url_param(messages, kwargs.get("history", [])) headers = _create_header() url = f"https://you.com/api/streamingSearch?{url_param}" response = requests.get( @@ -28,12 +28,15 @@ class You(BaseProvider): impersonate="chrome107", ) response.raise_for_status() - yield _parse_output(response.text) + yield _parse_output(response.text).encode().decode("unicode_escape") -def _create_url_param(messages: list[dict[str, str]]): - prompt = messages.pop()["content"] - chat = _convert_chat(messages) +def _create_url_param(messages: list[dict[str, str]], history: list[dict[str, str]]): + prompt = "" + for message in messages: + prompt += "%s: %s\n" % (message["role"], message["content"]) + prompt += "assistant:" + chat = _convert_chat(history) param = {"q": prompt, "domain": "youchat", "chat": chat} return urllib.parse.urlencode(param) -- 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/You.py | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'g4f/Provider/You.py') diff --git a/g4f/Provider/You.py b/g4f/Provider/You.py index 7ea699a2..38167999 100644 --- a/g4f/Provider/You.py +++ b/g4f/Provider/You.py @@ -1,33 +1,27 @@ -import re -import urllib.parse -import json +import urllib.parse, json -from curl_cffi import requests - -from ..typing import Any, CreateResult +from curl_cffi import requests +from ..typing import Any, CreateResult from .base_provider import BaseProvider class You(BaseProvider): - url = "https://you.com" - working = True + url = "https://you.com" + working = True supports_gpt_35_turbo = True @staticmethod def create_completion( model: str, messages: list[dict[str, str]], - stream: bool, - **kwargs: Any, - ) -> CreateResult: + stream: bool, **kwargs: Any) -> CreateResult: + url_param = _create_url_param(messages, kwargs.get("history", [])) - headers = _create_header() - url = f"https://you.com/api/streamingSearch?{url_param}" - response = requests.get( - url, - headers=headers, - impersonate="chrome107", - ) + headers = _create_header() + + response = requests.get(f"https://you.com/api/streamingSearch?{url_param}", + headers=headers, impersonate="chrome107") + response.raise_for_status() start = 'data: {"youChatToken": ' -- 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/You.py | 76 ++++++++++++++++++++--------------------------------- 1 file changed, 29 insertions(+), 47 deletions(-) (limited to 'g4f/Provider/You.py') diff --git a/g4f/Provider/You.py b/g4f/Provider/You.py index 38167999..1fbe1eb6 100644 --- a/g4f/Provider/You.py +++ b/g4f/Provider/You.py @@ -1,55 +1,37 @@ -import urllib.parse, json +from aiohttp import ClientSession +import json -from curl_cffi import requests -from ..typing import Any, CreateResult -from .base_provider import BaseProvider +from ..typing import AsyncGenerator +from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies -class You(BaseProvider): - url = "https://you.com" - working = True +class You(AsyncGeneratorProvider): + url = "https://you.com" + working = True supports_gpt_35_turbo = True + supports_stream = True @staticmethod - def create_completion( + async def create_async_generator( model: str, messages: list[dict[str, str]], - stream: bool, **kwargs: Any) -> CreateResult: - - url_param = _create_url_param(messages, kwargs.get("history", [])) - headers = _create_header() - - response = requests.get(f"https://you.com/api/streamingSearch?{url_param}", - headers=headers, impersonate="chrome107") - - response.raise_for_status() - - start = 'data: {"youChatToken": ' - for line in response.content.splitlines(): - line = line.decode('utf-8') - if line.startswith(start): - yield json.loads(line[len(start): -1]) - -def _create_url_param(messages: list[dict[str, str]], history: list[dict[str, str]]): - prompt = "" - for message in messages: - prompt += "%s: %s\n" % (message["role"], message["content"]) - prompt += "assistant:" - chat = _convert_chat(history) - param = {"q": prompt, "domain": "youchat", "chat": chat} - return urllib.parse.urlencode(param) - - -def _convert_chat(messages: list[dict[str, str]]): - message_iter = iter(messages) - return [ - {"question": user["content"], "answer": assistant["content"]} - for user, assistant in zip(message_iter, message_iter) - ] - - -def _create_header(): - return { - "accept": "text/event-stream", - "referer": "https://you.com/search?fromSearchBar=true&tbm=youchat", - } \ No newline at end of file + cookies: dict = None, + **kwargs, + ) -> AsyncGenerator: + if not cookies: + cookies = get_cookies("you.com") + headers = { + "Accept": "text/event-stream", + "Referer": "https://you.com/search?fromSearchBar=true&tbm=youchat", + "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0" + } + async with ClientSession(headers=headers, cookies=cookies) as session: + async with session.get( + "https://you.com/api/streamingSearch", + params={"q": format_prompt(messages), "domain": "youchat", "chat": ""}, + ) as response: + start = 'data: {"youChatToken": ' + async for line in response.content: + line = line.decode('utf-8') + if line.startswith(start): + yield json.loads(line[len(start): -2]) \ No newline at end of file -- 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/You.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'g4f/Provider/You.py') diff --git a/g4f/Provider/You.py b/g4f/Provider/You.py index 1fbe1eb6..121d1dbd 100644 --- a/g4f/Provider/You.py +++ b/g4f/Provider/You.py @@ -1,6 +1,9 @@ -from aiohttp import ClientSession +from __future__ import annotations + import json +from aiohttp import ClientSession + from ..typing import AsyncGenerator from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies -- cgit v1.2.3