Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
Fixes issues with result as answer not properly exiting LLM loop (#1689)
Brandon Hancock (bhancock_ai)3285c1b19670
diff --git a/src/crewai/agent.py b/src/crewai/agent.pyindex 26380ebc2..abe678db1 100644--- a/src/crewai/agent.py+++ b/src/crewai/agent.py@@ -1,7 +1,7 @@import osimport shutilimport subprocess-from typing import Any, List, Literal, Optional, Union, Dict+from typing import Any, Dict, List, Literal, Optional, Unionfrom pydantic import Field, InstanceOf, PrivateAttr, model_validator@@ -9,9 +9,10 @@ from crewai.agents import CacheHandlerfrom crewai.agents.agent_builder.base_agent import BaseAgentfrom crewai.agents.crew_agent_executor import CrewAgentExecutorfrom crewai.cli.constants import ENV_VARS-from crewai.llm import LLMfrom crewai.knowledge.knowledge import Knowledgefrom crewai.knowledge.source.base_knowledge_source import BaseKnowledgeSource+from crewai.knowledge.utils.knowledge_utils import extract_knowledge_context+from crewai.llm import LLMfrom crewai.memory.contextual.contextual_memory import ContextualMemoryfrom crewai.task import Taskfrom crewai.tools import BaseTool@@ -21,7 +22,6 @@ from crewai.utilities.constants import TRAINED_AGENTS_DATA_FILE, TRAINING_DATA_Ffrom crewai.utilities.converter import generate_model_descriptionfrom crewai.utilities.token_counter_callback import TokenCalcHandlerfrom crewai.utilities.training_handler import CrewTrainingHandler-from crewai.knowledge.utils.knowledge_utils import extract_knowledge_contextdef mock_agent_ops_provider():diff --git a/src/crewai/agents/crew_agent_executor.py b/src/crewai/agents/crew_agent_executor.pyindex bf14e6915..2c880a634 100644--- a/src/crewai/agents/crew_agent_executor.py+++ b/src/crewai/agents/crew_agent_executor.py@@ -1,5 +1,6 @@import jsonimport re+from dataclasses import dataclassfrom typing import Any, Dict, List, Unionfrom crewai.agents.agent_builder.base_agent import BaseAgent@@ -12,6 +13,7 @@ from crewai.agents.parser import (OutputParserException,)from crewai.agents.tools_handler import ToolsHandler+from crewai.tools.base_tool import BaseToolfrom crewai.tools.tool_usage import ToolUsage, ToolUsageErrorExceptionfrom crewai.utilities import I18N, Printerfrom crewai.utilities.constants import TRAINING_DATA_FILE@@ -22,6 +24,12 @@ from crewai.utilities.logger import Loggerfrom crewai.utilities.training_handler import CrewTrainingHandler+@dataclass+class ToolResult:+ result: Any+ result_as_answer: bool++class CrewAgentExecutor(CrewAgentExecutorMixin):_logger: Logger = Logger()@@ -33,7 +41,7 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):agent: BaseAgent,prompt: dict[str, str],max_iter: int,- tools: List[Any],+ tools: List[BaseTool],tools_names: str,stop_words: List[str],tools_description: str,@@ -70,7 +78,9 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):self.iterations = 0self.log_error_after = 3self.have_forced_answer = False- self.name_to_tool_map = {tool.name: tool for tool in self.tools}+ self.tool_name_to_tool_map: Dict[str, BaseTool] = {+ tool.name: tool for tool in self.tools+ }if self.llm.stop:self.llm.stop = list(set(self.llm.stop + self.stop))else:@@ -140,9 +150,17 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):formatted_answer = self._format_answer(answer)if isinstance(formatted_answer, AgentAction):- action_result = self._use_tool(formatted_answer)- formatted_answer.text += f"\nObservation: {action_result}"- formatted_answer.result = action_result+ tool_result = self._execute_tool_and_check_finality(+ formatted_answer+ )+ formatted_answer.text += f"\nObservation: {tool_result.result}"+ formatted_answer.result = tool_result.result+ if tool_result.result_as_answer:+ return AgentFinish(+ thought="",+ output=tool_result.result,+ text=formatted_answer.text,+ )self._show_logs(formatted_answer)if self.step_callback:@@ -239,7 +257,7 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):content=f"\033[95m## Final Answer:\033[00m \033[92m\n{formatted_answer.output}\033[00m\n\n")- def _use_tool(self, agent_action: AgentAction) -> Any:+ def _execute_tool_and_check_finality(self, agent_action: AgentAction) -> ToolResult:tool_usage = ToolUsage(tools_handler=self.tools_handler,tools=self.tools,@@ -255,19 +273,25 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):if isinstance(tool_calling, ToolUsageErrorException):tool_result = tool_calling.message+ return ToolResult(result=tool_result, result_as_answer=False)else:if tool_calling.tool_name.casefold().strip() in [- name.casefold().strip() for name in self.name_to_tool_map+ name.casefold().strip() for name in self.tool_name_to_tool_map] or tool_calling.tool_name.casefold().replace("_", " ") in [- name.casefold().strip() for name in self.name_to_tool_map+ name.casefold().strip() for name in self.tool_name_to_tool_map]:tool_result = tool_usage.use(tool_calling, agent_action.text)+ tool = self.tool_name_to_tool_map.get(tool_calling.tool_name)+ if tool:+ return ToolResult(+ result=tool_result, result_as_answer=tool.result_as_answer+ )else:tool_result = self._i18n.errors("wrong_tool_name").format(tool=tool_calling.tool_name,tools=", ".join([tool.name.casefold() for tool in self.tools]),)- return tool_result+ return ToolResult(result=tool_result, result_as_answer=False)def _summarize_messages(self) -> None:messages_groups = []@@ -333,9 +357,9 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):if self.crew is not None and hasattr(self.crew, "_train_iteration"):train_iteration = self.crew._train_iterationif agent_id in training_data and isinstance(train_iteration, int):- training_data[agent_id][train_iteration]["improved_output"] = (- result.output- )+ training_data[agent_id][train_iteration][+ "improved_output"+ ] = result.outputtraining_handler.save(training_data)else:self._logger.log(diff --git a/src/crewai/tools/base_tool.py b/src/crewai/tools/base_tool.pyindex 46bfc03a6..c3840d23c 100644--- a/src/crewai/tools/base_tool.py+++ b/src/crewai/tools/base_tool.py@@ -73,6 +73,7 @@ class BaseTool(BaseModel, ABC):description=self.description,args_schema=self.args_schema,func=self._run,+ result_as_answer=self.result_as_answer,)@classmethoddiff --git a/src/crewai/tools/structured_tool.py b/src/crewai/tools/structured_tool.pyindex bd6818605..dfd23a9cb 100644--- a/src/crewai/tools/structured_tool.py+++ b/src/crewai/tools/structured_tool.py@@ -22,6 +22,7 @@ class CrewStructuredTool:description: str,args_schema: type[BaseModel],func: Callable[..., Any],+ result_as_answer: bool = False,) -> None:"""Initialize the structured tool.@@ -30,12 +31,14 @@ class CrewStructuredTool:description: A description of what the tool doesargs_schema: The pydantic model for the tool's argumentsfunc: The function to run when the tool is called+ result_as_answer: Whether to return the output directly"""self.name = nameself.description = descriptionself.args_schema = args_schemaself.func = funcself._logger = Logger()+ self.result_as_answer = result_as_answer# Validate the function signature matches the schemaself._validate_function_signature()@@ -98,6 +101,7 @@ class CrewStructuredTool:description=description,args_schema=schema,func=func,+ result_as_answer=return_direct,)@staticmethod