diff options
author | Tekky <98614666+xtekky@users.noreply.github.com> | 2024-09-07 21:37:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-07 21:37:24 +0200 |
commit | 07fa87b4d180259d1da86afb565e14ac3d60d50b (patch) | |
tree | d474a2bf8bd79cf94bfa48cbaca3917659dd19be /g4f/Provider/CodeNews.py | |
parent | Merge pull request #2206 from Parthsadaria/patch-1 (diff) | |
parent | g4f/models.py g4f/Provider/MagickPen.py (diff) | |
download | gpt4free-0.3.2.6.tar gpt4free-0.3.2.6.tar.gz gpt4free-0.3.2.6.tar.bz2 gpt4free-0.3.2.6.tar.lz gpt4free-0.3.2.6.tar.xz gpt4free-0.3.2.6.tar.zst gpt4free-0.3.2.6.zip |
Diffstat (limited to 'g4f/Provider/CodeNews.py')
-rw-r--r-- | g4f/Provider/CodeNews.py | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/g4f/Provider/CodeNews.py b/g4f/Provider/CodeNews.py new file mode 100644 index 00000000..05ec7a45 --- /dev/null +++ b/g4f/Provider/CodeNews.py @@ -0,0 +1,94 @@ +from __future__ import annotations + +from aiohttp import ClientSession +from asyncio import sleep + +from ..typing import AsyncResult, Messages +from .base_provider import AsyncGeneratorProvider, ProviderModelMixin +from .helper import format_prompt + + +class CodeNews(AsyncGeneratorProvider, ProviderModelMixin): + url = "https://codenews.cc" + api_endpoint = "https://codenews.cc/chatxyz13" + working = True + supports_gpt_35_turbo = True + supports_gpt_4 = False + supports_stream = True + supports_system_message = False + supports_message_history = False + + default_model = 'free_gpt' + models = ['free_gpt', 'gpt-4o-mini', 'deepseek-coder', 'chatpdf'] + + model_aliases = { + "glm-4": "free_gpt", + "gpt-3.5-turbo": "chatpdf", + "deepseek": "deepseek-coder", + } + + @classmethod + def get_model(cls, model: str) -> str: + if model in cls.models: + return model + elif model in cls.model_aliases: + return cls.model_aliases[model] + else: + return cls.default_model + + @classmethod + async def create_async_generator( + cls, + model: str, + messages: Messages, + proxy: str = None, + **kwargs + ) -> AsyncResult: + model = cls.get_model(model) + + headers = { + "accept": "application/json, text/javascript, */*; q=0.01", + "accept-language": "en-US,en;q=0.9", + "cache-control": "no-cache", + "content-type": "application/x-www-form-urlencoded; charset=UTF-8", + "origin": cls.url, + "pragma": "no-cache", + "priority": "u=1, i", + "referer": f"{cls.url}/chatgpt", + "sec-ch-ua": '"Chromium";v="127", "Not)A;Brand";v="99"', + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": '"Linux"', + "sec-fetch-dest": "empty", + "sec-fetch-mode": "cors", + "sec-fetch-site": "same-origin", + "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36", + "x-requested-with": "XMLHttpRequest", + } + async with ClientSession(headers=headers) as session: + prompt = format_prompt(messages) + data = { + "chatgpt_input": prompt, + "qa_type2": model, + "chatgpt_version_value": "20240804", + "enable_web_search": "0", + "enable_agent": "0", + "dy_video_text_extract": "0", + "enable_summary": "0", + } + async with session.post(cls.api_endpoint, data=data, proxy=proxy) as response: + response.raise_for_status() + json_data = await response.json() + chat_id = json_data["data"]["id"] + + headers["content-type"] = "application/x-www-form-urlencoded; charset=UTF-8" + data = {"current_req_count": "2"} + + while True: + async with session.post(f"{cls.url}/chat_stream", headers=headers, data=data, proxy=proxy) as response: + response.raise_for_status() + json_data = await response.json() + if json_data["data"]: + yield json_data["data"] + break + else: + await sleep(1) # Затримка перед наступним запитом |