mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-03 08:12:39 +00:00
@@ -1,17 +1,23 @@
|
||||
from langchain_core.exceptions import OutputParserException
|
||||
|
||||
from crewai.i18n import I18N
|
||||
|
||||
|
||||
class TaskRepeatedUsageException(OutputParserException):
|
||||
"""Exception raised when a task is used twice in a roll."""
|
||||
|
||||
i18n: I18N = I18N()
|
||||
error: str = "TaskRepeatedUsageException"
|
||||
message: str = "I just used the {tool} tool with input {tool_input}. So I already know the result of that and don't need to use it now.\n"
|
||||
message: str
|
||||
|
||||
def __init__(self, tool: str, tool_input: str, text: str):
|
||||
def __init__(self, i18n: I18N, tool: str, tool_input: str, text: str):
|
||||
self.i18n = i18n
|
||||
self.text = text
|
||||
self.tool = tool
|
||||
self.tool_input = tool_input
|
||||
self.message = self.message.format(tool=tool, tool_input=tool_input)
|
||||
self.message = self.i18n.errors("task_repeated_usage").format(
|
||||
tool=tool, tool_input=tool_input
|
||||
)
|
||||
|
||||
super().__init__(
|
||||
error=self.error,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import time
|
||||
from textwrap import dedent
|
||||
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
|
||||
|
||||
from langchain.agents import AgentExecutor
|
||||
@@ -12,11 +11,13 @@ from langchain_core.pydantic_v1 import root_validator
|
||||
from langchain_core.tools import BaseTool
|
||||
from langchain_core.utils.input import get_color_mapping
|
||||
|
||||
from ..tools.cache_tools import CacheTools
|
||||
from .cache.cache_hit import CacheHit
|
||||
from crewai.agents.cache.cache_hit import CacheHit
|
||||
from crewai.i18n import I18N
|
||||
from crewai.tools.cache_tools import CacheTools
|
||||
|
||||
|
||||
class CrewAgentExecutor(AgentExecutor):
|
||||
i18n: I18N = I18N()
|
||||
iterations: int = 0
|
||||
max_iterations: Optional[int] = 15
|
||||
force_answer_max_iterations: Optional[int] = None
|
||||
@@ -31,13 +32,7 @@ class CrewAgentExecutor(AgentExecutor):
|
||||
|
||||
def _force_answer(self, output: AgentAction):
|
||||
return AgentStep(
|
||||
action=output,
|
||||
observation=dedent(
|
||||
"""\
|
||||
I've used too many tools for this task.
|
||||
I'm going to give you my absolute BEST Final answer now and
|
||||
not use any more tools."""
|
||||
),
|
||||
action=output, observation=self.i18n.errors("used_too_many_tools")
|
||||
)
|
||||
|
||||
def _call(
|
||||
|
||||
@@ -4,9 +4,10 @@ from typing import Union
|
||||
from langchain.agents.output_parsers import ReActSingleInputOutputParser
|
||||
from langchain_core.agents import AgentAction, AgentFinish
|
||||
|
||||
from .cache import CacheHandler, CacheHit
|
||||
from .exceptions import TaskRepeatedUsageException
|
||||
from .tools_handler import ToolsHandler
|
||||
from crewai.agents.cache import CacheHandler, CacheHit
|
||||
from crewai.agents.exceptions import TaskRepeatedUsageException
|
||||
from crewai.agents.tools_handler import ToolsHandler
|
||||
from crewai.i18n import I18N
|
||||
|
||||
FINAL_ANSWER_ACTION = "Final Answer:"
|
||||
FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE = (
|
||||
@@ -46,6 +47,7 @@ class CrewAgentOutputParser(ReActSingleInputOutputParser):
|
||||
|
||||
tools_handler: ToolsHandler
|
||||
cache: CacheHandler
|
||||
i18n: I18N
|
||||
|
||||
def parse(self, text: str) -> Union[AgentAction, AgentFinish, CacheHit]:
|
||||
FINAL_ANSWER_ACTION in text
|
||||
@@ -65,10 +67,13 @@ class CrewAgentOutputParser(ReActSingleInputOutputParser):
|
||||
}
|
||||
if usage == last_tool_usage:
|
||||
raise TaskRepeatedUsageException(
|
||||
tool=action, tool_input=tool_input, text=text
|
||||
text=text,
|
||||
tool=action,
|
||||
tool_input=tool_input,
|
||||
i18n=self.i18n,
|
||||
)
|
||||
|
||||
if result := self.cache.read(action, tool_input):
|
||||
if self.cache.read(action, tool_input):
|
||||
action = AgentAction(action, tool_input, text)
|
||||
return CacheHit(action=action, cache=self.cache)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user