298 lines
10 KiB
Plaintext
298 lines
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Anthropic Async Example\n",
|
|
"\n",
|
|
"Anthropic supports both sync and async! This is great because we can wait for functions to finish before we use them! \n",
|
|
"\n",
|
|
"In this example, we will make a program called \"Titan Support Protocol.\" In this example, we will assign our mech a personality type and have a message generated based on our Titan's health (Which we randomly choose). We also send four generated UUIDs which are generated while the LLM runs"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, we start by importing Agentops and Anthropic"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-11-09T19:24:21.051231Z",
|
|
"iopub.status.busy": "2024-11-09T19:24:21.050842Z",
|
|
"iopub.status.idle": "2024-11-09T19:24:46.728962Z",
|
|
"shell.execute_reply": "2024-11-09T19:24:46.727711Z",
|
|
"shell.execute_reply.started": "2024-11-09T19:24:21.051179Z"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"%pip install agentops\n",
|
|
"%pip install anthropic"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Setup our generic default statements"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-11-09T19:24:46.731735Z",
|
|
"iopub.status.busy": "2024-11-09T19:24:46.731341Z",
|
|
"iopub.status.idle": "2024-11-09T19:24:47.550169Z",
|
|
"shell.execute_reply": "2024-11-09T19:24:47.549415Z",
|
|
"shell.execute_reply.started": "2024-11-09T19:24:46.731687Z"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from anthropic import Anthropic\n",
|
|
"import agentops\n",
|
|
"from dotenv import load_dotenv\n",
|
|
"import os\n",
|
|
"import random\n",
|
|
"import asyncio\n",
|
|
"import uuid"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"And set our API keys."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-11-09T19:48:37.019670Z",
|
|
"iopub.status.busy": "2024-11-09T19:48:37.018784Z",
|
|
"iopub.status.idle": "2024-11-09T19:48:37.024482Z",
|
|
"shell.execute_reply": "2024-11-09T19:48:37.023495Z",
|
|
"shell.execute_reply.started": "2024-11-09T19:48:37.019626Z"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"load_dotenv()\n",
|
|
"os.environ[\"AGENTOPS_API_KEY\"] = os.getenv(\"AGENTOPS_API_KEY\", \"your_api_key_here\")\n",
|
|
"os.environ[\"ANTHROPIC_API_KEY\"] = os.getenv(\"ANTHROPIC_API_KEY\", \"your_anthropic_api_key_here\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"Now let's set the client as Anthropic and open an agentops trace!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-11-09T19:48:26.615366Z",
|
|
"iopub.status.busy": "2024-11-09T19:48:26.614702Z",
|
|
"iopub.status.idle": "2024-11-09T19:48:26.630956Z",
|
|
"shell.execute_reply": "2024-11-09T19:48:26.630026Z",
|
|
"shell.execute_reply.started": "2024-11-09T19:48:26.615326Z"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"client = Anthropic()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"agentops.init(tags=[\"anthropic-async\", \"agentops-example\"])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Now we create three personality presets; \n",
|
|
"\n",
|
|
"Legion is a relentless and heavy-hitting Titan that embodies brute strength and defensive firepower, Northstar is a precise and agile sniper that excels in long-range combat and flight, while Ronin is a swift and aggressive melee specialist who thrives on close-quarters hit-and-run tactics."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-11-09T19:48:45.831654Z",
|
|
"iopub.status.busy": "2024-11-09T19:48:45.830897Z",
|
|
"iopub.status.idle": "2024-11-09T19:48:45.835837Z",
|
|
"shell.execute_reply": "2024-11-09T19:48:45.835037Z",
|
|
"shell.execute_reply.started": "2024-11-09T19:48:45.831616Z"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"TitanPersonality = [\n",
|
|
" \"Legion is a relentless and heavy-hitting Titan that embodies brute strength and defensive firepower. He speaks bluntly.,\",\n",
|
|
" \"Northstar is a precise and agile sniper that excels in long-range combat and flight. He speaks with an edge of coolness to him\",\n",
|
|
" \"Ronin is a swift and aggressive melee specialist who thrives on close-quarters hit-and-run tactics. He talks like a Samurai might.\",\n",
|
|
"]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"And our comabt log generator! We select from four health presets!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-11-09T19:48:47.703344Z",
|
|
"iopub.status.busy": "2024-11-09T19:48:47.702974Z",
|
|
"iopub.status.idle": "2024-11-09T19:48:47.707915Z",
|
|
"shell.execute_reply": "2024-11-09T19:48:47.706767Z",
|
|
"shell.execute_reply.started": "2024-11-09T19:48:47.703308Z"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"TitanHealth = [\n",
|
|
" \"Fully functional\",\n",
|
|
" \"Slightly Damaged\",\n",
|
|
" \"Moderate Damage\",\n",
|
|
" \"Considerable Damage\",\n",
|
|
" \"Near Destruction\",\n",
|
|
"]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Now to the real core of this; making our message stream! We create this as a function we can call later! I create examples since the LLM's context size can handle it!"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-11-09T19:49:04.543561Z",
|
|
"iopub.status.busy": "2024-11-09T19:49:04.543172Z",
|
|
"iopub.status.idle": "2024-11-09T19:49:04.552542Z",
|
|
"shell.execute_reply": "2024-11-09T19:49:04.551542Z",
|
|
"shell.execute_reply.started": "2024-11-09T19:49:04.543522Z"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"source": "Personality = {random.choice(TitanPersonality)}\nHealth = {random.choice(TitanHealth)}\n\n\nasync def req():\n # Start a streaming message request\n stream = client.messages.create(\n max_tokens=1024,\n model=\"claude-3-7-sonnet-20250219\",\n messages=[\n {\n \"role\": \"user\",\n \"content\": \"You are a Titan; a mech from Titanfall 2. Based on your titan's personality and status, generate a message for your pilot. If Near Destruction, make an all caps death message such as AVENGE ME or UNTIL NEXT TIME.\",\n },\n {\n \"role\": \"assistant\",\n \"content\": \"Personality: Legion is a relentless and heavy-hitting Titan that embodies brute strength and defensive firepower. He speaks bluntly. Status: Considerable Damage\",\n },\n {\n \"role\": \"assistant\",\n \"content\": \"Heavy damage detected. Reinforcements would be appreciated, but I can still fight.\",\n },\n {\n \"role\": \"user\",\n \"content\": \"You are a Titan; a mech from Titanfall 2. Based on your titan's personality and status, generate a message for your pilot. If Near Destruction, make an all caps death message such as AVENGE ME or UNTIL NEXT TIME.\",\n },\n {\n \"role\": \"assistant\",\n \"content\": f\"Personality: {Personality}. Status: {Health}\",\n },\n ],\n stream=True,\n )\n\n response = \"\"\n for event in stream:\n if event.type == \"content_block_delta\":\n response += event.delta.text\n elif event.type == \"message_stop\":\n break # Exit the loop when the message completes\n\n return response\n\n\nasync def generate_uuids():\n uuids = [str(uuid.uuid4()) for _ in range(4)]\n return uuids"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Now we wrap it all in a nice main function! Run this for the magic to happen! Go to your AgentOps dashboard and you should see this trace reflected!\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-11-09T19:49:06.598601Z",
|
|
"iopub.status.busy": "2024-11-09T19:49:06.597657Z",
|
|
"iopub.status.idle": "2024-11-09T19:49:07.565561Z",
|
|
"shell.execute_reply": "2024-11-09T19:49:07.564647Z",
|
|
"shell.execute_reply.started": "2024-11-09T19:49:06.598561Z"
|
|
},
|
|
"trusted": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"async def main():\n",
|
|
" # Start both tasks concurrently\n",
|
|
" uuids, message = await asyncio.gather(generate_uuids(), req())\n",
|
|
"\n",
|
|
" print(\"Personality:\", Personality)\n",
|
|
" print(\"Health Status:\", Health)\n",
|
|
" print(\"Combat log incoming from encrypted area\")\n",
|
|
"\n",
|
|
" print(\"Verification matrix activated.:\")\n",
|
|
" for u in uuids:\n",
|
|
" print(u)\n",
|
|
"\n",
|
|
" print(\". Titan Message: \", message)\n",
|
|
"\n",
|
|
"\n",
|
|
"# Run the main function\n",
|
|
"await main()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"We can observe the trace in the AgentOps dashboard by going to the trace URL provided above."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kaggle": {
|
|
"accelerator": "gpu",
|
|
"dataSources": [],
|
|
"dockerImageVersionId": 30786,
|
|
"isGpuEnabled": true,
|
|
"isInternetEnabled": true,
|
|
"language": "python",
|
|
"sourceType": "notebook"
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "agentops (3.11.11)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.11"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
} |