Spaces:
Paused
Paused
ehl0wr0ld Rafael Uzarowski commited on
feat: Deduplicate and optimize memory similarity threshold (#389)
Browse filesCo-authored-by: Rafael Uzarowski <uzarowski.rafael@proton.me>
python/tools/knowledge_tool.py
CHANGED
|
@@ -5,8 +5,11 @@ from python.helpers.tool import Tool, Response
|
|
| 5 |
from python.helpers.print_style import PrintStyle
|
| 6 |
from python.helpers.errors import handle_error
|
| 7 |
from python.helpers.searxng import search as searxng
|
|
|
|
| 8 |
|
| 9 |
SEARCH_ENGINE_RESULTS = 10
|
|
|
|
|
|
|
| 10 |
class Knowledge(Tool):
|
| 11 |
async def execute(self, question="", **kwargs):
|
| 12 |
# Create tasks for all three search methods
|
|
@@ -66,7 +69,7 @@ class Knowledge(Tool):
|
|
| 66 |
async def mem_search(self, question: str):
|
| 67 |
db = await memory.Memory.get(self.agent)
|
| 68 |
docs = await db.search_similarity_threshold(
|
| 69 |
-
query=question, limit=5, threshold=
|
| 70 |
)
|
| 71 |
text = memory.Memory.format_docs_plain(docs)
|
| 72 |
return "\n\n".join(text)
|
|
|
|
| 5 |
from python.helpers.print_style import PrintStyle
|
| 6 |
from python.helpers.errors import handle_error
|
| 7 |
from python.helpers.searxng import search as searxng
|
| 8 |
+
from python.tools.memory_load import DEFAULT_THRESHOLD as DEFAULT_MEMORY_THRESHOLD
|
| 9 |
|
| 10 |
SEARCH_ENGINE_RESULTS = 10
|
| 11 |
+
|
| 12 |
+
|
| 13 |
class Knowledge(Tool):
|
| 14 |
async def execute(self, question="", **kwargs):
|
| 15 |
# Create tasks for all three search methods
|
|
|
|
| 69 |
async def mem_search(self, question: str):
|
| 70 |
db = await memory.Memory.get(self.agent)
|
| 71 |
docs = await db.search_similarity_threshold(
|
| 72 |
+
query=question, limit=5, threshold=DEFAULT_MEMORY_THRESHOLD
|
| 73 |
)
|
| 74 |
text = memory.Memory.format_docs_plain(docs)
|
| 75 |
return "\n\n".join(text)
|
python/tools/memory_delete.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
from python.helpers.memory import Memory
|
| 2 |
from python.helpers.tool import Tool, Response
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
async def execute(self, ids="", **kwargs):
|
| 7 |
db = await Memory.get(self.agent)
|
| 8 |
ids = [id.strip() for id in ids.split(",") if id.strip()]
|
| 9 |
dels = await db.delete_documents_by_ids(ids=ids)
|
| 10 |
|
| 11 |
-
result =
|
| 12 |
-
return Response(message=result, break_loop=False)
|
|
|
|
| 1 |
from python.helpers.memory import Memory
|
| 2 |
from python.helpers.tool import Tool, Response
|
| 3 |
|
| 4 |
+
|
| 5 |
+
class MemoryDelete(Tool):
|
| 6 |
|
| 7 |
async def execute(self, ids="", **kwargs):
|
| 8 |
db = await Memory.get(self.agent)
|
| 9 |
ids = [id.strip() for id in ids.split(",") if id.strip()]
|
| 10 |
dels = await db.delete_documents_by_ids(ids=ids)
|
| 11 |
|
| 12 |
+
result = self.agent.read_prompt("fw.memories_deleted.md", memory_count=len(dels))
|
| 13 |
+
return Response(message=result, break_loop=False)
|
python/tools/memory_forget.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from python.helpers.memory import Memory
|
| 2 |
from python.helpers.tool import Tool, Response
|
|
|
|
| 3 |
|
| 4 |
-
DEFAULT_THRESHOLD = 0.75
|
| 5 |
|
| 6 |
class MemoryForget(Tool):
|
| 7 |
|
|
@@ -9,5 +9,5 @@ class MemoryForget(Tool):
|
|
| 9 |
db = await Memory.get(self.agent)
|
| 10 |
dels = await db.delete_documents_by_query(query=query, threshold=threshold, filter=filter)
|
| 11 |
|
| 12 |
-
result =
|
| 13 |
-
return Response(message=result, break_loop=False)
|
|
|
|
| 1 |
from python.helpers.memory import Memory
|
| 2 |
from python.helpers.tool import Tool, Response
|
| 3 |
+
from python.tools.memory_load import DEFAULT_THRESHOLD
|
| 4 |
|
|
|
|
| 5 |
|
| 6 |
class MemoryForget(Tool):
|
| 7 |
|
|
|
|
| 9 |
db = await Memory.get(self.agent)
|
| 10 |
dels = await db.delete_documents_by_query(query=query, threshold=threshold, filter=filter)
|
| 11 |
|
| 12 |
+
result = self.agent.read_prompt("fw.memories_deleted.md", memory_count=len(dels))
|
| 13 |
+
return Response(message=result, break_loop=False)
|
python/tools/memory_load.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
from python.helpers.memory import Memory
|
| 2 |
from python.helpers.tool import Tool, Response
|
| 3 |
|
| 4 |
-
DEFAULT_THRESHOLD = 0.
|
| 5 |
DEFAULT_LIMIT = 10
|
| 6 |
|
|
|
|
| 7 |
class MemoryLoad(Tool):
|
| 8 |
|
| 9 |
async def execute(self, query="", threshold=DEFAULT_THRESHOLD, limit=DEFAULT_LIMIT, filter="", **kwargs):
|
|
@@ -16,4 +17,4 @@ class MemoryLoad(Tool):
|
|
| 16 |
text = "\n\n".join(Memory.format_docs_plain(docs))
|
| 17 |
result = str(text)
|
| 18 |
|
| 19 |
-
return Response(message=result, break_loop=False)
|
|
|
|
| 1 |
from python.helpers.memory import Memory
|
| 2 |
from python.helpers.tool import Tool, Response
|
| 3 |
|
| 4 |
+
DEFAULT_THRESHOLD = 0.7
|
| 5 |
DEFAULT_LIMIT = 10
|
| 6 |
|
| 7 |
+
|
| 8 |
class MemoryLoad(Tool):
|
| 9 |
|
| 10 |
async def execute(self, query="", threshold=DEFAULT_THRESHOLD, limit=DEFAULT_LIMIT, filter="", **kwargs):
|
|
|
|
| 17 |
text = "\n\n".join(Memory.format_docs_plain(docs))
|
| 18 |
result = str(text)
|
| 19 |
|
| 20 |
+
return Response(message=result, break_loop=False)
|
python/tools/memory_save.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
from python.helpers.memory import Memory
|
| 2 |
from python.helpers.tool import Tool, Response
|
| 3 |
|
| 4 |
-
DEFAULT_THRESHOLD = 0.5
|
| 5 |
-
DEFAULT_LIMIT = 5
|
| 6 |
|
| 7 |
class MemorySave(Tool):
|
| 8 |
|
|
|
|
| 1 |
from python.helpers.memory import Memory
|
| 2 |
from python.helpers.tool import Tool, Response
|
| 3 |
|
|
|
|
|
|
|
| 4 |
|
| 5 |
class MemorySave(Tool):
|
| 6 |
|