Skip to content

Commit

Permalink
fix issue anthropics#28 add suggested alternate cells
Browse files Browse the repository at this point in the history
  • Loading branch information
eyefodder committed Sep 29, 2024
1 parent 58ab0d3 commit 7f840d6
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions anthropic_api_fundamentals/05_Streaming.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 7f840d6

Please sign in to comment.