-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Co-authored-by: reibs <[email protected]>
- Loading branch information
1 parent
842d9b3
commit 84a94dd
Showing
7 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.