@@ -48,6 +48,8 @@ def __init__(
4848
4949 self .is_authed = fb_password is not None and fb_email is not None
5050 self .cookies = self .get_cookies ()
51+ self .external_conversation_id = None
52+ self .offline_threading_id = None
5153
5254 def check_proxy (self , test_url : str = "https://api.ipify.org/?format=json" ) -> bool :
5355 """
@@ -76,6 +78,9 @@ def get_access_token(self) -> str:
7678 str: A valid access token.
7779 """
7880
81+ if self .access_token :
82+ return self .access_token
83+
7984 url = "https://www.meta.ai/api/graphql/"
8085 payload = {
8186 "lsd" : self .cookies ["lsd" ],
@@ -113,7 +118,11 @@ def get_access_token(self) -> str:
113118 return access_token
114119
115120 def prompt (
116- self , message : str , stream : bool = False , attempts : int = 0
121+ self ,
122+ message : str ,
123+ stream : bool = False ,
124+ attempts : int = 0 ,
125+ new_conversation : bool = False ,
117126 ) -> Dict or Generator [Dict , None , None ]:
118127 """
119128 Sends a message to the Meta AI and returns the response.
@@ -122,6 +131,7 @@ def prompt(
122131 message (str): The message to send.
123132 stream (bool): Whether to stream the response or not. Defaults to False.
124133 attempts (int): The number of attempts to retry if an error occurs. Defaults to 0.
134+ new_conversation (bool): Whether to start a new conversation or not. Defaults to False.
125135
126136 Returns:
127137 dict: A dictionary containing the response message and sources.
@@ -141,14 +151,17 @@ def prompt(
141151 # Need to sleep for a bit, for some reason the API doesn't like it when we send request too quickly
142152 # (maybe Meta needs to register Cookies on their side?)
143153 time .sleep (1 )
154+ if not self .external_conversation_id or new_conversation :
155+ external_id = str (uuid .uuid4 ())
156+ self .external_conversation_id = external_id
144157 payload = {
145158 ** auth_payload ,
146159 "fb_api_caller_class" : "RelayModern" ,
147160 "fb_api_req_friendly_name" : "useAbraSendMessageMutation" ,
148161 "variables" : json .dumps (
149162 {
150163 "message" : {"sensitive_string_value" : message },
151- "externalConversationId" : str ( uuid . uuid4 ()) ,
164+ "externalConversationId" : self . external_conversation_id ,
152165 "offlineThreadingId" : generate_offline_threading_id (),
153166 "suggestedPromptIndex" : None ,
154167 "flashVideoRecapInput" : {"images" : []},
@@ -206,8 +219,7 @@ def retry(self, message: str, stream: bool = False, attempts: int = 0):
206219 "Unable to obtain a valid response from Meta AI. Try again later."
207220 )
208221
209- @staticmethod
210- def extract_last_response (response : str ) -> Dict :
222+ def extract_last_response (self , response : str ) -> Dict :
211223 """
212224 Extracts the last response from the Meta AI API.
213225
@@ -229,6 +241,11 @@ def extract_last_response(response: str) -> Dict:
229241 .get ("node" , {})
230242 .get ("bot_response_message" , {})
231243 )
244+ chat_id = bot_response_message .get ("id" )
245+ if chat_id :
246+ external_conversation_id , offline_threading_id , _ = chat_id .split ("_" )
247+ self .external_conversation_id = external_conversation_id
248+ self .offline_threading_id = offline_threading_id
232249
233250 streaming_state = bot_response_message .get ("streaming_state" )
234251 if streaming_state == "OVERALL_DONE" :
@@ -390,5 +407,9 @@ def fetch_sources(self, fetch_id: str) -> List[Dict]:
390407
391408if __name__ == "__main__" :
392409 meta = MetaAI ()
393- resp = meta .prompt ("What was the Warriors score last game?" , stream = False )
410+ # resp = meta.prompt("What was the Warriors score last game?", stream=False)
411+ resp = meta .prompt ("what is 2 + 2?" , stream = False )
412+ print (resp )
413+
414+ resp = meta .prompt ("what was my previous question?" , stream = False )
394415 print (resp )
0 commit comments