From 7f840d6990b9a7e0f7e2af8f7cf33161baece256 Mon Sep 17 00:00:00 2001 From: Paul BH Date: Sun, 29 Sep 2024 19:54:13 -0400 Subject: [PATCH] fix issue #28 add suggested alternate cells --- anthropic_api_fundamentals/05_Streaming.ipynb | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/anthropic_api_fundamentals/05_Streaming.ipynb b/anthropic_api_fundamentals/05_Streaming.ipynb index e8ba432..812eba6 100644 --- a/anthropic_api_fundamentals/05_Streaming.ipynb +++ b/anthropic_api_fundamentals/05_Streaming.ipynb @@ -876,6 +876,79 @@ "await streaming_with_helpers()" ] }, + { + "cell_type": "markdown", + "source": [ + "If you need to access more events than just the text content, you can iterate through the MessageStream. In this\n", + "example, we print out generated text as it is received, along with the other event types as they occur:" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 23, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "on_event fired: message_start\n", + "on_event fired: content_block_start\n", + "on_event fired: content_block_delta\n", + "\u001B[32mWhis\u001B[0m\n", + "on_event fired: content_block_delta\n", + "\u001B[32mpers dance\u001B[0m\n", + "on_event fired: content_block_delta\n", + "\u001B[32m,\u001B[0m\n", + "on_event fired: content_block_delta\n", + "\u001B[32m embrac\u001B[0m\n", + "on_event fired: content_block_delta\n", + "\u001B[32ming eternal\u001B[0m\n", + "on_event fired: content_block_delta\n", + "\u001B[32m love\u001B[0m\n", + "on_event fired: content_block_delta\n", + "\u001B[32m.\u001B[0m\n", + "on_event fired: content_block_stop\n", + "on_event fired: message_delta\n", + "on_event fired: message_stop\n" + ] + } + ], + "source": [ + "from anthropic import AsyncAnthropic\n", + "\n", + "client = AsyncAnthropic()\n", + "\n", + "green = '\\033[32m'\n", + "reset = '\\033[0m'\n", + "\n", + "async def streaming_events_demo():\n", + " async with client.messages.stream(\n", + " max_tokens=1024,\n", + " messages=[\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": \"Generate a 5-word poem\",\n", + " }\n", + " ],\n", + " model=\"claude-3-opus-20240229\",\n", + " ) as stream:\n", + " async for event in stream:\n", + " if event.type == \"text\":\n", + " # This runs only on text delta stream messages\n", + " print(green + event.text + reset, flush=True)\n", + " else:\n", + " # This runs on any stream event\n", + " print(\"on_event fired:\", event.type)\n", + "\n", + "await streaming_events_demo()" + ], + "metadata": { + "collapsed": false + } + }, { "attachments": { "streaming_chat_exercise.gif": {