summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/Snova.py
blob: 53d8f0bd55560242f0b4838a1d432b95caab6c74 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from __future__ import annotations

import json
from typing import AsyncGenerator

from aiohttp import ClientSession

from ..typing import AsyncResult, Messages
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
from .helper import format_prompt


class Snova(AsyncGeneratorProvider, ProviderModelMixin):
    url = "https://fast.snova.ai"
    api_endpoint = "https://fast.snova.ai/api/completion"
    working = True
    supports_stream = True
    supports_system_message = True
    supports_message_history = True
    
    default_model = 'Meta-Llama-3.1-8B-Instruct'
    models = [
        'Meta-Llama-3.1-8B-Instruct',
        'Meta-Llama-3.1-70B-Instruct',
        'Meta-Llama-3.1-405B-Instruct',
        'Samba-CoE',
        'ignos/Mistral-T5-7B-v1', # Error with the answer
        'v1olet/v1olet_merged_dpo_7B',
        'macadeliccc/WestLake-7B-v2-laser-truthy-dpo',
    ]
    
    model_aliases = {
        "llama-3.1-8b": "Meta-Llama-3.1-8B-Instruct",
        "llama-3.1-70b": "Meta-Llama-3.1-70B-Instruct",
        "llama-3.1-405b": "Meta-Llama-3.1-405B-Instruct",
        
        "mistral-7b": "ignos/Mistral-T5-7B-v1",
        
        "samba-coe-v0.1": "Samba-CoE",
        "v1olet-merged-7b": "v1olet/v1olet_merged_dpo_7B",
        "westlake-7b-v2": "macadeliccc/WestLake-7B-v2-laser-truthy-dpo",
    }

    @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
    ) -> AsyncGenerator[str, None]:
        model = cls.get_model(model)
        
        headers = {
            "accept": "text/event-stream",
            "accept-language": "en-US,en;q=0.9",
            "cache-control": "no-cache",
            "content-type": "application/json",
            "origin": cls.url,
            "pragma": "no-cache",
            "priority": "u=1, i",
            "referer": f"{cls.url}/",
            "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"
        }
        async with ClientSession(headers=headers) as session:
            data = {
                "body": {
                    "messages": [
                        {
                            "role": "system",
                            "content": "You are a helpful assistant."
                        },
                        {
                            "role": "user",
                            "content": format_prompt(messages),
                            "id": "1-id",
                            "ref": "1-ref",
                            "revision": 1,
                            "draft": False,
                            "status": "done",
                            "enableRealTimeChat": False,
                            "meta": None
                        }
                    ],
                    "max_tokens": 1000,
                    "stop": ["<|eot_id|>"],
                    "stream": True,
                    "stream_options": {"include_usage": True},
                    "model": model
                },
                "env_type": "tp16"
            }
            async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
                response.raise_for_status()
                full_response = ""
                async for line in response.content:
                    line = line.decode().strip()
                    if line.startswith("data: "):
                        data = line[6:]
                        if data == "[DONE]":
                            break
                        try:
                            json_data = json.loads(data)
                            choices = json_data.get("choices", [])
                            if choices:
                                delta = choices[0].get("delta", {})
                                content = delta.get("content", "")
                                full_response += content
                        except json.JSONDecodeError:
                            continue
                        except Exception as e:
                            print(f"Error processing chunk: {e}")
                            print(f"Problematic data: {data}")
                            continue
                
                yield full_response.strip()