File size: 10,666 Bytes
7a8853f
 
a0bae9e
7a8853f
 
 
 
 
ca099ef
7a8853f
 
 
 
 
 
 
a0bae9e
 
ca099ef
d991b3a
7a8853f
ca099ef
7a8853f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0bae9e
 
 
 
 
 
 
 
 
7a8853f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0bae9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a8853f
a0bae9e
7a8853f
 
 
 
 
 
 
a0bae9e
 
 
7a8853f
 
 
 
 
 
 
 
 
 
 
a0bae9e
 
 
7a8853f
 
 
 
 
 
 
 
 
 
 
 
a0bae9e
 
 
 
53aeb8a
7a8853f
 
 
 
 
 
 
 
a0bae9e
 
 
7a8853f
 
 
 
 
 
 
 
03633ab
7a8853f
 
a0bae9e
 
 
 
 
7a8853f
 
 
 
 
7719960
7a8853f
 
 
 
 
 
a0bae9e
 
 
7a8853f
a0bae9e
 
7a8853f
 
 
c52a088
fa7b266
 
 
 
7719960
fa7b266
 
 
 
 
 
a0bae9e
 
 
fa7b266
 
 
 
7a8853f
ca099ef
a0bae9e
7a8853f
 
ca099ef
7a8853f
ca099ef
7a8853f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03633ab
a0bae9e
03633ab
32521b9
c677b6f
7a8853f
 
fa7b266
7a8853f
 
 
 
 
cd7ff2e
7a8853f
 
32521b9
7a8853f
 
 
 
08868c6
7a8853f
 
 
 
 
 
 
32521b9
7a8853f
a0bae9e
 
 
7a8853f
 
ad2ec99
7a8853f
a0bae9e
7a8853f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0bae9e
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
import os
import helpers.helper as helper

import google.generativeai as genai
from g4f.client import Client
from litellm import completion
import random

from g4f.Provider import DeepInfraChat,Glider,Liaobots,Blackbox,ChatGptEs,LambdaChat,TypeGPT
os.environ["OPENROUTER_API_KEY"] = "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"
os.environ["GROQ_API_KEY"] ="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"
gemini_api_keys=["AIzaSyB7yKIdfW7Umv62G47BCdJjoHTJ9TeiAko","AIzaSyDtP05TyoIy9j0uPL7_wLEhgQEE75AZQSc","AIzaSyDOyjfqFhHmGlGJ2raX82XWTtmMcZxRshs"]
groq_api_keys=["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","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","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"]
#[,"AIzaSyBPfR-HG_HeUgLF0LYW1XQgQUxFF6jF_0U","AIzaSyBz01gZCb9kzZF3lNHuwy_iajWhi9ivyDk"]]
os.environ["GEMINI_API_KEY"] =random.choice(gemini_api_keys)
os.environ["TOGETHERAI_API_KEY"] ="30bed0b842ed3268372d57f588c22452798e9af96aa8d3129ba745ef226282a8"
DeepInfraChat.models = ["google/gemma-3-27b-it","deepseek-ai/DeepSeek-R1-Turbo","Qwen/QwQ-32B","deepseek-ai/DeepSeek-R1","deepseek-ai/DeepSeek-V3-0324","meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8","meta-llama/Llama-4-Scout-17B-16E-Instruct","microsoft/Phi-4-multimodal-instruct"]
deepinframodels=["meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8","microsoft/Phi-4-multimodal-instruct","google/gemma-3-27b-it","meta-llama/Llama-4-Scout-17B-16E-Instruct"]
REASONING_CORRESPONDANCE = {"DeepSeek-R1-Glider":Glider, "DeepSeekR1-LAMBDA":LambdaChat,"DeepSeekR1":DeepInfraChat,"deepseek-slow":TypeGPT}
REASONING_QWQ = {"qwq-32b":DeepInfraChat}

CHAT_CORRESPONDANCE = {"DeepSeek-V3":DeepInfraChat}

