From ed1da54225ddd103a25c8d1cc1eabac681a11d2d Mon Sep 17 00:00:00 2001 From: Pratyush Shukla Date: Wed, 14 Aug 2024 17:49:11 +0530 Subject: [PATCH] add async example --- examples/anthropic/anthropic_example.ipynb | 82 +++++++++++++++++++--- 1 file changed, 71 insertions(+), 11 deletions(-) diff --git a/examples/anthropic/anthropic_example.ipynb b/examples/anthropic/anthropic_example.ipynb index 486e28ee..3f287621 100644 --- a/examples/anthropic/anthropic_example.ipynb +++ b/examples/anthropic/anthropic_example.ipynb @@ -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" @@ -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()" ] }, { @@ -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", @@ -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." ] }, { @@ -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", @@ -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)" ] }, {