{ "cells": [ { "cell_type": "markdown", "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", "metadata": {}, "source": [ "# How to stream updates from your graph\n", "\n", "There are multiple different streaming modes.\n", "\n", "- `values`: This streaming mode streams back values of the graph. This is the **full state of the graph** after each node is called.\n", "- `updates`: This streaming mode streams back updates to the graph. This is the **update to the state of the graph** after each node is called.\n", "- `messages`: This streaming mode streams back messages - both complete messages (at the end of a node) as well as **tokens** for any messages generated inside a node. This mode is primarily meant for powering chat applications.\n", "\n", "\n", "This notebook covers `streaming_mode=\"updates\"`." ] }, { "cell_type": "code", "execution_count": 1, "id": "521d975b-e94b-4c37-bfa1-82d969e2a4dc", "metadata": {}, "outputs": [], "source": [ "from langgraph_sdk import get_client" ] }, { "cell_type": "code", "execution_count": 2, "id": "27a1392b-86c3-464e-99a8-90ffc965f3ec", "metadata": {}, "outputs": [], "source": [ "client = get_client()" ] }, { "cell_type": "code", "execution_count": 3, "id": "4947e9bc-111f-4991-8c41-1041da9bf0ba", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n", " 'graph_id': 'agent',\n", " 'config': {},\n", " 'created_at': '2024-05-18T00:19:39.688822+00:00',\n", " 'updated_at': '2024-05-18T00:19:39.688822+00:00',\n", " 'metadata': {'created_by': 'system'}}]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# There should always be a default assistant with no configuration\n", "assistants = await client.assistants.search()\n", "assistants = [a for a in assistants if not a['config']]\n", "assistants" ] }, { "cell_type": "code", "execution_count": 4, "id": "230c0464-a6e5-420f-9e38-ca514e5634ce", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n", " 'graph_id': 'agent',\n", " 'config': {},\n", " 'created_at': '2024-05-18T00:19:39.688822+00:00',\n", " 'updated_at': '2024-05-18T00:19:39.688822+00:00',\n", " 'metadata': {'created_by': 'system'}}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "assistant = assistants[0]\n", "assistant" ] }, { "cell_type": "code", "execution_count": 5, "id": "56aa5159-5583-4134-9210-709b969bda6f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'thread_id': '1eee9bd4-ec61-4300-a38a-2e13b8925d39',\n", " 'created_at': '2024-05-18T19:57:36.509105+00:00',\n", " 'updated_at': '2024-05-18T19:57:36.509105+00:00',\n", " 'metadata': {}}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "thread = await client.threads.create()\n", "thread" ] }, { "cell_type": "code", "execution_count": 6, "id": "147c3f98-f889-4f05-a090-6b31f2a0b291", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "runs = await client.runs.list(thread['thread_id'])\n", "runs" ] }, { "cell_type": "code", "execution_count": 7, "id": "7da70e20-1a4e-4df2-b996-1927f474c835", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Receiving new event of type: metadata...\n", "{'run_id': 'cfc96c16-ed9a-44bd-b5bb-c30e3c0725f0'}\n", "\n", "\n", "\n", "Receiving new event of type: data...\n", "{'agent': {'messages': [{'content': [{'id': 'toolu_0148tMmDK51iLQfG1yaNwRHM', 'input': {'query': 'weather in los angeles'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}], 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'ai', 'name': None, 'id': 'run-1a9d32b0-7007-4a36-abde-8df812a0ed94', 'example': False, 'tool_calls': [{'name': 'tavily_search_results_json', 'args': {'query': 'weather in los angeles'}, 'id': 'toolu_0148tMmDK51iLQfG1yaNwRHM'}], 'invalid_tool_calls': []}]}}\n", "\n", "\n", "\n", "Receiving new event of type: data...\n", "{'action': {'messages': [{'content': '[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{\\'location\\': {\\'name\\': \\'Los Angeles\\', \\'region\\': \\'California\\', \\'country\\': \\'United States of America\\', \\'lat\\': 34.05, \\'lon\\': -118.24, \\'tz_id\\': \\'America/Los_Angeles\\', \\'localtime_epoch\\': 1716062239, \\'localtime\\': \\'2024-05-18 12:57\\'}, \\'current\\': {\\'last_updated_epoch\\': 1716061500, \\'last_updated\\': \\'2024-05-18 12:45\\', \\'temp_c\\': 18.9, \\'temp_f\\': 66.0, \\'is_day\\': 1, \\'condition\\': {\\'text\\': \\'Overcast\\', \\'icon\\': \\'//cdn.weatherapi.com/weather/64x64/day/122.png\\', \\'code\\': 1009}, \\'wind_mph\\': 2.2, \\'wind_kph\\': 3.6, \\'wind_degree\\': 10, \\'wind_dir\\': \\'N\\', \\'pressure_mb\\': 1017.0, \\'pressure_in\\': 30.02, \\'precip_mm\\': 0.0, \\'precip_in\\': 0.0, \\'humidity\\': 65, \\'cloud\\': 100, \\'feelslike_c\\': 18.9, \\'feelslike_f\\': 66.0, \\'vis_km\\': 16.0, \\'vis_miles\\': 9.0, \\'uv\\': 6.0, \\'gust_mph\\': 7.5, \\'gust_kph\\': 12.0}}\"}]', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'tool', 'name': 'tavily_search_results_json', 'id': 'a36e8cd1-0e96-4417-9c15-f10a945d2b42', 'tool_call_id': 'toolu_0148tMmDK51iLQfG1yaNwRHM'}]}}\n", "\n", "\n", "\n", "Receiving new event of type: data...\n", "{'agent': {'messages': [{'content': 'The weather in Los Angeles is currently overcast with a temperature of around 66°F (18.9°C). There are light winds from the north at around 2-3 mph. The humidity is 65% and visibility is good at 9 miles. Overall, mild spring weather conditions in LA.', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'ai', 'name': None, 'id': 'run-d5c1c2f0-b12d-41ce-990b-f36570e7483d', 'example': False, 'tool_calls': [], 'invalid_tool_calls': []}]}}\n", "\n", "\n", "\n", "Receiving new event of type: end...\n", "None\n", "\n", "\n", "\n" ] } ], "source": [ "input = {\"messages\": [{\"role\": \"human\", \"content\": \"whats the weather in la\"}]}\n", "async for chunk in client.runs.stream(thread['thread_id'], assistant['assistant_id'], input=input, stream_mode=\"updates\", ):\n", " print(f\"Receiving new event of type: {chunk.event}...\")\n", " print(chunk.data)\n", " print(\"\\n\\n\")" ] }, { "cell_type": "code", "execution_count": null, "id": "53800469-354a-4739-8e77-b88044c772d5", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.1" } }, "nbformat": 4, "nbformat_minor": 5 }