perplexityai 1.0.4__tar.gz → 1.0.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of perplexityai might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: perplexityai
3
- Version: 1.0.4
3
+ Version: 1.0.5
4
4
  Summary: A simple module to use Perplexity AI in Python.
5
5
  Home-page: https://github.com/Ruu3f/perplexityai
6
6
  Author: Ruu3f
@@ -1,5 +1,5 @@
1
1
  __author__ = "Ruu3f"
2
- __version__ = "1.0.4"
2
+ __version__ = "1.0.5"
3
3
 
4
4
  from uuid import uuid4
5
5
  from requests import Session
@@ -19,11 +19,10 @@ class Perplexity:
19
19
  }
20
20
  self.session.headers.update(self.user_agent)
21
21
  self.t = format(getrandbits(32), "08x")
22
- self.sid = loads(
23
- self.session.get(
24
- url=f"https://www.perplexity.ai/socket.io/?EIO=4&transport=polling&t={self.t}"
25
- ).text[1:]
26
- )["sid"]
22
+ response = self.session.get(
23
+ url=f"https://www.perplexity.ai/socket.io/?EIO=4&transport=polling&t={self.t}"
24
+ ).text[1:]
25
+ self.sid = loads(response)["sid"]
27
26
  self.n = 1
28
27
  self.base = 420
29
28
  self.finished = True
@@ -70,17 +69,18 @@ class Perplexity:
70
69
  self.queue.append(message)
71
70
  self.finished = True
72
71
 
73
- cookies = ""
74
- for key, value in self.session.cookies.get_dict().items():
75
- cookies += f"{key}={value}; "
76
- return WebSocketApp(
72
+ cookies = "; ".join(
73
+ [f"{key}={value}" for key, value in self.session.cookies.get_dict().items()]
74
+ )
75
+ ws = WebSocketApp(
77
76
  url=f"wss://www.perplexity.ai/socket.io/?EIO=4&transport=websocket&sid={self.sid}",
78
77
  header=self.user_agent,
79
- cookie=cookies[:-2],
78
+ cookie=cookies,
80
79
  on_open=on_open,
81
80
  on_message=on_message,
82
81
  on_error=lambda ws, err: print(f"WebSocket error: {err}"),
83
82
  )
83
+ return ws
84
84
 
85
85
  def generate_answer(self, query):
86
86
  self.finished = False
@@ -127,73 +127,66 @@ class Labs:
127
127
  "X-Client-Name": "Perplexity-iOS",
128
128
  }
129
129
  )
130
- self._init_session_without_login()
131
-
130
+ self.session.get(url=f"https://www.perplexity.ai/search/{str(uuid4())}")
132
131
  self.t = format(getrandbits(32), "08x")
133
- self.sid = loads(
134
- self.session.get(
135
- url="https://labs-api.perplexity.ai/socket.io/?transport=polling&EIO=4"
136
- ).text[1:]
137
- )["sid"]
132
+ response = self.session.get(
133
+ url="https://labs-api.perplexity.ai/socket.io/?transport=polling&EIO=4"
134
+ ).text[1:]
135
+ self.sid = loads(response)["sid"]
138
136
 
139
137
  self.queue = []
140
138
  self.finished = True
141
139
 
142
- assert self._ask_anonymous_user(), "failed to ask anonymous user"
143
- self.ws = WebSocketApp(
144
- url=f"wss://labs-api.perplexity.ai/socket.io/?EIO=4&transport=websocket&sid={self.sid}",
145
- header=self._get_headers(),
146
- on_open=self._on_open,
147
- on_message=self._on_message,
148
- on_error=lambda ws, err: print(f"websocket error: {err}"),
149
- )
150
- Thread(target=self.ws.run_forever).start()
151
- self._auth_session()
152
-
153
- while not (self.ws.sock and self.ws.sock.connected):
154
- sleep(0.01)
155
-
156
- def _init_session_without_login(self):
157
- self.session.get(url=f"https://www.perplexity.ai/search/{str(uuid4())}")
158
- self.session.headers.update(
159
- {"User-Agent": "Ask/2.2.1/334 (iOS; iPhone) isiOSOnMac/false"}
160
- )
161
-
162
- def _ask_anonymous_user(self):
163
140
  response = self.session.post(
164
141
  url=f"https://labs-api.perplexity.ai/socket.io/?EIO=4&transport=polling&t={self.t}&sid={self.sid}",
165
142
  data='40{"jwt":"anonymous-ask-user"}',
166
143
  ).text
