Spaces:
Running
Running
Update utils/llms.py
Browse files- utils/llms.py +107 -35
utils/llms.py
CHANGED
|
@@ -4,21 +4,24 @@ from litellm import completion
|
|
| 4 |
import random
|
| 5 |
import json
|
| 6 |
import os
|
|
|
|
| 7 |
|
|
|
|
| 8 |
from g4f.Provider import DeepInfraChat,LambdaChat
|
|
|
|
| 9 |
|
| 10 |
gemini_api_keys=json.loads(os.environ.get("GEMINI_KEY_LIST"))
|
| 11 |
groq_api_keys=json.loads(os.environ.get("GROQ_API_KEYS"))
|
| 12 |
chutes_key=os.environ.get("CHUTES_API_KEY")
|
| 13 |
github_key=os.environ.get("GITHUB_API_KEY")
|
| 14 |
|
| 15 |
-
DeepInfraChat.models = ["Qwen/Qwen3-235B-A22B","Qwen/Qwen3-30B-A3B","Qwen/Qwen3-32B","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"]
|
| 16 |
|
| 17 |
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"]
|
| 18 |
chutes_models={'Llama-4-Maverick-17B-128E-Instruct-FP8':'chutesai/Llama-4-Maverick-17B-128E-Instruct-FP8', "Qwen3-235B":"Qwen/Qwen3-235B-A22B","MAI-DS-R1-FP8":"microsoft/MAI-DS-R1-FP8","DeepSeek-V3-0324":"deepseek-ai/DeepSeek-V3-0324","deepseek-reasoner":"deepseek-ai/DeepSeek-R1-0528","GLM-4-32B-0414":"THUDM/GLM-4-32B-0414","GLM-Z1-32B-0414":"THUDM/GLM-Z1-32B-0414","DeepSeek-R1T-Chimera":"tngtech/DeepSeek-R1T-Chimera", "DeepSeek-R1-Zero":"deepseek-ai/DeepSeek-R1-Zero"}
|
| 19 |
github_models={"gpt4.1":"gpt-4.1","gpt-4o":"gpt-4o","o4-mini":"o4-mini"}
|
| 20 |
|
| 21 |
-
REASONING_CORRESPONDANCE = {"DeepSeekR1
|
| 22 |
os.environ["GEMINI_API_KEY"] =random.choice(gemini_api_keys)
|
| 23 |
|
| 24 |
REASONING_QWQ = {"qwq-32b":DeepInfraChat}
|
|
@@ -28,7 +31,8 @@ CHAT_CORRESPONDANCE = {"DeepSeek-V3":DeepInfraChat}
|
|
| 28 |
|
| 29 |
client = Client()
|
| 30 |
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
def chat(messages,response_format,model="gpt-4"):
|
| 34 |
if len(messages) ==1:
|
|
@@ -42,7 +46,7 @@ def chat(messages,response_format,model="gpt-4"):
|
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
-
def chatstream(messages,model,api_keys):
|
| 46 |
print(f"-------{model}--------")
|
| 47 |
global llmfree
|
| 48 |
global llmdeepseek
|
|
@@ -50,7 +54,27 @@ def chatstream(messages,model,api_keys):
|
|
| 50 |
|
| 51 |
cunk=""
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
try:
|
| 55 |
response = client.chat.completions.create(
|
| 56 |
provider=DeepInfraChat,
|
|
@@ -59,7 +83,6 @@ def chatstream(messages,model,api_keys):
|
|
| 59 |
stream=True,
|
| 60 |
response_format="json_object"
|
| 61 |
|
| 62 |
-
|
| 63 |
)
|
| 64 |
for part in response:
|
| 65 |
cunk=cunk+(part.choices[0].delta.content or "")
|
|
@@ -68,10 +91,26 @@ def chatstream(messages,model,api_keys):
|
|
| 68 |
except Exception as e:
|
| 69 |
pass
|
| 70 |
yield ("RESULT: "+cunk)
|
| 71 |
-
|
|
|
|
| 72 |
response = client.chat.completions.create(
|
| 73 |
provider=DeepInfraChat,
|
| 74 |
-
model=f"Qwen/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
messages=messages,
|
| 76 |
stream=True
|
| 77 |
|
|
@@ -146,26 +185,37 @@ def chatstream(messages,model,api_keys):
|
|
| 146 |
yield("<think> \n")
|
| 147 |
|
| 148 |
cunk=""
|
|
|
|
| 149 |
for part in response:
|
| 150 |
-
cunk=cunk+(part.choices[0].delta.content or "")
|
| 151 |
x=str(part.choices[0].delta.content)
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
yield("RESULT: "+cunk)
|
| 159 |
|
| 160 |
elif model in github_models:
|
| 161 |
-
response = completion(model=f"github/{github_models[model]}",api_key=github_key, messages=messages, stream=True)
|
| 162 |
|
| 163 |
cunk=""
|
| 164 |
for part in response:
|
|
|
|
| 165 |
cunk=cunk+(part.choices[0].delta.content or "")
|
|
|
|
| 166 |
|
| 167 |
-
|
| 168 |
-
yield(part.choices[0].delta.content or "")
|
| 169 |
yield("RESULT: "+cunk)
|
| 170 |
|
| 171 |
elif "gemini" in model:
|
|
@@ -173,14 +223,17 @@ def chatstream(messages,model,api_keys):
|
|
| 173 |
try:
|
| 174 |
os.environ["GEMINI_API_KEY"] =key
|
| 175 |
|
| 176 |
-
response = completion(model=f"gemini/{model}", messages=messages, stream=True)
|
| 177 |
|
| 178 |
cunk=""
|
| 179 |
for part in response:
|
| 180 |
cunk=cunk+(part.choices[0].delta.content or "")
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
break
|
| 186 |
except Exception as e:
|
|
@@ -192,38 +245,49 @@ def chatstream(messages,model,api_keys):
|
|
| 192 |
elif model=="deepseek.r1" or model=="deepseek-chat":
|
| 193 |
|
| 194 |
cunk=""
|
| 195 |
-
|
| 196 |
if "chat" in model:
|
| 197 |
providers = CHAT_CORRESPONDANCE
|
| 198 |
model_name="deepseek-ai/DeepSeek-V3-0324"
|
| 199 |
else:
|
| 200 |
providers = REASONING_CORRESPONDANCE
|
| 201 |
-
model_name="deepseek-
|
| 202 |
|
| 203 |
-
for
|
| 204 |
try:
|
| 205 |
response = client.chat.completions.create(
|
| 206 |
-
provider=
|
| 207 |
model=model_name,
|
| 208 |
messages=messages,
|
| 209 |
-
stream=True
|
| 210 |
-
response_format="json_object"
|
| 211 |
-
|
| 212 |
|
| 213 |
# Add any other necessary parameters
|
| 214 |
)
|
| 215 |
for part in response:
|
| 216 |
-
|
| 217 |
-
cunk=cunk+
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
break
|
| 222 |
except Exception as e:
|
| 223 |
#yield(str(e))
|
| 224 |
print(e)
|
| 225 |
pass
|
| 226 |
print("STOPPING")
|
|
|
|
|
|
|
|
|
|
| 227 |
yield("RESULT: "+cunk)
|
| 228 |
|
| 229 |
|
|
@@ -276,4 +340,12 @@ def chatstream(messages,model,api_keys):
|
|
| 276 |
except Exception as e:
|
| 277 |
pass
|
| 278 |
print("STOPPING")
|
| 279 |
-
yield("RESULT: "+cunk)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import random
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
+
load_dotenv()
|
| 10 |
from g4f.Provider import DeepInfraChat,LambdaChat
|
| 11 |
+
from backup import Client as PerplexityClient,cookies
|
| 12 |
|
| 13 |
gemini_api_keys=json.loads(os.environ.get("GEMINI_KEY_LIST"))
|
| 14 |
groq_api_keys=json.loads(os.environ.get("GROQ_API_KEYS"))
|
| 15 |
chutes_key=os.environ.get("CHUTES_API_KEY")
|
| 16 |
github_key=os.environ.get("GITHUB_API_KEY")
|
| 17 |
|
| 18 |
+
DeepInfraChat.models = ["moonshotai/Kimi-K2-Instruct","Qwen/Qwen3-235B-A22B-Thinking-2507","Qwen/Qwen3-235B-A22B","Qwen/Qwen3-30B-A3B","Qwen/Qwen3-32B","google/gemma-3-27b-it","deepseek-ai/DeepSeek-R1-Turbo","Qwen/QwQ-32B","deepseek-ai/DeepSeek-R1-0528","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"]
|
| 19 |
|
| 20 |
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"]
|
| 21 |
chutes_models={'Llama-4-Maverick-17B-128E-Instruct-FP8':'chutesai/Llama-4-Maverick-17B-128E-Instruct-FP8', "Qwen3-235B":"Qwen/Qwen3-235B-A22B","MAI-DS-R1-FP8":"microsoft/MAI-DS-R1-FP8","DeepSeek-V3-0324":"deepseek-ai/DeepSeek-V3-0324","deepseek-reasoner":"deepseek-ai/DeepSeek-R1-0528","GLM-4-32B-0414":"THUDM/GLM-4-32B-0414","GLM-Z1-32B-0414":"THUDM/GLM-Z1-32B-0414","DeepSeek-R1T-Chimera":"tngtech/DeepSeek-R1T-Chimera", "DeepSeek-R1-Zero":"deepseek-ai/DeepSeek-R1-Zero"}
|
| 22 |
github_models={"gpt4.1":"gpt-4.1","gpt-4o":"gpt-4o","o4-mini":"o4-mini"}
|
| 23 |
|
| 24 |
+
REASONING_CORRESPONDANCE = {"DeepSeekR1":DeepInfraChat}
|
| 25 |
os.environ["GEMINI_API_KEY"] =random.choice(gemini_api_keys)
|
| 26 |
|
| 27 |
REASONING_QWQ = {"qwq-32b":DeepInfraChat}
|
|
|
|
| 31 |
|
| 32 |
client = Client()
|
| 33 |
|
| 34 |
+
perplexity_cli=PerplexityClient(cookies=cookies)
|
| 35 |
+
|
| 36 |
|
| 37 |
def chat(messages,response_format,model="gpt-4"):
|
| 38 |
if len(messages) ==1:
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
+
def chatstream(messages,model,api_keys,tools):
|
| 50 |
print(f"-------{model}--------")
|
| 51 |
global llmfree
|
| 52 |
global llmdeepseek
|
|
|
|
| 54 |
|
| 55 |
cunk=""
|
| 56 |
|
| 57 |
+
|
| 58 |
+
if model in ["o3","gpt-4.1",'grok-4','gemini-2.5-pro','claude-sonnet-4-20250514','sonar-pro','r1-1778']:
|
| 59 |
+
raw_perplex_msg = "".join(
|
| 60 |
+
(( (f"[{message['role']}]" ) + ("(#message)" if message['role']!="system" else "(#instructions)") ) if message['role'] != "assistant" else "") + f"\n{message['content']}\n\n"
|
| 61 |
+
for message in messages
|
| 62 |
+
)
|
| 63 |
+
with open("perplexity_messages.txt", "w",encoding='utf-8') as f:
|
| 64 |
+
f.write(raw_perplex_msg)
|
| 65 |
+
resp = perplexity_cli.search(raw_perplex_msg, mode='reasoning', model="grok4", sources=[], files={}, stream=True, language='en-US', follow_up=None, incognito=False)
|
| 66 |
+
for i in resp:
|
| 67 |
+
try:
|
| 68 |
+
cunk = cunk+(i["blocks"][0]["markdown_block"]["chunks"][0])
|
| 69 |
+
if "```json" not in cunk or "```" not in cunk:
|
| 70 |
+
yield i["blocks"][0]["markdown_block"]["chunks"][0]
|
| 71 |
+
# print(i["blocks"][0]["markdown_block"]["chunks"][0],end="")
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(e)
|
| 74 |
+
pass
|
| 75 |
+
yield ("RESULT: "+cunk)
|
| 76 |
+
|
| 77 |
+
elif model in deepinframodels:
|
| 78 |
try:
|
| 79 |
response = client.chat.completions.create(
|
| 80 |
provider=DeepInfraChat,
|
|
|
|
| 83 |
stream=True,
|
| 84 |
response_format="json_object"
|
| 85 |
|
|
|
|
| 86 |
)
|
| 87 |
for part in response:
|
| 88 |
cunk=cunk+(part.choices[0].delta.content or "")
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
pass
|
| 93 |
yield ("RESULT: "+cunk)
|
| 94 |
+
|
| 95 |
+
elif model == "Qwen3-235B-A22B-Thinking-2507" :
|
| 96 |
response = client.chat.completions.create(
|
| 97 |
provider=DeepInfraChat,
|
| 98 |
+
model=f"Qwen/Qwen3-235B-A22B-Thinking-2507",
|
| 99 |
+
messages=messages,
|
| 100 |
+
stream=True
|
| 101 |
+
|
| 102 |
+
)
|
| 103 |
+
for part in response:
|
| 104 |
+
resp=str(part.choices[0].delta.content)
|
| 105 |
+
cunk=cunk+(resp or "")
|
| 106 |
+
|
| 107 |
+
if ("```json" not in cunk or "```" not in cunk) and resp != "None":
|
| 108 |
+
yield (resp or "")
|
| 109 |
+
yield ("RESULT: "+str(cunk))
|
| 110 |
+
elif model == "Kimi-K2-Instruct" :
|
| 111 |
+
response = client.chat.completions.create(
|
| 112 |
+
provider=DeepInfraChat,
|
| 113 |
+
model=f"moonshotai/Kimi-K2-Instruct",
|
| 114 |
messages=messages,
|
| 115 |
stream=True
|
| 116 |
|
|
|
|
| 185 |
yield("<think> \n")
|
| 186 |
|
| 187 |
cunk=""
|
| 188 |
+
hist = ""
|
| 189 |
for part in response:
|
|
|
|
| 190 |
x=str(part.choices[0].delta.content)
|
| 191 |
+
cunk=cunk+x
|
| 192 |
+
print(x, end="")
|
| 193 |
+
|
| 194 |
+
if "```" in x and "```json" not in cunk:
|
| 195 |
+
hist = x
|
| 196 |
+
continue
|
| 197 |
+
if hist!="" and "json" not in cunk:
|
| 198 |
+
yield(hist + x)
|
| 199 |
+
hist = ""
|
| 200 |
+
continue
|
| 201 |
+
|
| 202 |
+
if ("```json" not in cunk) and (x != "None"):
|
| 203 |
+
if "None" not in x:
|
| 204 |
+
yield(x)
|
| 205 |
+
before, found, after = cunk.partition('</think>')
|
| 206 |
+
cunk=after
|
| 207 |
yield("RESULT: "+cunk)
|
| 208 |
|
| 209 |
elif model in github_models:
|
| 210 |
+
response = completion(model=f"github/{github_models[model]}",api_key=github_key, messages=messages,tools=tools, stream=True)
|
| 211 |
|
| 212 |
cunk=""
|
| 213 |
for part in response:
|
| 214 |
+
chunk_dict = part.model_dump()
|
| 215 |
cunk=cunk+(part.choices[0].delta.content or "")
|
| 216 |
+
yield ("RAW: " +json.dumps(chunk_dict, indent=4))
|
| 217 |
|
| 218 |
+
|
|
|
|
| 219 |
yield("RESULT: "+cunk)
|
| 220 |
|
| 221 |
elif "gemini" in model:
|
|
|
|
| 223 |
try:
|
| 224 |
os.environ["GEMINI_API_KEY"] =key
|
| 225 |
|
| 226 |
+
response = completion(model=f"gemini/{model}", messages=messages, tools=tools,stream=True)
|
| 227 |
|
| 228 |
cunk=""
|
| 229 |
for part in response:
|
| 230 |
cunk=cunk+(part.choices[0].delta.content or "")
|
| 231 |
+
chunk_dict = part.model_dump()
|
| 232 |
+
|
| 233 |
+
yield ("RAW: " +json.dumps(chunk_dict, indent=4))
|
| 234 |
+
|
| 235 |
+
# if "```json" not in cunk:
|
| 236 |
+
# yield(part.choices[0].delta.content or "")
|
| 237 |
|
| 238 |
break
|
| 239 |
except Exception as e:
|
|
|
|
| 245 |
elif model=="deepseek.r1" or model=="deepseek-chat":
|
| 246 |
|
| 247 |
cunk=""
|
| 248 |
+
hist = ""
|
| 249 |
if "chat" in model:
|
| 250 |
providers = CHAT_CORRESPONDANCE
|
| 251 |
model_name="deepseek-ai/DeepSeek-V3-0324"
|
| 252 |
else:
|
| 253 |
providers = REASONING_CORRESPONDANCE
|
| 254 |
+
model_name="deepseek-ai/DeepSeek-R1-0528"
|
| 255 |
|
| 256 |
+
for i in range(2):
|
| 257 |
try:
|
| 258 |
response = client.chat.completions.create(
|
| 259 |
+
provider=DeepInfraChat,
|
| 260 |
model=model_name,
|
| 261 |
messages=messages,
|
| 262 |
+
stream=True
|
|
|
|
|
|
|
| 263 |
|
| 264 |
# Add any other necessary parameters
|
| 265 |
)
|
| 266 |
for part in response:
|
| 267 |
+
x=str(part.choices[0].delta.content)
|
| 268 |
+
cunk=cunk+x
|
| 269 |
+
print(x, end="")
|
| 270 |
+
|
| 271 |
+
if "```" in x and "```json" not in cunk:
|
| 272 |
+
hist = x
|
| 273 |
+
continue
|
| 274 |
+
if hist!="" and "json" not in cunk:
|
| 275 |
+
yield(hist + x)
|
| 276 |
+
hist = ""
|
| 277 |
+
continue
|
| 278 |
+
|
| 279 |
+
if ("```json" not in cunk) and (x != "None"):
|
| 280 |
+
if "None" not in x:
|
| 281 |
+
yield(x)
|
| 282 |
break
|
| 283 |
except Exception as e:
|
| 284 |
#yield(str(e))
|
| 285 |
print(e)
|
| 286 |
pass
|
| 287 |
print("STOPPING")
|
| 288 |
+
before, found, after = cunk.partition('</think>')
|
| 289 |
+
cunk=after
|
| 290 |
+
|
| 291 |
yield("RESULT: "+cunk)
|
| 292 |
|
| 293 |
|
|
|
|
| 340 |
except Exception as e:
|
| 341 |
pass
|
| 342 |
print("STOPPING")
|
| 343 |
+
yield("RESULT: "+cunk)
|
| 344 |
+
|
| 345 |
+
# #Predict top colleges in BHU for me via CUET 2024 based on 2024 cut off scores.
|
| 346 |
+
# Degree:BA (Hons) Pol. Sci.
|
| 347 |
+
# Respond in csv format with the following headings and info: College,Cut-off Score(Out of 800),Cutoff Rank.Do not output the sources like [1],[2]...within the csv code.
|
| 348 |
+
# Note:
|
| 349 |
+
# 1)First search and research the web thoroughly end to end.
|
| 350 |
+
# 2)Always Use the website https://collegedunia.com to search about the cut offs.Dont use any other website but only this website.
|
| 351 |
+
# Mandatory:**Give Atleast 10 college names**
|