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 ++++---- g4f/Provider/Aibn.py | 6 +++--- g4f/Provider/Aivvm.py | 6 +++--- g4f/Provider/ChatForAi.py | 6 +++--- g4f/Provider/ChatgptDuo.py | 20 ++++++++++---------- g4f/Provider/FreeGpt.py | 6 +++--- g4f/Provider/Ylokh.py | 13 +++++++------ 7 files changed, 33 insertions(+), 32 deletions(-) (limited to 'g4f/Provider') 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: diff --git a/g4f/Provider/Aibn.py b/g4f/Provider/Aibn.py index 1ef928be..fe278f84 100644 --- a/g4f/Provider/Aibn.py +++ b/g4f/Provider/Aibn.py @@ -4,7 +4,7 @@ import time import hashlib from ..typing import AsyncGenerator -from g4f.requests import AsyncSession +from ..requests import StreamRequest from .base_provider import AsyncGeneratorProvider @@ -20,7 +20,7 @@ class Aibn(AsyncGeneratorProvider): messages: list[dict[str, str]], **kwargs ) -> AsyncGenerator: - async with AsyncSession(impersonate="chrome107") as session: + async with StreamRequest(impersonate="chrome107") as session: timestamp = int(time.time()) data = { "messages": messages, @@ -30,7 +30,7 @@ class Aibn(AsyncGeneratorProvider): } async with session.post(f"{cls.url}/api/generate", json=data) as response: response.raise_for_status() - async for chunk in response.content.iter_any(): + async for chunk in response.iter_content(): yield chunk.decode() @classmethod diff --git a/g4f/Provider/Aivvm.py b/g4f/Provider/Aivvm.py index 5cd91546..c4ec677c 100644 --- a/g4f/Provider/Aivvm.py +++ b/g4f/Provider/Aivvm.py @@ -1,6 +1,6 @@ from __future__ import annotations -from ..requests import AsyncSession +from ..requests import StreamSession from .base_provider import AsyncGeneratorProvider from ..typing import AsyncGenerator @@ -43,10 +43,10 @@ class Aivvm(AsyncGeneratorProvider): "prompt" : kwargs.get("system_message", "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown."), "temperature" : kwargs.get("temperature", 0.7) } - async with AsyncSession(impersonate="chrome107") as session: + async with StreamSession(impersonate="chrome107") as session: async with session.post(f"{cls.url}/api/chat", json=json_data) as response: response.raise_for_status() - async for chunk in response.content.iter_any(): + async for chunk in response.iter_content(): yield chunk.decode() @classmethod diff --git a/g4f/Provider/ChatForAi.py b/g4f/Provider/ChatForAi.py index efb5478e..779799cf 100644 --- a/g4f/Provider/ChatForAi.py +++ b/g4f/Provider/ChatForAi.py @@ -3,7 +3,7 @@ from __future__ import annotations import time, hashlib from ..typing import AsyncGenerator -from g4f.requests import AsyncSession +from ..requests import StreamSession from .base_provider import AsyncGeneratorProvider @@ -19,7 +19,7 @@ class ChatForAi(AsyncGeneratorProvider): messages: list[dict[str, str]], **kwargs ) -> AsyncGenerator: - async with AsyncSession(impersonate="chrome107") as session: + async with StreamSession(impersonate="chrome107") as session: conversation_id = f"id_{int(time.time())}" prompt = messages[-1]["content"] timestamp = int(time.time()) @@ -43,7 +43,7 @@ class ChatForAi(AsyncGeneratorProvider): } async with session.post(f"{cls.url}/api/handle/provider-openai", json=data) as response: response.raise_for_status() - async for chunk in response.content.iter_any(): + async for chunk in response.iter_content(): yield chunk.decode() @classmethod diff --git a/g4f/Provider/ChatgptDuo.py b/g4f/Provider/ChatgptDuo.py index 07f4c16c..abed8a3c 100644 --- a/g4f/Provider/ChatgptDuo.py +++ b/g4f/Provider/ChatgptDuo.py @@ -1,6 +1,6 @@ from __future__ import annotations -from g4f.requests import AsyncSession +from curl_cffi.requests import AsyncSession from .base_provider import AsyncProvider, format_prompt @@ -23,17 +23,17 @@ class ChatgptDuo(AsyncProvider): "search": prompt, "purpose": "ask", } - async with session.post(f"{cls.url}/", data=data) as response: - response.raise_for_status() - data = await response.json() + response = await session.post(f"{cls.url}/", data=data) + response.raise_for_status() + data = response.json() - cls._sources = [{ - "title": source["title"], - "url": source["link"], - "snippet": source["snippet"] - } for source in data["results"]] + cls._sources = [{ + "title": source["title"], + "url": source["link"], + "snippet": source["snippet"] + } for source in data["results"]] - return data["answer"] + return data["answer"] @classmethod def get_sources(cls): diff --git a/g4f/Provider/FreeGpt.py b/g4f/Provider/FreeGpt.py index 534b69a5..092e1bb6 100644 --- a/g4f/Provider/FreeGpt.py +++ b/g4f/Provider/FreeGpt.py @@ -3,7 +3,7 @@ from __future__ import annotations import time, hashlib, random from ..typing import AsyncGenerator -from g4f.requests import AsyncSession +from ..requests import StreamSession from .base_provider import AsyncGeneratorProvider domains = [ @@ -23,7 +23,7 @@ class FreeGpt(AsyncGeneratorProvider): messages: list[dict[str, str]], **kwargs ) -> AsyncGenerator: - async with AsyncSession(impersonate="chrome107") as session: + async with StreamSession(impersonate="chrome107") as session: prompt = messages[-1]["content"] timestamp = int(time.time()) data = { @@ -35,7 +35,7 @@ class FreeGpt(AsyncGeneratorProvider): url = random.choice(domains) async with session.post(f"{url}/api/generate", json=data) as response: response.raise_for_status() - async for chunk in response.content.iter_any(): + async for chunk in response.iter_content(): yield chunk.decode() @classmethod diff --git a/g4f/Provider/Ylokh.py b/g4f/Provider/Ylokh.py index 2187eb78..3c8b32dd 100644 --- a/g4f/Provider/Ylokh.py +++ b/g4f/Provider/Ylokh.py @@ -2,7 +2,7 @@ from __future__ import annotations import json -from ..requests import AsyncSession +from ..requests import StreamSession from .base_provider import AsyncGeneratorProvider from ..typing import AsyncGenerator @@ -37,18 +37,19 @@ class Ylokh(AsyncGeneratorProvider): "stream": stream, **kwargs } - async with AsyncSession( - headers=headers + async with StreamSession( + headers=headers, + proxies={"https": proxy} ) as session: - async with session.post("https://chatapi.ylokh.xyz/v1/chat/completions", json=data, proxy=proxy) as response: + async with session.post("https://chatapi.ylokh.xyz/v1/chat/completions", json=data) as response: response.raise_for_status() if stream: - async for line in response.content: + async for line in response.iter_lines(): line = line.decode() if line.startswith("data: "): if line.startswith("data: [DONE]"): break - line = json.loads(line[6:-1]) + line = json.loads(line[6:]) content = line["choices"][0]["delta"].get("content") if content: yield content -- cgit v1.2.3