Skip to content

Commit

Permalink
AgentOps Implementation (#411)
Browse files Browse the repository at this point in the history
* implements agentops with a langchain handler, agent tracking and tool call recording

* track tool usage

* end session after completion

* track tool usage time

* better tool and llm tracking

* code cleanup

* make agentops optional

* optional dependency usage

* remove telemetry code

* optional agentops

* agentops version bump

* remove org key

* true dependency

* add crew org key to agentops

* cleanup

* Update pyproject.toml

* Revert "true dependency"

This reverts commit e52e8e9.

* Revert "cleanup"

This reverts commit 7f5635f.

* optional parent key

* agentops 0.1.5

* Revert "Revert "cleanup""

This reverts commit cea33d9.

* Revert "Revert "true dependency""

This reverts commit 4d1b460

* cleanup

* Forcing version 0.1.5

* Update pyproject.toml

---------

Co-authored-by: João Moura <joaomdmoura@gmail.com>
  • Loading branch information
bboynton97 and joaomdmoura committed Apr 20, 2024
1 parent ff76715 commit 3d52575
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ click = "^8.1.7"
python-dotenv = "^1.0.0"
embedchain = "^0.1.98"
appdirs = "^1.4.4"
agentops = "0.1.6"

[tool.poetry.extras]
tools = ["crewai-tools"]
Expand Down
5 changes: 5 additions & 0 deletions src/crewai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
from crewai.memory.contextual.contextual_memory import ContextualMemory
from crewai.utilities import I18N, Logger, Prompts, RPMController
from crewai.utilities.token_counter_callback import TokenCalcHandler, TokenProcess
from agentops.agent import track_agent


@track_agent()
class Agent(BaseModel):
"""Represents an agent in a system.
Expand Down Expand Up @@ -55,6 +57,8 @@ class Agent(BaseModel):
_rpm_controller: RPMController = PrivateAttr(default=None)
_request_within_rpm_limit: Any = PrivateAttr(default=None)
_token_process: TokenProcess = TokenProcess()
agent_ops_agent_name: str = None
agent_ops_agent_id: str = None

formatting_errors: int = 0
model_config = ConfigDict(arbitrary_types_allowed=True)
Expand Down Expand Up @@ -129,6 +133,7 @@ class Agent(BaseModel):
def __init__(__pydantic_self__, **data):
config = data.pop("config", {})
super().__init__(**config, **data)
__pydantic_self__.agent_ops_agent_name = __pydantic_self__.role

@field_validator("id", mode="before")
@classmethod
Expand Down
3 changes: 3 additions & 0 deletions src/crewai/crew.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from crewai.telemetry import Telemetry
from crewai.tools.agent_tools import AgentTools
from crewai.utilities import I18N, FileHandler, Logger, RPMController
import agentops


class Crew(BaseModel):
Expand Down Expand Up @@ -239,6 +240,7 @@ def kickoff(self, inputs: Optional[Dict[str, Any]] = {}) -> str:
self._set_tasks_callbacks()

i18n = I18N(language=self.language, language_file=self.language_file)
agentops.set_parent_key("daebe730-f54d-4af5-98df-e6946fb76d13")

for agent in self.agents:
agent.i18n = i18n
Expand Down Expand Up @@ -374,6 +376,7 @@ def _format_output(self, output: str) -> str:
def _finish_execution(self, output) -> None:
if self.max_rpm:
self._rpm_controller.stop_rpm_counter()
agentops.end_session(end_state="Success", end_state_reason="Finished Execution")
self._telemetry.end_crew(self, output)

def __repr__(self):
Expand Down
4 changes: 4 additions & 0 deletions src/crewai/tools/tool_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from crewai.telemetry import Telemetry
from crewai.tools.tool_calling import InstructorToolCalling, ToolCalling
from crewai.utilities import I18N, Converter, ConverterError, Printer
import agentops

OPENAI_BIGGER_MODELS = ["gpt-4"]

Expand Down Expand Up @@ -96,6 +97,7 @@ def _use(
tool: BaseTool,
calling: Union[ToolCalling, InstructorToolCalling],
) -> None:
tool_event = agentops.ToolEvent(name=calling.tool_name)
if self._check_tool_repeated_usage(calling=calling):
try:
result = self._i18n.errors("task_repeated_usage").format(
Expand Down Expand Up @@ -159,6 +161,7 @@ def _use(
self._printer.print(content=f"\n\n{error_message}\n", color="red")
return error
self.task.increment_tools_errors()
agentops.record(agentops.ErrorEvent(details=e, trigger_event=tool_event))
return self.use(calling=calling, tool_string=tool_string)

if self.tools_handler:
Expand All @@ -179,6 +182,7 @@ def _use(
)

self._printer.print(content=f"\n\n{result}\n", color="purple")
agentops.record(tool_event)
self._telemetry.tool_usage(
llm=self.function_calling_llm,
tool_name=tool.name,
Expand Down

0 comments on commit 3d52575

Please sign in to comment.