Spaces:
Sleeping
Sleeping
Shageenderan Sapai commited on
Commit ·
6d00e9d
1
Parent(s): 585cd3c
added get_daily messages
Browse files- app/__pycache__/flows.cpython-312.pyc +0 -0
- app/__pycache__/main.cpython-312.pyc +0 -0
- app/__pycache__/user.cpython-312.pyc +0 -0
- app/__pycache__/utils.cpython-312.pyc +0 -0
- app/main.py +7 -0
- app/user.py +33 -3
app/__pycache__/flows.cpython-312.pyc
CHANGED
|
Binary files a/app/__pycache__/flows.cpython-312.pyc and b/app/__pycache__/flows.cpython-312.pyc differ
|
|
|
app/__pycache__/main.cpython-312.pyc
CHANGED
|
Binary files a/app/__pycache__/main.cpython-312.pyc and b/app/__pycache__/main.cpython-312.pyc differ
|
|
|
app/__pycache__/user.cpython-312.pyc
CHANGED
|
Binary files a/app/__pycache__/user.cpython-312.pyc and b/app/__pycache__/user.cpython-312.pyc differ
|
|
|
app/__pycache__/utils.cpython-312.pyc
CHANGED
|
Binary files a/app/__pycache__/utils.cpython-312.pyc and b/app/__pycache__/utils.cpython-312.pyc differ
|
|
|
app/main.py
CHANGED
|
@@ -384,6 +384,13 @@ def process_gg_session(request: GGItem, api_key: str = Security(get_api_key)):
|
|
| 384 |
logger.info(f"GG session processed: {session_id}, response: {response}", extra={"user_id": user_id, "endpoint": "/process_gg_session"})
|
| 385 |
return {"response": response}
|
| 386 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
@app.post("/batch_refresh_users")
|
| 388 |
def refresh_multiple_users(user_ids: List[str], api_key: str = Security(get_api_key)):
|
| 389 |
logger.info("Refreshing multiple users", extra={"endpoint": "/batch_refresh_users"})
|
|
|
|
| 384 |
logger.info(f"GG session processed: {session_id}, response: {response}", extra={"user_id": user_id, "endpoint": "/process_gg_session"})
|
| 385 |
return {"response": response}
|
| 386 |
|
| 387 |
+
@app.get("/user_daily_messages")
|
| 388 |
+
def get_daily_message(user_id: str, api_key: str = Security(get_api_key)):
|
| 389 |
+
logger.info("Getting daily messages", extra={"user_id": user_id, "endpoint": "/user_daily_messages"})
|
| 390 |
+
user = get_user(user_id)
|
| 391 |
+
daily_messages = user.get_daily_messages()
|
| 392 |
+
return {"response": daily_messages}
|
| 393 |
+
|
| 394 |
@app.post("/batch_refresh_users")
|
| 395 |
def refresh_multiple_users(user_ids: List[str], api_key: str = Security(get_api_key)):
|
| 396 |
logger.info("Refreshing multiple users", extra={"endpoint": "/batch_refresh_users"})
|
app/user.py
CHANGED
|
@@ -40,6 +40,7 @@ class ConversationManager:
|
|
| 40 |
self.state = {'date': pd.Timestamp.now().strftime("%d-%m-%Y %a %H:%M:%S")}
|
| 41 |
|
| 42 |
self.current_thread = self.create_thread()
|
|
|
|
| 43 |
|
| 44 |
logger.info("Initializing conversation state", extra={"user_id": self.user.user_id, "endpoint": "conversation_init"})
|
| 45 |
|
|
@@ -71,13 +72,13 @@ class ConversationManager:
|
|
| 71 |
""")
|
| 72 |
return thread
|
| 73 |
|
| 74 |
-
def _get_current_thread_history(self, remove_system_message=True,
|
| 75 |
if thread is None:
|
| 76 |
thread = self.current_thread
|
| 77 |
if not remove_system_message:
|
| 78 |
return [{"role": msg.role, "content": msg.content[0].text.value} for msg in self.client.beta.threads.messages.list(thread.id, order="asc")]
|
| 79 |
-
if
|
| 80 |
-
return [{"role": msg.role, "content": msg.content[0].text.value} for msg in self.client.beta.threads.messages.list(thread.id, order="asc", after=
|
| 81 |
return [{"role": msg.role, "content": msg.content[0].text.value} for msg in self.client.beta.threads.messages.list(thread.id, order="asc")][1:] # remove the system message
|
| 82 |
|
| 83 |
def add_message_to_thread(self, thread_id, role, content):
|
|
@@ -151,6 +152,32 @@ class ConversationManager:
|
|
| 151 |
def _add_ai_message(self, text):
|
| 152 |
return self.add_message_to_thread(self.current_thread.id, "assistant", text)
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
def _send_morning_message(self, text):
|
| 155 |
# create a new thread
|
| 156 |
# OPENAI LIMITATION: Can only attach a maximum of 32 messages when creating a new thread
|
|
@@ -735,6 +762,9 @@ Use bullet points or numbered lists where appropriate to enhance readability."""
|
|
| 735 |
except Exception as e:
|
| 736 |
return False
|
| 737 |
|
|
|
|
|
|
|
|
|
|
| 738 |
def change_assistant(self, asst_id):
|
| 739 |
self.asst_id = asst_id
|
| 740 |
self.conversations.assistants['general'] = Assistant(self.asst_id, self.conversations)
|
|
|
|
| 40 |
self.state = {'date': pd.Timestamp.now().strftime("%d-%m-%Y %a %H:%M:%S")}
|
| 41 |
|
| 42 |
self.current_thread = self.create_thread()
|
| 43 |
+
self.daily_thread = None
|
| 44 |
|
| 45 |
logger.info("Initializing conversation state", extra={"user_id": self.user.user_id, "endpoint": "conversation_init"})
|
| 46 |
|
|
|
|
| 72 |
""")
|
| 73 |
return thread
|
| 74 |
|
| 75 |
+
def _get_current_thread_history(self, remove_system_message=True, _msg=None, thread=None):
|
| 76 |
if thread is None:
|
| 77 |
thread = self.current_thread
|
| 78 |
if not remove_system_message:
|
| 79 |
return [{"role": msg.role, "content": msg.content[0].text.value} for msg in self.client.beta.threads.messages.list(thread.id, order="asc")]
|
| 80 |
+
if _msg:
|
| 81 |
+
return [{"role": msg.role, "content": msg.content[0].text.value} for msg in self.client.beta.threads.messages.list(thread.id, order="asc", after=_msg.id)][1:]
|
| 82 |
return [{"role": msg.role, "content": msg.content[0].text.value} for msg in self.client.beta.threads.messages.list(thread.id, order="asc")][1:] # remove the system message
|
| 83 |
|
| 84 |
def add_message_to_thread(self, thread_id, role, content):
|
|
|
|
| 152 |
def _add_ai_message(self, text):
|
| 153 |
return self.add_message_to_thread(self.current_thread.id, "assistant", text)
|
| 154 |
|
| 155 |
+
def get_daily_thread(self):
|
| 156 |
+
if self.daily_thread is None:
|
| 157 |
+
messages = self._get_current_thread_history(remove_system_message=False)
|
| 158 |
+
|
| 159 |
+
self.daily_thread = self.client.beta.threads.create(
|
| 160 |
+
messages=messages[:30]
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
# Add remaining messages one by one if there are more than 30
|
| 164 |
+
for msg in messages[30:]:
|
| 165 |
+
self.add_message_to_thread(
|
| 166 |
+
self.daily_thread.id,
|
| 167 |
+
msg['role'],
|
| 168 |
+
msg['content']
|
| 169 |
+
)
|
| 170 |
+
self.last_daily_message = list(self.client.beta.threads.messages.list(self.daily_thread.id, order="asc"))[-1]
|
| 171 |
+
else:
|
| 172 |
+
messages = self._get_current_thread_history(remove_system_message=False, _msg=self.last_daily_message)
|
| 173 |
+
self.client.beta.threads.delete(self.daily_thread.id)
|
| 174 |
+
self.daily_thread = self.client.beta.threads.create(messages=messages)
|
| 175 |
+
self.last_daily_message = list(self.client.beta.threads.messages.list(self.daily_thread.id, order="asc"))[-1]
|
| 176 |
+
logger.info(f"Daily Thread: {self._get_current_thread_history(thread=self.daily_thread)}", extra={"user_id": self.user.user_id, "endpoint": "send_morning_message"})
|
| 177 |
+
logger.info(f"Last Daily Message: {self.last_daily_message}", extra={"user_id": self.user.user_id, "endpoint": "send_morning_message"})
|
| 178 |
+
return self._get_current_thread_history(thread=self.daily_thread)
|
| 179 |
+
# [{"role":, "content":}, ....]
|
| 180 |
+
|
| 181 |
def _send_morning_message(self, text):
|
| 182 |
# create a new thread
|
| 183 |
# OPENAI LIMITATION: Can only attach a maximum of 32 messages when creating a new thread
|
|
|
|
| 762 |
except Exception as e:
|
| 763 |
return False
|
| 764 |
|
| 765 |
+
def get_daily_messages(self):
|
| 766 |
+
return self.conversations.get_daily_thread()
|
| 767 |
+
|
| 768 |
def change_assistant(self, asst_id):
|
| 769 |
self.asst_id = asst_id
|
| 770 |
self.conversations.assistants['general'] = Assistant(self.asst_id, self.conversations)
|