Skip to content

Commit 84a94dd

Browse files
TKTSWalkerdot-agiareibman
authored
Anthropic Examples/Readme Guide Created and Proper Reference Noted. (#466)
* Anthropic Example Journals Added Contains 3 journals showcasing sync/async using anthropic and a tool example! * Fixed Location * Fixed Location * Fixed Location * Create anthropicguide.md draft * Delete examples/anthropic_examples/anthropic-example-async.ipynb Made better example * Add files via upload * Rename anthropic-example-async (1).ipynb to anthropic-example-async.ipynb * Update anthropic-example-async.ipynb * Delete examples/anthropic_examples/anthropic-example-async.ipynb * Add files via upload This example uses async in a little more meaningful way while also taking care of a few fixes and errors! * Update anthropicguide.md Finalized page and made clearer! Also fixed a number of issues within the async function example. * Update README.md Changed Anthropic example link to point to the guide readme * Refixed/Revamped Anthropic AgentOps Examples I made a few fixes which I forgot to account for during development, for example - Accidently using the "computer role" when only "assistant" and "user" role is allowed - Making several major changes for readability - Keeping output strings to show that the scripts work properly and - Properly starting an AgentOps session which properly logs everything * update to end sessio * update anthropic docs * update pages --------- Co-authored-by: Pratyush Shukla <ps4534@nyu.edu> Co-authored-by: reibs <areibman@gmail.com>
1 parent 842d9b3 commit 84a94dd

7 files changed

Lines changed: 208 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ agentops.end_session('Success')
272272

273273
Track agents built with the Anthropic Python SDK (>=0.32.0).
274274

275-
- [AgentOps integration example](examples/anthropic-sdk/anthropic_example.ipynb)
275+
- [AgentOps integration guide](https://docs.agentops.ai/v1/integrations/anthropic)
276276
- [Official Anthropic documentation](https://docs.anthropic.com/en/docs/welcome)
277277

278278
<details>

docs/mint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"v1/integrations/autogen",
9292
"v1/integrations/langchain",
9393
"v1/integrations/cohere",
94+
"v1/integrations/anthropic",
9495
"v1/integrations/litellm",
9596
"v1/integrations/multion"
9697
]

docs/v1/integrations/anthropic.mdx

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
title: Anthropic
3+
description: "AgentOps provides first class support for Anthropic's Claude"
4+
---
5+
6+
import CodeTooltip from '/snippets/add-code-tooltip.mdx'
7+
import EnvTooltip from '/snippets/add-env-tooltip.mdx'
8+
9+
<Note>
10+
This is a living integration. Should you need any added functionality, message us on [Discord](https://discord.gg/UgJyyxx7uc)!
11+
</Note>
12+
13+
<Card title="Anthropic" icon="robot" href="https://www.anthropic.com">
14+
First class support for Claude
15+
</Card>
16+
17+
<Steps>
18+
<Step title="Install the AgentOps SDK">
19+
<CodeGroup>
20+
```bash pip
21+
pip install agentops anthropic
22+
```
23+
```bash poetry
24+
poetry add agentops anthropic
25+
```
26+
</CodeGroup>
27+
</Step>
28+
<Step title="Add 3 lines of code">
29+
<CodeTooltip/>
30+
<span className="api-key-container">
31+
<CodeGroup>
32+
```python python
33+
import agentops
34+
from anthropic import Anthropic
35+
36+
agentops.init(<INSERT YOUR API KEY HERE>)
37+
client = Anthropic()
38+
...
39+
# End of program (e.g. main.py)
40+
agentops.end_session("Success") # Success|Fail|Indeterminate
41+
```
42+
</CodeGroup>
43+
</span>
44+
<EnvTooltip />
45+
<span className="api-key-container">
46+
<CodeGroup>
47+
```python .env
48+
AGENTOPS_API_KEY=<YOUR API KEY>
49+
ANTHROPIC_API_KEY=<YOUR ANTHROPIC API KEY>
50+
```
51+
</CodeGroup>
52+
Read more about environment variables in [Advanced Configuration](/v1/usage/advanced-configuration)
53+
</span>
54+
</Step>
55+
<Step title="Run your Agent">
56+
Execute your program and visit [app.agentops.ai/drilldown](https://app.agentops.ai/drilldown) to observe your Agent! 🕵️
57+
<Tip>
58+
After your run, AgentOps prints a clickable url to console linking directly to your session in the Dashboard
59+
</Tip>
60+
<div/>
61+
<Frame type="glass" caption="Clickable link to session">
62+
<img height="200" src="https://github.com/AgentOps-AI/agentops/blob/cf67191f13e0e2a09446a61b7393e1810b3eee95/docs/images/link-to-session.gif?raw=true" />
63+
</Frame>
64+
</Step>
65+
</Steps>
66+
67+
## Full Examples
68+
69+
<CodeGroup>
70+
```python sync
71+
from anthropic import Anthropic
72+
import agentops
73+
74+
agentops.init(<INSERT YOUR API KEY HERE>)
75+
client = Anthropic()
76+
77+
message = client.messages.create(
78+
model="claude-3-opus-20240229",
79+
max_tokens=1024,
80+
messages=[{
81+
"role": "user",
82+
"content": "Write a haiku about AI and humans working together"
83+
}]
84+
)
85+
86+
print(message.content)
87+
agentops.end_session('Success')
88+
```
89+
90+
```python async
91+
from anthropic import AsyncAnthropic
92+
import agentops
93+
import asyncio
94+
95+
async def main():
96+
agentops.init(<INSERT YOUR API KEY HERE>)
97+
client = AsyncAnthropic()
98+
99+
message = await client.messages.create(
100+
model="claude-3-opus-20240229",
101+
max_tokens=1024,
102+
messages=[{
103+
"role": "user",
104+
"content": "Write a haiku about AI and humans working together"
105+
}]
106+
)
107+
108+
print(message.content)
109+
agentops.end_session('Success')
110+
111+
asyncio.run(main())
112+
```
113+
</CodeGroup>
114+
115+
<script type="module" src="/scripts/github_stars.js"></script>
116+
<script type="module" src="/scripts/link_to_api_button.js"></script>
117+
<script type="module" src="/scripts/scroll-img-fadein-animation.js"></script>
118+
<script type="module" src="/scripts/button_heartbeat_animation.js"></script>
119+
<script type="css" src="/styles/styles.css"></script>
120+
<script type="module" src="/scripts/adjust_api_dynamically.js"></script>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"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}

examples/anthropic_examples/anthropic-example-sync.ipynb

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Anthropic and AgentOps
2+
3+
AgentOps supports Anthropic's API for conversing with their LLM backend!
4+
5+
To start, learn more about Antropic [here!](https://www.anthropic.com)
6+
If you want to get down to the gritty details, look [here](https://docs.anthropic.com/en/docs/welcome) for their documentation.
7+
8+
9+
> [!NOTE]
10+
> If it's your first time developing for an LLM, be sure to look at our intro to LLMs (coming soon)! Here, we explain generic functions such as giving the AI a memory to exploring novel concepts like summarizing chunks at regular intervals to keep some context while saving memory!
11+
12+
## Getting Started
13+
14+
You can get Anthropic's API working with a few lines of code!
15+
16+
### 1. Import agentops and anthropic to your environment
17+
18+
```python
19+
pip install agentops
20+
pip install anthropic
21+
```
22+
23+
### 2. Setup import statements
24+
25+
```python
26+
from anthropic import Anthropic, AsyncAnthropic
27+
import agentops
28+
import os
29+
from dotenv import load_dotenv
30+
```
31+
32+
### 3. Set your API keys
33+
34+
```python
35+
load_dotenv()
36+
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY") or "<your_anthropic_key>"
37+
AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY") or "<your_agentops_key>"
38+
```
39+
40+
From here, you have a number of ways you can interact with the Anthropic API!
41+
42+
## Examples
43+
44+
> [!NOTE]
45+
> You can download these journals directly and try them on Google Colab or Kaggle!
46+
47+
48+
> [!WARNING]
49+
> Remember; you need to set an API key for both Agentops and Anthropic!
50+
51+
52+
## Sync Example; Nier Storyteller
53+
54+
In this example, we generate a sentence from three parts before having Anthropic generate a short story based off it!
55+
56+
[Access the Journal By Clicking Here](./anthropic-example-sync.ipynb).
57+
58+
## Async Example; Titan Support Protocol
59+
60+
In this example, we generate a script line for a mech based on it's health and the type. At the same time, we generate 4 UUIDs. We finally wait for both functions to finish before printing them for the user.
61+
62+
[Access the Journal By Clicking Here](./anthropic-example-async.ipynb)
63+
64+
## Tool Example; Cyberware
65+
66+
In this example, we have the LLM call a simulated tool which gives one random piece of Cyberware based on the user's requested company. From there, the AI tells the user if the cyberware is good for the user's intended purpose. (combatant, hacker, etc.),
67+
68+
[Access the Journal By Clicking Here](./antrophic-example-tool.ipynb)
69+
70+
71+
72+
73+
74+
## Looking for a Barebones, Straight To The Point Journal?
75+
76+
This journal directly shows the bare basics on using Anthropic and AgentOps!
77+
78+
> [!WARNING]
79+
> This is mainly recommended for those adept at programming or those who already understand how AgentOps and LLM APIs generally work!
80+
81+
[Access the Journal By Clicking Here](./anthropic_example.ipynb)
82+
83+

examples/anthropic_examples/antrophic-example-tool.ipynb

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)