From 1e07627214768b2fad022a410aa093fe4cdd5583 Mon Sep 17 00:00:00 2001 From: OdyAsh Date: Sat, 8 Mar 2025 16:46:30 +0200 Subject: [PATCH] Fix Message Retrieval Logic Refactor the message retrieval logic in `ansari_db.py` regarding whatsapp messages to retrieve the correct tool messages format when a whatsapp thread is retrieved from DB (i.e., user sends more than one message in a 3-hour time frame). --- .gitignore | 4 +++- src/ansari/ansari_db.py | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index ab27d63..424a4c2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,13 +5,15 @@ abandoned/ bin/ datasources/ diskcache_dir/ -docs/recordings/00_chapters.txt +docs/recordings/* etc/ example_projects/ lib/ share/ +tmp/* src/ansari_backend.egg-info/* +.__atomic-write* .env .history *.pyc diff --git a/src/ansari/ansari_db.py b/src/ansari/ansari_db.py index 4641662..5b98015 100644 --- a/src/ansari/ansari_db.py +++ b/src/ansari/ansari_db.py @@ -616,11 +616,11 @@ def get_thread_llm_whatsapp(self, thread_id: str, user_id_whatsapp: int) -> list ORDER BY timestamp; """ result = self._execute_query(select_cmd, (thread_id, user_id_whatsapp), "all")[0] - return ( - [{"role": x[0], "content": x[1], "name": x[2]} if x[2] else {"role": x[0], "content": x[1]} for x in result] - if result - else [] - ) + msgs = [] + for db_row in result: + msgs.extend(self.convert_message_llm(db_row)) + + return msgs except Exception as e: logger.warning(f"Warning (possible error): {e}") return []