Skip to content

Commit

Permalink
Merge branch 'development' into testing
Browse files Browse the repository at this point in the history
  • Loading branch information
frdel committed Jul 14, 2024
2 parents 453b91a + 947aa9b commit 65bc834
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 215 deletions.
110 changes: 66 additions & 44 deletions agent.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from dataclasses import dataclass, field
import time, importlib, inspect, os, json
from typing import Any, Optional, Dict
from python.helpers import extract_tools, rate_limiter, files, errors
from python.helpers.print_style import PrintStyle
import traceback
from typing import Optional, Dict, TypedDict
from tools.helpers import extract_tools, rate_limiter, files, errors
from tools.helpers.print_style import PrintStyle
from langchain.schema import AIMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.embeddings import Embeddings
from tools.helpers.rate_limiter import RateLimiter

# rate_limit = rate_limiter.rate_limiter(30,160000) #TODO! implement properly

<<<<<<< Updated upstream
=======
@dataclass
class AgentConfig:
chat_model:BaseChatModel
Expand All @@ -26,6 +29,7 @@ class AgentConfig:
msgs_keep_max: int = 25
msgs_keep_start: int = 5
msgs_keep_end: int = 10
response_timeout_seconds: int = 60
max_tool_response_length: int = 3000
code_exec_docker_enabled: bool = True
code_exec_docker_name: str = "agent-zero-exe"
Expand All @@ -39,20 +43,48 @@ class AgentConfig:
code_exec_ssh_pass: str = "toor"
additional: Dict[str, Any] = field(default_factory=dict)

>>>>>>> Stashed changes

class Agent:

paused=False
streaming_agent=None

def __init__(self, number:int, config: AgentConfig):

# agent config
self.config = config
def __init__(self,
agent_number: int,
chat_model:BaseChatModel,
embeddings_model:Embeddings,
memory_subdir: str = "",
auto_memory_count: int = 3,
auto_memory_skip: int = 2,
rate_limit_seconds: int = 60,
rate_limit_requests: int = 30,
rate_limit_input_tokens: int = 0,
rate_limit_output_tokens: int = 0,
msgs_keep_max: int = 25,
msgs_keep_start: int = 5,
msgs_keep_end: int = 10,
max_tool_response_length: int = 3000,
**kwargs):

# agent config
self.agent_number = agent_number
self.chat_model = chat_model
self.embeddings_model = embeddings_model
self.memory_subdir = memory_subdir
self.auto_memory_count = auto_memory_count
self.auto_memory_skip = auto_memory_skip
self.rate_limit_seconds = rate_limit_seconds
self.rate_limit_requests = rate_limit_requests
self.rate_limit_input_tokens = rate_limit_input_tokens
self.rate_limit_output_tokens = rate_limit_output_tokens
self.msgs_keep_max = msgs_keep_max
self.msgs_keep_start = msgs_keep_start
self.msgs_keep_end = msgs_keep_end
self.max_tool_response_length = max_tool_response_length

# non-config vars
self.number = number
self.agent_name = f"Agent {self.number}"
self.agent_name = f"Agent {self.agent_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("}", "}}")
Expand All @@ -61,7 +93,7 @@ def __init__(self, number:int, config: AgentConfig):
self.last_message = ""
self.intervention_message = ""
self.intervention_status = False
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.rate_limiter = RateLimiter(max_calls=rate_limit_requests,max_input_tokens=rate_limit_input_tokens,max_output_tokens=rate_limit_output_tokens,window_seconds=rate_limit_seconds)
self.data = {} # free data object all the tools can use

os.chdir(files.get_abs_path("./work_dir")) #change CWD to work_dir
Expand Down Expand Up @@ -90,7 +122,7 @@ def message_loop(self, msg: str):
MessagesPlaceholder(variable_name="messages") ])

inputs = {"messages": self.history}
chain = prompt | self.config.chat_model
chain = prompt | self.chat_model

formatted_inputs = prompt.format(messages=self.history)
tokens = int(len(formatted_inputs)/4)
Expand Down Expand Up @@ -147,7 +179,7 @@ def append_message(self, msg: str, human: bool = False):
else:
new_message = HumanMessage(content=msg) if human else AIMessage(content=msg)
self.history.append(new_message)
self.cleanup_history(self.config.msgs_keep_max, self.config.msgs_keep_start, self.config.msgs_keep_end)
self.cleanup_history(self.msgs_keep_max, self.msgs_keep_start, self.msgs_keep_end)
if message_type=="ai":
self.last_message = msg

Expand All @@ -159,7 +191,7 @@ def send_adhoc_message(self, system: str, msg: str, output_label:str):
SystemMessage(content=system),
HumanMessage(content=msg)])

chain = prompt | self.config.utility_model
chain = prompt | self.chat_model
response = ""
printer = None

Expand Down Expand Up @@ -234,35 +266,29 @@ def handle_intervention(self, progress:str="") -> bool:
def process_tools(self, msg: str):
# search for tool usage requests in agent message
tool_request = extract_tools.json_parse_dirty(msg)
tool_name = tool_request.get("tool_name", "")
tool_args = tool_request.get("tool_args", {})

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
tool = self.get_tool(
tool_name,
tool_args,
msg)

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)
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


def get_tool(self, name: str, args: dict, message: str, **kwargs):
from python.tools.unknown import Unknown
from python.helpers.tool import Tool
from tools.unknown import Unknown
from tools.helpers.tool import Tool

tool_class = Unknown
if files.exists("python/tools",f"{name}.py"):
module = importlib.import_module("python.tools." + name) # Import the module
if files.exists("tools",f"{name}.py"):
module = importlib.import_module("tools." + name) # Import the module
class_list = inspect.getmembers(module, inspect.isclass) # Get all functions in the module

for cls in class_list:
Expand All @@ -273,24 +299,20 @@ def get_tool(self, name: str, args: dict, message: str, **kwargs):
return tool_class(agent=self, name=name, args=args, message=message, **kwargs)

def fetch_memories(self,reset_skip=False):
if self.config.auto_memory_count<=0: return ""
if reset_skip: self.memory_skip_counter = 0

if self.memory_skip_counter > 0:
self.memory_skip_counter-=1
return ""
else:
self.memory_skip_counter = self.config.auto_memory_skip
from python.tools import memory_tool
self.memory_skip_counter = self.auto_memory_skip
from tools import memory_tool
messages = self.concat_messages(self.history)
memories = memory_tool.process_query(self,messages,"load")
memories = memory_tool.search(messages)
input = {
"conversation_history" : messages,
"raw_memories": memories
}
cleanup_prompt = files.read_file("./prompts/msg.memory_cleanup.md").replace("{", "{{")
clean_memories = self.send_adhoc_message(cleanup_prompt,json.dumps(input), output_label="Memory injection")
return clean_memories

def call_extension(self, name: str, **kwargs) -> Any:
pass
return clean_memories
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use the latest slim version of Debian
FROM --platform=$TARGETPLATFORM debian:bookworm-slim
FROM --platform=$TARGETPLATFORM debian:testing-slim

# Set ARG for platform-specific commands
ARG TARGETPLATFORM
Expand Down
Loading

0 comments on commit 65bc834

Please sign in to comment.