Skip to content

Python SDK for AI agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks including CrewAI, Agno, OpenAI Agents SDK, Langchain, Autogen, AG2, and CamelAI

feature: add decorators to openai-gpt example

Shawn Qiua5f59cd45cb8
diff --git a/examples/openai-gpt.ipynb b/examples/openai-gpt.ipynb
index 1d8615d0..9a6a063e 100644
--- a/examples/openai-gpt.ipynb
+++ b/examples/openai-gpt.ipynb
@@ -11,16 +11,6 @@
"This is an example of how to use the AgentOps library for basic Agent monitoring with OpenAI's GPT"
]
},
- {
- "cell_type": "markdown",
- "id": "6d717e4c8bf0de9c",
- "metadata": {
- "collapsed": false
- },
- "source": [
- "_At this time, make sure to install openai==0.28.1_"
- ]
- },
{
"cell_type": "code",
"execution_count": null,
@@ -34,11 +24,10 @@
},
"outputs": [],
"source": [
- "import openai\n",
+ "from openai import OpenAI\n",
"import agentops\n",
- "from dotenv import load_dotenv\n",
"import os\n",
- "from openai import ChatCompletion"
+ "from dotenv import load_dotenv"
]
},
{
@@ -92,8 +81,8 @@
},
"outputs": [],
"source": [
- "ao_client = agentops.Client(AGENTOPS_API_KEY)\n",
- "openai.api_key = OPENAI_API_KEY"
+ "openai = OpenAI(api_key=OPENAI_API_KEY)\n",
+ "ao_client = agentops.Client(AGENTOPS_API_KEY, endpoint=\"http://localhost:8000\")\n"
]
},
{
@@ -130,8 +119,8 @@
"outputs": [],
"source": [
"message = {\"role\": \"user\", \"content\": \"Hello\"},\n",
- "ChatCompletion.create(\n",
- " model='gpt-3.5-turbo', messages=message, temperature=0.5)"
+ "openai.chat.completions.create(\n",
+ " model='gpt-3.5-turbo', messages=message, temperature=0.5, stream=True)"
]
},
{
@@ -179,7 +168,7 @@
"source": [
"# Events\n",
"Additionally, you can track custom events via AgentOps.\n",
- "Lets record an event when an LLM says \"Hello\""
+ "Let's start a new session and record some events "
]
},
{
@@ -203,21 +192,11 @@
]
},
{
- "cell_type": "code",
- "execution_count": null,
- "id": "7daafba014c1a4d8",
- "metadata": {
- "ExecuteTime": {
- "end_time": "2023-12-15T21:33:51.321518Z",
- "start_time": "2023-12-15T21:33:50.915612Z"
- },
- "collapsed": false
- },
- "outputs": [],
+ "cell_type": "markdown",
+ "id": "e94f1b56",
+ "metadata": {},
"source": [
- "message = {\"role\": \"user\", \"content\": \"Hello\"},\n",
- "response = ChatCompletion.create(\n",
- " model='gpt-3.5-turbo', messages=message, temperature=0.5)"
+ "The easiest way to record actions is through the use of AgentOp's decorators"
]
},
{
@@ -232,12 +211,39 @@
"collapsed": false
},
"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)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "1e3cc512",
+ "metadata": {},
+ "source": [
+ "We can also manually craft an event exactly the way we want"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "2ac84c35",
+ "metadata": {},
+ "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",
- " print('📝 Recording Event')\n",
- " ao_client.record(ActionEvent(action_type=\"said hello\"))"
+ " ao_client.record(ActionEvent(action_type=\"Agent says hello\", params=str(message), returns=str(response.choices[0].message.content) ))"
]
},
{
@@ -266,14 +272,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
- "version": 2
+ "version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.6"
+ "pygments_lexer": "ipython3",
+ "version": "3.11.8"
}
},
"nbformat": 4,