diff options
author | Tekky <98614666+xtekky@users.noreply.github.com> | 2023-10-02 12:20:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-02 12:20:18 +0200 |
commit | c0632c27411b5cc8a643e0bffd71537f4224473c (patch) | |
tree | d8a07c23a8172e26004560e4d6b08982f637e71c /g4f/Provider/AItianhu.py | |
parent | ~ | v-0.1.4.2 `pip install -U g4f` (diff) | |
parent | Fix: There is no current event loop in thread (diff) | |
download | gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.tar gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.tar.gz gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.tar.bz2 gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.tar.lz gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.tar.xz gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.tar.zst gpt4free-c0632c27411b5cc8a643e0bffd71537f4224473c.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/Provider/AItianhu.py | 36 |
1 files changed, 25 insertions, 11 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 |