summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/CodeNews.py
blob: a7eb0bb11242bd87de72630ead6c6537b31005b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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 = {
        "glm4": "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)  # Затримка перед наступним запитом