summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/airforce/AirforceChat.py
blob: 8affbe5c33b821021b23fba51ea4109b7dd47da4 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
from __future__ import annotations
import re
from aiohttp import ClientSession
import json
from typing import List

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

def clean_response(text: str) -> str:
    """Clean response from unwanted patterns."""
    patterns = [
        r"One message exceeds the \d+chars per message limit\..+https:\/\/discord\.com\/invite\/\S+",
        r"Rate limit \(\d+\/minute\) exceeded\. Join our discord for more: .+https:\/\/discord\.com\/invite\/\S+",
        r"Rate limit \(\d+\/hour\) exceeded\. Join our discord for more: https:\/\/discord\.com\/invite\/\S+",
        r"</s>", # zephyr-7b-beta
    ]

    for pattern in patterns:
        text = re.sub(pattern, '', text)
    return text.strip()

def split_message(message: dict, chunk_size: int = 995) -> List[dict]:
    """Split a message into chunks of specified size."""
    content = message.get('content', '')
    if len(content) <= chunk_size:
        return [message]

    chunks = []
    while content:
        chunk = content[:chunk_size]
        content = content[chunk_size:]
        chunks.append({
            'role': message['role'],
            'content': chunk
        })
    return chunks

def split_messages(messages: Messages, chunk_size: int = 995) -> Messages:
    """Split all messages that exceed chunk_size into smaller messages."""
    result = []
    for message in messages:
        result.extend(split_message(message, chunk_size))
    return result

