Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anthropic Examples/Readme Guide Created and Proper Reference Noted. #466

Merged
merged 19 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ agentops.end_session('Success')

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

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

<details>
Expand Down
1 change: 1 addition & 0 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"v1/integrations/autogen",
"v1/integrations/langchain",
"v1/integrations/cohere",
"v1/integrations/anthropic",
"v1/integrations/litellm",
"v1/integrations/multion"
]
Expand Down
120 changes: 120 additions & 0 deletions docs/v1/integrations/anthropic.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
title: Anthropic
description: "AgentOps provides first class support for Anthropic's Claude"
---

import CodeTooltip from '/snippets/add-code-tooltip.mdx'
import EnvTooltip from '/snippets/add-env-tooltip.mdx'

<Note>
This is a living integration. Should you need any added functionality, message us on [Discord](https://discord.gg/UgJyyxx7uc)!
</Note>

<Card title="Anthropic" icon="robot" href="https://www.anthropic.com">
First class support for Claude
</Card>

<Steps>
<Step title="Install the AgentOps SDK">
<CodeGroup>
```bash pip
pip install agentops anthropic
```
```bash poetry
poetry add agentops anthropic
```
</CodeGroup>
</Step>
<Step title="Add 3 lines of code">
<CodeTooltip/>
<span className="api-key-container">
<CodeGroup>
```python python
import agentops
from anthropic import Anthropic

agentops.init(<INSERT YOUR API KEY HERE>)
client = Anthropic()
...
# End of program (e.g. main.py)
agentops.end_session("Success") # Success|Fail|Indeterminate
```
</CodeGroup>
</span>
<EnvTooltip />
<span className="api-key-container">
<CodeGroup>
```python .env
AGENTOPS_API_KEY=<YOUR API KEY>
ANTHROPIC_API_KEY=<YOUR ANTHROPIC API KEY>
```
</CodeGroup>
Read more about environment variables in [Advanced Configuration](/v1/usage/advanced-configuration)
</span>
</Step>
<Step title="Run your Agent">
Execute your program and visit [app.agentops.ai/drilldown](https://app.agentops.ai/drilldown) to observe your Agent! 🕵️
<Tip>
After your run, AgentOps prints a clickable url to console linking directly to your session in the Dashboard
</Tip>
<div/>
<Frame type="glass" caption="Clickable link to session">
<img height="200" src="https://github.com/AgentOps-AI/agentops/blob/cf67191f13e0e2a09446a61b7393e1810b3eee95/docs/images/link-to-session.gif?raw=true" />
</Frame>
</Step>
</Steps>

## Full Examples

<CodeGroup>
```python sync
from anthropic import Anthropic
import agentops

agentops.init(<INSERT YOUR API KEY HERE>)
client = Anthropic()

message = client.messages.create(
model="claude-3-opus-20240229",
max_tokens=1024,
messages=[{
"role": "user",
"content": "Write a haiku about AI and humans working together"
}]
)

print(message.content)
agentops.end_session('Success')
```

```python async
from anthropic import AsyncAnthropic
import agentops
import asyncio

async def main():
agentops.init(<INSERT YOUR API KEY HERE>)
client = AsyncAnthropic()

message = await client.messages.create(
model="claude-3-opus-20240229",
max_tokens=1024,
messages=[{
"role": "user",
"content": "Write a haiku about AI and humans working together"
}]
)

print(message.content)
agentops.end_session('Success')

asyncio.run(main())
```
</CodeGroup>

<script type="module" src="/scripts/github_stars.js"></script>
<script type="module" src="/scripts/link_to_api_button.js"></script>
<script type="module" src="/scripts/scroll-img-fadein-animation.js"></script>
<script type="module" src="/scripts/button_heartbeat_animation.js"></script>
<script type="css" src="/styles/styles.css"></script>
<script type="module" src="/scripts/adjust_api_dynamically.js"></script>
areibman marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions examples/anthropic_examples/anthropic-example-async.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +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}
1 change: 1 addition & 0 deletions examples/anthropic_examples/anthropic-example-sync.ipynb

Large diffs are not rendered by default.

83 changes: 83 additions & 0 deletions examples/anthropic_examples/anthropicguide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Anthropic and AgentOps

AgentOps supports Anthropic's API for conversing with their LLM backend!

To start, learn more about Antropic [here!](https://www.anthropic.com)
If you want to get down to the gritty details, look [here](https://docs.anthropic.com/en/docs/welcome) for their documentation.


> [!NOTE]
> 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!

## Getting Started

You can get Anthropic's API working with a few lines of code!

### 1. Import agentops and anthropic to your environment

```python
pip install agentops
pip install anthropic
```

### 2. Setup import statements

```python
from anthropic import Anthropic, AsyncAnthropic
import agentops
import os
from dotenv import load_dotenv
```

### 3. Set your API keys

```python
load_dotenv()
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY") or "<your_anthropic_key>"
AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY") or "<your_agentops_key>"
```

From here, you have a number of ways you can interact with the Anthropic API!

## Examples

> [!NOTE]
> You can download these journals directly and try them on Google Colab or Kaggle!


> [!WARNING]
> Remember; you need to set an API key for both Agentops and Anthropic!


## Sync Example; Nier Storyteller

In this example, we generate a sentence from three parts before having Anthropic generate a short story based off it!

[Access the Journal By Clicking Here](./anthropic-example-sync.ipynb).

## Async Example; Titan Support Protocol

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.

[Access the Journal By Clicking Here](./anthropic-example-async.ipynb)

## Tool Example; Cyberware

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.),

[Access the Journal By Clicking Here](./antrophic-example-tool.ipynb)





## Looking for a Barebones, Straight To The Point Journal?

This journal directly shows the bare basics on using Anthropic and AgentOps!

> [!WARNING]
> This is mainly recommended for those adept at programming or those who already understand how AgentOps and LLM APIs generally work!

[Access the Journal By Clicking Here](./anthropic_example.ipynb)


1 change: 1 addition & 0 deletions examples/anthropic_examples/antrophic-example-tool.ipynb

Large diffs are not rendered by default.

Loading