implements agentops with a langchain handler, agent tracking and tool call recording

This commit is contained in:
Braelyn Boynton
2024-03-19 18:47:22 -07:00
parent 637bd885cf
commit f6ee12dbc5
4 changed files with 75 additions and 2 deletions

64
poetry.lock generated
View File

@@ -1,5 +1,27 @@
# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. # This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand.
[[package]]
name = "agentops"
version = "0.1.0b1"
description = "Python SDK for developing AI agent evals and observability"
optional = false
python-versions = ">=3.7"
files = [
{file = "agentops-0.1.0b1-py3-none-any.whl", hash = "sha256:825ab57ac5f7840f5a7f8ac195f4af75ec07a9c0972b17d1a57a595420d06208"},
{file = "agentops-0.1.0b1.tar.gz", hash = "sha256:f5ce4b34999fe4b21a4ce3643980253d30f8ea9c55f01d96cd35631355fc7ac3"},
]
[package.dependencies]
packaging = ">=23.1,<24.0"
psutil = "5.9.8"
pydantic = ">=1.9.0"
requests = "2.31.0"
toml = ">=0.10.2"
[package.extras]
dev = ["pytest (==7.4.0)", "requests-mock (==1.11.0)"]
langchain = ["langchain (>=0.0.354)"]
[[package]] [[package]]
name = "aiohttp" name = "aiohttp"
version = "3.9.3" version = "3.9.3"
@@ -3520,6 +3542,34 @@ files = [
{file = "protobuf-4.25.3.tar.gz", hash = "sha256:25b5d0b42fd000320bd7830b349e3b696435f3b329810427a6bcce6a5492cc5c"}, {file = "protobuf-4.25.3.tar.gz", hash = "sha256:25b5d0b42fd000320bd7830b349e3b696435f3b329810427a6bcce6a5492cc5c"},
] ]
[[package]]
name = "psutil"
version = "5.9.8"
description = "Cross-platform lib for process and system monitoring in Python."
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*"
files = [
{file = "psutil-5.9.8-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:26bd09967ae00920df88e0352a91cff1a78f8d69b3ecabbfe733610c0af486c8"},
{file = "psutil-5.9.8-cp27-cp27m-manylinux2010_i686.whl", hash = "sha256:05806de88103b25903dff19bb6692bd2e714ccf9e668d050d144012055cbca73"},
{file = "psutil-5.9.8-cp27-cp27m-manylinux2010_x86_64.whl", hash = "sha256:611052c4bc70432ec770d5d54f64206aa7203a101ec273a0cd82418c86503bb7"},
{file = "psutil-5.9.8-cp27-cp27mu-manylinux2010_i686.whl", hash = "sha256:50187900d73c1381ba1454cf40308c2bf6f34268518b3f36a9b663ca87e65e36"},
{file = "psutil-5.9.8-cp27-cp27mu-manylinux2010_x86_64.whl", hash = "sha256:02615ed8c5ea222323408ceba16c60e99c3f91639b07da6373fb7e6539abc56d"},
{file = "psutil-5.9.8-cp27-none-win32.whl", hash = "sha256:36f435891adb138ed3c9e58c6af3e2e6ca9ac2f365efe1f9cfef2794e6c93b4e"},
{file = "psutil-5.9.8-cp27-none-win_amd64.whl", hash = "sha256:bd1184ceb3f87651a67b2708d4c3338e9b10c5df903f2e3776b62303b26cb631"},
{file = "psutil-5.9.8-cp36-abi3-macosx_10_9_x86_64.whl", hash = "sha256:aee678c8720623dc456fa20659af736241f575d79429a0e5e9cf88ae0605cc81"},
{file = "psutil-5.9.8-cp36-abi3-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:8cb6403ce6d8e047495a701dc7c5bd788add903f8986d523e3e20b98b733e421"},
{file = "psutil-5.9.8-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d06016f7f8625a1825ba3732081d77c94589dca78b7a3fc072194851e88461a4"},
{file = "psutil-5.9.8-cp36-cp36m-win32.whl", hash = "sha256:7d79560ad97af658a0f6adfef8b834b53f64746d45b403f225b85c5c2c140eee"},
{file = "psutil-5.9.8-cp36-cp36m-win_amd64.whl", hash = "sha256:27cc40c3493bb10de1be4b3f07cae4c010ce715290a5be22b98493509c6299e2"},
{file = "psutil-5.9.8-cp37-abi3-win32.whl", hash = "sha256:bc56c2a1b0d15aa3eaa5a60c9f3f8e3e565303b465dbf57a1b730e7a2b9844e0"},
{file = "psutil-5.9.8-cp37-abi3-win_amd64.whl", hash = "sha256:8db4c1b57507eef143a15a6884ca10f7c73876cdf5d51e713151c1236a0e68cf"},
{file = "psutil-5.9.8-cp38-abi3-macosx_11_0_arm64.whl", hash = "sha256:d16bbddf0693323b8c6123dd804100241da461e41d6e332fb0ba6058f630f8c8"},
{file = "psutil-5.9.8.tar.gz", hash = "sha256:6be126e3225486dff286a8fb9a06246a5253f4c7c53b475ea5f5ac934e64194c"},
]
[package.extras]
test = ["enum34", "ipaddress", "mock", "pywin32", "wmi"]
[[package]] [[package]]
name = "pulsar-client" name = "pulsar-client"
version = "3.4.0" version = "3.4.0"
@@ -4138,6 +4188,7 @@ files = [
{file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"},
{file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"},
{file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"},
{file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a08c6f0fe150303c1c6b71ebcd7213c2858041a7e01975da3a99aed1e7a378ef"},
{file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"},
{file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"},
{file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"},
@@ -4888,6 +4939,17 @@ dev = ["tokenizers[testing]"]
docs = ["setuptools_rust", "sphinx", "sphinx_rtd_theme"] docs = ["setuptools_rust", "sphinx", "sphinx_rtd_theme"]
testing = ["black (==22.3)", "datasets", "numpy", "pytest", "requests"] testing = ["black (==22.3)", "datasets", "numpy", "pytest", "requests"]
[[package]]
name = "toml"
version = "0.10.2"
description = "Python Library for Tom's Obvious, Minimal Language"
optional = false
python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*"
files = [
{file = "toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b"},
{file = "toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"},
]
[[package]] [[package]]
name = "tomli" name = "tomli"
version = "2.0.1" version = "2.0.1"
@@ -5618,4 +5680,4 @@ tools = ["crewai-tools"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = ">=3.10,<=3.13" python-versions = ">=3.10,<=3.13"
content-hash = "f22a0fe0aa5dfe6b4de28f2b4dc1d5676b70e80d70e3615d8c4bbcfde35c427a" content-hash = "615d9cc89cc11afd28fd66452593ecb40377332b1cf3daf2a8666cf335d51d3c"

View File

@@ -27,6 +27,7 @@ regex = "^2023.12.25"
crewai-tools = { version = "^0.0.15", optional = true } crewai-tools = { version = "^0.0.15", optional = true }
click = "^8.1.7" click = "^8.1.7"
python-dotenv = "1.0.0" python-dotenv = "1.0.0"
agentops = "0.1.0b1"
[tool.poetry.extras] [tool.poetry.extras]
tools = ["crewai-tools"] tools = ["crewai-tools"]
@@ -50,6 +51,7 @@ crewai_tools = "^0.0.15"
profile = "black" profile = "black"
known_first_party = ["crewai"] known_first_party = ["crewai"]
[tool.poetry.group.test.dependencies] [tool.poetry.group.test.dependencies]
pytest = "^8.0.0" pytest = "^8.0.0"
pytest-vcr = "^1.0.2" pytest-vcr = "^1.0.2"

View File

@@ -24,8 +24,11 @@ from pydantic_core import PydanticCustomError
from crewai.agents import CacheHandler, CrewAgentExecutor, CrewAgentParser, ToolsHandler from crewai.agents import CacheHandler, CrewAgentExecutor, CrewAgentParser, ToolsHandler
from crewai.utilities import I18N, Logger, Prompts, RPMController from crewai.utilities import I18N, Logger, Prompts, RPMController
from crewai.utilities.token_counter_callback import TokenCalcHandler, TokenProcess from crewai.utilities.token_counter_callback import TokenCalcHandler, TokenProcess
from agentops.agent import track_agent
from agentops import LangchainCallbackHandler
@track_agent(name=None)
class Agent(BaseModel): class Agent(BaseModel):
"""Represents an agent in a system. """Represents an agent in a system.
@@ -55,6 +58,8 @@ class Agent(BaseModel):
_rpm_controller: RPMController = PrivateAttr(default=None) _rpm_controller: RPMController = PrivateAttr(default=None)
_request_within_rpm_limit: Any = PrivateAttr(default=None) _request_within_rpm_limit: Any = PrivateAttr(default=None)
_token_process: TokenProcess = TokenProcess() _token_process: TokenProcess = TokenProcess()
agent_ops_agent_name: str = None
agent_ops_agent_id: str = None
formatting_errors: int = 0 formatting_errors: int = 0
model_config = ConfigDict(arbitrary_types_allowed=True) model_config = ConfigDict(arbitrary_types_allowed=True)
@@ -119,6 +124,7 @@ class Agent(BaseModel):
def __init__(__pydantic_self__, **data): def __init__(__pydantic_self__, **data):
config = data.pop("config", {}) config = data.pop("config", {})
super().__init__(**config, **data) super().__init__(**config, **data)
__pydantic_self__._agent_ops_agent_name = __pydantic_self__.role
@field_validator("id", mode="before") @field_validator("id", mode="before")
@classmethod @classmethod
@@ -151,7 +157,8 @@ class Agent(BaseModel):
"""set agent executor is set.""" """set agent executor is set."""
if hasattr(self.llm, "model_name"): if hasattr(self.llm, "model_name"):
self.llm.callbacks = [ self.llm.callbacks = [
TokenCalcHandler(self.llm.model_name, self._token_process) TokenCalcHandler(self.llm.model_name, self._token_process),
LangchainCallbackHandler()
] ]
if not self.agent_executor: if not self.agent_executor:
self.set_cache_handler(self.cache_handler) self.set_cache_handler(self.cache_handler)

View File

@@ -9,6 +9,7 @@ from crewai.agents.tools_handler import ToolsHandler
from crewai.telemetry import Telemetry from crewai.telemetry import Telemetry
from crewai.tools.tool_calling import InstructorToolCalling, ToolCalling from crewai.tools.tool_calling import InstructorToolCalling, ToolCalling
from crewai.utilities import I18N, Converter, ConverterError, Printer from crewai.utilities import I18N, Converter, ConverterError, Printer
from agentops import record, ToolEvent
OPENAI_BIGGER_MODELS = ["gpt-4"] OPENAI_BIGGER_MODELS = ["gpt-4"]
@@ -80,6 +81,7 @@ class ToolUsage:
return error return error
try: try:
tool = self._select_tool(calling.tool_name) tool = self._select_tool(calling.tool_name)
record(ToolEvent(name=calling.tool_name))
except Exception as e: except Exception as e:
error = getattr(e, "message", str(e)) error = getattr(e, "message", str(e))
self.task.increment_tools_errors() self.task.increment_tools_errors()