Agent Zero AI framework
docker files, agent number fix, json misformat message
frdelef22702eff5b
diff --git a/agent.py b/agent.pyindex 8b5993f17..1563ecfed 100644--- a/agent.py+++ b/agent.py@@ -15,7 +15,6 @@ from python.helpers.rate_limiter import RateLimiter@dataclassclass AgentConfig:- agent_number: intchat_model:BaseChatModelembeddings_model:Embeddingsmemory_subdir: str = ""@@ -54,7 +53,7 @@ class Agent:# non-config varsself.number = number- self.agent_name = f"Agent {self.config.agent_number}"+ self.agent_name = f"Agent {self.number}"self.system_prompt = files.read_file("./prompts/agent.system.md").replace("{", "{{").replace("}", "}}")self.tools_prompt = files.read_file("./prompts/agent.tools.md").replace("{", "{{").replace("}", "}}")@@ -236,20 +235,26 @@ class Agent:def process_tools(self, msg: str):# search for tool usage requests in agent messagetool_request = extract_tools.json_parse_dirty(msg)- tool_name = tool_request.get("tool_name", "")- tool_args = tool_request.get("tool_args", {})- tool = self.get_tool(- tool_name,- tool_args,- msg)+ if tool_request is not None:+ tool_name = tool_request.get("tool_name", "")+ tool_args = tool_request.get("tool_args", {})++ tool = self.get_tool(+ tool_name,+ tool_args,+ msg)++ if self.handle_intervention(): return # wait if paused and handle intervention message if needed- if self.handle_intervention(): return # wait if paused and handle intervention message if needed-- tool.before_execution(**tool_args)- response = tool.execute(**tool_args)- tool.after_execution(response)- if response.break_loop: return response.message+ tool.before_execution(**tool_args)+ response = tool.execute(**tool_args)+ tool.after_execution(response)+ if response.break_loop: return response.message+ else:+ msg = files.read_file("prompts/fw.msg_misformat.md")+ self.append_message(msg, human=True)+ PrintStyle(font_color="red", padding=True).print(msg)def get_tool(self, name: str, args: dict, message: str, **kwargs):diff --git a/docker/.bashrc b/docker/.bashrcnew file mode 100644index 000000000..88a273c5f--- /dev/null+++ b/docker/.bashrc@@ -0,0 +1,9 @@+# .bashrc++# Source global definitions+if [ -f /etc/bashrc ]; then+ . /etc/bashrc+fi++# Activate the virtual environment+source /opt/venv/bin/activatediff --git a/docker/Dockerfile b/docker/Dockerfilenew file mode 100644index 000000000..6afc9f2d7--- /dev/null+++ b/docker/Dockerfile@@ -0,0 +1,43 @@+# Use the latest slim version of Debian+FROM --platform=$TARGETPLATFORM debian:bookworm-slim++# Set ARG for platform-specific commands+ARG TARGETPLATFORM++# Update and install necessary packages+RUN apt-get update && apt-get install -y \+ python3 \+ python3-pip \+ python3-venv \+ nodejs \+ npm \+ openssh-server \+ sudo \+ && rm -rf /var/lib/apt/lists/*++# Set up SSH+RUN mkdir /var/run/sshd && \+ echo 'root:toor' | chpasswd && \+ sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config++# Create and activate Python virtual environment+ENV VIRTUAL_ENV=/opt/venv+RUN python3 -m venv $VIRTUAL_ENV++# Copy initial .bashrc with virtual environment activation to a temporary location+COPY .bashrc /etc/skel/.bashrc++# Copy the script to ensure .bashrc is in the root directory+COPY initialize.sh /usr/local/bin/initialize.sh+RUN chmod +x /usr/local/bin/initialize.sh++# Ensure the virtual environment and pip setup+RUN $VIRTUAL_ENV/bin/pip install --upgrade pip++# Expose SSH port+EXPOSE 22++# Init .bashrc+CMD ["/usr/local/bin/initialize.sh"]++diff --git a/docker/initialize.sh b/docker/initialize.shnew file mode 100644index 000000000..63216db4f--- /dev/null+++ b/docker/initialize.sh@@ -0,0 +1,16 @@+#!/bin/bash++# Ensure .bashrc is in the root directory+if [ ! -f /root/.bashrc ]; then+ cp /etc/skel/.bashrc /root/.bashrc+ chmod 444 /root/.bashrc+fi++# Ensure .profile is in the root directory+if [ ! -f /root/.profile ]; then+ cp /etc/skel/.bashrc /root/.profile+ chmod 444 /root/.profile+fi++# Start SSH service+exec /usr/sbin/sshd -Ddiff --git a/main.py b/main.pyindex 60b1da7c7..7662d2528 100644--- a/main.py+++ b/main.py@@ -9,7 +9,6 @@ from python.helpers import filesinput_lock = threading.Lock()-os.chdir(files.get_abs_path("./work_dir")) #change CWD to work_dir@@ -25,6 +24,10 @@ def initialize():# chat_llm = models.get_anthropic_sonnet_35(temperature=0)# chat_llm = models.get_anthropic_haiku(temperature=0)# chat_llm = models.get_ollama_dolphin()+ # chat_llm = models.get_ollama(model_name="gemma2:27b")+ # chat_llm = models.get_ollama(model_name="llama3:8b-text-fp16")+ # chat_llm = models.get_ollama(model_name="gemma2:latest")+ # chat_llm = models.get_ollama(model_name="qwen:14b")# embedding model used for memory# embedding_llm = models.get_embedding_openai()@@ -32,7 +35,6 @@ def initialize():# agent configurationconfig = AgentConfig(- agent_number = 0,chat_model = chat_llm,embeddings_model = embedding_llm,# memory_subdir = "",diff --git a/models.py b/models.pyindex 2174eaebd..40def8086 100755--- a/models.py+++ b/models.py@@ -68,6 +68,9 @@ def get_groq_llama8b(api_key=None, temperature=DEFAULT_TEMPERATURE):api_key = api_key or get_api_key("groq")return ChatGroq(model_name="Llama3-8b-8192", temperature=temperature, api_key=api_key) # type: ignore+def get_ollama(model_name, temperature=DEFAULT_TEMPERATURE):+ return Ollama(model=model_name,temperature=temperature)+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: ignorediff --git a/prompts/agent.tools.md b/prompts/agent.tools.mdindex 2314f1fb5..3c4e0101b 100644--- a/prompts/agent.tools.md+++ b/prompts/agent.tools.md@@ -86,6 +86,7 @@ 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".+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.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.diff --git a/prompts/fw.msg_misformat.md b/prompts/fw.msg_misformat.mdnew file mode 100644index 000000000..4adabcd7d--- /dev/null+++ b/prompts/fw.msg_misformat.md@@ -0,0 +1,5 @@+~~~json+{+ "system_warning": "You have misformatted your message. Follow system prompt instructions on JSON message formatting precisely."+}+~~~\ No newline at end of filediff --git a/python/helpers/docker.py b/python/helpers/docker.pyindex 85a245c3f..80d599ec5 100644--- a/python/helpers/docker.py+++ b/python/helpers/docker.py@@ -35,7 +35,7 @@ class DockerContainerManager:#print(f"Container with name '{self.name}' is already running with ID: {existing_container.id}")passelse:- print(f"Initializing docker container {self.name}...")+ print(f"Initializing docker container {self.name} for safe code execution...")self.container = self.client.containers.run(self.image,detach=True,diff --git a/python/helpers/extract_tools.py b/python/helpers/extract_tools.pyindex 16101b9be..86289cc1b 100644--- a/python/helpers/extract_tools.py+++ b/python/helpers/extract_tools.py@@ -6,17 +6,17 @@ from .dirty_json import DirtyJsonimport regex-def json_parse_dirty(json:str) -> dict[str,Any]:+def json_parse_dirty(json:str) -> dict[str,Any] | None:ext_json = extract_json_object_string(json)- # ext_json = fix_json_string(ext_json)- data = DirtyJson.parse_string(ext_json)- if isinstance(data,dict): return data- return {}+ if ext_json:+ # ext_json = fix_json_string(ext_json)+ data = DirtyJson.parse_string(ext_json)+ if isinstance(data,dict): return data+ return Nonedef extract_json_object_string(content):start = content.find('{')if start == -1:- print("No JSON content found.")return ""# Find the first '{'