+{"cells":[{"cell_type":"markdown","metadata":{},"source":["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":2,"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, AsyncAnthropic\n","import agentops\n","import os\n","import random #We don't need this for agentops, we use this to generate a message later\n","import asyncio #We don't need this for agentops, we use this to run both async tasks and await them both finishing later\n","import uuid #We don't need this for agentops, we use this to generate UUIDs\n","from dotenv import load_dotenv"]},{"cell_type":"markdown","metadata":{},"source":["And set our API keys."]},{"cell_type":"code","execution_count":3,"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","ANTHROPIC_API_KEY = os.getenv(\"ANTHROPIC_API_KEY\") or \"<your_anthropic_key>\"\n","AGENTOPS_API_KEY = os.getenv(\"AGENTOPS_API_KEY\") or \"<your_agentops_key>\""]},{"cell_type":"markdown","metadata":{},"source":["\n","Now let's set the client as Anthropic and open an agentops session!"]},{"cell_type":"code","execution_count":4,"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(api_key=ANTHROPIC_API_KEY)"]},{"cell_type":"code","execution_count":null,"metadata":{"trusted":true},"outputs":[],"source":["agentops.init(AGENTOPS_API_KEY, default_tags=[\"anthropic-async\"])"]},{"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":6,"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":7,"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\", \"Slightly Damaged\", \"Moderate Damage\", \"Considerable Damage\", \"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","execution_count":8,"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)}\n","Health = {random.choice(TitanHealth)}\n","\n","async def req():\n"," # Start a streaming message request\n"," stream = client.messages.create(\n"," max_tokens=1024,\n"," model=\"claude-3-5-sonnet-20240620\",\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"," Returned = response\n"," break # Exit the loop when the message completes\n","\n"," return response\n"," Returned = response\n","\n","async def generate_uuids():\n"," uuids = [str(uuid.uuid4()) for _ in range(4)]\n"," return uuids\n"," \n"]},{"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 session 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","# Run the main function\n","await main()"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["agentops.end_session(\"Success\")"]},{"cell_type":"markdown","metadata":{},"source":["Run this for the magic to happen! Go to your AgentOps dashboard and you should see this session reflected!"]}],"metadata":{"kaggle":{"accelerator":"gpu","dataSources":[],"dockerImageVersionId":30786,"isGpuEnabled":true,"isInternetEnabled":true,"language":"python","sourceType":"notebook"},"kernelspec":{"display_name":"Python 3","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.5"}},"nbformat":4,"nbformat_minor":4}
0 commit comments