Spaces:
Running
Running
Update utils/llms.py
Browse files- utils/llms.py +94 -6
utils/llms.py
CHANGED
@@ -6,6 +6,21 @@ import json
|
|
6 |
import os
|
7 |
from g4f.Provider import DeepInfraChat,LambdaChat
|
8 |
from backup import Client as PerplexityClient,cookies
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"))
|
@@ -52,24 +67,97 @@ def chatstream(messages,model,api_keys,tools):
|
|
52 |
cunk=""
|
53 |
|
54 |
|
|
|
55 |
if model in ["o3","gpt-4.1",'grok-4','gemini-2.5-pro','claude-sonnet-4-20250514','sonar-pro','r1-1778']:
|
|
|
56 |
raw_perplex_msg = "".join(
|
57 |
(( (f"[{message['role']}]" ) + ("(#message)" if message['role']!="system" else "(#instructions)") ) if message['role'] != "assistant" else "") + f"\n{message['content']}\n\n"
|
58 |
for message in messages
|
59 |
)
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
f.write(raw_perplex_msg)
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
63 |
for i in resp:
|
64 |
try:
|
65 |
-
cunk = cunk+(i["blocks"][0]["markdown_block"]["chunks"][0])
|
66 |
if "```json" not in cunk or "```" not in cunk:
|
67 |
yield i["blocks"][0]["markdown_block"]["chunks"][0]
|
68 |
-
# print(i["blocks"][0]["markdown_block"]["chunks"][0],end="")
|
69 |
except Exception as e:
|
70 |
print(e)
|
71 |
pass
|
72 |
-
yield ("RESULT: "+cunk)
|
73 |
|
74 |
elif model in deepinframodels:
|
75 |
try:
|
@@ -345,4 +433,4 @@ def chatstream(messages,model,api_keys,tools):
|
|
345 |
# Note:
|
346 |
# 1)First search and research the web thoroughly end to end.
|
347 |
# 2)Always Use the website https://collegedunia.com to search about the cut offs.Dont use any other website but only this website.
|
348 |
-
# Mandatory:**Give Atleast 10 college names**
|
|
|
6 |
import os
|
7 |
from g4f.Provider import DeepInfraChat,LambdaChat
|
8 |
from backup import Client as PerplexityClient,cookies
|
9 |
+
import re
|
10 |
+
import base64
|
11 |
+
import mimetypes
|
12 |
+
import urllib.parse
|
13 |
+
|
14 |
+
def _ext_from_url(url):
|
15 |
+
try:
|
16 |
+
path = urllib.parse.urlparse(url).path
|
17 |
+
root, ext = os.path.splitext(path)
|
18 |
+
if ext:
|
19 |
+
return ext
|
20 |
+
except Exception:
|
21 |
+
pass
|
22 |
+
return '.jpg'
|
23 |
+
|
24 |
|
25 |
gemini_api_keys=json.loads(os.environ.get("GEMINI_KEY_LIST"))
|
26 |
groq_api_keys=json.loads(os.environ.get("GROQ_API_KEYS"))
|
|
|
67 |
cunk=""
|
68 |
|
69 |
|
70 |
+
|
71 |
if model in ["o3","gpt-4.1",'grok-4','gemini-2.5-pro','claude-sonnet-4-20250514','sonar-pro','r1-1778']:
|
72 |
+
# build the raw message text as before
|
73 |
raw_perplex_msg = "".join(
|
74 |
(( (f"[{message['role']}]" ) + ("(#message)" if message['role']!="system" else "(#instructions)") ) if message['role'] != "assistant" else "") + f"\n{message['content']}\n\n"
|
75 |
for message in messages
|
76 |
)
|
77 |
+
|
78 |
+
# --- extract images / image urls into files dict ---
|
79 |
+
files = {}
|
80 |
+
image_counter = 1
|
81 |
+
|
82 |
+
# regexes
|
83 |
+
markdown_image_pattern = re.compile(r'!\[.*?\]\((.*?)\)')
|
84 |
+
image_url_pattern = re.compile(r'https?://\S+\.(?:png|jpg|jpeg|gif|webp|svg)', re.IGNORECASE)
|
85 |
+
data_uri_pattern = re.compile(r'data:(image/[\w.+-]+);base64,([A-Za-z0-9+/=\n\r]+)', re.IGNORECASE)
|
86 |
+
|
87 |
+
for message in messages:
|
88 |
+
# 1) attachments (common OpenAI-like shape)
|
89 |
+
atts = message.get('attachments') or message.get('attachment') or []
|
90 |
+
if isinstance(atts, dict):
|
91 |
+
atts = [atts]
|
92 |
+
if isinstance(atts, list):
|
93 |
+
for att in atts:
|
94 |
+
if not isinstance(att, dict):
|
95 |
+
continue
|
96 |
+
# common keys for urls
|
97 |
+
url = att.get('url') or att.get('image_url') or att.get('src') or att.get('href')
|
98 |
+
if url:
|
99 |
+
ext = _ext_from_url(url)
|
100 |
+
fname = f'image_{image_counter}{ext}'
|
101 |
+
files[fname] = url # pass URL as file value (per your example style)
|
102 |
+
image_counter += 1
|
103 |
+
|
104 |
+
# 2) content as string -> look for data-URIs, markdown images, plain image URLs
|
105 |
+
content = message.get('content', '') or ''
|
106 |
+
if not isinstance(content, str):
|
107 |
+
try:
|
108 |
+
content = str(content)
|
109 |
+
except Exception:
|
110 |
+
content = ''
|
111 |
+
|
112 |
+
# data URIs -> decode and attach raw bytes
|
113 |
+
for m in data_uri_pattern.finditer(content):
|
114 |
+
mime_type, b64 = m.groups()
|
115 |
+
try:
|
116 |
+
b = base64.b64decode(b64)
|
117 |
+
except Exception:
|
118 |
+
continue
|
119 |
+
ext = mimetypes.guess_extension(mime_type) or '.bin'
|
120 |
+
fname = f'image_{image_counter}{ext}'
|
121 |
+
files[fname] = b # raw bytes
|
122 |
+
image_counter += 1
|
123 |
+
|
124 |
+
# markdown images: 
|
125 |
+
for m in markdown_image_pattern.finditer(content):
|
126 |
+
url = m.group(1)
|
127 |
+
ext = _ext_from_url(url)
|
128 |
+
fname = f'image_{image_counter}{ext}'
|
129 |
+
files[fname] = url
|
130 |
+
image_counter += 1
|
131 |
+
|
132 |
+
# plain image URLs in text
|
133 |
+
for m in image_url_pattern.finditer(content):
|
134 |
+
url = m.group(0)
|
135 |
+
ext = _ext_from_url(url)
|
136 |
+
fname = f'image_{image_counter}{ext}'
|
137 |
+
files[fname] = url
|
138 |
+
image_counter += 1
|
139 |
+
|
140 |
+
# also include the textual messages file (so Perplexity gets the full text payload as a file)
|
141 |
+
files['perplexity_messages.txt'] = raw_perplex_msg
|
142 |
+
|
143 |
+
# write the textual file to disk as before (optional, kept for compatibility/debugging)
|
144 |
+
with open("perplexity_messages.txt", "w", encoding='utf-8') as f:
|
145 |
f.write(raw_perplex_msg)
|
146 |
+
|
147 |
+
# run the search, passing files (which may contain URL strings or raw bytes for data URIs)
|
148 |
+
resp = perplexity_cli.search(raw_perplex_msg, mode='reasoning', model=model, sources=[], files=files, stream=True, language='en-US', follow_up=None, incognito=False)
|
149 |
+
|
150 |
+
# stream the response, aggregate into cunk (init if needed)
|
151 |
+
cunk = ""
|
152 |
for i in resp:
|
153 |
try:
|
154 |
+
cunk = cunk + (i["blocks"][0]["markdown_block"]["chunks"][0])
|
155 |
if "```json" not in cunk or "```" not in cunk:
|
156 |
yield i["blocks"][0]["markdown_block"]["chunks"][0]
|
|
|
157 |
except Exception as e:
|
158 |
print(e)
|
159 |
pass
|
160 |
+
yield ("RESULT: " + cunk)
|
161 |
|
162 |
elif model in deepinframodels:
|
163 |
try:
|
|
|
433 |
# Note:
|
434 |
# 1)First search and research the web thoroughly end to end.
|
435 |
# 2)Always Use the website https://collegedunia.com to search about the cut offs.Dont use any other website but only this website.
|
436 |
+
# Mandatory:**Give Atleast 10 college names**
|