Skip to content

Commit

Permalink
Examples (#146)
Browse files Browse the repository at this point in the history
* examples

* examples
  • Loading branch information
bboynton97 authored Apr 9, 2024
1 parent d4c98bc commit 46e8f49
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 3 deletions.
2 changes: 1 addition & 1 deletion agentops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def start_session(tags: Optional[List[str]] = None, config: Optional[Configurati
Client().start_session(tags, config)


def record(event: Event):
def record(event: Event | ErrorEvent):
Client().record(event)


Expand Down
11 changes: 11 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# AgentOps Examples

## Quickstart
- [Single LLM](./openai-gpt.ipynb)
- [Recording Events](./recording-events.ipynb)
- [Multi-Agent](./multi_agent_example.ipynb)

## Integrations
- [Using Langchain](./langchain_examples.ipynb)
- [Crew.ai](https://github.com/joaomdmoura/crewAI-examples/tree/main/markdown_validator)
- Crew is a framework for developing agents, a number of their example projects use AgentOps
4 changes: 2 additions & 2 deletions examples/openai-gpt.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"collapsed": false
},
"source": [
"# Monitoring \n",
"# AgentOps Basic Monitoring \n",
"This is an example of how to use the AgentOps library for basic Agent monitoring with OpenAI's GPT"
]
},
Expand Down Expand Up @@ -70,7 +70,7 @@
"outputs": [],
"source": [
"openai = OpenAI(api_key=OPENAI_API_KEY)\n",
"agentops.init(AGENTOPS_API_KEY)\n"
"agentops.init(AGENTOPS_API_KEY)"
]
},
{
Expand Down
179 changes: 179 additions & 0 deletions examples/recording-events.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Recording Events\n",
"AgentOps has a number of different [Event Types](https://docs.agentops.ai/v1/details/events)"
],
"metadata": {
"collapsed": false
},
"id": "dc8cfd2cfa8a594b"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"# Create new session\n",
"agentops.start_session()\n",
"\n",
"# Optionally, we can add tags to the session\n",
"# agentops.start_session(['Hello Tracker'])"
],
"metadata": {
"collapsed": false,
"is_executing": true
},
"id": "168ecd05cc123de0",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"The easiest way to record actions is through the use of AgentOp's decorators"
],
"metadata": {
"collapsed": false
},
"id": "c6d06ee8c66dad17"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from agentops import record_function\n",
"\n",
"@record_function(\"add numbers\")\n",
"def add(x, y):\n",
" return x + y\n",
"\n",
"add(2,4)"
],
"metadata": {
"collapsed": false
},
"id": "b460318317adc624",
"execution_count": 0
},
{
"cell_type": "markdown",
"source": [
"We can also manually craft an event exactly the way we want"
],
"metadata": {
"collapsed": false
},
"id": "9068a4cdd328f652"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from agentops import ActionEvent\n",
"\n",
"message = {\"role\": \"user\", \"content\": \"Hello\"},\n",
"response = openai.chat.completions.create(\n",
" model='gpt-3.5-turbo', messages=message, temperature=0.5)\n",
"\n",
"if \"hello\" in str(response.choices[0].message.content).lower():\n",
" agentops.record(ActionEvent(action_type=\"Agent says hello\", params=str(message), returns=str(response.choices[0].message.content) ))"
],
"metadata": {
"collapsed": false
},
"id": "b62ad88921ff26f2",
"execution_count": 0
},
{
"cell_type": "code",
"outputs": [],
"source": [
"agentops.end_session('Success')"
],
"metadata": {
"collapsed": false
},
"id": "e10a89e06fe6b2be",
"execution_count": null
},
{
"cell_type": "markdown",
"source": [
"## Tool Event\n",
"Agents use tools. These tools are useful to track with information such as name, end status, runtime, etc. To record tool usage, you can create and record a `ToolEvent` similar to above."
],
"metadata": {
"collapsed": false
},
"id": "f7c947d815f581e7"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from agentops import ToolEvent, record\n",
"def scrape_website(url: str):\n",
" tool_event = ToolEvent(name='scrape_website', params={'url':url}) # the start timestamp is set when the obj is created\n",
" result = integration.scrape_website(data) # perform tool logic\n",
" tool_event.returns = result\n",
" record(tool_event)"
],
"metadata": {
"collapsed": false
},
"id": "5d387a071a1c70cf"
},
{
"cell_type": "markdown",
"source": [
"## Error Events\n",
"Error events can be used alone or in reference to another event. Lets add a catch block to the code above"
],
"metadata": {
"collapsed": false
},
"id": "968d1503dd0aae9a"
},
{
"cell_type": "code",
"outputs": [],
"source": [
"from agentops import ToolEvent, record, ErrorEvent\n",
"def scrape_website(url: str):\n",
" tool_event = ToolEvent(name='scrape_website', params={'url':url}) # the start timestamp is set when the obj is created\n",
" try:\n",
" result = integration.scrape_website(data) # perform tool logic\n",
" tool_event.returns = result\n",
" except Error as e:\n",
" record(ErrorEvent(message=e, trigger_event=tool_event))\n",
" record(tool_event)"
],
"metadata": {
"collapsed": false
},
"id": "eb23c1325298e22f"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 46e8f49

Please sign in to comment.