Skip to content

Commit

Permalink
fix issue anthropics#28 remove block related to removed `event_handle…
Browse files Browse the repository at this point in the history
…r` code
  • Loading branch information
eyefodder committed Sep 29, 2024
1 parent b4f26ae commit 58ab0d3
Showing 1 changed file with 0 additions and 131 deletions.
131 changes: 0 additions & 131 deletions anthropic_api_fundamentals/05_Streaming.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -876,137 +876,6 @@
"await streaming_with_helpers()"
]
},
{
"cell_type": "markdown",
"id": "c97eafbd",
"metadata": {},
"source": [
"\n",
"When using `client.messages.stream()`, we can also define custom event handlers that run when any stream event occurs, or only when text is generated, etc.\n",
"\n",
"The example below uses two of these custom event handlers. We use `client.messages.stream()` and ask the model to \"generate a 5-word poem\". We define our own `MyStream` class which has two event handlers defined:\n",
"\n",
"* `on_text` - The event is fired when a text ContentBlock object is being accumulated. The first argument is the text delta and the second is the current accumulated text. In the example below, we're using this event handler to print out any generated text as it streams in. The text is printed out in green to make it easier to visualize.\n",
"* `on_stream_event` - The event is fired when any event is received from the API. In the example below, we're printing the event type anytime an event is received.\n",
"\n",
"We then pass an `event_handler` argument to `client.messages.stream` to register callback methods that are fired when certain events happen:\n"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "f6ef03c1",
"metadata": {},
"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\u001b[0m\n",
"on_event fired: content_block_delta\n",
"\u001b[32m 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 secrets\u001b[0m\n",
"on_event fired: content_block_delta\n",
"\u001b[32m unf\u001b[0m\n",
"on_event fired: content_block_delta\n",
"\u001b[32mol\u001b[0m\n",
"on_event fired: content_block_delta\n",
"\u001b[32md,\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",
"accumulated final message: {\n",
" \"id\": \"msg_014G44rr3M14DzadHXPn9Xaj\",\n",
" \"content\": [\n",
" {\n",
" \"text\": \"Whispers dance, secrets unfold, love.\",\n",
" \"type\": \"text\"\n",
" }\n",
" ],\n",
" \"model\": \"claude-3-opus-20240229\",\n",
" \"role\": \"assistant\",\n",
" \"stop_reason\": \"end_turn\",\n",
" \"stop_sequence\": null,\n",
" \"type\": \"message\",\n",
" \"usage\": {\n",
" \"input_tokens\": 14,\n",
" \"output_tokens\": 14\n",
" }\n",
"}\n"
]
}
],
"source": [
"from anthropic import AsyncAnthropic, AsyncMessageStream\n",
"\n",
"client = AsyncAnthropic()\n",
"\n",
"green = '\\033[32m'\n",
"reset = '\\033[0m'\n",
"\n",
"class MyStream(AsyncMessageStream):\n",
" async def on_text(self, text, snapshot):\n",
" # This runs only on text delta stream messages\n",
" print(green + text + reset, flush=True) #model generated content is printed in green\n",
"\n",
" async def on_stream_event(self, event):\n",
" # This runs on any stream event\n",
" print(\"on_event fired:\", event.type)\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",
" event_handler=MyStream,\n",
" ) as stream:\n",
" # Get the final accumulated message, after the stream is exhausted\n",
" message = await stream.get_final_message()\n",
" print(\"accumulated final message: \", message.to_json())\n",
"\n",
"await streaming_events_demo()"
]
},
{
"cell_type": "markdown",
"id": "ff205093",
"metadata": {},
"source": [
"The Python SDK gives us access to a handful of other event handlers we can utilize including: \n",
"\n",
"##### `on_message(message: Message)`\n",
"The event is fired when a full Message object has been accumulated. This corresponds to the message_stop SSE.\n",
"\n",
"##### `on_content_block(content_block: ContentBlock)`\n",
"The event is fired when a full ContentBlock object has been accumulated. This corresponds to the content_block_stop SSE.\n",
"\n",
"##### `on_exception(exception: Exception)`\n",
"The event is fired when an exception is encountered while streaming the response.\n",
"\n",
"##### `on_timeout()`\n",
"The event is fired when the request times out.\n",
"\n",
"##### `on_end()`\n",
"The last event fired in the stream."
]
},
{
"attachments": {
"streaming_chat_exercise.gif": {
Expand Down

0 comments on commit 58ab0d3

Please sign in to comment.