class AirforceChat(AsyncGeneratorProvider, ProviderModelMixin):
    label = "AirForce Chat"
    api_endpoint = "https://api.airforce/chat/completions"
    supports_stream = True
    supports_system_message = True
    supports_message_history = True

    default_model = 'llama-3-70b-chat'
    text_models = [
        # anthropic
        'claude-3-haiku-20240307', 
        'claude-3-sonnet-20240229', 
        'claude-3-5-sonnet-20240620', 
        'claude-3-5-sonnet-20241022', 
        'claude-3-opus-20240229', 
        
        # openai
        'chatgpt-4o-latest',
        'gpt-4',
        'gpt-4-turbo',
        'gpt-4o-2024-05-13',
        'gpt-4o-mini-2024-07-18',
        'gpt-4o-mini',
        'gpt-4o-2024-08-06',
        'gpt-3.5-turbo',
        'gpt-3.5-turbo-0125',
        'gpt-3.5-turbo-1106',
        'gpt-4o',
        'gpt-4-turbo-2024-04-09',
        'gpt-4-0125-preview',
        'gpt-4-1106-preview',
        
        # meta-llama
        default_model,
        'llama-3-70b-chat-turbo',
        'llama-3-8b-chat',
        'llama-3-8b-chat-turbo',
        'llama-3-70b-chat-lite',
        'llama-3-8b-chat-lite',
        'llama-2-13b-chat',
        'llama-3.1-405b-turbo',
        'llama-3.1-70b-turbo',
        'llama-3.1-8b-turbo',
        'LlamaGuard-2-8b',
        'llamaguard-7b',
        'Llama-Vision-Free',
        'Llama-Guard-7b',
        'Llama-3.2-90B-Vision-Instruct-Turbo',
        'Meta-Llama-Guard-3-8B',
        'Llama-3.2-11B-Vision-Instruct-Turbo',
        'Llama-Guard-3-11B-Vision-Turbo',
        'Llama-3.2-3B-Instruct-Turbo',
        'Llama-3.2-1B-Instruct-Turbo',
        'llama-2-7b-chat-int8',
        'llama-2-7b-chat-fp16',
        'Llama 3.1 405B Instruct',
        'Llama 3.1 70B Instruct',
        'Llama 3.1 8B Instruct',
        
        # mistral-ai
        'Mixtral-8x7B-Instruct-v0.1',
        'Mixtral-8x22B-Instruct-v0.1',
        'Mistral-7B-Instruct-v0.1',
        'Mistral-7B-Instruct-v0.2',
        'Mistral-7B-Instruct-v0.3',
        
        # Gryphe
        'MythoMax-L2-13b-Lite',
        'MythoMax-L2-13b',
        
        # openchat
        'openchat-3.5-0106',
        
        # qwen
        #'Qwen1.5-72B-Chat', # Empty answer
        #'Qwen1.5-110B-Chat', # Empty answer
        'Qwen2-72B-Instruct',
        'Qwen2.5-7B-Instruct-Turbo',
        'Qwen2.5-72B-Instruct-Turbo',
        
        # google
        'gemma-2b-it',
        'gemma-2-9b-it',
        'gemma-2-27b-it',
        
        # gemini
        'gemini-1.5-flash',
        'gemini-1.5-pro',
        
        # databricks
        'dbrx-instruct',
        
        # deepseek-ai
        'deepseek-coder-6.7b-base',
        'deepseek-coder-6.7b-instruct',
        'deepseek-math-7b-instruct',
        
        # NousResearch
        'deepseek-math-7b-instruct',
        'Nous-Hermes-2-Mixtral-8x7B-DPO',
        'hermes-2-pro-mistral-7b',
        
        # teknium
        'openhermes-2.5-mistral-7b',
        
        # microsoft
        'WizardLM-2-8x22B',
        'phi-2',
        
        # upstage
        'SOLAR-10.7B-Instruct-v1.0',
        
        # pawan
        'cosmosrp',
        
        # liquid
        'lfm-40b-moe',
        
        # DiscoResearch
        'discolm-german-7b-v1',
        
        # tiiuae
        'falcon-7b-instruct',
        
        # defog
        'sqlcoder-7b-2',
        
        # tinyllama
        'tinyllama-1.1b-chat',
        
        # HuggingFaceH4
        'zephyr-7b-beta',
    ]
    
    models = [*text_models]
    
    model_aliases = {
		# anthropic
		"claude-3-haiku": "claude-3-haiku-20240307",
		"claude-3-sonnet": "claude-3-sonnet-20240229",
		"claude-3.5-sonnet": "claude-3-5-sonnet-20240620",
		"claude-3.5-sonnet": "claude-3-5-sonnet-20241022",
		"claude-3-opus": "claude-3-opus-20240229",

		# openai
		"gpt-4o": "chatgpt-4o-latest",
		#"gpt-4": "gpt-4",
		#"gpt-4-turbo": "gpt-4-turbo",
		"gpt-4o": "gpt-4o-2024-05-13",
		"gpt-4o-mini": "gpt-4o-mini-2024-07-18",
		#"gpt-4o-mini": "gpt-4o-mini",
		"gpt-4o": "gpt-4o-2024-08-06",
		"gpt-3.5-turbo": "gpt-3.5-turbo",
		"gpt-3.5-turbo": "gpt-3.5-turbo-0125",
		"gpt-3.5-turbo": "gpt-3.5-turbo-1106",
		#"gpt-4o": "gpt-4o",
		"gpt-4-turbo": "gpt-4-turbo-2024-04-09",
		"gpt-4": "gpt-4-0125-preview",
		"gpt-4": "gpt-4-1106-preview",

		# meta-llama
		"llama-3-70b": "llama-3-70b-chat",
		"llama-3-8b": "llama-3-8b-chat",
		"llama-3-8b": "llama-3-8b-chat-turbo",
		"llama-3-70b": "llama-3-70b-chat-lite",
		"llama-3-8b": "llama-3-8b-chat-lite",
		"llama-2-13b": "llama-2-13b-chat",
		"llama-3.1-405b": "llama-3.1-405b-turbo",
		"llama-3.1-70b": "llama-3.1-70b-turbo",
		"llama-3.1-8b": "llama-3.1-8b-turbo",
		"llamaguard-2-8b": "LlamaGuard-2-8b",
		"llamaguard-7b": "llamaguard-7b",
		#"llama_vision_free": "Llama-Vision-Free", # Unknown
		"llamaguard-7b": "Llama-Guard-7b",
		"llama-3.2-90b": "Llama-3.2-90B-Vision-Instruct-Turbo",
		"llamaguard-3-8b": "Meta-Llama-Guard-3-8B",
		"llama-3.2-11b": "Llama-3.2-11B-Vision-Instruct-Turbo",
		"llamaguard-3-11b": "Llama-Guard-3-11B-Vision-Turbo",
		"llama-3.2-3b": "Llama-3.2-3B-Instruct-Turbo",
		"llama-3.2-1b": "Llama-3.2-1B-Instruct-Turbo",
		"llama-2-7b": "llama-2-7b-chat-int8",
		"llama-2-7b": "llama-2-7b-chat-fp16",
		"llama-3.1-405b": "Llama 3.1 405B Instruct",
		"llama-3.1-70b": "Llama 3.1 70B Instruct",
		"llama-3.1-8b": "Llama 3.1 8B Instruct",

		# mistral-ai
		"mixtral-8x7b": "Mixtral-8x7B-Instruct-v0.1",
		"mixtral-8x22b": "Mixtral-8x22B-Instruct-v0.1",
		"mixtral-8x7b": "Mistral-7B-Instruct-v0.1",
		"mixtral-8x7b": "Mistral-7B-Instruct-v0.2",
		"mixtral-8x7b": "Mistral-7B-Instruct-v0.3",

		# Gryphe
		"mythomax-13b": "MythoMax-L2-13b-Lite",
		"mythomax-13b": "MythoMax-L2-13b",

		# openchat
		"openchat-3.5": "openchat-3.5-0106",

		# qwen
		#"qwen-1.5-72b": "Qwen1.5-72B-Chat", # Empty answer
		#"qwen-1.5-110b": "Qwen1.5-110B-Chat", # Empty answer
		"qwen-2-72b": "Qwen2-72B-Instruct",
		"qwen-2-5-7b": "Qwen2.5-7B-Instruct-Turbo",
		"qwen-2-5-72b": "Qwen2.5-72B-Instruct-Turbo",

		# google
		"gemma-2b": "gemma-2b-it",
		"gemma-2-9b": "gemma-2-9b-it",
		"gemma-2b-27b": "gemma-2-27b-it",

		# gemini
		"gemini-flash": "gemini-1.5-flash",
		"gemini-pro": "gemini-1.5-pro",

		# databricks
		"dbrx-instruct": "dbrx-instruct",

		# deepseek-ai
		#"deepseek-coder": "deepseek-coder-6.7b-base",
		"deepseek-coder": "deepseek-coder-6.7b-instruct",
		#"deepseek-math": "deepseek-math-7b-instruct",

		# NousResearch
		#"deepseek-math": "deepseek-math-7b-instruct",
		"hermes-2-dpo": "Nous-Hermes-2-Mixtral-8x7B-DPO",
		"hermes-2": "hermes-2-pro-mistral-7b",

		# teknium
		"openhermes-2.5": "openhermes-2.5-mistral-7b",

		# microsoft
		"wizardlm-2-8x22b": "WizardLM-2-8x22B",
		#"phi-2": "phi-2",

		# upstage
		"solar-10-7b": "SOLAR-10.7B-Instruct-v1.0",

		# pawan
		#"cosmosrp": "cosmosrp",

		# liquid
		"lfm-40b": "lfm-40b-moe",

		# DiscoResearch
		"german-7b": "discolm-german-7b-v1",

		# tiiuae
		#"falcon-7b": "falcon-7b-instruct",

		# defog
		#"sqlcoder-7b": "sqlcoder-7b-2",

		# tinyllama
		#"tinyllama-1b": "tinyllama-1.1b-chat",

		# HuggingFaceH4
		"zephyr-7b": "zephyr-7b-beta",
    }

    @classmethod
    async def create_async_generator(
        cls,
        model: str,
        messages: Messages,
        stream: bool = False,
        proxy: str = None,
        max_tokens: str = 4096,
        temperature: str = 1,
        top_p: str = 1,
        **kwargs
    ) -> AsyncResult:
        model = cls.get_model(model)

        chunked_messages = split_messages(messages)

        headers = {
            'accept': '*/*',
            'accept-language': 'en-US,en;q=0.9',
            'authorization': 'Bearer missing api key',
            'cache-control': 'no-cache',
            'content-type': 'application/json',
            'origin': 'https://llmplayground.net',
            'pragma': 'no-cache',
            'priority': 'u=1, i',
            'referer': 'https://llmplayground.net/',
            'sec-ch-ua': '"Not?A_Brand";v="99", "Chromium";v="130"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Linux"',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'cross-site',
            'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36'
        }

        data = {
            "messages": chunked_messages,
            "model": model,
            "max_tokens": max_tokens,
            "temperature": temperature,
            "top_p": top_p,
            "stream": stream
        }

        async with ClientSession(headers=headers) as session:
            async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
                response.raise_for_status()
                text = ""
                if stream:
                    async for line in response.content:
                        line = line.decode('utf-8')
                        if line.startswith('data: '):
                            json_str = line[6:]
                            try:
                                chunk = json.loads(json_str)
                                if 'choices' in chunk and chunk['choices']:
                                    content = chunk['choices'][0].get('delta', {}).get('content', '')
                                    text += content
                            except json.JSONDecodeError as e:
                                print(f"Error decoding JSON: {json_str}, Error: {e}")
                        elif line.strip() == "[DONE]":
                            break
                    yield clean_response(text)
                else:
                    response_json = await response.json()
                    text = response_json["choices"][0]["message"]["content"]
                    yield clean_response(text)