summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHeiner Lohaus <heiner@lohaus.eu>2023-10-02 06:47:07 +0200
committerHeiner Lohaus <heiner@lohaus.eu>2023-10-02 06:47:07 +0200
commitd116f043227f789d6582bc12f69ea4ee7a9330ea (patch)
treed8a07c23a8172e26004560e4d6b08982f637e71c
parentRemove fake_useragent module (diff)
downloadgpt4free-d116f043227f789d6582bc12f69ea4ee7a9330ea.tar
gpt4free-d116f043227f789d6582bc12f69ea4ee7a9330ea.tar.gz
gpt4free-d116f043227f789d6582bc12f69ea4ee7a9330ea.tar.bz2
gpt4free-d116f043227f789d6582bc12f69ea4ee7a9330ea.tar.lz
gpt4free-d116f043227f789d6582bc12f69ea4ee7a9330ea.tar.xz
gpt4free-d116f043227f789d6582bc12f69ea4ee7a9330ea.tar.zst
gpt4free-d116f043227f789d6582bc12f69ea4ee7a9330ea.zip
-rw-r--r--g4f/Provider/AItianhu.py36
-rw-r--r--g4f/Provider/AItianhuSpace.py3
-rw-r--r--g4f/Provider/Aibn.py4
-rw-r--r--g4f/Provider/helper.py6
-rw-r--r--testing/test_async.py19
5 files changed, 41 insertions, 27 deletions
diff --git a/g4f/Provider/AItianhu.py b/g4f/Provider/AItianhu.py
index 0f01e536..42631d7e 100644
--- a/g4f/Provider/AItianhu.py
+++ b/g4f/Provider/AItianhu.py
@@ -1,24 +1,25 @@
from __future__ import annotations
import json
-from curl_cffi.requests import AsyncSession
-from .base_provider import AsyncProvider, format_prompt
+from ..typing import AsyncGenerator
+from ..requests import StreamSession
+from .base_provider import AsyncGeneratorProvider, format_prompt
-class AItianhu(AsyncProvider):
+class AItianhu(AsyncGeneratorProvider):
url = "https://www.aitianhu.com"
working = True
supports_gpt_35_turbo = True
@classmethod
- async def create_async(
+ async def create_async_generator(
cls,
model: str,
messages: list[dict[str, str]],
proxy: str = None,
**kwargs
- ) -> str:
+ ) -> AsyncGenerator:
data = {
"prompt": format_prompt(messages),
"options": {},
@@ -27,12 +28,25 @@ class AItianhu(AsyncProvider):
"top_p": 1,
**kwargs
}
- async with AsyncSession(proxies={"https": proxy}, impersonate="chrome107", verify=False) as session:
- response = await session.post(cls.url + "/api/chat-process", json=data)
- response.raise_for_status()
- line = response.text.splitlines()[-1]
- line = json.loads(line)
- return line["text"]
+ headers = {
+ "Authority": cls.url,
+ "Accept": "application/json, text/plain, */*",
+ "Origin": cls.url,
+ "Referer": f"{cls.url}/"
+ }
+ async with StreamSession(headers=headers, proxies={"https": proxy}, impersonate="chrome107", verify=False) as session:
+ async with session.post(f"{cls.url}/api/chat-process", json=data) as response:
+ response.raise_for_status()
+ 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)
+ if "detail" in line:
+ content = line["detail"]["choices"][0]["delta"].get("content")
+ if content:
+ yield content
+ else:
+ raise RuntimeError(f"Response: {line}")
@classmethod
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:
diff --git a/g4f/Provider/Aibn.py b/g4f/Provider/Aibn.py
index fe278f84..df56189b 100644
--- a/g4f/Provider/Aibn.py
+++ b/g4f/Provider/Aibn.py
@@ -4,7 +4,7 @@ import time
import hashlib
from ..typing import AsyncGenerator
-from ..requests import StreamRequest
+from ..requests import StreamSession
from .base_provider import AsyncGeneratorProvider
@@ -20,7 +20,7 @@ class Aibn(AsyncGeneratorProvider):
messages: list[dict[str, str]],
**kwargs
) -> AsyncGenerator:
- async with StreamRequest(impersonate="chrome107") as session:
+ async with StreamSession(impersonate="chrome107") as session:
timestamp = int(time.time())
data = {
"messages": messages,
diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py
index 544c5a76..234cdaa1 100644
--- a/g4f/Provider/helper.py
+++ b/g4f/Provider/helper.py
@@ -21,7 +21,11 @@ def get_event_loop() -> AbstractEventLoop:
try:
asyncio.get_running_loop()
except RuntimeError:
- return asyncio.get_event_loop()
+ try:
+ return asyncio.get_event_loop()
+ except RuntimeError:
+ asyncio.set_event_loop(asyncio.new_event_loop())
+ return asyncio.get_event_loop()
try:
event_loop = asyncio.get_event_loop()
if not hasattr(event_loop.__class__, "_nest_patched"):
diff --git a/testing/test_async.py b/testing/test_async.py
index bef2c75f..76b109b1 100644
--- a/testing/test_async.py
+++ b/testing/test_async.py
@@ -5,31 +5,26 @@ import asyncio
sys.path.append(str(Path(__file__).parent.parent))
import g4f
-from g4f.Provider import AsyncProvider
from testing.test_providers import get_providers
from testing.log_time import log_time_async
async def create_async(provider):
- model = g4f.models.gpt_35_turbo.name if provider.supports_gpt_35_turbo else g4f.models.default.name
try:
response = await log_time_async(
provider.create_async,
- model=model,
- messages=[{"role": "user", "content": "Hello Assistant!"}]
+ model=g4f.models.default.name,
+ messages=[{"role": "user", "content": "Hello, are you GPT 3.5?"}]
)
print(f"{provider.__name__}:", response)
except Exception as e:
- return f"{provider.__name__}: {e.__class__.__name__}: {e}"
+ print(f"{provider.__name__}: {e.__class__.__name__}: {e}")
async def run_async():
responses: list = [
- create_async(_provider)
- for _provider in get_providers()
- if _provider.working and issubclass(_provider, AsyncProvider)
+ create_async(provider)
+ for provider in get_providers()
+ if provider.working
]
- responses = await asyncio.gather(*responses)
- for error in responses:
- if error:
- print(error)
+ await asyncio.gather(*responses)
print("Total:", asyncio.run(log_time_async(run_async))) \ No newline at end of file