Agent Zero AI framework
memory tool update, minor cleanup, utility models
frdelb3adcc7bca8e
diff --git a/agent.py b/agent.pyindex 1563ecfed..a2ff200d0 100644--- a/agent.py+++ b/agent.py@@ -1,7 +1,6 @@from dataclasses import dataclass, fieldimport time, importlib, inspect, os, json-import traceback-from typing import Any, Optional, Dict, TypedDict+from typing import Any, Optional, Dictfrom python.helpers import extract_tools, rate_limiter, files, errorsfrom python.helpers.print_style import PrintStylefrom langchain.schema import AIMessage@@ -9,20 +8,20 @@ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholderfrom langchain_core.messages import HumanMessage, SystemMessagefrom langchain_core.language_models.chat_models import BaseChatModelfrom langchain_core.embeddings import Embeddings-from python.helpers.rate_limiter import RateLimiter# rate_limit = rate_limiter.rate_limiter(30,160000) #TODO! implement properly@dataclassclass AgentConfig:chat_model:BaseChatModel+ utility_model: BaseChatModelembeddings_model:Embeddingsmemory_subdir: str = ""auto_memory_count: int = 3auto_memory_skip: int = 2rate_limit_seconds: int = 60- rate_limit_requests: int = 30- rate_limit_input_tokens: int = 0+ rate_limit_requests: int = 15+ rate_limit_input_tokens: int = 1000000rate_limit_output_tokens: int = 0msgs_keep_max: int = 25msgs_keep_start: int = 5@@ -62,7 +61,7 @@ class Agent:self.last_message = ""self.intervention_message = ""self.intervention_status = False- self.rate_limiter = RateLimiter(max_calls=self.config.rate_limit_requests,max_input_tokens=self.config.rate_limit_input_tokens,max_output_tokens=self.config.rate_limit_output_tokens,window_seconds=self.config.rate_limit_seconds)+ self.rate_limiter = rate_limiter.RateLimiter(max_calls=self.config.rate_limit_requests,max_input_tokens=self.config.rate_limit_input_tokens,max_output_tokens=self.config.rate_limit_output_tokens,window_seconds=self.config.rate_limit_seconds)self.data = {} # free data object all the tools can useos.chdir(files.get_abs_path("./work_dir")) #change CWD to work_dir@@ -160,7 +159,7 @@ class Agent:SystemMessage(content=system),HumanMessage(content=msg)])- chain = prompt | self.config.chat_model+ chain = prompt | self.config.utility_modelresponse = ""printer = Nonediff --git a/example.env b/example.envindex 2aa725749..342a8765b 100644--- a/example.env+++ b/example.env@@ -2,7 +2,7 @@ API_KEY_OPENAI=API_KEY_ANTHROPIC=API_KEY_GROQ=API_KEY_PERPLEXITY=-+API_KEY_GOOGLE=TOKENIZERS_PARALLELISM=truePYDEVD_DISABLE_FILE_VALIDATION=1\ No newline at end of filediff --git a/main.py b/main.pyindex d7527441d..a57269806 100644--- a/main.py+++ b/main.py@@ -1,8 +1,5 @@import signal-import sys-import termiosimport threading, time, models, os-import ttyfrom ansio import application_keypad, mouse_input, raw_inputfrom ansio.input import InputEvent, get_input_eventfrom agent import Agent, AgentConfig@@ -33,6 +30,9 @@ def initialize():# chat_llm = models.get_ollama(model_name="qwen:14b")chat_llm = models.get_google_chat()+ utility_llm = models.get_anthropic_haiku(temperature=0)++# embedding model used for memory# embedding_llm = models.get_embedding_openai()embedding_llm = models.get_embedding_hf()@@ -40,6 +40,7 @@ def initialize():# agent configurationconfig = AgentConfig(chat_model = chat_llm,+ utility_model = utility_llm,embeddings_model = embedding_llm,# memory_subdir = "",auto_memory_count = 0,@@ -81,13 +82,13 @@ def chat(agent:Agent):with input_lock:timeout = agent.get_data("timeout") # how long the agent is willing to waitif not timeout: # if agent wants to wait for user input forever- PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ('exit' to leave):")+ PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ('e' to leave):")import readline # this fixes arrow keys in terminaluser_input = input("> ")PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")else: # otherwise wait for user input with a timeout- PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ({timeout}s timeout, 'wait' to wait, 'exit' to leave):")+ PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ({timeout}s timeout, 'w' to wait, 'e' to leave):")import readline # this fixes arrow keys in terminal# user_input = timed_input("> ", timeout=timeout)user_input = timeout_input("> ", timeout=timeout)@@ -97,14 +98,14 @@ def chat(agent:Agent):PrintStyle(font_color="white", padding=False).stream(f"{user_input}")else:user_input = user_input.strip()- if user_input.lower()=="wait": # the user needs more time+ if user_input.lower()=="w": # the user needs more timeuser_input = input("> ").strip()PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")# exit the conversation when the user types 'exit'- if user_input.lower() == 'exit': break+ if user_input.lower() == 'e': break# send message to agent0,assistant_response = agent.message_loop(user_input)@@ -118,13 +119,13 @@ def chat(agent:Agent):def intervention():if Agent.streaming_agent and not Agent.paused:Agent.paused = True # stop agent streaming- PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User intervention ('exit' to leave, empty to continue):")+ PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User intervention ('e' to leave, empty to continue):")import readline # this fixes arrow keys in terminaluser_input = input("> ").strip()PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")- if user_input.lower() == 'exit': os._exit(0) # exit the conversation when the user types 'exit'+ if user_input.lower() == 'e': os._exit(0) # exit the conversation when the user types 'exit'if user_input: Agent.streaming_agent.intervention_message = user_input # set intervention message if non-emptyAgent.paused = False # continue agent streamingdiff --git a/models.py b/models.pyindex 321041c3a..22fbe4b09 100755--- a/models.py+++ b/models.py@@ -5,7 +5,7 @@ from langchain_openai import ChatOpenAI, OpenAI, OpenAIEmbeddingsfrom langchain_anthropic import ChatAnthropicfrom langchain_groq import ChatGroqfrom langchain_huggingface import HuggingFaceEmbeddings-from langchain_google_genai import ChatGoogleGenerativeAI+from langchain_google_genai import ChatGoogleGenerativeAI, HarmBlockThreshold, HarmCategory# Load environment variables@@ -76,15 +76,16 @@ def get_groq_gemma(api_key=None, temperature=DEFAULT_TEMPERATURE):api_key = api_key or get_api_key("groq")return ChatGroq(model_name="gemma-7b-it", temperature=temperature, api_key=api_key) # type: ignore-def get_ollama_dolphin(api_key=None, temperature=DEFAULT_TEMPERATURE):+def get_ollama_dolphin(temperature=DEFAULT_TEMPERATURE):return Ollama(model="dolphin-llama3:8b-256k-v2.9-fp16", temperature=temperature)-def get_ollama_phi(api_key=None, temperature=DEFAULT_TEMPERATURE):+def get_ollama_phi(temperature=DEFAULT_TEMPERATURE):return Ollama(model="phi3:3.8b-mini-instruct-4k-fp16",temperature=temperature)def get_google_chat(model_name="gemini-1.5-flash-latest", api_key=None, temperature=DEFAULT_TEMPERATURE):api_key = api_key or get_api_key("google")- return ChatGoogleGenerativeAI(model=model_name, temperature=temperature, google_api_key=api_key) # type: ignore+ return ChatGoogleGenerativeAI(model=model_name, temperature=temperature, google_api_key=api_key,+ safety_settings={HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE }) # type: ignoredef get_embedding_hf(model_name="sentence-transformers/all-MiniLM-L6-v2"):return HuggingFaceEmbeddings(model_name=model_name)diff --git a/prompts/agent.tools.md b/prompts/agent.tools.mdindex 3c4e0101b..f6e2042f9 100644--- a/prompts/agent.tools.md+++ b/prompts/agent.tools.md@@ -62,11 +62,28 @@ Always verify memory by online.}~~~-### memorize:-Save information to persistent memory.+### memory_tool:+Manage long term memories. Allowed arguments are "query", "memorize" and "forget".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 forgetting, provide memory IDs from loaded memories separated by commas in "forget" argument.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**:+1. load:+~~~json+{+ "thoughts": [+ "Let's search my memory for...",+ ],+ "tool_name": "memory_tool",+ "tool_args": {+ "query": "File compression library for...",+ "threshold": 0.1+ }+}+~~~+2. save:~~~json{"thoughts": [@@ -74,9 +91,21 @@ Provide a title, short summary and and all the necessary information to help you"Details of this process will be valuable...","Let's save tools and code used...",],- "tool_name": "memorize",+ "tool_name": "memory_tool","tool_args": {- "memory": "# How to...",+ "memorize": "# How to...",+ }+}+~~~+3. delete:+~~~json+{+ "thoughts": [+ "User asked to delete memories...",+ ],+ "tool_name": "memory_tool",+ "tool_args": {+ "forget": "32cd37ffd1-101f-4112-80e2-33b795548116, d1306e36-6a9c-4e6a-bfc3-c8335035dcf8 ...",}}~~~@@ -93,7 +122,8 @@ When tool outputs error, you need to change your code accordingly before tryingKeep in mind that current working directory CWD automatically resets before every tool call.IMPORTANT!: Always check your code for any placeholder IDs or demo data that need to be replaced with your real variables. Do not simply reuse code snippets from tutorials.Do not use in combination with other tools except for thoughts. Wait for response before using other tools.-**Example usage**:+**Example usages:**+1. Execute python code~~~json{"thoughts": [@@ -101,10 +131,52 @@ Do not use in combination with other tools except for thoughts. Wait for respons"I can use library...","Then I can...",],- "tool_name": "memory_tool",+ "tool_name": "code_execution_tool","tool_args": {"runtime": "python","code": "import os\nreturn os.getcwd()",}}+~~~++2. Execute terminal command+~~~json+{+ "thoughts": [+ "I need to do...",+ "I need to install...",+ ],+ "tool_name": "code_execution_tool",+ "tool_args": {+ "runtime": "terminal",+ "code": "apt-get install zip",+ }+}+~~~++2. 1. Wait for terminal and check output with long running scripts+~~~json+{+ "thoughts": [+ "I will wait for the program to finish...",+ ],+ "tool_name": "code_execution_tool",+ "tool_args": {+ "runtime": "output",+ }+}+~~~++2. 2. Answer terminal dialog+~~~json+{+ "thoughts": [+ "Program needs confirmation...",+ ],+ "tool_name": "code_execution_tool",+ "tool_args": {+ "runtime": "terminal",+ "code": "Y",+ }+}~~~\ No newline at end of filediff --git a/python/helpers/vdb.py b/python/helpers/vdb.pynew file mode 100644index 000000000..325112091--- /dev/null+++ b/python/helpers/vdb.py@@ -0,0 +1,69 @@+from langchain.storage import InMemoryByteStore, LocalFileStore+from langchain.embeddings import CacheBackedEmbeddings+from langchain_core.embeddings import Embeddings++from langchain_chroma import Chroma+import chromadb+from chromadb.config import Settings++from . import files+from langchain_core.documents import Document+import uuid+++class VectorDB:++ def __init__(self, embeddings_model:Embeddings, in_memory=False, cache_dir="./cache"):+ print("Initializing VectorDB...")+ self.embeddings_model = embeddings_model++ db_cache = files.get_abs_path(cache_dir,"database")++ self.client =chromadb.PersistentClient(path=db_cache)+ self.collection = self.client.create_collection("my_collection")+ self.collection+++ def search(self, query:str, results=2):+ emb = self.embeddings_model.embed_query(query)+ res = self.collection.query(query_embeddings=[emb],n_results=results)+ best = res["documents"][0][0] # type: ignore++ # def delete_documents(self, query):+ # score_limit = 1+ # k = 2+ # tot = 0+ # while True:+ # # Perform similarity search with score+ # docs = self.db.similarity_search_with_score(query, k=k)++ # # Extract document IDs and filter based on score+ # document_ids = [result[0].metadata["id"] for result in docs if result[1] < score_limit]++ # # 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"])++ # # If fewer than K document IDs, break the loop+ # if len(document_ids) < k:+ # break++ # return tot++ def insert(self, data:str):++ id = str(uuid.uuid4())+ emb = self.embeddings_model.embed_documents([data])[0]++ self.collection.add(+ ids=[id],+ embeddings=[emb],+ documents=[data],+ )++ return id+++diff --git a/python/helpers/vector_db.py b/python/helpers/vector_db.pyindex 2bcc6ae91..4ffdb376b 100644--- a/python/helpers/vector_db.py+++ b/python/helpers/vector_db.py@@ -1,6 +1,7 @@from langchain.storage import InMemoryByteStore, LocalFileStorefrom langchain.embeddings import CacheBackedEmbeddingsfrom langchain_chroma import Chroma+from . import filesfrom langchain_core.documents import Documentimport uuid@@ -29,8 +30,12 @@ class VectorDB:self.db = Chroma(embedding_function=self.embedder,persist_directory=db_cache)+def search_similarity(self, query, results=3):return self.db.similarity_search(query,results)++ def search_similarity_threshold(self, query, results=3, threshold=0.5):+ return self.db.search(query,search_type="similarity_score_threshold",score_threshold=threshold)def search_max_rel(self, query, results=3):return self.db.max_marginal_relevance_search(query,results)@@ -60,7 +65,8 @@ class VectorDB:def insert_document(self, data):id = str(uuid.uuid4())- self.db.add_documents(documents=[ Document(data, metadata={"id": id}) ])+ self.db.add_documents(documents=[ Document(data, metadata={"id": id}) ], ids=[id])+return iddiff --git a/python/tools/code_execution_tool.py b/python/tools/code_execution_tool.pyindex c64dc6bd3..d9c2cdc80 100644--- a/python/tools/code_execution_tool.py+++ b/python/tools/code_execution_tool.py@@ -32,6 +32,8 @@ class CodeExecution(Tool):response = self.execute_nodejs_code(self.args["code"])elif runtime == "terminal":response = self.execute_terminal_command(self.args["code"])+ elif runtime == "output":+ response = self.get_terminal_output()else:response = files.read_file("./prompts/fw.code_runtime_wrong.md", runtime=runtime)@@ -78,7 +80,9 @@ class CodeExecution(Tool):self.state.shell.send_command(command)PrintStyle(background_color="white",font_color="#85C1E9",bold=True).print(f"{self.agent.agent_name} code execution output:")+ return self.get_terminal_output()+ def get_terminal_output(self):idle=0while True:time.sleep(0.1) # Wait for some output to be generateddiff --git a/python/tools/memorize.py b/python/tools/memorize.pydeleted file mode 100644index bdef2032c..000000000--- a/python/tools/memorize.py+++ /dev/null@@ -1,14 +0,0 @@-from agent import Agent-from python.helpers import files-from python.helpers.tool import Tool, Response-import memory_tool--class Memorize(Tool):- def execute(self,**kwargs):-- memory_tool.process_query(self.agent, str(self.args), "save")-- return Response(- message=files.read_file("prompts/fw.memorized.md"),- break_loop=False,- )\ No newline at end of filediff --git a/python/tools/memory_tool.py b/python/tools/memory_tool.pyindex 31eb1ec7f..2abdf81d1 100644--- a/python/tools/memory_tool.py+++ b/python/tools/memory_tool.py@@ -9,7 +9,18 @@ db: VectorDB | None = Noneclass Memory(Tool):def execute(self,**kwargs):- result = process_query(self.agent, self.args["memory"],self.args["action"], result_count=self.agent.config.auto_memory_count)+ result=[]++ if "query" in kwargs:+ if "threshold" in kwargs: threshold = float(kwargs["threshold"])+ else: threshold = 0.1+ result = process_query(self.agent, kwargs["query"], action="load", threshold=threshold, result_count=3)+ elif "memorize" in kwargs:+ result = process_query(self.agent, kwargs["memorize"], action="save")+ elif "forget" in kwargs:+ result = process_query(self.agent, kwargs["forget"], action="delete")++ # result = process_query(self.agent, self.args["memory"],self.args["action"], result_count=self.agent.config.auto_memory_count)return Response(message="\n\n".join(result), break_loop=False)@@ -19,21 +30,21 @@ def initialize(embeddings_model, subdir=""):db = VectorDB(embeddings_model=embeddings_model, in_memory=False, cache_dir=dir)-def process_query(agent:Agent, message: str, action: str = "load", result_count: int = 3, **kwargs):+def process_query(agent:Agent, message: str, action: str = "load", result_count: int = 3, threshold: float = 0.1, **kwargs):if not db: initialize(agent.config.embeddings_model, subdir=agent.config.memory_subdir)if action.strip().lower() == "save":id = db.insert_document(str(message)) # type: ignore- return files.read_file("./prompts/fw.memory_saved.md")+ return [files.read_file("./prompts/fw.memory_saved.md")]elif action.strip().lower() == "delete":deleted = db.delete_documents(message) # type: ignore- return files.read_file("./prompts/fw.memories_deleted.md", count=deleted)+ return [files.read_file("./prompts/fw.memories_deleted.md", count=deleted)]else:results=[]- docs = db.search_max_rel(message,result_count) # type: ignore- if len(docs)==0: return files.read_file("./prompts/fw.memories_not_found.md", query=message)+ docs = db.search_similarity_threshold(message,result_count,threshold) # type: ignore+ if len(docs)==0: return [files.read_file("./prompts/fw.memories_not_found.md", query=message)]for doc in docs:results.append(doc.page_content)return resultsdiff --git a/requirements.txt b/requirements.txtindex f27283000..44fced019 100644--- a/requirements.txt+++ b/requirements.txt@@ -1,14 +1,14 @@ansio==0.0.1python-dotenv==1.0.1-langchain-groq==0.1.5+langchain-groq==0.1.6langchain-huggingface==0.0.3-langchain-openai==0.1.8-langchain-community==0.2.4-langchain-anthropic==0.1.15-langchain-chroma==0.1.1-google-generativeai==0.5.4+langchain-openai==0.1.15+langchain-community==0.2.7+langchain-anthropic==0.1.19+langchain-chroma==0.1.2+langchain-google-genai==1.0.7webcolors==24.6.0sentence-transformers==3.0.1pytimedinput==2.0.1docker==7.1.0-paramiko==3.4.0+paramiko==3.4.0\ No newline at end of file