Skip to content

Agent Zero AI framework

memory fix

frdel947aa9bbffd6
diff --git a/agent.py b/agent.py
index 752104b8c..3b011eb92 100644
--- a/agent.py
+++ b/agent.py
@@ -12,6 +12,38 @@ from tools.helpers.rate_limiter import RateLimiter
# rate_limit = rate_limiter.rate_limiter(30,160000) #TODO! implement properly
+<<<<<<< Updated upstream
+=======
+@dataclass
+class AgentConfig:
+ chat_model:BaseChatModel
+ utility_model: BaseChatModel
+ embeddings_model:Embeddings
+ memory_subdir: str = ""
+ auto_memory_count: int = 3
+ auto_memory_skip: int = 2
+ rate_limit_seconds: int = 60
+ rate_limit_requests: int = 15
+ rate_limit_input_tokens: int = 1000000
+ rate_limit_output_tokens: int = 0
+ msgs_keep_max: int = 25
+ msgs_keep_start: int = 5
+ msgs_keep_end: int = 10
+ response_timeout_seconds: int = 60
+ max_tool_response_length: int = 3000
+ code_exec_docker_enabled: bool = True
+ code_exec_docker_name: str = "agent-zero-exe"
+ code_exec_docker_image: str = "frdel/agent-zero-exe:latest"
+ code_exec_docker_ports: dict[str,int] = field(default_factory=lambda: {"22/tcp": 50022})
+ code_exec_docker_volumes: dict[str, dict[str, str]] = field(default_factory=lambda: {files.get_abs_path("work_dir"): {"bind": "/root", "mode": "rw"}})
+ code_exec_ssh_enabled: bool = True
+ code_exec_ssh_addr: str = "localhost"
+ code_exec_ssh_port: int = 50022
+ code_exec_ssh_user: str = "root"
+ code_exec_ssh_pass: str = "toor"
+ additional: Dict[str, Any] = field(default_factory=dict)
+
+>>>>>>> Stashed changes
class Agent:
@@ -276,7 +308,7 @@ class Agent:
self.memory_skip_counter = self.auto_memory_skip
from tools import memory_tool
messages = self.concat_messages(self.history)
- memories = memory_tool.process_query(self,messages,"load")
+ memories = memory_tool.search(messages)
input = {
"conversation_history" : messages,
"raw_memories": memories
diff --git a/docker/Dockerfile b/docker/Dockerfile
new file mode 100644
index 000000000..f4d73c220
--- /dev/null
+++ b/docker/Dockerfile
@@ -0,0 +1,43 @@
+# Use the latest slim version of Debian
+FROM --platform=$TARGETPLATFORM debian:testing-slim
+
+# Set ARG for platform-specific commands
+ARG TARGETPLATFORM
+
+# Update and install necessary packages
+RUN apt-get update && apt-get install -y \
+ python3 \
+ python3-pip \
+ python3-venv \
+ nodejs \
+ npm \
+ openssh-server \
+ sudo \
+ && rm -rf /var/lib/apt/lists/*
+
+# Set up SSH
+RUN mkdir /var/run/sshd && \
+ echo 'root:toor' | chpasswd && \
+ sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
+
+# Create and activate Python virtual environment
+ENV VIRTUAL_ENV=/opt/venv
+RUN python3 -m venv $VIRTUAL_ENV
+
+# Copy initial .bashrc with virtual environment activation to a temporary location
+COPY .bashrc /etc/skel/.bashrc
+
+# Copy the script to ensure .bashrc is in the root directory
+COPY initialize.sh /usr/local/bin/initialize.sh
+RUN chmod +x /usr/local/bin/initialize.sh
+
+# Ensure the virtual environment and pip setup
+RUN $VIRTUAL_ENV/bin/pip install --upgrade pip
+
+# Expose SSH port
+EXPOSE 22
+
+# Init .bashrc
+CMD ["/usr/local/bin/initialize.sh"]
+
+
diff --git a/main.py b/main.py
index fe3d59b96..b32349a8d 100644
--- a/main.py
+++ b/main.py
@@ -28,8 +28,43 @@ def chat():
# chat_llm = models.get_ollama_dolphin()
# embedding model used for memory
+<<<<<<< Updated upstream
# embedding_llm = models.get_embedding_openai()
embedding_llm = models.get_embedding_hf()
+=======
+ embedding_llm = models.get_embedding_openai()
+ # embedding_llm = models.get_embedding_hf()
+
+ # agent configuration
+ config = AgentConfig(
+ chat_model = chat_llm,
+ utility_model = utility_llm,
+ embeddings_model = embedding_llm,
+ # memory_subdir = "",
+ auto_memory_count = 0,
+ # auto_memory_skip = 2,
+ # rate_limit_seconds = 60,
+ # rate_limit_requests = 30,
+ # rate_limit_input_tokens = 0,
+ # rate_limit_output_tokens = 0,
+ # msgs_keep_max = 25,
+ # msgs_keep_start = 5,
+ # msgs_keep_end = 10,
+ # response_timeout_seconds = 60,
+ # max_tool_response_length = 3000,
+ code_exec_docker_enabled = True,
+ # code_exec_docker_name = "agent-zero-exe",
+ # code_exec_docker_image = "frdel/agent-zero-exe:latest",
+ # code_exec_docker_ports = { "22/tcp": 50022 }
+ # code_exec_docker_volumes = { files.get_abs_path("work_dir"): {"bind": "/root", "mode": "rw"} }
+ code_exec_ssh_enabled = True,
+ # code_exec_ssh_addr = "localhost",
+ # code_exec_ssh_port = 50022,
+ # code_exec_ssh_user = "root",
+ # code_exec_ssh_pass = "toor",
+ # additional = {},
+ )
+>>>>>>> Stashed changes
# create the first agent
agent0 = Agent( agent_number=0,
diff --git a/prompts/agent.tools.md b/prompts/agent.tools.md
index 2314f1fb5..1a9e08b88 100644
--- a/prompts/agent.tools.md
+++ b/prompts/agent.tools.md
@@ -62,9 +62,19 @@ Always verify memory by online.
}
~~~
+<<<<<<< Updated upstream
### memorize:
Save information to persistent memory.
Memories can help you remember important details and later reuse them.
+=======
+### memory_tool:
+Manage long term memories. Allowed arguments are "query", "memorize", "forget" and "delete".
+Memories can help you remember important details and later reuse them.
+When querying, provide a "query" argument to search for. You will retrieve IDs and contents of relevant memories. Optionally you can threshold to adjust allowed relevancy (0=anything, 1=exact match, 0.1 is default).
+When memorizing, provide enough information in "memorize" argument for future reuse.
+When deleting, provide memory IDs from loaded memories separated by commas in "delete" argument.
+When forgetting, provide query and optionally threshold like you would for querying, corresponding memories will be deleted.
+>>>>>>> Stashed changes
Provide a title, short summary and and all the necessary information to help you later solve similiar tasks including details like code executed, libraries used etc.
**Example usages**:
~~~json
@@ -76,7 +86,35 @@ Provide a title, short summary and and all the necessary information to help you
],
"tool_name": "memorize",
"tool_args": {
+<<<<<<< Updated upstream
"memory": "# How to...",
+=======
+ "memorize": "# How to...",
+ }
+}
+~~~
+3. delete:
+~~~json
+{
+ "thoughts": [
+ "User asked to delete specific memories...",
+ ],
+ "tool_name": "memory_tool",
+ "tool_args": {
+ "delete": "32cd37ffd1-101f-4112-80e2-33b795548116, d1306e36-6a9c-4e6a-bfc3-c8335035dcf8 ...",
+ }
+}
+~~~
+4. forget:
+~~~json
+{
+ "thoughts": [
+ "User asked to delete information from memory...",
+ ],
+ "tool_name": "memory_tool",
+ "tool_args": {
+ "forget": "User's contact information",
+>>>>>>> Stashed changes
}
}
~~~
diff --git a/prompts/fw.memories_deleted.md b/prompts/fw.memories_deleted.md
index fb7c38028..077861463 100644
--- a/prompts/fw.memories_deleted.md
+++ b/prompts/fw.memories_deleted.md
@@ -1 +1 @@
-Memories deleted: {{memories}}
\ No newline at end of file
+Memories deleted: {{memory_count}}
\ No newline at end of file
diff --git a/prompts/fw.memory_saved.md b/prompts/fw.memory_saved.md
index efcd3fc84..caa75f31b 100644
--- a/prompts/fw.memory_saved.md
+++ b/prompts/fw.memory_saved.md
@@ -1 +1 @@
-Memory has been saved.
\ No newline at end of file
+Memory has been saved with id {{memory_id}}.
\ No newline at end of file
diff --git a/python/tools/memory_tool.py b/python/tools/memory_tool.py
new file mode 100644
index 000000000..cc4856680
--- /dev/null
+++ b/python/tools/memory_tool.py
@@ -0,0 +1,62 @@
+import re
+from agent import Agent
+from python.helpers.vector_db import VectorDB, Document
+from python.helpers import files
+import os, json
+from python.helpers.tool import Tool, Response
+from python.helpers.print_style import PrintStyle
+
+# TODO multiple DBs at once
+db: VectorDB | None= None
+
+class Memory(Tool):
+ def execute(self,**kwargs):
+ result=""
+
+ if "query" in kwargs:
+ if "threshold" in kwargs: threshold = float(kwargs["threshold"])
+ else: threshold = 0.1
+ if "count" in kwargs: count = int(kwargs["count"])
+ else: count = 5
+ result = search(self.agent, kwargs["query"], count, threshold)
+ elif "memorize" in kwargs:
+ result = save(self.agent, kwargs["memorize"])
+ elif "forget" in kwargs:
+ result = forget(self.agent, kwargs["forget"])
+ elif "delete" in kwargs:
+ result = delete(self.agent, kwargs["delete"])
+
+ # result = process_query(self.agent, self.args["memory"],self.args["action"], result_count=self.agent.config.auto_memory_count)
+ return Response(message=result, break_loop=False)
+
+def search(agent:Agent, query:str, count:int=5, threshold:float=0.1):
+ initialize(agent)
+ docs = db.search_similarity_threshold(query,count,threshold) # type: ignore
+ if len(docs)==0: return files.read_file("./prompts/fw.memories_not_found.md", query=query)
+ else: return str(docs)
+
+def save(agent:Agent, text:str):
+ initialize(agent)
+ id = db.insert_document(text) # type: ignore
+ return files.read_file("./prompts/fw.memory_saved.md", memory_id=id)
+
+def delete(agent:Agent, ids_str:str):
+ initialize(agent)
+ ids = extract_guids(ids_str)
+ deleted = db.delete_documents_by_ids(ids) # type: ignore
+ return files.read_file("./prompts/fw.memories_deleted.md", memory_count=deleted)
+
+def forget(agent:Agent, query:str):
+ initialize(agent)
+ deleted = db.delete_documents_by_query(query) # type: ignore
+ return files.read_file("./prompts/fw.memories_deleted.md", memory_count=deleted)
+
+def initialize(agent:Agent):
+ global db
+ if not db:
+ dir = os.path.join("memory",agent.config.memory_subdir)
+ db = VectorDB(embeddings_model=agent.config.embeddings_model, in_memory=False, cache_dir=dir)
+
+def extract_guids(text):
+ pattern = r'\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}\b'
+ return re.findall(pattern, text)
\ No newline at end of file
diff --git a/tools/helpers/vector_db.py b/tools/helpers/vector_db.py
index 2bcc6ae91..8c6a0edf3 100644
--- a/tools/helpers/vector_db.py
+++ b/tools/helpers/vector_db.py
@@ -35,29 +35,39 @@ class VectorDB:
def search_max_rel(self, query, results=3):
return self.db.max_marginal_relevance_search(query,results)
- def delete_documents(self, query):
- score_limit = 1
- k = 2
+ def delete_documents_by_query(self, query:str, threshold=0.1):
+ k = 100
tot = 0
while True:
# Perform similarity search with score
- docs = self.db.similarity_search_with_score(query, k=k)
+ docs = self.search_similarity_threshold(query, results=k, threshold=threshold)
# Extract document IDs and filter based on score
- document_ids = [result[0].metadata["id"] for result in docs if result[1] < score_limit]
+ # document_ids = [result[0].metadata["id"] for result in docs if result[1] < score_limit]
+ document_ids = [result.metadata["id"] for result in docs]
+
# Delete documents with IDs over the threshold score
if document_ids:
- fnd = self.db.get(where={"id": {"$in": document_ids}})
- if fnd["ids"]: self.db.delete(ids=fnd["ids"])
- tot += len(fnd["ids"])
-
+ # fnd = self.db.get(where={"id": {"$in": document_ids}})
+ # if fnd["ids"]: self.db.delete(ids=fnd["ids"])
+ # tot += len(fnd["ids"])
+ self.db.delete(ids=document_ids)
+ tot += len(document_ids)
+
# If fewer than K document IDs, break the loop
if len(document_ids) < k:
break
return tot
+ def delete_documents_by_ids(self, ids:list[str]):
+ # pre = self.db.get(ids=ids)["ids"]
+ self.db.delete(ids=ids)
+ # post = self.db.get(ids=ids)["ids"]
+ #TODO? compare pre and post
+ return len(ids)
+
def insert_document(self, data):
id = str(uuid.uuid4())
self.db.add_documents(documents=[ Document(data, metadata={"id": id}) ])
diff --git a/tools/knowledge_tool.py b/tools/knowledge_tool.py
index 062d159ee..d076784ea 100644
--- a/tools/knowledge_tool.py
+++ b/tools/knowledge_tool.py
@@ -13,7 +13,7 @@ class Knowledge(Tool):
with concurrent.futures.ThreadPoolExecutor() as executor:
# Schedule the two functions to be run in parallel
future_online = executor.submit(online_knowledge_tool.process_question, question)
- future_memory = executor.submit(memory_tool.process_query, self.agent, question)
+ future_memory = executor.submit(memory_tool.search, self.agent, question)
# Wait for both functions to complete
online_result = future_online.result()
diff --git a/tools/response.py b/tools/response.py
index e5325bc72..02848b00a 100644
--- a/tools/response.py
+++ b/tools/response.py
@@ -10,11 +10,8 @@ from tools.helpers.print_style import PrintStyle
class ResponseTool(Tool):
def execute(self,**kwargs):
- # superior = self.agent.get_data("superior")
- # if superior:
- self.agent.set_data("timeout", 60)
+ self.agent.set_data("timeout", self.agent.config.response_timeout_seconds)
return Response(message=self.args["text"], break_loop=True)
- # else:
def after_execution(self, response, **kwargs):
pass # do add anything to the history or output
\ No newline at end of file