Agent Zero AI framework
v0.7 testing
frdele209a0c213fc
diff --git a/agent.py b/agent.pyindex 5e4da261a..137f16198 100644--- a/agent.py+++ b/agent.py@@ -1,7 +1,7 @@import asynciofrom dataclasses import dataclass, fieldimport time, importlib, inspect, os, json-from typing import Any, Optional, Dict+from typing import Any, Optional, Dict, TypedDictimport uuidfrom python.helpers import extract_tools, rate_limiter, files, errorsfrom python.helpers.print_style import PrintStyle@@ -122,6 +122,15 @@ class AgentConfig:additional: Dict[str, Any] = field(default_factory=dict)+class LoopData:+ def __init__(self):+ self.iteration = -1+ self.system = []+ self.message = ""+ self.history_from = 0+ self.history = []++# intervention exception class - skips rest of message loop iterationclass InterventionException(Exception):pass@@ -162,40 +171,35 @@ class Agent:async def monologue(self, msg: str):try:-# loop data dictionary to pass to extensions- loop_data: dict[str, Any] = {- "message": msg,- "iteration": -1,- "history_from": len(self.history),- }+ loop_data = LoopData()+ loop_data.message = msg+ loop_data.history_from = len(self.history)# call monologue_start extensionsawait self.call_extensions("monologue_start", loop_data=loop_data)printer = PrintStyle(italic=True, font_color="#b3ffd9", padding=False)- user_message = self.read_prompt("fw.user_message.md", message=msg)+ user_message = self.read_prompt("fw.user_message.md", message=loop_data.message)await self.append_message(user_message, human=True)- await self.call_extensions(- "monologue_start", loop_data=loop_data- ) # call monologue_end extensions-+# let the agent run message loop until he stops it with a response toolwhile True:self.context.streaming_agent = self # mark self as current streameragent_response = ""- loop_data["iteration"] += 1+ loop_data.iteration += 1try:# set system prompt and message history- loop_data["system"] = [- self.read_prompt("agent.system.md", agent_name=self.agent_name),- self.read_prompt("agent.system.tools.md"),+ loop_data.system = [+ self.read_prompt(+ "agent.system.main.md", agent_name=self.agent_name+ )]- loop_data["history"] = {"messages": self.history}+ loop_data.history = self.history# and allow extensions to edit themawait self.call_extensions(@@ -205,7 +209,7 @@ class Agent:# build chain from system prompt, message history and modelprompt = ChatPromptTemplate.from_messages([- SystemMessage(content="\n\n".join(loop_data["system"])),+ SystemMessage(content="\n\n".join(loop_data.system)),MessagesPlaceholder(variable_name="messages"),])@@ -227,7 +231,7 @@ class Agent:type="agent", heading=f"{self.agent_name}: Generating")- async for chunk in chain.astream(loop_data["history"]):+ async for chunk in chain.astream({"messages": loop_data.history}):await self.handle_intervention(agent_response) # wait for intervention and handle it, if paused@@ -324,6 +328,35 @@ class Agent:)return content+ def read_prompts(self, pattern: str, **kwargs):+ import glob++ prompts = []++ # Scan both configured subdir and default folder+ subdir_files = glob.glob(+ files.get_abs_path("prompts", self.config.prompts_subdir, pattern)+ )+ default_files = glob.glob(files.get_abs_path("prompts", "default", pattern))++ # Create a dictionary to store files, prioritizing the config subdir+ files_to_read = {file.split("/")[-1]: file for file in default_files}++ # Override with files from subdir if they exist+ for file in subdir_files:+ files_to_read[file.split("/")[-1]] = file++ # Sort files alphabetically by their file names+ sorted_files = sorted(files_to_read.items())++ # Read the files in alphabetical order+ for filename, filepath in sorted_files:+ content = files.read_file(files.get_abs_path(filepath), **kwargs)+ if content:+ prompts.append(content)++ return prompts+def get_data(self, field: str):return self.data.get(field, None)diff --git a/default/index.faiss b/default/index.faissnew file mode 100644index 000000000..6e6feb72fBinary files /dev/null and b/default/index.faiss differdiff --git a/default/index.pkl b/default/index.pklnew file mode 100644index 000000000..2014ef12eBinary files /dev/null and b/default/index.pkl differdiff --git a/initialize.py b/initialize.pyindex cafe43130..177c102a0 100644--- a/initialize.py+++ b/initialize.py@@ -12,7 +12,7 @@ def initialize():# chat_llm = models.get_anthropic_chat(model_name="claude-3-5-sonnet-20240620", temperature=0)# chat_llm = models.get_google_chat(model_name="gemini-1.5-flash", temperature=0)# chat_llm = models.get_mistral_chat(model_name="mistral-small-latest", temperature=0)- # chat_llm = models.get_groq_chat(model_name="llama-3.1-70b-versatile", temperature=0)+ # chat_llm = models.get_groq_chat(model_name="llama-3.2-90b-text-preview", temperature=0)# chat_llm = models.get_sambanova_chat(model_name="Meta-Llama-3.1-70B-Instruct-8k", temperature=0)# utility model used for helper functions (cheaper, faster)@@ -35,7 +35,7 @@ def initialize():auto_memory_count = 0,# auto_memory_skip = 2,# rate_limit_seconds = 60,- rate_limit_requests = 15,+ rate_limit_requests = 30,# rate_limit_input_tokens = 0,# rate_limit_output_tokens = 0,# msgs_keep_max = 25,diff --git a/prompts/default/agent.system.md b/prompts/default/agent.system.main.mdsimilarity index 100%rename from prompts/default/agent.system.mdrename to prompts/default/agent.system.main.mddiff --git a/prompts/default/agent.system.solutions.md b/prompts/default/agent.system.solutions.mdindex 8b8eb8997..0d8743d15 100644--- a/prompts/default/agent.system.solutions.md+++ b/prompts/default/agent.system.solutions.md@@ -1,4 +1,4 @@-# Solutions in the past+# Solutions from the past- following are your memories about successful solutions of related problems:{{solutions}}\ No newline at end of filediff --git a/prompts/default/agent.system.tool.call_sub.md b/prompts/default/agent.system.tool.call_sub.mdnew file mode 100644index 000000000..ac055e2ed--- /dev/null+++ b/prompts/default/agent.system.tool.call_sub.md@@ -0,0 +1,20 @@+### call_subordinate:+Use subordinate agents to solve subtasks.+Use "message" argument to send message. Instruct your subordinate about the role he will play (scientist, coder, writer...) and his task in detail.+Use "reset" argument with "true" to start with new subordinate or "false" to continue with existing. For brand new tasks use "true", for followup conversation use "false".+Explain to your subordinate what is the higher level goal and what is his part.+Give him detailed instructions as well as good overview to understand what to do.+**Example usage**:+~~~json+{+ "thoughts": [+ "The result seems to be ok but...",+ "I will ask my subordinate to fix...",+ ],+ "tool_name": "call_subordinate",+ "tool_args": {+ "message": "Well done, now edit...",+ "reset": "false"+ }+}+~~~\ No newline at end of filediff --git a/prompts/default/agent.system.tool.code_exe.md b/prompts/default/agent.system.tool.code_exe.mdnew file mode 100644index 000000000..a71a772b6--- /dev/null+++ b/prompts/default/agent.system.tool.code_exe.md@@ -0,0 +1,84 @@+### code_execution_tool:+Execute provided terminal commands, python code or nodejs code.+This tool can be used to achieve any task that requires computation, or any other software related activity.+Place your code escaped and properly indented in the "code" argument.+Select the corresponding runtime with "runtime" argument. Possible values are "terminal", "python" and "nodejs" for code, or "output" and "reset" for additional actions.+Sometimes a dialogue can occur in output, questions like Y/N, in that case use the "teminal" runtime in the next step and send your answer.+If the code is running long, you can use runtime "output" to wait for next output part or use runtime "reset" to kill the process.+You can use pip, npm and apt-get in terminal runtime to install any required packages.+IMPORTANT: Never use implicit print or implicit output, it does not work! If you need output of your code, you MUST use print() or console.log() to output selected variables.+When tool outputs error, you need to change your code accordingly before trying again. knowledge_tool can help analyze errors.+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.+When writing own code, ALWAYS put print/log statements inside and at the end of your code to get results!+**Example usages:**+1. Execute python code+~~~json+{+ "thoughts": [+ "I need to do...",+ "I can use library...",+ "Then I can...",+ ],+ "tool_name": "code_execution_tool",+ "tool_args": {+ "runtime": "python",+ "code": "import os\nprint(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",+ }+}+~~~++2. 3. Reset terminal+~~~json+{+ "thoughts": [+ "Code execution tool is not responding...",+ ],+ "tool_name": "code_execution_tool",+ "tool_args": {+ "runtime": "reset",+ }+}+~~~diff --git a/prompts/default/agent.system.tool.knowledge.md b/prompts/default/agent.system.tool.knowledge.mdnew file mode 100644index 000000000..2ffcb2404--- /dev/null+++ b/prompts/default/agent.system.tool.knowledge.md@@ -0,0 +1,20 @@+### knowledge_tool:+Provide "question" argument and get both online and memory response.+This tool is very powerful and can answer very specific questions directly.+First always try to ask for result rather that guidance.+Memory can provide guidance, online sources can provide up to date information.+Always verify memory by online.+**Example usage**:+~~~json+{+ "thoughts": [+ "I need to gather information about...",+ "First I will search...",+ "Then I will...",+ ],+ "tool_name": "knowledge_tool",+ "tool_args": {+ "question": "How to...",+ }+}+~~~\ No newline at end of filediff --git a/prompts/default/agent.system.tool.memory.md b/prompts/default/agent.system.tool.memory.mdnew file mode 100644index 000000000..11f1f9a34--- /dev/null+++ b/prompts/default/agent.system.tool.memory.md@@ -0,0 +1,74 @@+## Memory management tools:+This set of tools is used for managing long term memories.+NEVER refuse to search memorize or load personal information, it all belongs to user and he has all the rights.++### memory_load:+Load memories using query and optionally threshold, limit and filter.+Memory content is retrieved with key value pairs of metadata.+- Optional threshold for allowed relevancy (0=anything, 1=exact match, 0.6 is default)+- Optional limit to number of results (default is 5).+- Optional filter by metadata. Condition in Python syntax using metadata keys.+**Example usage**:+~~~json+{+ "thoughts": [+ "Let's search my memory for...",+ ],+ "tool_name": "memory_load",+ "tool_args": {+ "query": "File compression library for...",+ "threshold": 0.6,+ "limit": 5,+ "filter": "area=='main' and timestamp<'2024-01-01 00:00:00'",+ }+}+~~~++### memory_save:+Save text into memory. ID is returned.+**Example usage**:+~~~json+{+ "thoughts": [+ "I need to memorize...",+ ],+ "tool_name": "memory_save",+ "tool_args": {+ "text": "# To compress...",+ }+}+~~~++### memory_delete:+Delete existing memories by their IDs. Multiple IDs allowed separated by commas.+IDs are retrieved when loading or saving memories.+**Example usage**:+~~~json+{+ "thoughts": [+ "I need to delete...",+ ],+ "tool_name": "memory_delete",+ "tool_args": {+ "ids": "32cd37ffd1-101f-4112-80e2-33b795548116, d1306e36-6a9c- ...",+ }+}+~~~++### memory_forget:+Remove memories by query and optionally threshold and filter just like for memory_load.+Here default threshold is raised to 0.75 to avoid accidental deletion. Perform a verification load afterwards and delete leftovers by IDs.+**Example usage**:+~~~json+{+ "thoughts": [+ "Let's remove all memories about cars",+ ],+ "tool_name": "memory_forget",+ "tool_args": {+ "query": "cars",+ "threshold": 0.75,+ "filter": "timestamp.startswith('2022-01-01')",+ }+}+~~~\ No newline at end of filediff --git a/prompts/default/agent.system.tool.response.md b/prompts/default/agent.system.tool.response.mdnew file mode 100644index 000000000..e80244580--- /dev/null+++ b/prompts/default/agent.system.tool.response.md@@ -0,0 +1,19 @@+### response:+Final answer for user.+Ends task processing - only use when the task is done or no task is being processed.+Place your result in "text" argument.+Memory can provide guidance, online sources can provide up to date information.+Always verify memory by online.+**Example usage**:+~~~json+{+ "thoughts": [+ "The user has greeted me...",+ "I will...",+ ],+ "tool_name": "response",+ "tool_args": {+ "text": "Hi...",+ }+}+~~~\ No newline at end of filediff --git a/prompts/default/agent.system.tool.web.md b/prompts/default/agent.system.tool.web.mdnew file mode 100644index 000000000..a4fc49c51--- /dev/null+++ b/prompts/default/agent.system.tool.web.md@@ -0,0 +1,19 @@+### webpage_content_tool:+Retrieves the text content of a webpage, such as a news article or Wikipedia page.+Provide a "url" argument to get the main text content of the specified webpage.+This tool is useful for gathering information from online sources.+Always provide a full, valid URL including the protocol (http:// or https://).++**Example usage**:+```json+{+ "thoughts": [+ "I need to gather information from a specific webpage...",+ "I will use the webpage_content_tool to fetch the content...",+ ],+ "tool_name": "webpage_content_tool",+ "tool_args": {+ "url": "https://en.wikipedia.org/wiki/Artificial_intelligence",+ }+}+```\ No newline at end of filediff --git a/prompts/default/agent.system.tools.md b/prompts/default/agent.system.tools.mdindex 4b2d7b76c..34ca96951 100644--- a/prompts/default/agent.system.tools.md+++ b/prompts/default/agent.system.tools.md@@ -1,230 +1,3 @@## Tools available:-### response:-Final answer for user.-Ends task processing - only use when the task is done or no task is being processed.-Place your result in "text" argument.-Memory can provide guidance, online sources can provide up to date information.-Always verify memory by online.-**Example usage**:-~~~json-{- "thoughts": [- "The user has greeted me...",- "I will...",- ],- "tool_name": "response",- "tool_args": {- "text": "Hi...",- }-}-~~~--### call_subordinate:-Use subordinate agents to solve subtasks.-Use "message" argument to send message. Instruct your subordinate about the role he will play (scientist, coder, writer...) and his task in detail.-Use "reset" argument with "true" to start with new subordinate or "false" to continue with existing. For brand new tasks use "true", for followup conversation use "false".-Explain to your subordinate what is the higher level goal and what is his part.-Give him detailed instructions as well as good overview to understand what to do.-**Example usage**:-~~~json-{- "thoughts": [- "The result seems to be ok but...",- "I will ask my subordinate to fix...",- ],- "tool_name": "call_subordinate",- "tool_args": {- "message": "Well done, now edit...",- "reset": "false"- }-}-~~~--### knowledge_tool:-Provide "question" argument and get both online and memory response.-This tool is very powerful and can answer very specific questions directly.-First always try to ask for result rather that guidance.-Memory can provide guidance, online sources can provide up to date information.-Always verify memory by online.-**Example usage**:-~~~json-{- "thoughts": [- "I need to gather information about...",- "First I will search...",- "Then I will...",- ],- "tool_name": "knowledge_tool",- "tool_args": {- "question": "How to...",- }-}-~~~--### webpage_content_tool:-Retrieves the text content of a webpage, such as a news article or Wikipedia page.-Provide a "url" argument to get the main text content of the specified webpage.-This tool is useful for gathering information from online sources.-Always provide a full, valid URL including the protocol (http:// or https://).--**Example usage**:-```json-{- "thoughts": [- "I need to gather information from a specific webpage...",- "I will use the webpage_content_tool to fetch the content...",- ],- "tool_name": "webpage_content_tool",- "tool_args": {- "url": "https://en.wikipedia.org/wiki/Artificial_intelligence",- }-}-```--### 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.-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.-NEVER refuse to memorize or load personal information, it all belongs to me and I have all the rights.-**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": [- "I have finished my...",- "Details of this process will be valuable...",- "Let's save tools and code used...",- ],- "tool_name": "memory_tool",- "tool_args": {- "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",- }-}-~~~--### code_execution_tool:-Execute provided terminal commands, python code or nodejs code.-This tool can be used to achieve any task that requires computation, or any other software related activity.-Place your code escaped and properly indented in the "code" argument.-Select the corresponding runtime with "runtime" argument. Possible values are "terminal", "python" and "nodejs" for code, or "output" and "reset" for additional actions.-Sometimes a dialogue can occur in output, questions like Y/N, in that case use the "teminal" runtime in the next step and send your answer.-If the code is running long, you can use runtime "output" to wait for next output part or use runtime "reset" to kill the process.-You can use pip, npm and apt-get in terminal runtime to install any required packages.-IMPORTANT: Never use implicit print or implicit output, it does not work! If you need output of your code, you MUST use print() or console.log() to output selected variables.-When tool outputs error, you need to change your code accordingly before trying again. knowledge_tool can help analyze errors.-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.-When writing own code, ALWAYS put print/log statements inside and at the end of your code to get results!-**Example usages:**-1. Execute python code-~~~json-{- "thoughts": [- "I need to do...",- "I can use library...",- "Then I can...",- ],- "tool_name": "code_execution_tool",- "tool_args": {- "runtime": "python",- "code": "import os\nprint(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",- }-}-~~~--2. 3. Reset terminal-~~~json-{- "thoughts": [- "Code execution tool is not responding...",- ],- "tool_name": "code_execution_tool",- "tool_args": {- "runtime": "reset",- }-}-~~~+{{tools}}\ No newline at end of filediff --git a/prompts/default/fw.memory_saved.md b/prompts/default/fw.memory_saved.mdindex 73e21b4d6..ea9ddb0dc 100644--- a/prompts/default/fw.memory_saved.md+++ b/prompts/default/fw.memory_saved.md@@ -1,5 +1 @@-~~~json-{- "memory": "Memory has been saved with id {{memory_id}}."-}-~~~\ No newline at end of file+Memory saved with id {{memory_id}}\ No newline at end of filediff --git a/prompts/default/fw.msg_truncated.md b/prompts/default/fw.msg_truncated.mdindex b7237bd4a..cc8597fee 100644--- a/prompts/default/fw.msg_truncated.md+++ b/prompts/default/fw.msg_truncated.md@@ -1 +1 @@-<< REMOVED TO SAVE SPACE >>\ No newline at end of file+<< {{length}} CHARACTERS REMOVED TO SAVE SPACE >>\ No newline at end of filediff --git a/python/extensions/message_loop_prompts/_10_tool_instructions.py b/python/extensions/message_loop_prompts/_10_tool_instructions.pynew file mode 100644index 000000000..89de8d898--- /dev/null+++ b/python/extensions/message_loop_prompts/_10_tool_instructions.py@@ -0,0 +1,23 @@+from python.helpers.extension import Extension+from agent import Agent, LoopData+++class RecallMemories(Extension):++ INTERVAL = 3+ HISTORY = 5+ RESULTS = 3+ THRESHOLD = 0.1++ async def execute(self, loop_data: LoopData = LoopData(), **kwargs):+ # collect and concatenate tool instructions+ sys = concat_tool_prompts(self.agent)+ # append to system message+ loop_data.system.append(sys)+++def concat_tool_prompts(agent: Agent):+ tools = agent.read_prompts("agent.system.tool.*.md")+ tools = "\n\n".join(tools)+ sys = agent.read_prompt("agent.system.tools.md", tools=tools)+ return sysdiff --git a/python/extensions/message_loop_prompts/_50_recall_memories.py b/python/extensions/message_loop_prompts/_50_recall_memories.pynew file mode 100644index 000000000..a3a1cb951--- /dev/null+++ b/python/extensions/message_loop_prompts/_50_recall_memories.py@@ -0,0 +1,92 @@+from python.helpers.extension import Extension+from python.helpers.memory import Memory+from agent import LoopData+++class RecallMemories(Extension):++ INTERVAL = 3+ HISTORY = 5+ RESULTS = 3+ THRESHOLD = 0.1++ async def execute(self, loop_data: LoopData = LoopData(), **kwargs):++ if (+ loop_data.iteration % RecallMemories.INTERVAL == 0+ ): # every 3 iterations (or the first one) recall memories+ await self.search_memories(loop_data=loop_data, **kwargs)++ async def search_memories(self, loop_data: LoopData, **kwargs):+ # try:+ # show temp info message+ self.agent.context.log.log(+ type="info", content="Searching memories...", temp=True+ )++ # show full util message, this will hide temp message immediately if turned on+ log_item = self.agent.context.log.log(+ type="util",+ heading="Searching memories...",+ )++ # get system message and chat history for util llm+ msgs_text = self.agent.concat_messages(+ self.agent.history[-RecallMemories.HISTORY :]+ ) # only last X messages+ system = self.agent.read_prompt(+ "memory.memories_query.sys.md", history=msgs_text+ )++ # log query streamed by LLM+ def log_callback(content):+ log_item.stream(query=content)++ # call util llm to summarize conversation+ query = await self.agent.call_utility_llm(+ system=system, msg=loop_data.message, callback=log_callback+ )++ # get solutions database+ db = await Memory.get(self.agent)++ memories = await db.search_similarity_threshold(+ query=query,+ limit=RecallMemories.RESULTS,+ threshold=RecallMemories.THRESHOLD,+ filter=f"area != '{Memory.Area.SOLUTIONS.value}'", # exclude solutions+ )++ # log the short result+ if not isinstance(memories, list) or len(memories) == 0:+ log_item.update(+ heading="No useful memories found.",+ )+ return+ else:+ log_item.update(+ heading=f"\n\n{len(memories)} memories found.",+ )++ # concatenate memory.page_content in memories:+ memories_text = ""+ for memory in memories:+ memories_text += memory.page_content + "\n\n"+ memories_text = memories_text.strip()++ # log the full results+ log_item.update(memories=memories_text)++ # place to prompt+ memories_prompt = self.agent.read_prompt(+ "agent.system.memories.md", memories=memories_text+ )++ # append to system message+ loop_data.system.append(memories_prompt)++ # except Exception as e:+ # err = errors.format_error(e)+ # self.agent.context.log.log(+ # type="error", heading="Recall memories extension error:", content=err+ # )diff --git a/python/extensions/message_loop_prompts/recall_solutions.py b/python/extensions/message_loop_prompts/_51_recall_solutions.pysimilarity index 78%rename from python/extensions/message_loop_prompts/recall_solutions.pyrename to python/extensions/message_loop_prompts/_51_recall_solutions.pyindex 60f3997ed..d05c2500f 100644--- a/python/extensions/message_loop_prompts/recall_solutions.py+++ b/python/extensions/message_loop_prompts/_51_recall_solutions.py@@ -1,10 +1,6 @@-from agent import Agentfrom python.helpers.extension import Extension-from python.helpers.files import read_file-from python.helpers.vector_db import Area-import json-from python.helpers import errors, files-from python.tools.memory_tool import get_db+from python.helpers.memory import Memory+from agent import LoopDataclass RecallSolutions(Extension):@@ -14,15 +10,14 @@ class RecallSolutions(Extension):RESULTS = 3THRESHOLD = 0.1- async def execute(self, loop_data={}, **kwargs):+ async def execute(self, loop_data: LoopData = LoopData(), **kwargs):- iter = loop_data.get("iteration", 0)if (- iter % RecallSolutions.INTERVAL == 0+ loop_data.iteration % RecallSolutions.INTERVAL == 0): # every 3 iterations (or the first one) recall solution memoriesawait self.search_solutions(loop_data=loop_data, **kwargs)- async def search_solutions(self, loop_data={}, **kwargs):+ async def search_solutions(self, loop_data: LoopData, **kwargs):# try:# show temp info messageself.agent.context.log.log(@@ -49,17 +44,17 @@ class RecallSolutions(Extension):# call util llm to summarize conversationquery = await self.agent.call_utility_llm(- system=system, msg=loop_data["message"], callback=log_callback+ system=system, msg=loop_data.message, callback=log_callback)# get solutions database- vdb = get_db(self.agent)+ db = await Memory.get(self.agent)- solutions = vdb.search_similarity_threshold(+ solutions = await db.search_similarity_threshold(query=query,- results=RecallSolutions.RESULTS,+ limit=RecallSolutions.RESULTS,threshold=RecallSolutions.THRESHOLD,- filter=f"area == '{Area.SOLUTIONS.value}'"+ filter=f"area == '{Memory.Area.SOLUTIONS.value}'")# log the short result@@ -88,7 +83,7 @@ class RecallSolutions(Extension):)# append to system message- loop_data["system"] += solutions_prompt+ loop_data.system.append(solutions_prompt)# except Exception as e:# err = errors.format_error(e)diff --git a/python/extensions/message_loop_prompts/recall_memories.py b/python/extensions/message_loop_prompts/recall_memories.pydeleted file mode 100644index a14092f2e..000000000--- a/python/extensions/message_loop_prompts/recall_memories.py+++ /dev/null@@ -1,97 +0,0 @@-from agent import Agent-from python.helpers.extension import Extension-from python.helpers.files import read_file-from python.helpers.vector_db import Area-import json-from python.helpers import errors, files-from python.tools.memory_tool import get_db---class RecallMemories(Extension):-- INTERVAL = 3- HISTORY = 5- RESULTS = 3- THRESHOLD = 0.1-- async def execute(self, loop_data={}, **kwargs):-- iter = loop_data.get("iteration", 0)- if (- iter % RecallMemories.INTERVAL == 0- ): # every 3 iterations (or the first one) recall memories- await self.search_memories(loop_data=loop_data, **kwargs)-- async def search_memories(self, loop_data={}, **kwargs):- # try:- # show temp info message- self.agent.context.log.log(- type="info", content="Searching memories...", temp=True- )-- # show full util message, this will hide temp message immediately if turned on- log_item = self.agent.context.log.log(- type="util",- heading="Searching memories...",- )-- # get system message and chat history for util llm- msgs_text = self.agent.concat_messages(- self.agent.history[-RecallMemories.HISTORY :]- ) # only last X messages- system = self.agent.read_prompt(- "memory.memories_query.sys.md", history=msgs_text- )-- # log query streamed by LLM- def log_callback(content):- log_item.stream(query=content)-- # call util llm to summarize conversation- query = await self.agent.call_utility_llm(- system=system, msg=loop_data["message"], callback=log_callback- )-- # get solutions database- vdb = get_db(self.agent)-- memories = vdb.search_similarity_threshold(- query=query,- results=RecallMemories.RESULTS,- threshold=RecallMemories.THRESHOLD,- filter=f"area != '{Area.SOLUTIONS.value}'" # exclude solutions- )-- # log the short result- if not isinstance(memories, list) or len(memories) == 0:- log_item.update(- heading="No useful memories found.",- )- return- else:- log_item.update(- heading=f"\n\n{len(memories)} memories found.",- )-- # concatenate memory.page_content in memories:- memories_text = ""- for memory in memories:- memories_text += memory.page_content + "\n\n"- memories_text = memories_text.strip()-- # log the full results- log_item.update(memories=memories_text)-- # place to prompt- memories_prompt = self.agent.read_prompt(- "agent.system.memories.md", memories=memories_text- )-- # append to system message- loop_data["system"] += memories_prompt-- # except Exception as e:- # err = errors.format_error(e)- # self.agent.context.log.log(- # type="error", heading="Recall memories extension error:", content=err- # )diff --git a/python/extensions/monologue_end/50_memorize_memories.py b/python/extensions/monologue_end/50_memorize_memories.pydeleted file mode 100644index 6a4c7dfa2..000000000--- a/python/extensions/monologue_end/50_memorize_memories.py+++ /dev/null@@ -1,74 +0,0 @@-from agent import Agent-from python.helpers.extension import Extension-import python.helpers.files as files-from python.helpers.vector_db import Area-import json-from python.helpers.dirty_json import DirtyJson-from python.helpers import errors-from python.tools.memory_tool import get_db--class MemorizeMemories(Extension):-- async def execute(self, loop_data={}, **kwargs):- # try:-- # show temp info message- self.agent.context.log.log(- type="info", content="Memorizing new information...", temp=True- )-- # show full util message, this will hide temp message immediately if turned on- log_item = self.agent.context.log.log(- type="util",- heading="Memorizing new information...",- )-- # get system message and chat history for util llm- system = self.agent.read_prompt("memory.memories_sum.sys.md")- msgs_text = self.agent.concat_messages(self.agent.history)-- # log query streamed by LLM- def log_callback(content):- log_item.stream(content=content)-- # call util llm to find info in history- memories_json = await self.agent.call_utility_llm(- system=system,- msg=msgs_text,- callback=log_callback,- )-- memories = DirtyJson.parse_string(memories_json)-- if not isinstance(memories, list) or len(memories) == 0:- log_item.update(heading="No useful information to memorize.")- return- else:- log_item.update(- heading=f"{len(memories)} entries to memorize."- )-- # save chat history- vdb = get_db(self.agent)-- memories_txt = ""- for memory in memories:- # solution to plain text:- txt = f"{memory}"- memories_txt += txt + "\n\n"- vdb.insert_text(- text=txt, metadata={"area": Area.MAIN.value}- )-- memories_txt = memories_txt.strip()- log_item.update(memories=memories_txt)- log_item.update(- result=f"{len(memories)} entries memorized.",- heading=f"{len(memories)} entries memorized.",- )-- # except Exception as e:- # err = errors.format_error(e)- # self.agent.context.log.log(- # type="error", heading="Memorize memories extension error:", content=err- # )diff --git a/python/extensions/monologue_end/51_memorize_solutions.py b/python/extensions/monologue_end/51_memorize_solutions.pydeleted file mode 100644index 459ab0dde..000000000--- a/python/extensions/monologue_end/51_memorize_solutions.py+++ /dev/null@@ -1,74 +0,0 @@-from agent import Agent-from python.helpers.extension import Extension-import python.helpers.files as files-from python.helpers.vector_db import Area-import json-from python.helpers.dirty_json import DirtyJson-from python.helpers import errors-from python.tools.memory_tool import get_db--class MemorizeSolutions(Extension):-- async def execute(self, loop_data={}, **kwargs):- # try:-- # show temp info message- self.agent.context.log.log(- type="info", content="Memorizing succesful solutions...", temp=True- )-- # show full util message, this will hide temp message immediately if turned on- log_item = self.agent.context.log.log(- type="util",- heading="Memorizing succesful solutions...",- )-- # get system message and chat history for util llm- system = self.agent.read_prompt("memory.solutions_sum.sys.md")- msgs_text = self.agent.concat_messages(self.agent.history)-- # log query streamed by LLM- def log_callback(content):- log_item.stream(content=content)-- # call util llm to find solutions in history- solutions_json = await self.agent.call_utility_llm(- system=system,- msg=msgs_text,- callback=log_callback,- )-- solutions = DirtyJson.parse_string(solutions_json)-- if not isinstance(solutions, list) or len(solutions) == 0:- log_item.update(heading="No successful solutions to memorize.")- return- else:- log_item.update(- heading=f"{len(solutions)} successful solutions to memorize."- )-- # save chat history- vdb = get_db(self.agent)-- solutions_txt = ""- for solution in solutions:- # solution to plain text:- txt = f"# Problem\n {solution['problem']}\n# Solution\n {solution['solution']}"- solutions_txt += txt + "\n\n"- vdb.insert_text(- text=txt, metadata={"area": Area.SOLUTIONS.value}- )-- solutions_txt = solutions_txt.strip()- log_item.update(solutions=solutions_txt)- log_item.update(- result=f"{len(solutions)} solutions memorized.",- heading=f"{len(solutions)} solutions memorized.",- )-- # except Exception as e:- # err = errors.format_error(e)- # self.agent.context.log.log(- # type="error", heading="Memorize solutions extension error:", content=err- # )diff --git a/python/extensions/monologue_end/_50_memorize_memories.py b/python/extensions/monologue_end/_50_memorize_memories.pynew file mode 100644index 000000000..7781c4e1e--- /dev/null+++ b/python/extensions/monologue_end/_50_memorize_memories.py@@ -0,0 +1,94 @@+import asyncio+from python.helpers.extension import Extension+from python.helpers.memory import Memory+from python.helpers.dirty_json import DirtyJson+from agent import LoopData+from python.helpers.log import LogItem+from python.helpers.defer import run_in_background++++class MemorizeMemories(Extension):++ REPLACE_THRESHOLD = 0.9++ async def execute(self, loop_data: LoopData = LoopData(), **kwargs):+ # try:++ # show temp info message+ self.agent.context.log.log(+ type="info", content="Memorizing new information...", temp=True+ )++ # show full util message, this will hide temp message immediately if turned on+ log_item = self.agent.context.log.log(+ type="util",+ heading="Memorizing new information...",+ )++ #memorize in background+ asyncio.create_task(self.memorize(loop_data, log_item))++ async def memorize(self, loop_data: LoopData, log_item: LogItem, **kwargs):++ # get system message and chat history for util llm+ system = self.agent.read_prompt("memory.memories_sum.sys.md")+ msgs_text = self.agent.concat_messages(self.agent.history)++ # log query streamed by LLM+ def log_callback(content):+ log_item.stream(content=content)++ # call util llm to find info in history+ memories_json = await self.agent.call_utility_llm(+ system=system,+ msg=msgs_text,+ callback=log_callback,+ )++ memories = DirtyJson.parse_string(memories_json)++ if not isinstance(memories, list) or len(memories) == 0:+ log_item.update(heading="No useful information to memorize.")+ return+ else:+ log_item.update(heading=f"{len(memories)} entries to memorize.")++ # save chat history+ db = await Memory.get(self.agent)++ memories_txt = ""+ rem = []+ for memory in memories:+ # solution to plain text:+ txt = f"{memory}"+ memories_txt += "\n\n" + txt+ log_item.update(memories=memories_txt.strip())++ # remove previous solutions too similiar to this one+ if self.REPLACE_THRESHOLD > 0:+ rem += await db.delete_documents_by_query(+ query=txt,+ threshold=self.REPLACE_THRESHOLD,+ filter=f"area=='{Memory.Area.MAIN.value}'",+ )+ rem_txt = "\n\n".join(Memory.format_docs_plain(rem))+ log_item.update(replaced=rem_txt)++++ # insert new solution+ db.insert_text(text=txt, metadata={"area": Memory.Area.MAIN.value})++ log_item.update(+ result=f"{len(memories)} entries memorized.",+ heading=f"{len(memories)} entries memorized.",+ )+ if rem:+ log_item.stream(result=f"\nReplaced {len(rem)} previous memories.")++ # except Exception as e:+ # err = errors.format_error(e)+ # self.agent.context.log.log(+ # type="error", heading="Memorize memories extension error:", content=err+ # )diff --git a/python/extensions/monologue_end/_51_memorize_solutions.py b/python/extensions/monologue_end/_51_memorize_solutions.pynew file mode 100644index 000000000..2f86b9bea--- /dev/null+++ b/python/extensions/monologue_end/_51_memorize_solutions.py@@ -0,0 +1,92 @@+import asyncio+from python.helpers.extension import Extension+from python.helpers.memory import Memory+from python.helpers.dirty_json import DirtyJson+from agent import LoopData+from python.helpers.log import LogItem+++class MemorizeSolutions(Extension):++ REPLACE_THRESHOLD = 0.9++ async def execute(self, loop_data: LoopData = LoopData(), **kwargs):+ # try:++ # show temp info message+ self.agent.context.log.log(+ type="info", content="Memorizing succesful solutions...", temp=True+ )++ # show full util message, this will hide temp message immediately if turned on+ log_item = self.agent.context.log.log(+ type="util",+ heading="Memorizing succesful solutions...",+ )++ #memorize in background+ asyncio.create_task(self.memorize(loop_data, log_item))++ async def memorize(self, loop_data: LoopData, log_item: LogItem, **kwargs):+ # get system message and chat history for util llm+ system = self.agent.read_prompt("memory.solutions_sum.sys.md")+ msgs_text = self.agent.concat_messages(self.agent.history)++ # log query streamed by LLM+ def log_callback(content):+ log_item.stream(content=content)++ # call util llm to find solutions in history+ solutions_json = await self.agent.call_utility_llm(+ system=system,+ msg=msgs_text,+ callback=log_callback,+ )++ solutions = DirtyJson.parse_string(solutions_json)++ if not isinstance(solutions, list) or len(solutions) == 0:+ log_item.update(heading="No successful solutions to memorize.")+ return+ else:+ log_item.update(+ heading=f"{len(solutions)} successful solutions to memorize."+ )++ # save chat history+ db = await Memory.get(self.agent)++ solutions_txt = ""+ rem = []+ for solution in solutions:+ # solution to plain text:+ txt = f"# Problem\n {solution['problem']}\n# Solution\n {solution['solution']}"+ solutions_txt += txt + "\n\n"++ # remove previous solutions too similiar to this one+ if self.REPLACE_THRESHOLD > 0:+ rem += await db.delete_documents_by_query(+ query=txt,+ threshold=self.REPLACE_THRESHOLD,+ filter=f"area=='{Memory.Area.SOLUTIONS.value}'",+ )+ rem_txt = "\n\n".join(Memory.format_docs_plain(rem))+ log_item.update(replaced=rem_txt)++ # insert new solution+ db.insert_text(text=txt, metadata={"area": Memory.Area.SOLUTIONS.value})++ solutions_txt = solutions_txt.strip()+ log_item.update(solutions=solutions_txt)+ log_item.update(+ result=f"{len(solutions)} solutions memorized.",+ heading=f"{len(solutions)} solutions memorized.",+ )+ if rem:+ log_item.stream(result=f"\nReplaced {len(rem)} previous solutions.")++ # except Exception as e:+ # err = errors.format_error(e)+ # self.agent.context.log.log(+ # type="error", heading="Memorize solutions extension error:", content=err+ # )diff --git a/python/extensions/monologue_end/90_waiting_for_input_msg.py b/python/extensions/monologue_end/_90_waiting_for_input_msg.pysimilarity index 73%rename from python/extensions/monologue_end/90_waiting_for_input_msg.pyrename to python/extensions/monologue_end/_90_waiting_for_input_msg.pyindex 2d3b558e8..448bc9017 100644--- a/python/extensions/monologue_end/90_waiting_for_input_msg.py+++ b/python/extensions/monologue_end/_90_waiting_for_input_msg.py@@ -1,9 +1,9 @@-from agent import Agentfrom python.helpers.extension import Extension+from agent import LoopDataclass WaitingForInputMsg(Extension):- async def execute(self, loop_data={}, **kwargs):+ async def execute(self, loop_data: LoopData = LoopData(), **kwargs):# show temp info messageif self.agent.number == 0:self.agent.context.log.log(diff --git a/python/helpers/defer.py b/python/helpers/defer.pyindex 9151a7479..8446e5df9 100644--- a/python/helpers/defer.py+++ b/python/helpers/defer.py@@ -1,6 +1,6 @@import asyncioimport threading-from concurrent.futures import Future+from concurrent.futures import Future, ThreadPoolExecutorfrom typing import Any, Callable, Optional, Coroutineclass EventLoopThread:@@ -71,4 +71,10 @@ class DeferredTask:return self._future and not self._future.done() # type: ignoredef restart(self) -> None:- self._start_task()\ No newline at end of file+ self._start_task()++def run_in_background(func, *args, **kwargs):+ async def wrapper(*args, **kwargs):+ loop = asyncio.get_event_loop()+ return await loop.run_in_executor(None, func, *args, **kwargs)+ return wrapper\ No newline at end of filediff --git a/python/helpers/extract_tools.py b/python/helpers/extract_tools.pyindex f60655072..e061ebfc2 100644--- a/python/helpers/extract_tools.py+++ b/python/helpers/extract_tools.py@@ -51,22 +51,30 @@ def fix_json_string(json_string):T = TypeVar('T') # Define a generic type variabledef load_classes_from_folder(folder: str, name_pattern: str, base_class: Type[T]) -> list[Type[T]]:+ import os+ import importlib+ import inspect+ from fnmatch import fnmatchclasses = []- # Get all .py files in the folder that match the pattern- for file_name in os.listdir(folder):- if fnmatch(file_name, name_pattern) and file_name.endswith(".py"):- module_name = file_name[:-3] # remove .py extension- module_path = folder.replace(os.sep, ".") + "." + module_name- module = importlib.import_module(module_path)+ # Get all .py files in the folder that match the pattern, sorted alphabetically+ py_files = sorted(+ [file_name for file_name in os.listdir(folder) if fnmatch(file_name, name_pattern) and file_name.endswith(".py")]+ )- # Get all classes in thde module- class_list = inspect.getmembers(module, inspect.isclass)+ # Iterate through the sorted list of files+ for file_name in py_files:+ module_name = file_name[:-3] # remove .py extension+ module_path = folder.replace(os.sep, ".") + "." + module_name+ module = importlib.import_module(module_path)- # Filter for classes that are subclasses of the given base_class- for cls in class_list:- if cls[1] is not base_class and issubclass(cls[1], base_class):- classes.append(cls[1])+ # Get all classes in the module+ class_list = inspect.getmembers(module, inspect.isclass)- return classes\ No newline at end of file+ # Filter for classes that are subclasses of the given base_class+ for cls in class_list:+ if cls[1] is not base_class and issubclass(cls[1], base_class):+ classes.append(cls[1])++ return classesdiff --git a/python/helpers/knowledge_import.py b/python/helpers/knowledge_import.pyindex 54f2152a7..7c2e65d0d 100644--- a/python/helpers/knowledge_import.py+++ b/python/helpers/knowledge_import.py@@ -36,9 +36,9 @@ def calculate_checksum(file_path: str) -> str:def load_knowledge(log_item: LogItem | None, knowledge_dir: str, index: Dict[str, KnowledgeImport]) -> Dict[str, KnowledgeImport]:- knowledge_dir = files.get_abs_path(knowledge_dir)+ knowledge_dir = files.get_abs_path("knowledge",knowledge_dir)- from python.helpers.vector_db import Area+ from python.helpers.memory import Memory# Mapping file extensions to corresponding loader classesfile_types_loaders = {@@ -53,7 +53,7 @@ def load_knowledge(cnt_files = 0cnt_docs = 0- for area in Area:+ for area in Memory.Area:subdir = files.get_abs_path(knowledge_dir, area.value)if not os.path.exists(subdir):diff --git a/python/helpers/log.py b/python/helpers/log.pyindex 1c1efe836..03eb63fc8 100644--- a/python/helpers/log.py+++ b/python/helpers/log.py@@ -81,6 +81,7 @@ class Log:self.updates: list[int] = []self.logs: list[LogItem] = []self.progress = ""+ self.progress_no = 0def log(self,@@ -101,8 +102,9 @@ class Log:)self.logs.append(item)self.updates += [item.no]- if heading:+ if heading and item.no >= self.progress_no:self.progress = heading+ self.progress_no = item.noreturn itemdef update_item(@@ -120,7 +122,9 @@ class Log:item.type = typeif heading is not None:item.heading = heading- self.progress = heading+ if no >= self.progress_no:+ self.progress = heading+ self.progress_no = noif content is not None:item.content = contentif kvps is not None:@@ -156,3 +160,4 @@ class Log:self.updates = []self.logs = []self.progress = ""+ self.progress_no = 0diff --git a/python/helpers/memory.py b/python/helpers/memory.pynew file mode 100644index 000000000..6fa9b6026--- /dev/null+++ b/python/helpers/memory.py@@ -0,0 +1,306 @@+from datetime import datetime+from typing import Any+from langchain.storage import InMemoryByteStore, LocalFileStore+from langchain.embeddings import CacheBackedEmbeddings++# from langchain_chroma import Chroma+from langchain_community.vectorstores import FAISS+import faiss+from langchain_community.docstore.in_memory import InMemoryDocstore+from langchain_community.vectorstores.utils import (+ DistanceStrategy,+)+import os, json++import numpy as np+from . import files+from langchain_core.documents import Document+import uuid+from python.helpers import knowledge_import+from python.helpers.log import Log, LogItem+from enum import Enum+from agent import Agent+++class Memory:++ class Area(Enum):+ MAIN = "main"+ SOLUTIONS = "solutions"++ index: dict[str, "FAISS"] = {}++ @staticmethod+ async def get(agent: Agent):+ memory_subdir = agent.config.memory_subdir or "default"+ if Memory.index.get(memory_subdir) is None:+ log_item = agent.context.log.log(+ type="util",+ heading=f"Initializing VectorDB in '/{memory_subdir}'",+ )+ db = Memory.initialize(+ log_item,+ agent.config.embeddings_model,+ memory_subdir,+ False,+ )+ Memory.index[memory_subdir] = db+ wrap = Memory(agent, db, memory_subdir=memory_subdir)+ if agent.config.knowledge_subdirs:+ await wrap.preload_knowledge(+ log_item, agent.config.knowledge_subdirs, memory_subdir+ )+ return wrap+ else:+ return Memory(+ agent=agent,+ db=Memory.index[memory_subdir],+ memory_subdir=memory_subdir,+ )++ @staticmethod+ def initialize(+ log_item: LogItem | None,+ embeddings_model,+ memory_subdir: str,+ in_memory=False,+ ):++ print("Initializing VectorDB...")++ if log_item:+ log_item.stream(progress="\nInitializing VectorDB")++ em_dir = files.get_abs_path(+ "memory/embeddings"+ ) # just caching, no need to parameterize+ db_dir = Memory._abs_db_dir(memory_subdir)++ # make sure embeddings and database directories exist+ os.makedirs(db_dir, exist_ok=True)++ if in_memory:+ store = InMemoryByteStore()+ else:+ os.makedirs(em_dir, exist_ok=True)+ store = LocalFileStore(em_dir)++ # here we setup the embeddings model with the chosen cache storage+ embedder = CacheBackedEmbeddings.from_bytes_store(+ embeddings_model,+ store,+ namespace=getattr(+ embeddings_model,+ "model",+ getattr(embeddings_model, "model_name", "default"),+ ),+ )++ # self.db = Chroma(+ # embedding_function=self.embedder,+ # persist_directory=db_dir)++ # if db folder exists and is not empty:+ if os.path.exists(db_dir) and files.exists(db_dir, "index.faiss"):+ db = FAISS.load_local(+ folder_path=db_dir,+ embeddings=embedder,+ allow_dangerous_deserialization=True,+ distance_strategy=DistanceStrategy.COSINE,+ # normalize_L2=True,+ relevance_score_fn=Memory._cosine_normalizer,+ )+ else:+ index = faiss.IndexFlatIP(len(embedder.embed_query("example")))++ db = FAISS(+ embedding_function=embedder,+ index=index,+ docstore=InMemoryDocstore(),+ index_to_docstore_id={},+ distance_strategy=DistanceStrategy.COSINE,+ # normalize_L2=True,+ relevance_score_fn=Memory._cosine_normalizer,+ )+ return db++ def __init__(+ self,+ agent: Agent,+ db: FAISS,+ memory_subdir: str,+ ):+ self.agent = agent+ self.db = db+ self.memory_subdir = memory_subdir++ async def preload_knowledge(+ self, log_item: LogItem | None, kn_dirs: list[str], memory_subdir: str+ ):+ # db abs path+ db_dir = Memory._abs_db_dir(memory_subdir)++ # Load the index file if it exists+ index_path = files.get_abs_path(db_dir, "knowledge_import.json")++ # make sure directory exists+ if not os.path.exists(db_dir):+ os.makedirs(db_dir)++ index: dict[str, knowledge_import.KnowledgeImport] = {}+ if os.path.exists(index_path):+ with open(index_path, "r") as f:+ index = json.load(f)++ for kn_dir in kn_dirs:+ index = knowledge_import.load_knowledge(log_item, kn_dir, index)++ for file in index:+ if index[file]["state"] in ["changed", "removed"] and index[file].get(+ "ids", []+ ): # for knowledge files that have been changed or removed and have IDs+ await self.delete_documents_by_ids(+ index[file]["ids"]+ ) # remove original version+ if index[file]["state"] == "changed":+ index[file]["ids"] = self.insert_documents(+ index[file]["documents"]+ ) # insert new version++ # remove index where state="removed"+ index = {k: v for k, v in index.items() if v["state"] != "removed"}++ # strip state and documents from index and save it+ for file in index:+ if "documents" in index[file]:+ del index[file]["documents"] # type: ignore+ if "state" in index[file]:+ del index[file]["state"] # type: ignore+ with open(index_path, "w") as f:+ json.dump(index, f)++ async def search_similarity_threshold(+ self, query: str, limit: int, threshold: float, filter: str = ""+ ):+ comparator = Memory._get_comparator(filter) if filter else None+ return await self.db.asearch(+ query,+ search_type="similarity_score_threshold",+ k=limit,+ score_threshold=threshold,+ filter=comparator,+ )++ async def delete_documents_by_query(+ self, query: str, threshold: float, filter: str = ""+ ):+ k = 100+ tot = 0+ removed = []++ while True:+ # Perform similarity search with score+ docs = await self.search_similarity_threshold(+ query, limit=k, threshold=threshold, filter=filter+ )+ removed += docs++ # 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.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"])+ 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++ if tot:+ self._save_db() # persist+ return removed++ async 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+ if ids:+ self._save_db() # persist+ return len(ids)++ def insert_text(self, text, metadata: dict = {}):+ id = str(uuid.uuid4())+ self.db.add_documents(+ documents=[+ Document(+ text,+ metadata={"id": id, "timestamp": self.get_timestamp(), **metadata},+ )+ ],+ ids=[id],+ )+ self._save_db() # persist+ return id++ def insert_documents(self, docs: list[Document]):+ ids = [str(uuid.uuid4()) for _ in range(len(docs))]+ timestamp = self.get_timestamp()+ if ids:+ for doc, id in zip(docs, ids):+ doc.metadata["id"] = id # add ids to documents metadata+ doc.metadata["timestamp"] = timestamp # add timestamp+ self.db.add_documents(documents=docs, ids=ids)+ self._save_db() # persist+ return ids++ def _save_db(self):+ self.db.save_local(folder_path=self._abs_db_dir(self.memory_subdir))++ @staticmethod+ def _get_comparator(condition: str):+ def comparator(data: dict[str, Any]):+ try:+ return eval(condition, {}, data)+ except Exception as e:+ # print(f"Error evaluating condition: {e}")+ return False++ return comparator++ @staticmethod+ def _score_normalizer(val: float) -> float:+ res = 1 - 1 / (1 + np.exp(val))+ return res++ @staticmethod+ def _cosine_normalizer(val: float) -> float:+ res = (1 + val) / 2+ res = max(+ 0, min(1, res)+ ) # float precision can cause values like 1.0000000596046448+ return res++ @staticmethod+ def _abs_db_dir(memory_subdir: str) -> str:+ return files.get_abs_path("memory", memory_subdir)++ @staticmethod+ def format_docs_plain(docs: list[Document]) -> list[str]:+ result = []+ for doc in docs:+ text = ""+ for k, v in doc.metadata.items():+ text += f"{k}: {v}\n"+ text += f"Content: {doc.page_content}"+ result.append(text)+ return result++ @staticmethod+ def get_timestamp():+ return datetime.now().strftime("%Y-%m-%d %H:%M:%S")diff --git a/python/helpers/messages.py b/python/helpers/messages.pyindex fceccb132..6033e2532 100644--- a/python/helpers/messages.py+++ b/python/helpers/messages.py@@ -5,8 +5,8 @@ def truncate_text(agent, output, threshold=1000):return output# Adjust the file path as needed- placeholder = agent.read_prompt("fw.msg_truncated.md", removed_chars=(len(output) - threshold))- # placeholder = files.read_file("./prompts/default/fw.msg_truncated.md", removed_chars=(len(output) - threshold))+ placeholder = agent.read_prompt("fw.msg_truncated.md", length=(len(output) - threshold))+ # placeholder = files.read_file("./prompts/default/fw.msg_truncated.md", length=(len(output) - threshold))start_len = (threshold - len(placeholder)) // 2end_len = threshold - len(placeholder) - start_lendiff --git a/python/helpers/vector_db.py b/python/helpers/vector_db.pydeleted file mode 100644index 2456393a3..000000000--- a/python/helpers/vector_db.py+++ /dev/null@@ -1,234 +0,0 @@-from typing import Any-from langchain.storage import InMemoryByteStore, LocalFileStore-from langchain.embeddings import CacheBackedEmbeddings--# from langchain_chroma import Chroma-from langchain_community.vectorstores import FAISS-import faiss-from langchain_community.docstore.in_memory import InMemoryDocstore--import os, json-from . import files-from langchain_core.documents import Document-import uuid-from python.helpers import knowledge_import-from python.helpers.log import Log, LogItem-import pandas as pd-from enum import Enum---class Area(Enum):- MAIN = "main"- SOLUTIONS = "solutions"---index: dict[str, "VectorDB"] = {}---def get_or_create_db(- logger: Log | None,- embeddings_model,- memory_dir: str,- in_memory=False,- knowledge_dirs: list[str] = [],-):- if index.get(memory_dir) is None:- log_item = None- if(logger): log_item = logger.log(type="util", heading=f"Initializing VectorDB in {memory_dir}")- index[memory_dir] = VectorDB(- log_item, embeddings_model, memory_dir, knowledge_dirs, in_memory- )- return index[memory_dir]---class VectorDB:-- def __init__(- self,- log_item: LogItem | None,- embeddings_model,- memory_dir: str,- knowledge_dirs: list[str] = [],- in_memory=False,- ):- self.log_item = log_item-- print("Initializing VectorDB...")- if(self.log_item): self.log_item.stream(progress="\nInitializing VectorDB")-- self.embeddings_model = embeddings_model-- self.em_dir = files.get_abs_path(- "./memory/embeddings"- ) # just caching, no need to parameterize- self.db_dir = files.get_abs_path("./memory", memory_dir, "database")-- # make sure embeddings and database directories exist- os.makedirs(self.db_dir, exist_ok=True)-- if in_memory:- self.store = InMemoryByteStore()- else:- os.makedirs(self.em_dir, exist_ok=True)- self.store = LocalFileStore(self.em_dir)-- # here we setup the embeddings model with the chosen cache storage- self.embedder = CacheBackedEmbeddings.from_bytes_store(- embeddings_model,- self.store,- namespace=getattr(- embeddings_model,- "model",- getattr(embeddings_model, "model_name", "default"),- ),- )-- # self.db = Chroma(- # embedding_function=self.embedder,- # persist_directory=db_dir)-- # if db folder exists and is not empty:- if os.path.exists(self.db_dir) and files.exists(self.db_dir, "index.faiss"):- self.db = FAISS.load_local(- folder_path=self.db_dir,- embeddings=self.embedder,- allow_dangerous_deserialization=True,- )- else:- index = faiss.IndexFlatL2(len(self.embedder.embed_query("example text")))-- self.db = FAISS(- embedding_function=self.embedder,- index=index,- docstore=InMemoryDocstore(),- index_to_docstore_id={},- )-- # preload knowledge files- if knowledge_dirs:- self.preload_knowledge(knowledge_dirs, self.db_dir)-- def preload_knowledge(self, kn_dirs: list[str], db_dir: str):-- # Load the index file if it exists- index_path = files.get_abs_path(db_dir, "knowledge_import.json")-- # make sure directory exists- if not os.path.exists(db_dir):- os.makedirs(db_dir)-- index: dict[str, knowledge_import.KnowledgeImport] = {}- if os.path.exists(index_path):- with open(index_path, "r") as f:- index = json.load(f)-- for kn_dir in kn_dirs:- index = knowledge_import.load_knowledge(self.log_item, kn_dir, index)-- for file in index:- if index[file]["state"] in ["changed", "removed"] and index[file].get(- "ids", []- ): # for knowledge files that have been changed or removed and have IDs- self.delete_documents_by_ids(- index[file]["ids"]- ) # remove original version- if index[file]["state"] == "changed":- index[file]["ids"] = self.insert_documents(- index[file]["documents"]- ) # insert new version-- # remove index where state="removed"- index = {k: v for k, v in index.items() if v["state"] != "removed"}-- # strip state and documents from index and save it- for file in index:- if "documents" in index[file]:- del index[file]["documents"] # type: ignore- if "state" in index[file]:- del index[file]["state"] # type: ignore- with open(index_path, "w") as f:- json.dump(index, f)-- def search_similarity(self, query, results=3):- return self.db.similarity_search(query, results)-- def search_similarity_threshold(- self, query: str, results=3, threshold=0.5, filter: str = ""- ):- comparator = VectorDB.get_comparator(filter) if filter else None- return self.db.search(- query,- search_type="similarity_score_threshold",- k=results,- score_threshold=threshold,- filter=comparator,- )-- def search_max_rel(self, query, results=3):- return self.db.max_marginal_relevance_search(query, results)-- def delete_documents_by_query(self, query: str, threshold=0.1):- k = 100- tot = 0- while True:- # Perform similarity search with score- 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.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"])- 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-- if tot:- self.db.save_local(folder_path=self.db_dir) # persist- 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- if ids:- self.db.save_local(folder_path=self.db_dir) # persist- return len(ids)-- def insert_text(self, text, metadata: dict = {}):- id = str(uuid.uuid4())- self.db.add_documents(- documents=[Document(text, metadata={"id": id, **metadata})], ids=[id]- )- self.db.save_local(folder_path=self.db_dir) # persist- return id-- def insert_documents(self, docs: list[Document]):- ids = [str(uuid.uuid4()) for _ in range(len(docs))]- if ids:- for doc, id in zip(docs, ids):- doc.metadata["id"] = id # add ids to documents metadata- self.db.add_documents(documents=docs, ids=ids)- self.db.save_local(folder_path=self.db_dir) # persist- return ids-- @staticmethod- def get_comparator(condition: str):- def comparator(data: dict[str, Any]):- try:- return eval(condition, {}, data)- except Exception as e:- print(f"Error evaluating condition: {e}")- return False-- return comparatordiff --git a/python/tools/call_subordinate.py b/python/tools/call_subordinate.pyindex d754590a0..227e87a03 100644--- a/python/tools/call_subordinate.py+++ b/python/tools/call_subordinate.py@@ -10,4 +10,5 @@ class Delegation(Tool):subordinate.set_data("superior", self.agent)self.agent.set_data("subordinate", subordinate)# run subordinate agent message loop- return Response( message= await self.agent.get_data("subordinate").message_loop(message), break_loop=False)\ No newline at end of file+ subordinate: Agent = self.agent.get_data("subordinate")+ return Response( message= await subordinate.monologue(message), break_loop=False)\ No newline at end of filediff --git a/python/tools/knowledge_tool.py b/python/tools/knowledge_tool.pyindex b0e9029da..d4b09744f 100644--- a/python/tools/knowledge_tool.py+++ b/python/tools/knowledge_tool.py@@ -1,55 +1,56 @@import os-from python.helpers import perplexity_search-from python.helpers import duckduckgo_search-from . import memory_tool-import concurrent.futures+import asyncio+from python.helpers import memory, perplexity_search, duckduckgo_searchfrom python.helpers.tool import Tool, Responsefrom python.helpers.print_style import PrintStylefrom python.helpers.errors import handle_errorclass Knowledge(Tool):async def execute(self, question="", **kwargs):- with concurrent.futures.ThreadPoolExecutor() as executor:- # Schedule the two functions to be run in parallel-- # perplexity search, if API key provided- if os.getenv("API_KEY_PERPLEXITY"):- perplexity = executor.submit(perplexity_search.perplexity_search, question)- else:- PrintStyle.hint("No API key provided for Perplexity. Skipping Perplexity search.")- self.agent.context.log.log(type="hint", content="No API key provided for Perplexity. Skipping Perplexity search.")- perplexity = None--- # duckduckgo search- duckduckgo = executor.submit(duckduckgo_search.search, question)-- # manual memory search- future_memory_man = executor.submit(memory_tool.search, self.agent, question)-- # Wait for both functions to complete- try:- perplexity_result = (perplexity.result() if perplexity else "") or ""- except Exception as e:- handle_error(e)- perplexity_result = "Perplexity search failed: " + str(e)-- try:- duckduckgo_result = duckduckgo.result()- except Exception as e:- handle_error(e)- duckduckgo_result = "DuckDuckGo search failed: " + str(e)-- try:- memory_result = future_memory_man.result()- except Exception as e:- handle_error(e)- memory_result = "Memory search failed: " + str(e)+ # Create tasks for all three search methods+ tasks = [+ self.perplexity_search(question),+ self.duckduckgo_search(question),+ self.mem_search(question)+ ]++ # Run all tasks concurrently+ results = await asyncio.gather(*tasks, return_exceptions=True)++ perplexity_result, duckduckgo_result, memory_result = results++ # Handle exceptions and format results+ perplexity_result = self.format_result(perplexity_result, "Perplexity")+ duckduckgo_result = self.format_result(duckduckgo_result, "DuckDuckGo")+ memory_result = self.format_result(memory_result, "Memory")msg = self.agent.read_prompt("tool.knowledge.response.md",- online_sources = ((perplexity_result + "\n\n") if perplexity else "") + str(duckduckgo_result),- memory = memory_result )+ online_sources = ((perplexity_result + "\n\n") if perplexity_result else "") + str(duckduckgo_result),+ memory = memory_result)- await self.agent.handle_intervention(msg) # wait for intervention and handle it, if paused+ await self.agent.handle_intervention(msg) # wait for intervention and handle it, if pausedreturn Response(message=msg, break_loop=False)++ async def perplexity_search(self, question):+ if os.getenv("API_KEY_PERPLEXITY"):+ return await asyncio.to_thread(perplexity_search.perplexity_search, question)+ else:+ PrintStyle.hint("No API key provided for Perplexity. Skipping Perplexity search.")+ self.agent.context.log.log(type="hint", content="No API key provided for Perplexity. Skipping Perplexity search.")+ return None++ async def duckduckgo_search(self, question):+ return await asyncio.to_thread(duckduckgo_search.search, question)++ async def mem_search(self, question: str):+ db = await memory.Memory.get(self.agent)+ docs = await db.search_similarity_threshold(query=question, limit=5, threshold=0.5)+ text = memory.Memory.format_docs_plain(docs)+ return "\n\n".join(text)++ def format_result(self, result, source):+ if isinstance(result, Exception):+ handle_error(result)+ return f"{source} search failed: {str(result)}"+ return result if result else ""\ No newline at end of filediff --git a/python/tools/memory_delete.py b/python/tools/memory_delete.pynew file mode 100644index 000000000..b692c5430--- /dev/null+++ b/python/tools/memory_delete.py@@ -0,0 +1,11 @@+from python.helpers.memory import Memory+from python.helpers.tool import Tool, Response++class MemoryForget(Tool):++ async def execute(self, ids=[], **kwargs):+ db = await Memory.get(self.agent)+ dels = await db.delete_documents_by_ids(ids=ids)++ result = self.agent.read_prompt("fw.memories_deleted.md", memory_count=dels)+ return Response(message=result, break_loop=False)\ No newline at end of filediff --git a/python/tools/memory_forget.py b/python/tools/memory_forget.pynew file mode 100644index 000000000..dd97a95ec--- /dev/null+++ b/python/tools/memory_forget.py@@ -0,0 +1,13 @@+from python.helpers.memory import Memory+from python.helpers.tool import Tool, Response++DEFAULT_THRESHOLD = 0.75++class MemoryForget(Tool):++ async def execute(self, query="", threshold=DEFAULT_THRESHOLD, filter="", **kwargs):+ db = await Memory.get(self.agent)+ dels = await db.delete_documents_by_query(query=query, threshold=threshold, filter=filter)++ result = self.agent.read_prompt("fw.memories_deleted.md", memory_count=len(dels))+ return Response(message=result, break_loop=False)\ No newline at end of filediff --git a/python/tools/memory_load.py b/python/tools/memory_load.pynew file mode 100644index 000000000..de08459fd--- /dev/null+++ b/python/tools/memory_load.py@@ -0,0 +1,19 @@+from python.helpers.memory import Memory+from python.helpers.tool import Tool, Response++DEFAULT_THRESHOLD = 0.6+DEFAULT_LIMIT = 10++class MemoryLoad(Tool):++ async def execute(self, query="", threshold=DEFAULT_THRESHOLD, limit=DEFAULT_LIMIT, filter="", **kwargs):+ db = await Memory.get(self.agent)+ docs = await db.search_similarity_threshold(query=query, limit=limit, threshold=threshold, filter=filter)++ if len(docs) == 0:+ result = self.agent.read_prompt("fw.memories_not_found.md", query=query)+ else:+ text = "\n\n".join(Memory.format_docs_plain(docs))+ result = str(text)++ return Response(message=result, break_loop=False)\ No newline at end of filediff --git a/python/tools/memory_save.py b/python/tools/memory_save.pynew file mode 100644index 000000000..be2b86cca--- /dev/null+++ b/python/tools/memory_save.py@@ -0,0 +1,20 @@+from python.helpers.memory import Memory+from python.helpers.tool import Tool, Response++DEFAULT_THRESHOLD = 0.5+DEFAULT_LIMIT = 5++class MemorySave(Tool):++ async def execute(self, text="", area="", **kwargs):++ if not area:+ area = Memory.Area.MAIN.value++ metadata = {"area": area, **kwargs}++ db = await Memory.get(self.agent)+ id = db.insert_text(text, metadata)++ result = self.agent.read_prompt("fw.memory_saved.md", memory_id=id)+ return Response(message=result, break_loop=False)diff --git a/python/tools/memory_tool.py b/python/tools/memory_tool.pydeleted file mode 100644index 11cfdf005..000000000--- a/python/tools/memory_tool.py+++ /dev/null@@ -1,75 +0,0 @@-import re-from agent import Agent-from python.helpers.vector_db import get_or_create_db-import os-from python.helpers.tool import Tool, Response-from python.helpers.print_style import PrintStyle-from python.helpers.errors import handle_error-from python.helpers import files--class Memory(Tool):- async def execute(self,**kwargs):- result=""-- area = kwargs.get("area", "manual") # when called by agent, it will always be manual-- try:- if "query" in kwargs:- threshold = float(kwargs.get("threshold", 0.1))- count = int(kwargs.get("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"])- except Exception as e:- handle_error(e)- # hint about embedding change with existing database- PrintStyle.hint("If you changed your embedding model, you will need to remove contents of /memory directory.")- self.agent.context.log.log(type="hint", content="If you changed your embedding model, you will need to remove contents of /memory directory.")- raise-- # 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):- db = get_db(agent)- # docs = db.search_similarity(query,count) # type: ignore- docs = db.search_similarity_threshold(query,count,threshold) # type: ignore- if len(docs)==0: return agent.read_prompt("fw.memories_not_found.md", query=query)- else: return str(docs)--def save(agent:Agent, text:str):- db = get_db(agent)- id = db.insert_text(text) # type: ignore- return agent.read_prompt("fw.memory_saved.md", memory_id=id)--def delete(agent:Agent, ids_str:str):- db = get_db(agent)- ids = extract_guids(ids_str)- deleted = db.delete_documents_by_ids(ids) # type: ignore- return agent.read_prompt("fw.memories_deleted.md", memory_count=deleted)--def forget(agent:Agent, query:str):- db = get_db(agent)- deleted = db.delete_documents_by_query(query) # type: ignore- return agent.read_prompt("fw.memories_deleted.md", memory_count=deleted)--def get_db(agent: Agent):- mem_dir = files.get_abs_path("memory", agent.config.memory_subdir or "default")- kn_dirs = [files.get_abs_path("knowledge", d) for d in agent.config.knowledge_subdirs or []]-- db = get_or_create_db(- agent.context.log,- embeddings_model=agent.config.embeddings_model,- in_memory=False,- memory_dir=mem_dir,- knowledge_dirs=kn_dirs)-- return db--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 filediff --git a/python/tools/memory_tool.py.txt b/python/tools/memory_tool.py.txtnew file mode 100644index 000000000..63a7637c1--- /dev/null+++ b/python/tools/memory_tool.py.txt@@ -0,0 +1,92 @@+import re+from agent import Agent+from python.helpers.vector_db import get_or_create_db, Area+import os+from python.helpers.tool import Tool, Response+from python.helpers.print_style import PrintStyle+from python.helpers.errors import handle_error+from python.helpers import files++DEFAULT_THRESHOLD = 0.5+++class Memory(Tool):++ async def execute(self, **kwargs):+ result = ""++ try:+ if "query" in kwargs:+ threshold = float(kwargs.get("threshold", DEFAULT_THRESHOLD))+ count = int(kwargs.get("limit", 5))+ result = search(self.agent, kwargs["query"], count, threshold)+ elif "memorize" in kwargs:+ meta = {"area": Area.MAIN.value}+ 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"])+ except Exception as e:+ handle_error(e)+ # hint about embedding change with existing database+ PrintStyle.hint(+ "If you changed your embedding model, you will need to remove contents of /memory directory."+ )+ self.agent.context.log.log(+ type="hint",+ content="If you changed your embedding model, you will need to remove contents of /memory directory.",+ )+ raise++ # 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 = DEFAULT_THRESHOLD+):+ db = get_db(agent)+ # docs = db.search_similarity(query,count) # type: ignore+ docs = db.search_similarity_threshold(query=query, limit=count, threshold=threshold) # type: ignore+ if len(docs) == 0:+ return agent.read_prompt("fw.memories_not_found.md", query=query)+ else:+ return str(docs)+++def save(agent: Agent, text: str, metadata: dict = {}):+ db = get_db(agent)+ id = db.insert_text(text, metadata) # type: ignore+ return agent.read_prompt("fw.memory_saved.md", memory_id=id)+++def delete(agent: Agent, ids_str: str):+ db = get_db(agent)+ ids = extract_guids(ids_str)+ deleted = db.delete_documents_by_ids(ids) # type: ignore+ return agent.read_prompt("fw.memories_deleted.md", memory_count=deleted)+++def forget(agent: Agent, query: str):+ db = get_db(agent)+ deleted = db.delete_documents_by_query(query) # type: ignore+ return agent.read_prompt("fw.memories_deleted.md", memory_count=deleted)+++def get_db(agent: Agent):+ mem_dir = files.get_abs_path("memory", agent.config.memory_subdir or "default")+ kn_dirs = [+ files.get_abs_path("knowledge", d) for d in agent.config.knowledge_subdirs or []+ ]++ db = get_or_create_db(+ agent=agent,+ )++ return db+++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)diff --git a/python/tools/unknown.py b/python/tools/unknown.pyindex 3f52c9af1..96c7f856f 100644--- a/python/tools/unknown.py+++ b/python/tools/unknown.py@@ -1,10 +1,15 @@from python.helpers.tool import Tool, Response+from python.extensions.message_loop_prompts._10_tool_instructions import (+ concat_tool_prompts,+)+class Unknown(Tool):async def execute(self, **kwargs):+ tools = concat_tool_prompts(self.agent)return Response(- message=self.agent.read_prompt("fw.tool_not_found.md",- tool_name=self.name,- tools_prompt=self.agent.read_prompt("agent.tools.md")),- break_loop=False)-+ message=self.agent.read_prompt(+ "fw.tool_not_found.md", tool_name=self.name, tools_prompt=tools+ ),+ break_loop=False,+ )diff --git a/requirements.txt b/requirements.txtindex b32c05443..08ef38c7a 100644--- a/requirements.txt+++ b/requirements.txt@@ -12,7 +12,6 @@ langchain_mistralai==0.1.8webcolors==24.6.0sentence-transformers==3.0.1docker==7.1.0-pandas==2.2.3paramiko==3.4.0duckduckgo_search==6.1.12inputimeout==1.0.4diff --git a/webui/index.css b/webui/index.cssindex 531f51da8..eb3c83f4b 100644--- a/webui/index.css+++ b/webui/index.css@@ -15,7 +15,7 @@--color-input-focus-dark: #1b1b1b;/* Light mode */- --color-background-light: #F5F7FA;+ --color-background-light: #e8e9e9;--color-text-light: #333333;--color-primary-light: #273b4d;--color-secondary-light: #e8eaf6;@@ -312,6 +312,7 @@ h4 {.message-user {background-color: #4a4a4a;border-bottom-right-radius: var(--spacing-xs);+ text-align: end;}.message-ai {@@ -345,15 +346,15 @@ h4 {/* Update message types for dark mode */.message-default { background-color: #1A242F; color: #E0E0E0; }-.message-agent { background-color: #2C3E50; color: #E0E0E0; }-.message-agent-response { background-color: #002b54; color: #E0E0E0; }-.message-agent-delegation { background-color: #00695C; color: #E0E0E0; }-.message-tool { background-color: #5D4C6A; color: #E0E0E0; }-.message-code-exe { background-color: #3a147c; color: #E0E0E0; }+.message-agent { background-color: #34506b; color: #E0E0E0; }+.message-agent-response { background-color: #1f3c1e; color: #E0E0E0; }+.message-agent-delegation { background-color: #12685e; color: #E0E0E0; }+.message-tool { background-color: #2a4170; color: #E0E0E0; }+.message-code-exe { background-color: #4b3a69; color: #E0E0E0; }.message-info { background-color: var(--color-panel); color: #E0E0E0; }.message-util { background-color: #23211a; color: #E0E0E0; display:none }-.message-warning { background-color: #c2771b; color: #E0E0E0; }-.message-error { background-color: #ab1313; color: #E0E0E0; }+.message-warning { background-color: #bc8036; color: #E0E0E0; }+.message-error { background-color: #af2222; color: #E0E0E0; }/* Agent and AI Info */.agent-start {@@ -615,18 +616,18 @@ input:checked + .slider:before {--color-input-focus: var(--color-input-focus-light);}-.light-mode .message-default { background-color: #E3F2FD; color: #1A242F; }-.light-mode .message-agent { background-color: #e9ebf3; color: #2C3E50; }-.light-mode .message-agent-response { background-color: #E1E6F2; color: #002b54; }-.light-mode .message-agent-delegation { background-color: #E0F2F1; color: #00695C; }-.light-mode .message-tool { background-color: #EDE7F6; color: #5D4C6A; }-.light-mode .message-code-exe { background-color: #FCE4EC; color: #3a147c; }-.light-mode .message-info { background-color: #E8EAF6; color: #2C3E50; }-.light-mode .message-util { background-color: #e8eaf6d6; color: #353c43; }-.light-mode .message-warning { background-color: #FFF3E0; color: #c2771b; }-.light-mode .message-error { background-color: #FFEBEE; color: #ab1313; }+.light-mode .message-default { background-color: #ffffff; color: #1A242F; }+.light-mode .message-agent { background-color: #ffffff; color: #356ca3; }+.light-mode .message-agent-response { background-color: #ffffff; color: #188216; }+.light-mode .message-agent-delegation { background-color: #ffffff; color: #12685e; }+.light-mode .message-tool { background-color: #ffffff; color: #1c3c88; }+.light-mode .message-code-exe { background-color: #ffffff; color: #6c43b0; }+.light-mode .message-info { background-color: #ffffff; color: #3f3f3f; }+.light-mode .message-util { background-color: #ffffff; color: #5b5540; }+.light-mode .message-warning { background-color: #ffffff; color: #8f4800; }+.light-mode .message-error { background-color: #ffffff; color: #8f1010; }.light-mode .message-user {- background-color: #eaeaea;+ background-color: #ffffff;color: #4e4e4e;}diff --git a/webui/index.html b/webui/index.htmlindex 09b7a118f..016afb559 100644--- a/webui/index.html+++ b/webui/index.html@@ -70,7 +70,7 @@<li x-data="{ autoScroll: true }"><span>Autoscroll</span><label class="switch">- <input type="checkbox" x-model="autoScroll"+ <input id="auto-scroll-switch" type="checkbox" x-model="autoScroll"x-effect="window.safeCall('toggleAutoScroll',autoScroll)"><span class="slider"></span></label>@@ -123,14 +123,14 @@<!--Chat--><div id="chat-history"></div>- <div id="progress-bar-box">- <h4 id="progress-bar-h"><span id="progress-bar-i">|></span><span id="progress-bar"></span></h4>- </div><div id="toast" class="toast"><div class="toast__message"></div><button class="toast__copy">Copy</button><button class="toast__close">Close</button></div>+ <div id="progress-bar-box">+ <h4 id="progress-bar-h"><span id="progress-bar-i">|></span><span id="progress-bar"></span></h4>+ </div><div id="input-section" x-data="{ paused: false }"><textarea id="chat-input" placeholder="Type your message here..." rows="1"></textarea><button class="chat-button" id="send-button" aria-label="Send message">diff --git a/webui/index.js b/webui/index.jsindex 7afed4769..56ecdd83f 100644--- a/webui/index.js+++ b/webui/index.js@@ -11,6 +11,7 @@ const statusSection = document.getElementById('status-section');const chatsSection = document.getElementById('chats-section');const scrollbarThumb = document.querySelector('#chat-history::-webkit-scrollbar-thumb');const progressBar = document.getElementById('progress-bar');+const autoScrollSwitch = document.getElementById('auto-scroll-switch');@@ -395,6 +396,23 @@ function toast(text, type = 'info') {}, 10000);}+function scrollChanged(isAtBottom) {+ const inputAS = Alpine.$data(autoScrollSwitch);+ inputAS.autoScroll = isAtBottom+ // autoScrollSwitch.checked = isAtBottom+ console.log(isAtBottom)+}++chatHistory.addEventListener('scroll', function () {+ // const toleranceEm = 1; // Tolerance in em units+ // const tolerancePx = toleranceEm * parseFloat(getComputedStyle(document.documentElement).fontSize); // Convert em to pixels+ const tolerancePx = 50;+ const chatHistory = document.getElementById('chat-history');+ const isAtBottom = (chatHistory.scrollHeight - chatHistory.scrollTop) <= (chatHistory.clientHeight + tolerancePx);++ scrollChanged(isAtBottom);+});+chatInput.addEventListener('input', adjustTextareaHeight);setInterval(poll, 250);