From 98895e5b09ea5e3f19b2263ddca028c7b296abb2 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sun, 1 Oct 2023 06:38:11 +0200 Subject: Update HuggingChat to new api Impersonate Aivvm Provider Add ChatForAi and FreeGpt Provider Update AItianhuSpace Provider Improve StreamRequest Support Update get_event_loop Helper --- g4f/Provider/AItianhuSpace.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'g4f/Provider/AItianhuSpace.py') diff --git a/g4f/Provider/AItianhuSpace.py b/g4f/Provider/AItianhuSpace.py index 8beb3355..eb072db5 100644 --- a/g4f/Provider/AItianhuSpace.py +++ b/g4f/Provider/AItianhuSpace.py @@ -2,7 +2,7 @@ from __future__ import annotations import random, json -from g4f.requests import AsyncSession, StreamRequest +from g4f.requests import AsyncSession from .base_provider import AsyncGeneratorProvider, format_prompt domains = { @@ -31,12 +31,9 @@ class AItianhuSpace(AsyncGeneratorProvider): chars = 'abcdefghijklmnopqrstuvwxyz0123456789' rand = ''.join(random.choice(chars) for _ in range(6)) domain = domains[model] - url = f'https://{rand}{domain}/api/chat-process' + url = f'https://{rand}{domain}' - headers = { - "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36", - } - async with AsyncSession(headers=headers, impersonate="chrome107", verify=False) as session: + async with AsyncSession(impersonate="chrome110", verify=False) as session: data = { "prompt": format_prompt(messages), "options": {}, @@ -45,9 +42,17 @@ class AItianhuSpace(AsyncGeneratorProvider): "top_p": 1, **kwargs } - async with StreamRequest(session, "POST", url, json=data) as response: + headers = { + "Authority": url, + "Accept": "application/json, text/plain, */*", + "Origin": url, + "Referer": f"{url}/" + } + async with session.post(f"{url}/api/chat-process", json=data, headers=headers) as response: response.raise_for_status() async for line in response.content: + if b"platform's risk control" in line: + raise RuntimeError("Platform's Risk Control") line = json.loads(line.rstrip()) if "detail" in line: content = line["detail"]["choices"][0]["delta"].get("content") @@ -56,7 +61,7 @@ class AItianhuSpace(AsyncGeneratorProvider): elif "message" in line and "AI-4接口非常昂贵" in line["message"]: raise RuntimeError("Rate limit for GPT 4 reached") else: - raise RuntimeError("Response: {line}") + raise RuntimeError(f"Response: {line}") @classmethod -- cgit v1.2.3 From eb0e2c6a93c3f21937457d13220ce2b7fca1f04a Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Mon, 2 Oct 2023 02:04:22 +0200 Subject: +Curl +Async +Stream Requests Update Model List --- g4f/Provider/AItianhuSpace.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'g4f/Provider/AItianhuSpace.py') diff --git a/g4f/Provider/AItianhuSpace.py b/g4f/Provider/AItianhuSpace.py index eb072db5..8805b1c0 100644 --- a/g4f/Provider/AItianhuSpace.py +++ b/g4f/Provider/AItianhuSpace.py @@ -2,7 +2,7 @@ from __future__ import annotations import random, json -from g4f.requests import AsyncSession +from ..requests import StreamSession from .base_provider import AsyncGeneratorProvider, format_prompt domains = { @@ -33,7 +33,7 @@ class AItianhuSpace(AsyncGeneratorProvider): domain = domains[model] url = f'https://{rand}{domain}' - async with AsyncSession(impersonate="chrome110", verify=False) as session: + async with StreamSession(impersonate="chrome110", verify=False) as session: data = { "prompt": format_prompt(messages), "options": {}, @@ -50,10 +50,10 @@ class AItianhuSpace(AsyncGeneratorProvider): } async with session.post(f"{url}/api/chat-process", json=data, headers=headers) as response: response.raise_for_status() - async for line in response.content: + async for line in response.iter_lines(): if b"platform's risk control" in line: raise RuntimeError("Platform's Risk Control") - line = json.loads(line.rstrip()) + line = json.loads(line) if "detail" in line: content = line["detail"]["choices"][0]["delta"].get("content") if content: -- cgit v1.2.3 From d116f043227f789d6582bc12f69ea4ee7a9330ea Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Mon, 2 Oct 2023 06:47:07 +0200 Subject: Fix: There is no current event loop in thread --- g4f/Provider/AItianhuSpace.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'g4f/Provider/AItianhuSpace.py') diff --git a/g4f/Provider/AItianhuSpace.py b/g4f/Provider/AItianhuSpace.py index 8805b1c0..a6bf9a58 100644 --- a/g4f/Provider/AItianhuSpace.py +++ b/g4f/Provider/AItianhuSpace.py @@ -2,6 +2,7 @@ from __future__ import annotations import random, json +from ..typing import AsyncGenerator from ..requests import StreamSession from .base_provider import AsyncGeneratorProvider, format_prompt @@ -22,7 +23,7 @@ class AItianhuSpace(AsyncGeneratorProvider): messages: list[dict[str, str]], stream: bool = True, **kwargs - ) -> str: + ) -> AsyncGenerator: if not model: model = "gpt-3.5-turbo" elif not model in domains: -- cgit v1.2.3 From 6a61cf811655fa87dbcb196025cc0b6040502293 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Wed, 4 Oct 2023 07:20:51 +0200 Subject: Sort providers in new dirs --- g4f/Provider/AItianhuSpace.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'g4f/Provider/AItianhuSpace.py') diff --git a/g4f/Provider/AItianhuSpace.py b/g4f/Provider/AItianhuSpace.py index a6bf9a58..27f2b1fa 100644 --- a/g4f/Provider/AItianhuSpace.py +++ b/g4f/Provider/AItianhuSpace.py @@ -52,6 +52,8 @@ class AItianhuSpace(AsyncGeneratorProvider): async with session.post(f"{url}/api/chat-process", json=data, headers=headers) as response: response.raise_for_status() async for line in response.iter_lines(): + if line == b"