mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-07 18:19:00 +00:00
refactor agentops.record to use _log_to_agentops method
This commit is contained in:
@@ -24,6 +24,7 @@ try:
|
|||||||
import agentops # type: ignore
|
import agentops # type: ignore
|
||||||
except ImportError:
|
except ImportError:
|
||||||
agentops = None
|
agentops = None
|
||||||
|
|
||||||
OPENAI_BIGGER_MODELS = [
|
OPENAI_BIGGER_MODELS = [
|
||||||
"gpt-4",
|
"gpt-4",
|
||||||
"gpt-4o",
|
"gpt-4o",
|
||||||
@@ -216,22 +217,20 @@ class ToolUsage:
|
|||||||
return error # type: ignore # No return value expected
|
return error # type: ignore # No return value expected
|
||||||
|
|
||||||
self.task.increment_tools_errors()
|
self.task.increment_tools_errors()
|
||||||
if agentops:
|
self._log_to_agentops(
|
||||||
agentops.record(
|
event_type=agentops.ErrorEvent,
|
||||||
agentops.ErrorEvent(
|
exception=e,
|
||||||
exception=e,
|
logs={
|
||||||
logs={
|
"tool_string": tool_string,
|
||||||
"tool_string": tool_string,
|
"tool": tool,
|
||||||
"tool": tool,
|
"tool_calling": calling,
|
||||||
"tool_calling": calling,
|
"error": error,
|
||||||
"error": error,
|
"run_attempts": self._run_attempts,
|
||||||
"run_attempts": self._run_attempts,
|
"llm": self.function_calling_llm,
|
||||||
"llm": self.function_calling_llm,
|
"task": self.task,
|
||||||
"task": self.task,
|
"agent": self.agent,
|
||||||
"agent": self.agent,
|
},
|
||||||
},
|
)
|
||||||
)
|
|
||||||
)
|
|
||||||
return self.use(calling=calling, tool_string=tool_string) # type: ignore # No return value expected
|
return self.use(calling=calling, tool_string=tool_string) # type: ignore # No return value expected
|
||||||
|
|
||||||
if self.tools_handler:
|
if self.tools_handler:
|
||||||
@@ -250,23 +249,21 @@ class ToolUsage:
|
|||||||
should_cache=should_cache,
|
should_cache=should_cache,
|
||||||
)
|
)
|
||||||
|
|
||||||
if agentops:
|
self._log_to_agentops(
|
||||||
agentops.record(
|
event_type=agentops.ToolEvent,
|
||||||
agentops.ToolEvent(
|
params=calling.arguments,
|
||||||
params=calling.arguments,
|
returns=result,
|
||||||
returns=result,
|
name=tool.name,
|
||||||
name=tool.name,
|
logs={
|
||||||
logs={
|
"tool_string": tool_string,
|
||||||
"tool_string": tool_string,
|
"tool": tool,
|
||||||
"tool": tool,
|
"tool_calling": calling,
|
||||||
"tool_calling": calling,
|
"run_attempts": self._run_attempts,
|
||||||
"run_attempts": self._run_attempts,
|
"llm": self.function_calling_llm,
|
||||||
"llm": self.function_calling_llm,
|
"task": self.task,
|
||||||
"task": self.task,
|
"agent": self.agent,
|
||||||
"agent": self.agent,
|
},
|
||||||
},
|
)
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
self._telemetry.tool_usage(
|
self._telemetry.tool_usage(
|
||||||
llm=self.function_calling_llm,
|
llm=self.function_calling_llm,
|
||||||
@@ -495,17 +492,15 @@ class ToolUsage:
|
|||||||
def on_tool_error(self, tool: Any, tool_calling: ToolCalling, e: Exception) -> None:
|
def on_tool_error(self, tool: Any, tool_calling: ToolCalling, e: Exception) -> None:
|
||||||
event_data = self._prepare_event_data(tool, tool_calling)
|
event_data = self._prepare_event_data(tool, tool_calling)
|
||||||
|
|
||||||
if agentops:
|
self._log_to_agentops(
|
||||||
agentops.record(
|
event_type=agentops.ErrorEvent,
|
||||||
agentops.ErrorEvent(
|
exception=e,
|
||||||
exception=e,
|
logs={
|
||||||
logs={
|
"tool": tool,
|
||||||
"tool": tool,
|
"tool_calling": tool_calling,
|
||||||
"tool_calling": tool_calling,
|
"event_data": event_data,
|
||||||
"event_data": event_data,
|
},
|
||||||
},
|
)
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
events.emit(
|
events.emit(
|
||||||
source=self, event=ToolUsageError(**{**event_data, "error": str(e)})
|
source=self, event=ToolUsageError(**{**event_data, "error": str(e)})
|
||||||
@@ -536,3 +531,11 @@ class ToolUsage:
|
|||||||
"tool_args": tool_calling.arguments,
|
"tool_args": tool_calling.arguments,
|
||||||
"tool_class": tool.__class__.__name__,
|
"tool_class": tool.__class__.__name__,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def _log_to_agentops(self, event_type, **kwargs) -> None:
|
||||||
|
"""Helper method to log events to agentops if available"""
|
||||||
|
|
||||||
|
if agentops:
|
||||||
|
agentops.record(event_type(**kwargs))
|
||||||
|
|
||||||
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user