mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-09 16:18:30 +00:00
feat: remove unused code and change ToolUsageStarted event place
This commit is contained in:
@@ -47,11 +47,6 @@ from crewai.utilities.events.llm_events import (
|
|||||||
LLMCallStartedEvent,
|
LLMCallStartedEvent,
|
||||||
LLMCallType,
|
LLMCallType,
|
||||||
)
|
)
|
||||||
from crewai.utilities.events.tool_usage_events import (
|
|
||||||
ToolUsageErrorEvent,
|
|
||||||
ToolUsageFinishedEvent,
|
|
||||||
ToolUsageStartedEvent,
|
|
||||||
)
|
|
||||||
from crewai.utilities.llm_utils import create_llm
|
from crewai.utilities.llm_utils import create_llm
|
||||||
from crewai.utilities.printer import Printer
|
from crewai.utilities.printer import Printer
|
||||||
from crewai.utilities.token_counter_callback import TokenCalcHandler
|
from crewai.utilities.token_counter_callback import TokenCalcHandler
|
||||||
@@ -412,18 +407,6 @@ class LiteAgent(BaseModel):
|
|||||||
formatted_answer = process_llm_response(answer, self.use_stop_words)
|
formatted_answer = process_llm_response(answer, self.use_stop_words)
|
||||||
|
|
||||||
if isinstance(formatted_answer, AgentAction):
|
if isinstance(formatted_answer, AgentAction):
|
||||||
# Emit tool usage started event
|
|
||||||
crewai_event_bus.emit(
|
|
||||||
self,
|
|
||||||
event=ToolUsageStartedEvent(
|
|
||||||
agent_key=self.key,
|
|
||||||
agent_role=self.role,
|
|
||||||
tool_name=formatted_answer.tool,
|
|
||||||
tool_args=formatted_answer.tool_input,
|
|
||||||
tool_class=formatted_answer.tool,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tool_result = execute_tool_and_check_finality(
|
tool_result = execute_tool_and_check_finality(
|
||||||
agent_action=formatted_answer,
|
agent_action=formatted_answer,
|
||||||
@@ -432,33 +415,7 @@ class LiteAgent(BaseModel):
|
|||||||
agent_key=self.key,
|
agent_key=self.key,
|
||||||
agent_role=self.role,
|
agent_role=self.role,
|
||||||
)
|
)
|
||||||
# Emit tool usage finished event
|
|
||||||
crewai_event_bus.emit(
|
|
||||||
self,
|
|
||||||
event=ToolUsageFinishedEvent(
|
|
||||||
agent_key=self.key,
|
|
||||||
agent_role=self.role,
|
|
||||||
tool_name=formatted_answer.tool,
|
|
||||||
tool_args=formatted_answer.tool_input,
|
|
||||||
tool_class=formatted_answer.tool,
|
|
||||||
started_at=datetime.now(),
|
|
||||||
finished_at=datetime.now(),
|
|
||||||
output=tool_result.result,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Emit tool usage error event
|
|
||||||
crewai_event_bus.emit(
|
|
||||||
self,
|
|
||||||
event=ToolUsageErrorEvent(
|
|
||||||
agent_key=self.key,
|
|
||||||
agent_role=self.role,
|
|
||||||
tool_name=formatted_answer.tool,
|
|
||||||
tool_args=formatted_answer.tool_input,
|
|
||||||
tool_class=formatted_answer.tool,
|
|
||||||
error=str(e),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
formatted_answer = handle_agent_action_core(
|
formatted_answer = handle_agent_action_core(
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import ast
|
|||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
from dataclasses import dataclass
|
|
||||||
from difflib import SequenceMatcher
|
from difflib import SequenceMatcher
|
||||||
from json import JSONDecodeError
|
from json import JSONDecodeError
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
@@ -23,6 +23,7 @@ from crewai.utilities.agent_utils import (
|
|||||||
)
|
)
|
||||||
from crewai.utilities.events.crewai_event_bus import crewai_event_bus
|
from crewai.utilities.events.crewai_event_bus import crewai_event_bus
|
||||||
from crewai.utilities.events.tool_usage_events import (
|
from crewai.utilities.events.tool_usage_events import (
|
||||||
|
ToolUsageStartedEvent,
|
||||||
ToolSelectionErrorEvent,
|
ToolSelectionErrorEvent,
|
||||||
ToolUsageErrorEvent,
|
ToolUsageErrorEvent,
|
||||||
ToolUsageFinishedEvent,
|
ToolUsageFinishedEvent,
|
||||||
@@ -148,7 +149,7 @@ class ToolUsage:
|
|||||||
tool_string: str,
|
tool_string: str,
|
||||||
tool: CrewStructuredTool,
|
tool: CrewStructuredTool,
|
||||||
calling: Union[ToolCalling, InstructorToolCalling],
|
calling: Union[ToolCalling, InstructorToolCalling],
|
||||||
) -> str:
|
) -> str:
|
||||||
if self._check_tool_repeated_usage(calling=calling): # type: ignore # _check_tool_repeated_usage of "ToolUsage" does not return a value (it only ever returns None)
|
if self._check_tool_repeated_usage(calling=calling): # type: ignore # _check_tool_repeated_usage of "ToolUsage" does not return a value (it only ever returns None)
|
||||||
try:
|
try:
|
||||||
result = self._i18n.errors("task_repeated_usage").format(
|
result = self._i18n.errors("task_repeated_usage").format(
|
||||||
@@ -166,6 +167,26 @@ class ToolUsage:
|
|||||||
if self.task:
|
if self.task:
|
||||||
self.task.increment_tools_errors()
|
self.task.increment_tools_errors()
|
||||||
|
|
||||||
|
if self.agent:
|
||||||
|
event_data = {
|
||||||
|
"agent_key": self.agent.key if self.agent else None,
|
||||||
|
"agent_role": self.agent.role if self.agent else None,
|
||||||
|
"tool_name": self.action.tool,
|
||||||
|
"tool_args": self.action.tool_input,
|
||||||
|
"tool_class": self.action.tool,
|
||||||
|
"agent": self.agent,
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.agent.fingerprint:
|
||||||
|
event_data.update(self.agent.fingerprint)
|
||||||
|
|
||||||
|
crewai_event_bus.emit(
|
||||||
|
self,
|
||||||
|
ToolUsageStartedEvent(
|
||||||
|
**event_data
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
started_at = time.time()
|
started_at = time.time()
|
||||||
from_cache = False
|
from_cache = False
|
||||||
result = None # type: ignore
|
result = None # type: ignore
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ from crewai.tools.base_tool import BaseTool
|
|||||||
from crewai.tools.structured_tool import CrewStructuredTool
|
from crewai.tools.structured_tool import CrewStructuredTool
|
||||||
from crewai.tools.tool_types import ToolResult
|
from crewai.tools.tool_types import ToolResult
|
||||||
from crewai.utilities import I18N, Printer
|
from crewai.utilities import I18N, Printer
|
||||||
from crewai.utilities.events.tool_usage_events import ToolUsageStartedEvent
|
|
||||||
from crewai.utilities.exceptions.context_window_exceeding_exception import (
|
from crewai.utilities.exceptions.context_window_exceeding_exception import (
|
||||||
LLMContextLengthExceededException,
|
LLMContextLengthExceededException,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5,11 +5,6 @@ from crewai.security import Fingerprint
|
|||||||
from crewai.tools.structured_tool import CrewStructuredTool
|
from crewai.tools.structured_tool import CrewStructuredTool
|
||||||
from crewai.tools.tool_types import ToolResult
|
from crewai.tools.tool_types import ToolResult
|
||||||
from crewai.tools.tool_usage import ToolUsage, ToolUsageErrorException
|
from crewai.tools.tool_usage import ToolUsage, ToolUsageErrorException
|
||||||
from crewai.utilities.events import crewai_event_bus
|
|
||||||
from crewai.utilities.events.tool_usage_events import (
|
|
||||||
ToolUsageErrorEvent,
|
|
||||||
ToolUsageStartedEvent,
|
|
||||||
)
|
|
||||||
from crewai.utilities.i18n import I18N
|
from crewai.utilities.i18n import I18N
|
||||||
|
|
||||||
|
|
||||||
@@ -42,10 +37,8 @@ def execute_tool_and_check_finality(
|
|||||||
ToolResult containing the execution result and whether it should be treated as a final answer
|
ToolResult containing the execution result and whether it should be treated as a final answer
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Create tool name to tool map
|
|
||||||
tool_name_to_tool_map = {tool.name: tool for tool in tools}
|
tool_name_to_tool_map = {tool.name: tool for tool in tools}
|
||||||
|
|
||||||
# Emit tool usage event if agent info is available
|
|
||||||
if agent_key and agent_role and agent:
|
if agent_key and agent_role and agent:
|
||||||
fingerprint_context = fingerprint_context or {}
|
fingerprint_context = fingerprint_context or {}
|
||||||
if agent:
|
if agent:
|
||||||
@@ -59,22 +52,6 @@ def execute_tool_and_check_finality(
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ValueError(f"Failed to set fingerprint: {e}")
|
raise ValueError(f"Failed to set fingerprint: {e}")
|
||||||
|
|
||||||
event_data = {
|
|
||||||
"agent_key": agent_key,
|
|
||||||
"agent_role": agent_role,
|
|
||||||
"tool_name": agent_action.tool,
|
|
||||||
"tool_args": agent_action.tool_input,
|
|
||||||
"tool_class": agent_action.tool,
|
|
||||||
"agent": agent,
|
|
||||||
}
|
|
||||||
event_data.update(fingerprint_context)
|
|
||||||
crewai_event_bus.emit(
|
|
||||||
agent,
|
|
||||||
event=ToolUsageStartedEvent(
|
|
||||||
**event_data,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Create tool usage instance
|
# Create tool usage instance
|
||||||
tool_usage = ToolUsage(
|
tool_usage = ToolUsage(
|
||||||
tools_handler=tools_handler,
|
tools_handler=tools_handler,
|
||||||
@@ -110,17 +87,4 @@ def execute_tool_and_check_finality(
|
|||||||
return ToolResult(tool_result, False)
|
return ToolResult(tool_result, False)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Emit error event if agent info is available
|
|
||||||
if agent_key and agent_role and agent:
|
|
||||||
crewai_event_bus.emit(
|
|
||||||
agent,
|
|
||||||
event=ToolUsageErrorEvent(
|
|
||||||
agent_key=agent_key,
|
|
||||||
agent_role=agent_role,
|
|
||||||
tool_name=agent_action.tool,
|
|
||||||
tool_args=agent_action.tool_input,
|
|
||||||
tool_class=agent_action.tool,
|
|
||||||
error=str(e),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
raise e
|
raise e
|
||||||
|
|||||||
Reference in New Issue
Block a user