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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import sys
|
3 |
+
import json
|
4 |
+
import random
|
5 |
+
import mimetypes
|
6 |
+
from uuid import uuid4
|
7 |
+
from curl_cffi import requests, CurlMime
|
8 |
+
|
9 |
+
cookies = {
|
10 |
+
'pplx.visitor-id': 'da5c198a-2ba9-4616-ac21-b6fa92040084',
|
11 |
+
'cf_clearance': 'IWD6WpGAz8BqOhKbqChKS4lZzlx9Zz2MwRJig.Eg23g-1753551228-1.2.1.1-FCjtyp4ikXRY2fTZE.fqFLn_cnlpdwQVmq9aS.HlfVszUnjKIjh6QmpaATTzqGgj3Kb34TbTrSAN2GEWk_IddRM8GJiAHNpm5oa5FGnTzw2b93Neetyd5ivFzgUGxC.mwFj8Ml7twAjUZ4wYPnEp5Y6N7ciMOfxDws_rJK8Lm3R1t0Urll7NiU21JCQwkp_9F1rDEBL8SnwUN97y60kQa9UIHvdorzv7moe8teN.U34',
|
12 |
+
'segmented-control-popover-studio': '1',
|
13 |
+
'sidebarHiddenHubs': '[]',
|
14 |
+
'_gcl_au': '1.1.1446397632.1751723919',
|
15 |
+
'__Secure-next-auth.session-token': 'eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..zpX6jlRXnbVw-ZTx.2Sj3psYc90towqn_3PAAcXeCpl64TZVVPFiON1LZjTKExF9sTxZm4ssg4FlFm4h7XY8uDzsVHQvAZ8yZJvcr1sjLuqofOd2FU-eLptrQzdGvkrMtOdm7h1jPBf2lRzlOcKH2IF8Cz0u94lQmCl1DYtSGnR3vlhhM0lKbLwnGTEFWttUUYzFZcOJXV-rYdjPa8rdl7zPWLqHPqjAbqCY6J9VVKsx_xn5zM5DCZ0pOFzmVfomKSpLhN9JO-ySEh0T-nIAtJtZENm2roUZAWGS5pobT_mA1zSWqx9gH2E8t2xH_qdPN3KYyH_4MZ7MwTUtt3i-LptiuZwg0yJak_bfr40bQ3b8-Kt7AjQKzboiI0VBmhbW20odKMXJnD3lABt9yuxpdK_Tx2SBNZQbec6MsDEAgz8eWNVxZ6kj0O1_sngdE_E2sY8BoCw.8CxZdLCzf4bX1KPQ-mbeOQ',
|
16 |
+
'pplx.metadata': '{%22qc%22:11%2C%22qcu%22:13%2C%22qcm%22:0%2C%22qcc%22:10%2C%22qcr%22:5%2C%22qcdr%22:1%2C%22qcs%22:0%2C%22qcd%22:0%2C%22hli%22:true%2C%22hcga%22:false%2C%22hcds%22:false%2C%22hso%22:false%2C%22hfo%22:false%2C%22fqa%22:1751723941278%2C%22lqa%22:1753690592635}',
|
17 |
+
'__podscribe_perplexityai_referrer': '_',
|
18 |
+
'__podscribe_perplexityai_landing_url': 'https://www.perplexity.ai/?login-source=tryPro^&login-new=false',
|
19 |
+
'_fbp': 'fb.1.1751723930574.245699921229618591',
|
20 |
+
'pplx.search-models-v3': '{%22research%22:%22pplx_alpha%22%2C%22search%22:%22claude37sonnetthinking%22}',
|
21 |
+
'__stripe_mid': 'a3785ab0-0fd2-4fe0-ac88-82067750fdb649476b',
|
22 |
+
'intercom-id-l2wyozh0': '314b3a1e-1c7a-45aa-9aa2-bf756467787b',
|
23 |
+
'intercom-session-l2wyozh0': '',
|
24 |
+
'intercom-device-id-l2wyozh0': 'acc4d380-2161-4a82-b7ee-91052897761d',
|
25 |
+
'AWSALB': 'pN1s+X8xGuy/Rd4EWzNH5KXhJCPPREibux4M/SALeVseSEQnnZdoHd/hDDExDLwWe+DzjdNCdXre+rXnyN8Sv4NeKjtKLs2N9GIW26ejOLMUZPPvJ+zo9NC2TiS/',
|
26 |
+
'AWSALBCORS': 'pN1s+X8xGuy/Rd4EWzNH5KXhJCPPREibux4M/SALeVseSEQnnZdoHd/hDDExDLwWe+DzjdNCdXre+rXnyN8Sv4NeKjtKLs2N9GIW26ejOLMUZPPvJ+zo9NC2TiS/',
|
27 |
+
'__ps_fva': '1753545417872',
|
28 |
+
'pplx.source-selection-v3-space-': '[]',
|
29 |
+
'pplx.source-selection-v3-space-deaca082-6dce-4526-a678-ace2198255bb': '[%22web%22]',
|
30 |
+
'pplx.search-mode': 'search',
|
31 |
+
'ph_phc_TXdpocbGVeZVm5VJmAsHTMrCofBQu3e0kN8HGMNGTVW_posthog': '%7B%22distinct_id%22%3A%2201984b16-8c3d-7985-9031-0eba8f3aea5a%22%2C%22%24sesid%22%3A%5B1753606545782%2C%2201984b16-8c3c-7faa-a67a-a0525a91704a%22%2C1753606425660%5D%7D',
|
32 |
+
'__cflb': '02DiuDyvFMmK5p9jVbVnMNSKYZhUL9aGmwpP9ftWXpJ3N',
|
33 |
+
'_rdt_uuid': '1751723930283.ed01281c-7332-44d4-85de-08299d6fcb90',
|
34 |
+
'__cf_bm': 'FAi5I7TNSaAZ9BYFgNnZKfEFzEQWTZdsjZpZYqheRZU-1753690586-1.0.1.1-Ahjp.tfhGXFmq4QYYQxFI3pnAwaen5yFuPhQxqWtpZ7XkF0f6eYsqvGyiqhoU6P2eniAQGsnnB_EAfqXZDgm2hBVo4nO8mqt4GK6WCTEaDQ',
|
35 |
+
'pplx.session-id': '0b02bcfe-a439-4533-9219-8edb6a14734d',
|
36 |
+
'_dd_s': 'aid=4abdc694-575a-41e1-98ce-bb6396152071^&rum=2^&id=0eb417e5-ac2e-486d-b955-ed978e39692a^&created=1753690584764^&expire=1753691492631^&logs=0',
|
37 |
+
'comet-custom-color-themes-enabled': 'true',
|
38 |
+
'__stripe_sid': 'b1ddb3f8-09ef-45fe-80e5-4884ec3fcb279dd03c',
|
39 |
+
}
|
40 |
+
|
41 |
+
class Client:
|
42 |
+
'''
|
43 |
+
A client for interacting with the Perplexity AI API.
|
44 |
+
'''
|
45 |
+
def __init__(self, cookies={}):
|
46 |
+
self.session = requests.Session(headers={
|
47 |
+
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
|
48 |
+
'accept-language': 'en-US,en;q=0.9',
|
49 |
+
'cache-control': 'max-age=0',
|
50 |
+
'dnt': '1',
|
51 |
+
'priority': 'u=0, i',
|
52 |
+
'sec-ch-ua': '"Not;A=Brand";v="24", "Chromium";v="128"',
|
53 |
+
'sec-ch-ua-arch': '"x86"',
|
54 |
+
'sec-ch-ua-bitness': '"64"',
|
55 |
+
'sec-ch-ua-full-version': '"128.0.6613.120"',
|
56 |
+
'sec-ch-ua-full-version-list': '"Not;A=Brand";v="24.0.0.0", "Chromium";v="128.0.6613.120"',
|
57 |
+
'sec-ch-ua-mobile': '?0',
|
58 |
+
'sec-ch-ua-model': '""',
|
59 |
+
'sec-ch-ua-platform': '"Windows"',
|
60 |
+
'sec-ch-ua-platform-version': '"19.0.0"',
|
61 |
+
'sec-fetch-dest': 'document',
|
62 |
+
'sec-fetch-mode': 'navigate',
|
63 |
+
'sec-fetch-site': 'same-origin',
|
64 |
+
'sec-fetch-user': '?1',
|
65 |
+
'upgrade-insecure-requests': '1',
|
66 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
|
67 |
+
}, cookies=cookies, impersonate='chrome')
|
68 |
+
self.own = bool(cookies)
|
69 |
+
self.copilot = 0 if not cookies else float('inf')
|
70 |
+
self.file_upload = 0 if not cookies else float('inf')
|
71 |
+
self.signin_regex = re.compile(r'"(https://www\.perplexity\.ai/api/auth/callback/email\?callbackUrl=.*?)"')
|
72 |
+
self.timestamp = format(random.getrandbits(32), '08x')
|
73 |
+
self.session.get('https://www.perplexity.ai/api/auth/session')
|
74 |
+
|
75 |
+
def create_account(self, cookies):
|
76 |
+
'''
|
77 |
+
Function to create a new account
|
78 |
+
'''
|
79 |
+
while True:
|
80 |
+
try:
|
81 |
+
emailnator_cli = Emailnator(cookies)
|
82 |
+
|
83 |
+
resp = self.session.post('https://www.perplexity.ai/api/auth/signin/email', data={
|
84 |
+
'email': emailnator_cli.email,
|
85 |
+
'csrfToken': self.session.cookies.get_dict()['next-auth.csrf-token'].split('%')[0],
|
86 |
+
'callbackUrl': 'https://www.perplexity.ai/',
|
87 |
+
'json': 'true'
|
88 |
+
})
|
89 |
+
|
90 |
+
if resp.ok:
|
91 |
+
new_msgs = emailnator_cli.reload(wait_for=lambda x: x['subject'] == 'Sign in to Perplexity', timeout=20)
|
92 |
+
|
93 |
+
if new_msgs:
|
94 |
+
break
|
95 |
+
else:
|
96 |
+
print('Perplexity account creating error:', resp)
|
97 |
+
|
98 |
+
except Exception:
|
99 |
+
pass
|
100 |
+
|
101 |
+
msg = emailnator_cli.get(func=lambda x: x['subject'] == 'Sign in to Perplexity')
|
102 |
+
new_account_link = self.signin_regex.search(emailnator_cli.open(msg['messageID'])).group(1)
|
103 |
+
|
104 |
+
self.session.get(new_account_link)
|
105 |
+
|
106 |
+
self.copilot = 5
|
107 |
+
self.file_upload = 10
|
108 |
+
|
109 |
+
return True
|
110 |
+
|
111 |
+
def search(self, query, mode='auto', model=None, sources=['web'], files={}, stream=False, language='en-US', follow_up=None, incognito=False):
|
112 |
+
'''
|
113 |
+
Query function
|
114 |
+
'''
|
115 |
+
assert mode in ['auto', 'pro', 'reasoning', 'deep research'], 'Search modes -> ["auto", "pro", "reasoning", "deep research"]'
|
116 |
+
# assert model in {
|
117 |
+
# 'auto': [None],
|
118 |
+
# 'pro': [None, 'sonar', 'gpt-4.5', 'gpt-4o', 'claude 3.7 sonnet', 'gemini 2.0 flash', 'grok-2'],
|
119 |
+
# 'reasoning': [None, 'r1', 'o3-mini', 'claude 3.7 sonnet'],
|
120 |
+
# 'deep research': [None]
|
121 |
+
# }[mode] if self.own else True, '''Models for modes -> {
|
122 |
+
# 'auto': [None],
|
123 |
+
# 'pro': [None, 'sonar', 'gpt-4.5', 'gpt-4o', 'claude 3.7 sonnet', 'gemini 2.0 flash', 'grok-2'],
|
124 |
+
# 'reasoning': [None, 'r1', 'o3-mini', 'claude 3.7 sonnet','grok4'],
|
125 |
+
# 'deep research': [None]
|
126 |
+
# }'''
|
127 |
+
assert all([source in ('web', 'scholar', 'social') for source in sources]), 'Sources -> ["web", "scholar", "social"]'
|
128 |
+
assert self.copilot > 0 if mode in ['pro', 'reasoning', 'deep research'] else True, 'You have used all of your enhanced (pro) queries'
|
129 |
+
assert self.file_upload - len(files) >= 0 if files else True, f'You have tried to upload {len(files)} files but you have {self.file_upload} file upload(s) remaining.'
|
130 |
+
|
131 |
+
self.copilot = self.copilot - 1 if mode in ['pro', 'reasoning', 'deep research'] else self.copilot
|
132 |
+
self.file_upload = self.file_upload - len(files) if files else self.file_upload
|
133 |
+
|
134 |
+
uploaded_files = []
|
135 |
+
|
136 |
+
for filename, file in files.items():
|
137 |
+
file_type = mimetypes.guess_type(filename)[0]
|
138 |
+
file_upload_info = (self.session.post(
|
139 |
+
'https://www.perplexity.ai/rest/uploads/create_upload_url?version=2.18&source=default',
|
140 |
+
json={
|
141 |
+
'content_type': file_type,
|
142 |
+
'file_size': sys.getsizeof(file),
|
143 |
+
'filename': filename,
|
144 |
+
'force_image': False,
|
145 |
+
'source': 'default',
|
146 |
+
}
|
147 |
+
)).json()
|
148 |
+
|
149 |
+
mp = CurlMime()
|
150 |
+
for key, value in file_upload_info['fields'].items():
|
151 |
+
mp.addpart(name=key, data=value)
|
152 |
+
mp.addpart(name='file', content_type=file_type, filename=filename, data=file)
|
153 |
+
|
154 |
+
upload_resp = self.session.post(file_upload_info['s3_bucket_url'], multipart=mp)
|
155 |
+
|
156 |
+
if not upload_resp.ok:
|
157 |
+
raise Exception('File upload error', upload_resp)
|
158 |
+
|
159 |
+
if 'image/upload' in file_upload_info['s3_object_url']:
|
160 |
+
uploaded_url = re.sub(
|
161 |
+
r'/private/s--.*?--/v\d+/user_uploads/',
|
162 |
+
'/private/user_uploads/',
|
163 |
+
upload_resp.json()['secure_url']
|
164 |
+
)
|
165 |
+
else:
|
166 |
+
uploaded_url = file_upload_info['s3_object_url']
|
167 |
+
|
168 |
+
uploaded_files.append(uploaded_url)
|
169 |
+
|
170 |
+
json_data = {
|
171 |
+
'query_str': query,
|
172 |
+
'params':
|
173 |
+
{
|
174 |
+
'attachments': uploaded_files + follow_up['attachments'] if follow_up else uploaded_files,
|
175 |
+
'frontend_context_uuid': str(uuid4()),
|
176 |
+
'frontend_uuid': str(uuid4()),
|
177 |
+
'is_incognito': incognito,
|
178 |
+
'language': language,
|
179 |
+
'last_backend_uuid': follow_up['backend_uuid'] if follow_up else None,
|
180 |
+
'mode': 'concise' if mode == 'auto' else 'copilot',
|
181 |
+
'model_preference': {
|
182 |
+
'auto': {
|
183 |
+
None: 'turbo'
|
184 |
+
},
|
185 |
+
'pro': {
|
186 |
+
None: 'pplx_pro',
|
187 |
+
'sonar': 'experimental',
|
188 |
+
'gpt-4.5': 'gpt45',
|
189 |
+
'gpt-4o': 'gpt4o',
|
190 |
+
'claude 3.7 sonnet': 'claude2',
|
191 |
+
'gemini 2.0 flash': 'gemini2flash',
|
192 |
+
'grok-2': 'grok'
|
193 |
+
},
|
194 |
+
'reasoning': {
|
195 |
+
None: 'pplx_reasoning',
|
196 |
+
'r1': 'r1-1776',
|
197 |
+
'o3-mini': 'o3mini',
|
198 |
+
'grok-4': 'grok4',
|
199 |
+
'gpt-4.1': 'gpt41',
|
200 |
+
'gemini-2.5-pro': 'gemini2flash',
|
201 |
+
'o3':'o3',
|
202 |
+
'claude-sonnet-4-20250514':'claude37sonnetthinking',
|
203 |
+
'sonar-pro':'pplx_alpha'
|
204 |
+
|
205 |
+
|
206 |
+
},
|
207 |
+
'deep research': {
|
208 |
+
None: 'pplx_alpha'
|
209 |
+
}
|
210 |
+
}[mode][model],
|
211 |
+
'source': 'default',
|
212 |
+
'sources': sources,
|
213 |
+
'version': '2.18'
|
214 |
+
}
|
215 |
+
}
|
216 |
+
|
217 |
+
resp = self.session.post('https://www.perplexity.ai/rest/sse/perplexity_ask', json=json_data, stream=True)
|
218 |
+
chunks = []
|
219 |
+
|
220 |
+
|
221 |
+
# Generator for streaming responses
|
222 |
+
def stream_response(resp):
|
223 |
+
for chunk in resp.iter_lines(delimiter=b'\r\n\r\n'):
|
224 |
+
content = chunk.decode('utf-8')
|
225 |
+
|
226 |
+
if content.startswith('event: message\r\n'):
|
227 |
+
# Parse the JSON data from the message
|
228 |
+
content_json = json.loads(content[len('event: message\r\ndata: '):])
|
229 |
+
# Yield the entire chunk for the consumer to process
|
230 |
+
yield content_json
|
231 |
+
|
232 |
+
elif content.startswith('event: end_of_stream\r\n'):
|
233 |
+
# End the generator when the stream is finished
|
234 |
+
return
|
235 |
+
|
236 |
+
# If streaming is requested, return the generator
|
237 |
+
if stream:
|
238 |
+
return stream_response(resp)
|
239 |
+
|
240 |
+
# Non-streaming: process the full response to find the final answer
|
241 |
+
full_answer = None
|
242 |
+
for chunk in resp.iter_lines(delimiter=b'\r\n\r\n'):
|
243 |
+
content = chunk.decode('utf-8')
|
244 |
+
|
245 |
+
if content.startswith('event: message\r\n'):
|
246 |
+
content_json = json.loads(content[len('event: message\r\ndata: '):])
|
247 |
+
# Check for the final answer within the new 'blocks' structure
|
248 |
+
for block in content_json.get('blocks', []):
|
249 |
+
if block.get('intended_usage') == 'ask_text':
|
250 |
+
markdown = block.get('markdown_block', {})
|
251 |
+
if markdown.get('progress') == 'DONE' and 'answer' in markdown:
|
252 |
+
full_answer = markdown['answer']
|
253 |
+
|
254 |
+
elif content.startswith('event: end_of_stream\r\n'):
|
255 |
+
# Stop processing once the stream ends
|
256 |
+
break
|
257 |
+
|
258 |
+
# Return the final answer, or None if not found
|
259 |
+
if full_answer is not None:
|
260 |
+
return full_answer
|
261 |
+
else:
|
262 |
+
print("No full response.")
|
263 |
+
return None
|
264 |
+
|
265 |
+
# ##
|
266 |
+
# perplexity_cli=Client()
|
267 |
+
# # incognito = Enables incognito mode, for people who are using their own account
|
268 |
+
# resp = perplexity_cli.search('Create an image on happiness', mode='auto', model=None, sources=[], files={}, stream=True, language='en-US', follow_up=None, incognito=False)
|
269 |
+
# for i in resp:
|
270 |
+
# try:
|
271 |
+
# print(i["blocks"][0]["markdown_block"]["chunks"][0],end="")
|
272 |
+
|
273 |
+
# # print(i["blocks"][0]["markdown_block"]["chunks"][0],end="")
|
274 |
+
# except:
|
275 |
+
# pass
|