client = Client()
genai.configure(api_key="AIzaSyAQgAtQPpY0bQaCqCISGxeyF6tpDePx-Jg")
modell = genai.GenerativeModel('gemini-1.5-pro')
generation_config = {
  "temperature": 1,
  "top_p": 0.95,
  "top_k": 40,
  "max_output_tokens": 8192,
  "response_mime_type": "text/plain",
}

model2flash = genai.GenerativeModel(
  model_name="gemini-2.0-flash-thinking-exp",
  generation_config=generation_config,
)

        

def clear():
    helper.stopped=False
    helper.q.put_nowait("END") 
    while not helper.q.empty():
        try:
            helper.q.get(block=False)
        except Exception as e:
            continue
        helper.q.task_done()

def gpt4(messages,model="gpt-4"):
    print(messages)
    if len(messages) ==1:
        messages[0]["role"]="user"
    response = completion(
        model="gemini/gemini-2.0-flash", 
        messages=messages
    )
    return str(response.choices[0].message.content)

def gpt4stream(messages,model,api_keys):
    print(f"-------{model}--------")
    global llmfree
    global llmdeepseek
    global llmgroq


    if model in deepinframodels:
        try:
            response = client.chat.completions.create(
                provider=DeepInfraChat,
                model=model,
                messages=messages,
                stream=True

                # Add any other necessary parameters
            )
            for part in response:
                cunk=cunk+(part.choices[0].delta.content or "")
                if "```json" not in cunk or "```" not in cunk:
                    helper.q.put_nowait(part.choices[0].delta.content or "")
                if helper.stopped: 
                    clear()
                    break 
        except Exception as e:
            
            pass
        helper.q.put_nowait("RESULT: "+cunk)

    elif model == "DeepSeekR1-togetherAI":
        response = completion(model="together_ai/deepseek-ai/DeepSeek-R1", messages=messages, stream=True)

        cunk=""
        for part in response:
            cunk=cunk+(part.choices[0].delta.content or "")
            if "```json" not in cunk:
                helper.q.put_nowait(part.choices[0].delta.content or "")
            if helper.stopped: 
                clear()
                break 
        helper.q.put_nowait("RESULT: "+cunk)
    
    elif model == "DeepSeekV3-togetherAI":
        response = completion(model="together_ai/deepseek-ai/DeepSeek-V3", messages=messages, stream=True)

        cunk=""
        for part in response:
            cunk=cunk+(part.choices[0].delta.content or "")

            if "```json" not in cunk:
                helper.q.put_nowait(part.choices[0].delta.content or "")
            if helper.stopped: 
                clear()
                break 
        helper.q.put_nowait("RESULT: "+cunk)

    elif model=="deepseek-r1-distill-llama-70b":
        os.environ["GROQ_API_KEY"] =random.choice(groq_api_keys)

        response = completion(model="groq/deepseek-r1-distill-llama-70b", messages=messages, stream=True)

        cunk=""
        for part in response:
            cunk=cunk+(part.choices[0].delta.content or "")
            if "```json" not in cunk:
                helper.q.put_nowait(part.choices[0].delta.content or "")
            if helper.stopped: 
                clear()
                break 
        helper.q.put_nowait("RESULT: "+cunk)
    elif model=="qwq-32b":
        os.environ["GROQ_API_KEY"] =random.choice(groq_api_keys)
        response = completion(model="groq/qwen-qwq-32b", messages=messages, stream=True)

        cunk=""
        for part in response:
            cunk=cunk+(part.choices[0].delta.content or "")
            if "```json" not in cunk:
                helper.q.put_nowait(part.choices[0].delta.content or "")
            if helper.stopped: 
                clear()
                break 
        helper.q.put_nowait("RESULT: "+cunk)

    elif model=="llama-3.3-70b-versatile":
        response = completion(model="groq/llama-3.3-70b-versatile", messages=messages, stream=True)

        cunk=""
        for part in response:
            cunk=cunk+(part.choices[0].delta.content or "")
            
            if "```json" not in cunk:
                helper.q.put_nowait(part.choices[0].delta.content or "")
            if helper.stopped: 
                clear()
                break 
        helper.q.put_nowait("RESULT: "+cunk)

    elif model=="gemini-2.0-flash-thinking-exp-01-21":
        for key in gemini_api_keys:
            try:
                os.environ["GEMINI_API_KEY"] =key

                response = completion(model="gemini/gemini-2.0-flash-thinking-exp-01-21", messages=messages, stream=True)

                cunk=""
                for part in response:
                    cunk=cunk+(part.choices[0].delta.content or "")
                    if "```json" not in cunk:
                        helper.q.put_nowait(part.choices[0].delta.content or "")
                    if helper.stopped: 
                        clear()
                        break 
                break         
            except Exception as e:
                helper.q.put_nowait(str(e))
                pass            
        helper.q.put_nowait("RESULT: "+cunk)

    elif model=="gemini-2.5-pro-exp-03-25" :
        for key in gemini_api_keys:
            try:
                os.environ["GEMINI_API_KEY"] =key

                response = completion(model="gemini/gemini-2.5-pro-exp-03-25", messages=messages, stream=True)

                cunk=""
                for part in response:
                    cunk=cunk+(part.choices[0].delta.content or "")
                    if "```json" not in cunk:
                        helper.q.put_nowait(part.choices[0].delta.content or "")
                    if helper.stopped: 
                        clear()
                        break 
                break         
            except:
                pass            
        helper.q.put_nowait("RESULT: "+cunk)

    elif model=="deepseek.r1" or model=="deepseek-chat":

        cunk=""

        if "chat" in model:
            providers = CHAT_CORRESPONDANCE
            model_name="deepseek-ai/DeepSeek-V3-0324"
        else:
            providers = REASONING_CORRESPONDANCE
            model_name="deepseek-r1"

        for provider in providers:
            try:
                response = client.chat.completions.create(
                    provider=providers[provider],
                    model=model_name,
                    messages=messages,
                    stream=True

                    # Add any other necessary parameters
                )
                for part in response:
                    cunk=cunk+(part.choices[0].delta.content or "")
                    if helper.stopped: 
                        clear()
                        break 
                    if "```json" not in cunk or "```" not in cunk:
                        helper.q.put_nowait(part.choices[0].delta.content or "")
                break
            except Exception as e:
                #helper.q.put_nowait(str(e)) 
                pass
        print("STOPPING")
        helper.q.put_nowait("RESULT: "+cunk)


    elif model=="deepseek-reasoner" :
        cunk=""
        providers=REASONING_QWQ
        helper.q.put_nowait("<think>")
        for provider in providers:
            try:
                response = client.chat.completions.create(
                    provider=providers[provider],
                    model="Qwen/QwQ-32B",
                    messages=messages,
                    stream=True

                    # Add any other necessary parameters
                )
                for part in response:
                    cunk=cunk+(part.choices[0].delta.content or "")
                    if "```json" not in cunk or "```" not in cunk:
                        helper.q.put_nowait(part.choices[0].delta.content or "")
                    if helper.stopped: 
                        clear()
                        break 
                break
            except Exception as e:
                
                pass
        helper.q.put_nowait("RESULT: "+cunk)


    elif "DeepSeek" in model and "dev" in model:
        cunk=""

        if "V3" in model:
            providers = CHAT_CORRESPONDANCE
        else:
            providers = REASONING_CORRESPONDANCE
        for provider in providers:
            try:
                response = client.chat.completions.create(
                    provider=providers[provider],
                    model="deepseek-r1",
                    messages=messages,
                    stream=True

                    # Add any other necessary parameters
                )
                for part in response:
                    cunk=cunk+(part.choices[0].delta.content or "")
                break
            except Exception as e:
                pass
        print("STOPPING")
        helper.q.put_nowait("RESULT: "+cunk)