Skip to content

Commit

Permalink
add async example
Browse files Browse the repository at this point in the history
  • Loading branch information
the-praxs committed Aug 14, 2024
1 parent c10deab commit ed1da54
Showing 1 changed file with 71 additions and 11 deletions.
82 changes: 71 additions & 11 deletions examples/anthropic/anthropic_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"metadata": {},
"outputs": [],
"source": [
"import anthropic\n",
"from anthropic import Anthropic, AsyncAnthropic\n",
"import agentops\n",
"import os\n",
"from dotenv import load_dotenv"
Expand Down Expand Up @@ -67,8 +67,24 @@
"metadata": {},
"outputs": [],
"source": [
"agentops.init(AGENTOPS_API_KEY, default_tags=[\"anthropic-example\"])\n",
"client = anthropic.Anthropic()"
"agentops.init(AGENTOPS_API_KEY, default_tags=[\"anthropic-example\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sync Example\n",
"We will demonstrate a basic sync call to Anthropic using the Claude 3.5 Sonnet model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"client = Anthropic()"
]
},
{
Expand All @@ -79,7 +95,7 @@
"source": [
"stream = client.messages.create(\n",
" max_tokens=1024,\n",
" model=\"claude-3-opus-20240229\",\n",
" model=\"claude-3-5-sonnet-20240620\",\n",
" messages=[\n",
" {\n",
" \"role\": \"user\",\n",
Expand All @@ -94,9 +110,55 @@
" if event.type == \"content_block_delta\":\n",
" response += event.delta.text\n",
" elif event.type == \"message_stop\":\n",
" print(\"\\n\")\n",
" print(response)\n",
" print(\"\\n\")"
" print(response)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Async Example\n",
"The async example is very similar to the sync example, but it uses the AsyncAnthropic client from the anthropic library."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"aclient = AsyncAnthropic()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"async def main() -> None:\n",
" message = await aclient.messages.create(\n",
" max_tokens=1024,\n",
" messages=[\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"Explain everything you know about the Leibniz Equation.\",\n",
" }\n",
" ],\n",
" model=\"claude-3-5-sonnet-20240620\",\n",
" )\n",
" print(message.content[0].text)\n",
"\n",
"\n",
"await main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tool Calling Example\n",
"Anthropic models support tool calling, which allows the model to call external APIs."
]
},
{
Expand All @@ -107,7 +169,7 @@
"source": [
"stream = client.messages.create(\n",
" max_tokens=1024,\n",
" model=\"claude-3-opus-20240229\",\n",
" model=\"claude-3-5-sonnet-20240620\",\n",
" tools=[\n",
" {\n",
" \"name\": \"web_search\",\n",
Expand Down Expand Up @@ -136,9 +198,7 @@
" if event.delta.type == \"text\":\n",
" response += event.delta.text\n",
" elif event.type == \"message_stop\":\n",
" print(\"\\n\")\n",
" print(response)\n",
" print(\"\\n\")"
" print(response)"
]
},
{
Expand Down

0 comments on commit ed1da54

Please sign in to comment.