Spaces:
Build error
Build error
Benedetto Scala commited on
Commit ·
b1dad49
1
Parent(s): e521d7f
add memory tool to let the chatbot remember things
Browse files
app.py
CHANGED
|
@@ -6,7 +6,141 @@ import yaml
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@tool
|
| 12 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -44,7 +178,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 44 |
|
| 45 |
agent = CodeAgent(
|
| 46 |
model=model,
|
| 47 |
-
tools=[final_answer, image_generation_tool], ## add your tools here (don't remove final answer)
|
| 48 |
max_steps=6,
|
| 49 |
verbosity_level=1,
|
| 50 |
grammar=None,
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
+
import sqlite3
|
| 10 |
+
import os
|
| 11 |
+
from smolagents import tool
|
| 12 |
|
| 13 |
+
DB_NAME = "robot_memory.db"
|
| 14 |
+
TABLE_NAME = "robot_memories"
|
| 15 |
+
|
| 16 |
+
def initialize_database():
|
| 17 |
+
"""Inizializza il database e crea la tabella della memoria se non esiste già."""
|
| 18 |
+
# Crea (o apre) il database
|
| 19 |
+
conn = sqlite3.connect(DB_NAME)
|
| 20 |
+
cursor = conn.cursor()
|
| 21 |
+
|
| 22 |
+
# Crea la tabella 'robot_memories' se non esiste già
|
| 23 |
+
cursor.execute(f"""
|
| 24 |
+
CREATE TABLE IF NOT EXISTS {TABLE_NAME} (
|
| 25 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 26 |
+
memory_text TEXT NOT NULL,
|
| 27 |
+
tags TEXT
|
| 28 |
+
);
|
| 29 |
+
""")
|
| 30 |
+
conn.commit()
|
| 31 |
+
conn.close()
|
| 32 |
+
|
| 33 |
+
@tool
|
| 34 |
+
def robot_memory_tool(action: str, content: str = "", tags: str = "") -> str:
|
| 35 |
+
"""A memory management tool that allows storing, retrieving, and deleting memories
|
| 36 |
+
from a SQLite database. It supports tagging and keyword search for efficient memory retrieval.
|
| 37 |
+
Args:
|
| 38 |
+
action: The action to perform. Supported actions are:
|
| 39 |
+
- 'store': Save a new memory with optional tags.
|
| 40 |
+
- 'retrieve': Search for stored memories based on text content and/or tags.
|
| 41 |
+
- 'delete': Remove memories based on text content and/or tags.
|
| 42 |
+
- 'help': Display usage examples.
|
| 43 |
+
content: The text of the memory or search criteria. Defaults to an empty string.
|
| 44 |
+
tags: Tags associated with the memory for categorization or search. Defaults to an empty string.
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
str: A message indicating the result of the action:
|
| 48 |
+
- For 'store': Confirmation of successful memory storage.
|
| 49 |
+
- For 'retrieve': A formatted list of matching memories or a message indicating no matches.
|
| 50 |
+
- For 'delete': Confirmation of the number of deleted memories.
|
| 51 |
+
- For 'help': A usage guide with examples.
|
| 52 |
+
- For unsupported actions: An error message.
|
| 53 |
+
|
| 54 |
+
Usage Examples:
|
| 55 |
+
>>> robot_memory_tool('store', content='Ho visto un gatto', tags='animale, esterno')
|
| 56 |
+
"[STORE] Ricordo memorizzato con successo: 'Ho visto un gatto' (tags: 'animale, esterno')"
|
| 57 |
+
|
| 58 |
+
>>> robot_memory_tool('retrieve', content='gatto')
|
| 59 |
+
"[RETRIEVE] Ricordi trovati:\n- ID: 1, Testo: 'Ho visto un gatto', Tags: 'animale, esterno'"
|
| 60 |
+
|
| 61 |
+
>>> robot_memory_tool('delete', tags='animale')
|
| 62 |
+
"[DELETE] Rimossi 1 ricordo/i dal database."
|
| 63 |
+
|
| 64 |
+
>>> robot_memory_tool('help')
|
| 65 |
+
"Esempi di utilizzo:\n1) robot_memory_tool('store', content='Ho visto un gatto', tags='animale, esterno')\n2) robot_memory_tool('retrieve', content='gatto')\n3) robot_memory_tool('delete', tags='animale')\n4) robot_memory_tool('help')"
|
| 66 |
+
"""
|
| 67 |
+
initialize_database() # Assicura che il DB e la tabella esistano
|
| 68 |
+
conn = sqlite3.connect(DB_NAME)
|
| 69 |
+
cursor = conn.cursor()
|
| 70 |
+
|
| 71 |
+
# Normalizza i parametri
|
| 72 |
+
action = action.lower().strip()
|
| 73 |
+
content = content.strip()
|
| 74 |
+
tags = tags.strip()
|
| 75 |
+
|
| 76 |
+
if action == 'store':
|
| 77 |
+
# Inserisce un nuovo record nella tabella
|
| 78 |
+
cursor.execute(f"""
|
| 79 |
+
INSERT INTO {TABLE_NAME} (memory_text, tags)
|
| 80 |
+
VALUES (?, ?);
|
| 81 |
+
""", (content, tags))
|
| 82 |
+
conn.commit()
|
| 83 |
+
conn.close()
|
| 84 |
+
return f"[STORE] Ricordo memorizzato con successo: '{content}' (tags: '{tags}')"
|
| 85 |
+
|
| 86 |
+
elif action == 'retrieve':
|
| 87 |
+
# Recupera i ricordi in base ai criteri di ricerca
|
| 88 |
+
query = f"SELECT id, memory_text, tags FROM {TABLE_NAME} WHERE 1=1"
|
| 89 |
+
params = []
|
| 90 |
+
|
| 91 |
+
if content:
|
| 92 |
+
query += " AND memory_text LIKE ?"
|
| 93 |
+
params.append(f"%{content}%")
|
| 94 |
+
if tags:
|
| 95 |
+
query += " AND tags LIKE ?"
|
| 96 |
+
params.append(f"%{tags}%")
|
| 97 |
+
|
| 98 |
+
cursor.execute(query, tuple(params))
|
| 99 |
+
rows = cursor.fetchall()
|
| 100 |
+
conn.close()
|
| 101 |
+
|
| 102 |
+
if not rows:
|
| 103 |
+
return "[RETRIEVE] Nessun ricordo trovato con i criteri specificati."
|
| 104 |
+
|
| 105 |
+
# Format del risultato
|
| 106 |
+
result = "[RETRIEVE] Ricordi trovati:\n"
|
| 107 |
+
for row in rows:
|
| 108 |
+
rec_id, memory_text, memory_tags = row
|
| 109 |
+
result += f"- ID: {rec_id}, Testo: '{memory_text}', Tags: '{memory_tags}'\n"
|
| 110 |
+
return result
|
| 111 |
+
|
| 112 |
+
elif action == 'delete':
|
| 113 |
+
# Elimina i ricordi in base ai criteri di ricerca
|
| 114 |
+
query = f"DELETE FROM {TABLE_NAME} WHERE 1=1"
|
| 115 |
+
params = []
|
| 116 |
+
|
| 117 |
+
if content:
|
| 118 |
+
query += " AND memory_text LIKE ?"
|
| 119 |
+
params.append(f"%{content}%")
|
| 120 |
+
if tags:
|
| 121 |
+
query += " AND tags LIKE ?"
|
| 122 |
+
params.append(f"%{tags}%")
|
| 123 |
+
|
| 124 |
+
cursor.execute(query, tuple(params))
|
| 125 |
+
deleted_count = cursor.rowcount
|
| 126 |
+
conn.commit()
|
| 127 |
+
conn.close()
|
| 128 |
+
|
| 129 |
+
return f"[DELETE] Rimossi {deleted_count} ricordo/i dal database."
|
| 130 |
+
|
| 131 |
+
elif action == 'help':
|
| 132 |
+
conn.close()
|
| 133 |
+
return (
|
| 134 |
+
"Esempi di utilizzo:\n"
|
| 135 |
+
"1) robot_memory_tool('store', content='Ho visto un gatto', tags='animale, esterno')\n"
|
| 136 |
+
"2) robot_memory_tool('retrieve', content='gatto')\n"
|
| 137 |
+
"3) robot_memory_tool('delete', tags='animale')\n"
|
| 138 |
+
"4) robot_memory_tool('help')"
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
else:
|
| 142 |
+
conn.close()
|
| 143 |
+
return "[ERROR] Azione non supportata. Usa 'store', 'retrieve', 'delete', oppure 'help'."
|
| 144 |
|
| 145 |
@tool
|
| 146 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 178 |
|
| 179 |
agent = CodeAgent(
|
| 180 |
model=model,
|
| 181 |
+
tools=[final_answer, robot_memory_tool, image_generation_tool], ## add your tools here (don't remove final answer)
|
| 182 |
max_steps=6,
|
| 183 |
verbosity_level=1,
|
| 184 |
grammar=None,
|