167
- return response == "OK"
144
+ assert response == "OK", "failed to ask anonymous user"
145
+
146
+ self._init_websocket()
147
+
148
+ while not (self.ws.sock and self.ws.sock.connected):
149
+ sleep(0.01)
150
+
151
+ def _init_websocket(self):
152
+ def on_open(ws):
153
+ ws.send("2probe")
154
+ ws.send("5")
168
155
 
169
- def _get_cookies_str(self):
170
- return "; ".join(
156
+ def on_message(ws, message):
157
+ if message == "2":
158
+ ws.send("3")
159
+ elif message.startswith("42"):
160
+ message = loads(message[2:])[1]
161
+ if "status" not in message:
162
+ self.queue.append(message)
163
+ elif message["status"] == "completed":
164
+ self.finished = True
165
+ self.history.append(
166
+ {
167
+ "role": "assistant",
168
+ "content": message["output"],
169
+ "priority": 0,
170
+ }
171
+ )
172
+ elif message["status"] == "failed":
173
+ self.finished = True
174
+
175
+ cookies = "; ".join(
171
176
  [f"{key}={value}" for key, value in self.session.cookies.get_dict().items()]
172
177
  )
173
-
174
- def _get_headers(self):
175
- headers = {"User-Agent": "Ask/2.2.1/334 (iOS; iPhone) isiOSOnMac/false"}
176
- headers["Cookie"] = self._get_cookies_str()
177
- return headers
178
-
179
- def _on_open(self, ws):
180
- ws.send("2probe")
181
- ws.send("5")
182
-
183
- def _on_message(self, ws, message):
184
- if message == "2":
185
- ws.send("3")
186
- elif message.startswith("42"):
187
- message = loads(message[2:])[1]
188
- if "status" not in message:
189
- self.queue.append(message)
190
- elif message["status"] == "completed":
191
- self.finished = True
192
- self.history.append(
193
- {"role": "assistant", "content": message["output"], "priority": 0}
194
- )
195
- elif message["status"] == "failed":
196
- self.finished = True
178
+ self.ws = WebSocketApp(
179
+ url=f"wss://labs-api.perplexity.ai/socket.io/?EIO=4&transport=websocket&sid={self.sid}",
180
+ header={
181
+ "User-Agent": "Ask/2.2.1/334 (iOS; iPhone) isiOSOnMac/false",
182
+ "Cookie": cookies,
183
+ },
184
+ on_open=on_open,
185
+ on_message=on_message,
186
+ on_error=lambda ws, err: print(f"websocket error: {err}"),
187
+ )
188
+ Thread(target=self.ws.run_forever).start()
189
+ self.session.get(url="https://www.perplexity.ai/api/auth/session")
197
190
 
198
191
  def generate_answer(self, prompt, model="mistral-7b-instruct"):
199
192
  assert self.finished, "already searching"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: perplexityai
3
- Version: 1.0.4
3
+ Version: 1.0.5
4
4
  Summary: A simple module to use Perplexity AI in Python.
5
5
  Home-page: https://github.com/Ruu3f/perplexityai
6
6
  Author: Ruu3f
@@ -5,7 +5,7 @@ with open("README.md", encoding="utf-8") as f:
5
5
 
6
6
  setup(
7
7
  name="perplexityai",
8
- version="1.0.4",
8
+ version="1.0.5",
9
9
  description="A simple module to use Perplexity AI in Python.",
10
10
  long_description=README,
11
11
  long_description_content_type="text/markdown",
File without changes
File without changes
File without changes