Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AgentOps integration #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import json
import agentops
from dotenv import load_dotenv

from typing import Any, List
from langchain_core.tools import Tool
Expand All @@ -10,11 +12,15 @@
ToolAgentAction,
)

# Load environment variables
load_dotenv()

# Initialize AgentOps
agentops.init(os.getenv('AGENTOPS_API_KEY'))

class LangchainCodeInterpreterToolInput(BaseModel):
code: str = Field(description="Python code to execute.")


class CodeInterpreterFunctionTool:
"""
This class calls arbitrary code against a Python Jupyter notebook.
Expand All @@ -23,6 +29,7 @@ class CodeInterpreterFunctionTool:

tool_name: str = "code_interpreter"

@agentops.record_function('CodeInterpreterFunctionTool_init')
def __init__(self):
# Instantiate the E2B sandbox - this is a long lived object
# that's pinging E2B cloud to keep the sandbox alive.
Expand All @@ -32,24 +39,34 @@ def __init__(self):
)
self.code_interpreter = CodeInterpreter()

@agentops.record_function('CodeInterpreterFunctionTool_close')
def close(self):
self.code_interpreter.close()

@agentops.record_function('CodeInterpreterFunctionTool_call')
def call(self, parameters: dict, **kwargs: Any):
code = parameters.get("code", "")
print(f"***Code Interpreting...\n{code}\n====")
execution = self.code_interpreter.notebook.exec_cell(code)

with agentops.record_span('code_execution'):
execution = self.code_interpreter.notebook.exec_cell(code)

# Record metrics
agentops.record_metric('code_execution_success', 1 if not execution.error else 0)
agentops.record_metric('code_execution_time', execution.duration)

return {
"results": execution.results,
"stdout": execution.logs.stdout,
"stderr": execution.logs.stderr,
"error": execution.error,
}

# langchain does not return a dict as a parameter, only a code string
@agentops.record_function('CodeInterpreterFunctionTool_langchain_call')
def langchain_call(self, code: str):
return self.call({"code": code})

@agentops.record_function('CodeInterpreterFunctionTool_to_langchain_tool')
def to_langchain_tool(self) -> Tool:
tool = Tool(
name=self.tool_name,
Expand All @@ -60,6 +77,7 @@ def to_langchain_tool(self) -> Tool:
return tool

@staticmethod
@agentops.record_function('CodeInterpreterFunctionTool_format_to_tool_message')
def format_to_tool_message(
agent_action: ToolAgentAction,
observation: dict,
Expand All @@ -77,4 +95,10 @@ def format_to_tool_message(
ToolMessage(content=content, tool_call_id=agent_action.tool_call_id)
)

# Record metric for message count
agentops.record_metric('tool_message_count', len(new_messages))

return new_messages

# End of program
agentops.end_session('Success')