Adding support for agents without tools

This commit is contained in:
João Moura
2024-02-24 01:39:29 -03:00
parent 101b80c234
commit 7f98a99e90
2 changed files with 19 additions and 8 deletions

View File

@@ -153,6 +153,7 @@ class Agent(BaseModel):
) )
tools = self._parse_tools(tools or self.tools) tools = self._parse_tools(tools or self.tools)
self.create_agent_executor(tools=tools)
self.agent_executor.tools = tools self.agent_executor.tools = tools
self.agent_executor.task = task self.agent_executor.task = task
self.agent_executor.tools_description = render_text_description(tools) self.agent_executor.tools_description = render_text_description(tools)
@@ -191,12 +192,14 @@ class Agent(BaseModel):
self._rpm_controller = rpm_controller self._rpm_controller = rpm_controller
self.create_agent_executor() self.create_agent_executor()
def create_agent_executor(self) -> None: def create_agent_executor(self, tools=None) -> None:
"""Create an agent executor for the agent. """Create an agent executor for the agent.
Returns: Returns:
An instance of the CrewAgentExecutor class. An instance of the CrewAgentExecutor class.
""" """
tools = tools or self.tools
agent_args = { agent_args = {
"input": lambda x: x["input"], "input": lambda x: x["input"],
"tools": lambda x: x["tools"], "tools": lambda x: x["tools"],
@@ -209,7 +212,7 @@ class Agent(BaseModel):
executor_args = { executor_args = {
"llm": self.llm, "llm": self.llm,
"i18n": self.i18n, "i18n": self.i18n,
"tools": self._parse_tools(self.tools), "tools": self._parse_tools(tools),
"verbose": self.verbose, "verbose": self.verbose,
"handle_parsing_errors": True, "handle_parsing_errors": True,
"max_iterations": self.max_iter, "max_iterations": self.max_iter,
@@ -229,11 +232,9 @@ class Agent(BaseModel):
) )
executor_args["memory"] = summary_memory executor_args["memory"] = summary_memory
agent_args["chat_history"] = lambda x: x["chat_history"] agent_args["chat_history"] = lambda x: x["chat_history"]
prompt = Prompts( prompt = Prompts(i18n=self.i18n, tools=tools).task_execution_with_memory()
i18n=self.i18n, tools=self.tools
).task_execution_with_memory()
else: else:
prompt = Prompts(i18n=self.i18n, tools=self.tools).task_execution() prompt = Prompts(i18n=self.i18n, tools=tools).task_execution()
execution_prompt = prompt.partial( execution_prompt = prompt.partial(
goal=self.goal, goal=self.goal,

View File

@@ -15,7 +15,12 @@ class Prompts(BaseModel):
def task_execution_with_memory(self) -> BasePromptTemplate: def task_execution_with_memory(self) -> BasePromptTemplate:
"""Generate a prompt for task execution with memory components.""" """Generate a prompt for task execution with memory components."""
slices = ["role_playing", "tools", "memory", "task"] slices = ["role_playing"]
if len(self.tools) > 0:
slices.append("tools")
else:
slices.append("no_tools")
slices.extend(["memory", "task"])
return self._build_prompt(slices) return self._build_prompt(slices)
def task_execution_without_tools(self) -> BasePromptTemplate: def task_execution_without_tools(self) -> BasePromptTemplate:
@@ -24,7 +29,12 @@ class Prompts(BaseModel):
def task_execution(self) -> BasePromptTemplate: def task_execution(self) -> BasePromptTemplate:
"""Generate a standard prompt for task execution.""" """Generate a standard prompt for task execution."""
slices = ["role_playing", "tools", "task"] slices = ["role_playing"]
if len(self.tools) > 0:
slices.append("tools")
else:
slices.append("no_tools")
slices.append("task")
return self._build_prompt(slices) return self._build_prompt(slices)
def _build_prompt(self, components: list[str]) -> BasePromptTemplate: def _build_prompt(self, components: list[str]) -> BasePromptTemplate: