-
Notifications
You must be signed in to change notification settings - Fork 52
/
main.py
391 lines (335 loc) · 13.9 KB
/
main.py
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import json
import logging
import time
import urllib
import uuid
from typing import Dict, List, Generator, Iterator
import requests
from requests_html import HTMLSession
from meta_ai_api.utils import (
generate_offline_threading_id,
extract_value,
format_response,
)
from meta_ai_api.utils import get_fb_session
from meta_ai_api.exceptions import FacebookRegionBlocked
MAX_RETRIES = 3
class MetaAI:
"""
A class to interact with the Meta AI API to obtain and use access tokens for sending
and receiving messages from the Meta AI Chat API.
"""
def __init__(self, fb_email: str = None, fb_password: str = None, proxy: dict = None):
self.session = requests.Session()
self.session.headers.update(
{
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
}
)
self.access_token = None
self.fb_email = fb_email
self.fb_password = fb_password
self.proxy = proxy
if self.proxy and not self.check_proxy():
raise ConnectionError("Unable to connect to proxy. Please check your proxy settings.")
self.is_authed = fb_password is not None and fb_email is not None
self.cookies = self.get_cookies()
def check_proxy(self, test_url: str="https://api.ipify.org/?format=json") -> bool:
"""
Checks the proxy connection by making a request to a test URL.
Args:
test_url (str): A test site from which we check that the proxy is installed correctly.
Returns:
bool: True if the proxy is working, False otherwise.
"""
try:
response = self.session.get(test_url, proxies=self.proxy, timeout=10)
if response.status_code == 200:
print(response.json())
self.session.proxies = self.proxy
return True
return False
except requests.RequestException:
return False
def get_access_token(self) -> str:
"""
Retrieves an access token using Meta's authentication API.
Returns:
str: A valid access token.
"""
url = "https://www.meta.ai/api/graphql/"
payload = {
"lsd": self.cookies["lsd"],
"fb_api_caller_class": "RelayModern",
"fb_api_req_friendly_name": "useAbraAcceptTOSForTempUserMutation",
"variables": {
"dob": "1999-01-01",
"icebreaker_type": "TEXT",
"__relay_internal__pv__WebPixelRatiorelayprovider": 1,
},
"doc_id": "7604648749596940",
}
payload = urllib.parse.urlencode(payload) # noqa
headers = {
"content-type": "application/x-www-form-urlencoded",
"cookie": f'_js_datr={self.cookies["_js_datr"]}; '
f'abra_csrf={self.cookies["abra_csrf"]}; datr={self.cookies["datr"]};',
"sec-fetch-site": "same-origin",
"x-fb-friendly-name": "useAbraAcceptTOSForTempUserMutation",
}
response = self.session.post(url, headers=headers, data=payload)
try:
auth_json = response.json()
except json.JSONDecodeError:
raise FacebookRegionBlocked(
"Unable to receive a valid response from Meta AI. This is likely due to your region being blocked. "
"Try manually accessing https://www.meta.ai/ to confirm."
)
access_token = auth_json["data"]["xab_abra_accept_terms_of_service"][
"new_temp_user_auth"
]["access_token"]
return access_token
def prompt(
self, message: str, stream: bool = False, attempts: int = 0
) -> Dict or Generator[Dict, None, None]:
"""
Sends a message to the Meta AI and returns the response.
Args:
message (str): The message to send.
stream (bool): Whether to stream the response or not. Defaults to False.
attempts (int): The number of attempts to retry if an error occurs. Defaults to 0.
Returns:
dict: A dictionary containing the response message and sources.
Raises:
Exception: If unable to obtain a valid response after several attempts.
"""
if not self.is_authed:
self.access_token = self.get_access_token()
auth_payload = {"access_token": self.access_token}
url = "https://graph.meta.ai/graphql?locale=user"
else:
auth_payload = {"fb_dtsg": self.cookies["fb_dtsg"]}
url = "https://www.meta.ai/api/graphql/"
# Need to sleep for a bit, for some reason the API doesn't like it when we send request too quickly
# (maybe Meta needs to register Cookies on their side?)
time.sleep(1)
payload = {
**auth_payload,
"fb_api_caller_class": "RelayModern",
"fb_api_req_friendly_name": "useAbraSendMessageMutation",
"variables": json.dumps(
{
"message": {"sensitive_string_value": message},
"externalConversationId": str(uuid.uuid4()),
"offlineThreadingId": generate_offline_threading_id(),
"suggestedPromptIndex": None,
"flashVideoRecapInput": {"images": []},
"flashPreviewInput": None,
"promptPrefix": None,
"entrypoint": "ABRA__CHAT__TEXT",
"icebreaker_type": "TEXT",
"__relay_internal__pv__AbraDebugDevOnlyrelayprovider": False,
"__relay_internal__pv__WebPixelRatiorelayprovider": 1,
}
),
"server_timestamps": "true",
"doc_id": "7783822248314888",
}
payload = urllib.parse.urlencode(payload) # noqa
headers = {
"content-type": "application/x-www-form-urlencoded",
"x-fb-friendly-name": "useAbraSendMessageMutation",
}
if self.is_authed:
headers["cookie"] = f'abra_sess={self.cookies["abra_sess"]}'
# Recreate the session to avoid cookie leakage when user is authenticated
self.session = requests.Session()
self.session.proxies = self.proxy
response = self.session.post(url, headers=headers, data=payload, stream=stream)
if not stream:
raw_response = response.text
last_streamed_response = self.extract_last_response(raw_response)
if not last_streamed_response:
return self.retry(message, stream=stream, attempts=attempts)
extracted_data = self.extract_data(last_streamed_response)
return extracted_data
else:
lines = response.iter_lines()
is_error = json.loads(next(lines))
if len(is_error.get("errors", [])) > 0:
return self.retry(message, stream=stream, attempts=attempts)
return self.stream_response(lines)
def retry(self, message: str, stream: bool = False, attempts: int = 0):
"""
Retries the prompt function if an error occurs.
"""
if attempts <= MAX_RETRIES:
logging.warning(
f"Was unable to obtain a valid response from Meta AI. Retrying... Attempt {attempts + 1}/{MAX_RETRIES}."
)
time.sleep(3)
return self.prompt(message, stream=stream, attempts=attempts + 1)
else:
raise Exception(
"Unable to obtain a valid response from Meta AI. Try again later."
)
@staticmethod
def extract_last_response(response: str) -> Dict:
"""
Extracts the last response from the Meta AI API.
Args:
response (str): The response to extract the last response from.
Returns:
dict: A dictionary containing the last response.
"""
last_streamed_response = None
for line in response.split("\n"):
try:
json_line = json.loads(line)
except json.JSONDecodeError:
continue
bot_response_message = (
json_line.get("data", {})
.get("node", {})
.get("bot_response_message", {})
)
streaming_state = bot_response_message.get("streaming_state")
if streaming_state == "OVERALL_DONE":
last_streamed_response = json_line
return last_streamed_response
def stream_response(self, lines: Iterator[str]):
"""
Streams the response from the Meta AI API.
Args:
lines (Iterator[str]): The lines to stream.
Yields:
dict: A dictionary containing the response message and sources.
"""
for line in lines:
if line:
json_line = json.loads(line)
extracted_data = self.extract_data(json_line)
if not extracted_data.get("message"):
continue
yield extracted_data
def extract_data(self, json_line: dict):
"""
Extract data and sources from a parsed JSON line.
Args:
json_line (dict): Parsed JSON line.
Returns:
Tuple (str, list): Response message and list of sources.
"""
bot_response_message = (
json_line.get("data", {}).get("node", {}).get("bot_response_message", {})
)
response = format_response(response=json_line)
fetch_id = bot_response_message.get("fetch_id")
sources = self.fetch_sources(fetch_id) if fetch_id else []
medias = self.extract_media(bot_response_message)
return {"message": response, "sources": sources, "media": medias}
def extract_media(self, json_line: dict) -> List[Dict]:
"""
Extract media from a parsed JSON line.
Args:
json_line (dict): Parsed JSON line.
Returns:
list: A list of dictionaries containing the extracted media.
"""
medias = []
imagine_card = json_line.get("imagine_card", {})
session = imagine_card.get("session", {}) if imagine_card else {}
media_sets = (
(json_line.get("imagine_card", {}).get("session", {}).get("media_sets", []))
if imagine_card and session
else []
)
for media_set in media_sets:
imagine_media = media_set.get("imagine_media", [])
for media in imagine_media:
medias.append(
{
"url": media.get("uri"),
"type": media.get("media_type"),
"prompt": media.get("prompt"),
}
)
return medias
def get_cookies(self) -> dict:
"""
Extracts necessary cookies from the Meta AI main page.
Returns:
dict: A dictionary containing essential cookies.
"""
session = HTMLSession()
headers = {}
if self.fb_email is not None and self.fb_password is not None:
fb_session = get_fb_session(self.fb_email, self.fb_password)
headers = {"cookie": f"abra_sess={fb_session['abra_sess']}"}
response = session.get(
"https://www.meta.ai/",
headers=headers,
)
cookies = {
"_js_datr": extract_value(
response.text, start_str='_js_datr":{"value":"', end_str='",'
),
"datr": extract_value(
response.text, start_str='datr":{"value":"', end_str='",'
),
"lsd": extract_value(
response.text, start_str='"LSD",[],{"token":"', end_str='"}'
),
"fb_dtsg": extract_value(
response.text, start_str='DTSGInitData",[],{"token":"', end_str='"'
),
}
if len(headers) > 0:
cookies["abra_sess"] = fb_session["abra_sess"]
else:
cookies["abra_csrf"] = extract_value(
response.text, start_str='abra_csrf":{"value":"', end_str='",'
)
return cookies
def fetch_sources(self, fetch_id: str) -> List[Dict]:
"""
Fetches sources from the Meta AI API based on the given query.
Args:
fetch_id (str): The fetch ID to use for the query.
Returns:
list: A list of dictionaries containing the fetched sources.
"""
url = "https://graph.meta.ai/graphql?locale=user"
payload = {
"access_token": self.access_token,
"fb_api_caller_class": "RelayModern",
"fb_api_req_friendly_name": "AbraSearchPluginDialogQuery",
"variables": json.dumps({"abraMessageFetchID": fetch_id}),
"server_timestamps": "true",
"doc_id": "6946734308765963",
}
payload = urllib.parse.urlencode(payload) # noqa
headers = {
"authority": "graph.meta.ai",
"accept-language": "en-US,en;q=0.9,fr-FR;q=0.8,fr;q=0.7",
"content-type": "application/x-www-form-urlencoded",
"cookie": f'dpr=2; abra_csrf={self.cookies.get("abra_csrf")}; datr={self.cookies.get("datr")}; ps_n=1; ps_l=1',
"x-fb-friendly-name": "AbraSearchPluginDialogQuery",
}
response = self.session.post(url, headers=headers, data=payload)
response_json = response.json()
message = response_json.get("data", {}).get("message", {})
search_results = (
(response_json.get("data", {}).get("message", {}).get("searchResults"))
if message
else None
)
if search_results is None:
return []
references = search_results["references"]
return references
if __name__ == "__main__":
meta = MetaAI()
resp = meta.prompt("What was the Warriors score last game?", stream=False)
print(resp)