mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-17 21:25:46 +02:00
[docs]: adding js/curl tabs (#1558)
* jsifying * x * z * x * fix background run output * reformatting * z * z * z * z * z * add tabs to stream debug * default urls
This commit is contained in:
@@ -1,388 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# How to kick off background runs\n",
|
||||
"\n",
|
||||
"This guide covers how to kick off background runs for your agent.\n",
|
||||
"This can be useful for long running jobs."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "b8e6408a-b37e-428f-9567-077fa55d58e8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Initialize the client\n",
|
||||
"from langgraph_sdk import get_client\n",
|
||||
"\n",
|
||||
"client = get_client()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "4947e9bc-111f-4991-8c41-1041da9bf0ba",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'assistant_id': 'e90fee30-be91-43aa-a33c-d54bd219072e',\n",
|
||||
" 'graph_id': 'agent',\n",
|
||||
" 'created_at': '2024-06-18T18:06:55.102231+00:00',\n",
|
||||
" 'updated_at': '2024-06-18T18:06:55.102231+00:00',\n",
|
||||
" 'config': {'configurable': {'model_name': 'anthropic'}},\n",
|
||||
" 'metadata': {}}"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# List available assistants\n",
|
||||
"assistants = await client.assistants.search()\n",
|
||||
"assistants[0]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "230c0464-a6e5-420f-9e38-ca514e5634ce",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# NOTE: we can use `assistant_id` UUID from the above response, or just pass graph ID instead when creating runs. we'll use graph ID here\n",
|
||||
"assistant_id = \"agent\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "56aa5159-5583-4134-9210-709b969bda6f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d',\n",
|
||||
" 'created_at': '2024-06-21T14:58:02.079462+00:00',\n",
|
||||
" 'updated_at': '2024-06-21T14:58:02.079462+00:00',\n",
|
||||
" 'metadata': {}}"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Create a new thread\n",
|
||||
"thread = await client.threads.create()\n",
|
||||
"thread"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "147c3f98-f889-4f05-a090-6b31f2a0b291",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[]"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# If we list runs on this thread, we can see it is empty\n",
|
||||
"runs = await client.runs.list(thread[\"thread_id\"])\n",
|
||||
"runs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "8c7b44ef-4816-496d-88a1-2f7327cf576d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Let's kick off a run\n",
|
||||
"input = {\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in sf\"}]}\n",
|
||||
"run = await client.runs.create(thread[\"thread_id\"], assistant_id, input=input)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "d84b4d80-b0aa-4d9f-a05d-0744b2fe8f72",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'run_id': '1ef2fdea-814c-6165-8b2a-a40e2a028198',\n",
|
||||
" 'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d',\n",
|
||||
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n",
|
||||
" 'created_at': '2024-06-21T14:58:02.095911+00:00',\n",
|
||||
" 'updated_at': '2024-06-21T14:58:02.095911+00:00',\n",
|
||||
" 'metadata': {},\n",
|
||||
" 'status': 'pending',\n",
|
||||
" 'kwargs': {'input': {'messages': [{'role': 'human',\n",
|
||||
" 'content': 'what's the weather in sf'}]},\n",
|
||||
" 'config': {'metadata': {'created_by': 'system'},\n",
|
||||
" 'configurable': {'run_id': '1ef2fdea-814c-6165-8b2a-a40e2a028198',\n",
|
||||
" 'user_id': '',\n",
|
||||
" 'graph_id': 'agent',\n",
|
||||
" 'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d',\n",
|
||||
" 'thread_ts': None,\n",
|
||||
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'}},\n",
|
||||
" 'webhook': None,\n",
|
||||
" 'temporary': False,\n",
|
||||
" 'stream_mode': ['events'],\n",
|
||||
" 'feedback_keys': None,\n",
|
||||
" 'interrupt_after': None,\n",
|
||||
" 'interrupt_before': None},\n",
|
||||
" 'multitask_strategy': 'reject'}"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# The first time we poll it, we can see `status=pending`\n",
|
||||
"await client.runs.get(thread[\"thread_id\"], run[\"run_id\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "3639da3c-bfe5-454c-ab1e-8ed7af394dfe",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Wait until the run finishes\n",
|
||||
"await client.runs.join(thread[\"thread_id\"], run[\"run_id\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "8fa206ed-515e-4607-9a80-bebafe76cc24",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'run_id': '1ef2fdea-814c-6165-8b2a-a40e2a028198',\n",
|
||||
" 'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d',\n",
|
||||
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n",
|
||||
" 'created_at': '2024-06-21T14:58:02.095911+00:00',\n",
|
||||
" 'updated_at': '2024-06-21T14:58:02.095911+00:00',\n",
|
||||
" 'metadata': {},\n",
|
||||
" 'status': 'success',\n",
|
||||
" 'kwargs': {'input': {'messages': [{'role': 'human',\n",
|
||||
" 'content': 'what's the weather in sf'}]},\n",
|
||||
" 'config': {'metadata': {'created_by': 'system'},\n",
|
||||
" 'configurable': {'run_id': '1ef2fdea-814c-6165-8b2a-a40e2a028198',\n",
|
||||
" 'user_id': '',\n",
|
||||
" 'graph_id': 'agent',\n",
|
||||
" 'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d',\n",
|
||||
" 'thread_ts': None,\n",
|
||||
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'}},\n",
|
||||
" 'webhook': None,\n",
|
||||
" 'temporary': False,\n",
|
||||
" 'stream_mode': ['events'],\n",
|
||||
" 'feedback_keys': None,\n",
|
||||
" 'interrupt_after': None,\n",
|
||||
" 'interrupt_before': None},\n",
|
||||
" 'multitask_strategy': 'reject'}"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Eventually, it should finish and we should see `status=success`\n",
|
||||
"await client.runs.get(thread[\"thread_id\"], run[\"run_id\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "8de4495f-7873-487c-b1a8-ad2a78a1ff35",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# We can get the final results\n",
|
||||
"final_result = await client.threads.get_state(thread[\"thread_id\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"id": "9da76fce-66e4-4f1b-8c24-09759889e50e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'values': {'messages': [{'content': 'what's the weather in sf',\n",
|
||||
" 'additional_kwargs': {},\n",
|
||||
" 'response_metadata': {},\n",
|
||||
" 'type': 'human',\n",
|
||||
" 'name': None,\n",
|
||||
" 'id': 'bfe07fff-cb40-40be-84d5-a061d2c40006',\n",
|
||||
" 'example': False},\n",
|
||||
" {'content': [{'id': 'toolu_01QUzhhfDQkpbPSediUrXvQb',\n",
|
||||
" 'input': {'query': 'weather in san francisco'},\n",
|
||||
" 'name': 'tavily_search_results_json',\n",
|
||||
" 'type': 'tool_use'}],\n",
|
||||
" 'additional_kwargs': {},\n",
|
||||
" 'response_metadata': {},\n",
|
||||
" 'type': 'ai',\n",
|
||||
" 'name': None,\n",
|
||||
" 'id': 'run-6d8665ca-a77d-4b44-9a7b-4e975b155fb1',\n",
|
||||
" 'example': False,\n",
|
||||
" 'tool_calls': [{'name': 'tavily_search_results_json',\n",
|
||||
" 'args': {'query': 'weather in san francisco'},\n",
|
||||
" 'id': 'toolu_01QUzhhfDQkpbPSediUrXvQb'}],\n",
|
||||
" 'invalid_tool_calls': [],\n",
|
||||
" 'usage_metadata': None},\n",
|
||||
" {'content': '[{\"url\": \"https://www.timeanddate.com/weather/usa/san-francisco/historic\", \"content\": \"San Francisco Weather History for the Previous 24 Hours Show weather for: Previous 24 hours June 17, 2024 June 16, 2024 June 15, 2024 June 14, 2024 June 13, 2024 June 12, 2024 June 11, 2024 June 10, 2024 June 9, 2024 June 8, 2024 June 7, 2024 June 6, 2024 June 5, 2024 June 4, 2024 June 3, 2024 June 2, 2024\"}]',\n",
|
||||
" 'additional_kwargs': {},\n",
|
||||
" 'response_metadata': {},\n",
|
||||
" 'type': 'tool',\n",
|
||||
" 'name': 'tavily_search_results_json',\n",
|
||||
" 'id': '257a1f29-2f66-4f9e-b35d-c8818dbbaa3f',\n",
|
||||
" 'tool_call_id': 'toolu_01QUzhhfDQkpbPSediUrXvQb'},\n",
|
||||
" {'content': [{'text': 'The search results provide historic weather data for San Francisco, but do not give the current weather conditions. To get the current weather forecast for San Francisco, I would need to refine my search query. Here is an updated search:',\n",
|
||||
" 'type': 'text'},\n",
|
||||
" {'id': 'toolu_01RLJEcWYRvRoBhiHdrhoRZx',\n",
|
||||
" 'input': {'query': 'san francisco weather forecast today'},\n",
|
||||
" 'name': 'tavily_search_results_json',\n",
|
||||
" 'type': 'tool_use'}],\n",
|
||||
" 'additional_kwargs': {},\n",
|
||||
" 'response_metadata': {},\n",
|
||||
" 'type': 'ai',\n",
|
||||
" 'name': None,\n",
|
||||
" 'id': 'run-ca41dbf8-7e89-4ff2-a245-87098d7928ba',\n",
|
||||
" 'example': False,\n",
|
||||
" 'tool_calls': [{'name': 'tavily_search_results_json',\n",
|
||||
" 'args': {'query': 'san francisco weather forecast today'},\n",
|
||||
" 'id': 'toolu_01RLJEcWYRvRoBhiHdrhoRZx'}],\n",
|
||||
" 'invalid_tool_calls': [],\n",
|
||||
" 'usage_metadata': None},\n",
|
||||
" {'content': '[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{\\'location\\': {\\'name\\': \\'San Francisco\\', \\'region\\': \\'California\\', \\'country\\': \\'United States of America\\', \\'lat\\': 37.78, \\'lon\\': -122.42, \\'tz_id\\': \\'America/Los_Angeles\\', \\'localtime_epoch\\': 1718981382, \\'localtime\\': \\'2024-06-21 7:49\\'}, \\'current\\': {\\'last_updated_epoch\\': 1718981100, \\'last_updated\\': \\'2024-06-21 07:45\\', \\'temp_c\\': 12.8, \\'temp_f\\': 55.0, \\'is_day\\': 1, \\'condition\\': {\\'text\\': \\'Overcast\\', \\'icon\\': \\'//cdn.weatherapi.com/weather/64x64/day/122.png\\', \\'code\\': 1009}, \\'wind_mph\\': 6.9, \\'wind_kph\\': 11.2, \\'wind_degree\\': 200, \\'wind_dir\\': \\'SSW\\', \\'pressure_mb\\': 1011.0, \\'pressure_in\\': 29.84, \\'precip_mm\\': 0.01, \\'precip_in\\': 0.0, \\'humidity\\': 86, \\'cloud\\': 100, \\'feelslike_c\\': 12.2, \\'feelslike_f\\': 53.9, \\'windchill_c\\': 11.2, \\'windchill_f\\': 52.1, \\'heatindex_c\\': 12.0, \\'heatindex_f\\': 53.5, \\'dewpoint_c\\': 9.4, \\'dewpoint_f\\': 48.8, \\'vis_km\\': 16.0, \\'vis_miles\\': 9.0, \\'uv\\': 3.0, \\'gust_mph\\': 7.6, \\'gust_kph\\': 12.2}}\"}]',\n",
|
||||
" 'additional_kwargs': {},\n",
|
||||
" 'response_metadata': {},\n",
|
||||
" 'type': 'tool',\n",
|
||||
" 'name': 'tavily_search_results_json',\n",
|
||||
" 'id': 'c80a3720-6a9f-4ff0-9ce2-6112e66a6f81',\n",
|
||||
" 'tool_call_id': 'toolu_01RLJEcWYRvRoBhiHdrhoRZx'},\n",
|
||||
" {'content': 'The updated search provides the current weather forecast for San Francisco. According to the results, as of 7:49am on June 21, 2024 in San Francisco, the temperature is 55°F (12.8°C), it is overcast with 100% cloud cover, and there are light winds from the south-southwest around 7 mph (11 km/h). The forecast also shows low precipitation of 0.01 mm, high humidity of 86%, and visibility of 9 miles (16 km).\\n\\nIn summary, the current weather in San Francisco is cool, overcast, and breezy based on this weather forecast data. Let me know if you need any other details!',\n",
|
||||
" 'additional_kwargs': {},\n",
|
||||
" 'response_metadata': {},\n",
|
||||
" 'type': 'ai',\n",
|
||||
" 'name': None,\n",
|
||||
" 'id': 'run-4f23b53d-a8ec-4038-b3ed-08b2560bf81c',\n",
|
||||
" 'example': False,\n",
|
||||
" 'tool_calls': [],\n",
|
||||
" 'invalid_tool_calls': [],\n",
|
||||
" 'usage_metadata': None}]},\n",
|
||||
" 'next': [],\n",
|
||||
" 'config': {'configurable': {'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d',\n",
|
||||
" 'thread_ts': '1ef2fdea-f879-65a5-8005-443b6a4039aa'}},\n",
|
||||
" 'metadata': {'step': 5,\n",
|
||||
" 'run_id': '1ef2fdea-814c-6165-8b2a-a40e2a028198',\n",
|
||||
" 'source': 'loop',\n",
|
||||
" 'writes': {'agent': {'messages': [{'id': 'run-4f23b53d-a8ec-4038-b3ed-08b2560bf81c',\n",
|
||||
" 'name': None,\n",
|
||||
" 'type': 'ai',\n",
|
||||
" 'content': 'The updated search provides the current weather forecast for San Francisco. According to the results, as of 7:49am on June 21, 2024 in San Francisco, the temperature is 55°F (12.8°C), it is overcast with 100% cloud cover, and there are light winds from the south-southwest around 7 mph (11 km/h). The forecast also shows low precipitation of 0.01 mm, high humidity of 86%, and visibility of 9 miles (16 km).\\n\\nIn summary, the current weather in San Francisco is cool, overcast, and breezy based on this weather forecast data. Let me know if you need any other details!',\n",
|
||||
" 'example': False,\n",
|
||||
" 'tool_calls': [],\n",
|
||||
" 'usage_metadata': None,\n",
|
||||
" 'additional_kwargs': {},\n",
|
||||
" 'response_metadata': {},\n",
|
||||
" 'invalid_tool_calls': []}]}},\n",
|
||||
" 'user_id': '',\n",
|
||||
" 'graph_id': 'agent',\n",
|
||||
" 'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d',\n",
|
||||
" 'created_by': 'system',\n",
|
||||
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},\n",
|
||||
" 'created_at': '2024-06-21T14:58:14.591805+00:00',\n",
|
||||
" 'parent_config': {'configurable': {'thread_id': '5fc20631-47b7-48cd-8aa2-9f2eace9778d',\n",
|
||||
" 'thread_ts': '1ef2fdea-d44c-6fc4-8004-d2713436777d'}}}"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"final_result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"id": "ddd6e698-4609-4389-b84a-bb8939fff08b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'The updated search provides the current weather forecast for San Francisco. According to the results, as of 7:49am on June 21, 2024 in San Francisco, the temperature is 55°F (12.8°C), it is overcast with 100% cloud cover, and there are light winds from the south-southwest around 7 mph (11 km/h). The forecast also shows low precipitation of 0.01 mm, high humidity of 86%, and visibility of 9 miles (16 km).\\n\\nIn summary, the current weather in San Francisco is cool, overcast, and breezy based on this weather forecast data. Let me know if you need any other details!'"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# We can get the content of the final message\n",
|
||||
"final_result[\"values\"][\"messages\"][-1][\"content\"]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "langgraph-example-dev",
|
||||
"language": "python",
|
||||
"name": "langgraph-example-dev"
|
||||
},
|
||||
"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.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -1,198 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "68c0837d-c40a-4209-9f88-5d08c00c31b0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# How to create agents with configuration\n",
|
||||
"\n",
|
||||
"One of the benefits of LangGraph API is that it lets you create agents with different configurations.\n",
|
||||
"This is useful when you want to:\n",
|
||||
"\n",
|
||||
"- Define a cognitive architecture once as a LangGraph\n",
|
||||
"- Let that LangGraph be configurable across some attributes (for example, system message or LLM to use)\n",
|
||||
"- Let users create agents with arbitrary configurations, save them, and then use them in the future\n",
|
||||
"\n",
|
||||
"In this guide we will show how to do that for the default agent we have built in.\n",
|
||||
"\n",
|
||||
"If you look at the agent we defined, you can see that inside the `call_model` node we have created the model based on some configuration. That node looks like:\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
"def call_model(state, config):\n",
|
||||
" messages = state[\"messages\"]\n",
|
||||
" model_name = config.get('configurable', {}).get(\"model_name\", \"anthropic\")\n",
|
||||
" model = _get_model(model_name)\n",
|
||||
" response = model.invoke(messages)\n",
|
||||
" # We return a list, because this will get added to the existing list\n",
|
||||
" return {\"messages\": [response]}\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"We are looking inside the config for a `model_name` parameter (which defaults to `anthropic` if none is found).\n",
|
||||
"That means that by default we are using Anthropic as our model provider.\n",
|
||||
"In this example we will see an example of how to create an example agent that is configured to use OpenAI.\n",
|
||||
"\n",
|
||||
"We've also communicated to the graph that it should expect configuration with this key. \n",
|
||||
"We've done this by passing `config_schema` when constructing the graph, eg:\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
"class GraphConfig(TypedDict):\n",
|
||||
" model_name: Literal[\"anthropic\", \"openai\"]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Define a new graph\n",
|
||||
"workflow = StateGraph(AgentState, config_schema=GraphConfig)\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "f69c9a4f-2ef9-4998-827b-fe86d12bfd76",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langgraph_sdk import get_client\n",
|
||||
"\n",
|
||||
"client = get_client()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "9a37bfb5-7331-4004-8054-508838e54f18",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# First, let's check what valid configuration can be\n",
|
||||
"# We can do this by getting the default assistant\n",
|
||||
"# 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",
|
||||
"base_assistant = assistants[0]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"id": "70193a08-127c-44b3-a102-10db260d7e3b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'model_name': {'title': 'Model Name',\n",
|
||||
" 'enum': ['anthropic', 'openai'],\n",
|
||||
" 'type': 'string'}}"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# We can now call `.get_schemas` to get schemas associated with this graph\n",
|
||||
"schemas = await client.assistants.get_schemas(\n",
|
||||
" assistant_id=base_assistant[\"assistant_id\"]\n",
|
||||
")\n",
|
||||
"# There are multiple types of schemas\n",
|
||||
"# We can get the `config_schema` to look at the the configurable parameters\n",
|
||||
"schemas[\"config_schema\"][\"definitions\"][\"Configurable\"][\"properties\"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"id": "99be5aee-9a6b-4515-b72f-ba135a893c65",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"assistant = await client.assistants.create(\n",
|
||||
" graph_id=\"agent\", config={\"configurable\": {\"model_name\": \"openai\"}}\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4f10d346-69e6-44f4-8ff0-ef539ba938df",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We can see that this assistant has saved the config"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"id": "3898ca35-eb2c-4b12-97ea-e0cc6a7c6a2e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'assistant_id': '40a3a2bf-5319-4fae-a2ac-05e075615cdc',\n",
|
||||
" 'graph_id': 'agent',\n",
|
||||
" 'config': {'configurable': {'model_name': 'openai'}},\n",
|
||||
" 'created_at': '2024-06-05T23:12:30.519458+00:00',\n",
|
||||
" 'updated_at': '2024-06-05T23:12:30.519458+00:00',\n",
|
||||
" 'metadata': {}}"
|
||||
]
|
||||
},
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"assistant"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"id": "68ed7a1b-74be-4560-8c55-c76d49d3d348",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"StreamPart(event='metadata', data={'run_id': '1ef23911-c23b-6d8c-b1dc-94bb982ca7b1'})\n",
|
||||
"StreamPart(event='values', data={'messages': [{'role': 'user', 'content': 'who made you?'}]})\n",
|
||||
"StreamPart(event='values', data={'messages': [{'content': 'who made you?', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': 'ed93c1c9-80d6-4f2b-a048-ef859ea533f9', 'example': False}, {'content': 'I was created by OpenAI, a research organization focused on developing and advancing artificial intelligence technology.', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-6560cd65-5c9c-434b-8835-0baadc684760', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}]})\n",
|
||||
"StreamPart(event='end', data=None)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"thread = await client.threads.create()\n",
|
||||
"input = {\"messages\": [{\"role\": \"user\", \"content\": \"who made you?\"}]}\n",
|
||||
"async for event in client.runs.stream(\n",
|
||||
" thread[\"thread_id\"], assistant[\"assistant_id\"], input=input\n",
|
||||
"):\n",
|
||||
" print(event)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Cron Jobs\n",
|
||||
"\n",
|
||||
"Sometimes you don't want to run your graph based on user interaction, but rather you would like to schedule your graph to run on a schedule - for example if you wish for your graph to compose and send out a weekly email of to-dos for your team. LangGraph Cloud allows you to do this without having to write your own script by using the `Crons` client. To schedule a graph job, you need to pass a [cron expression](https://crontab.cronhub.io/) to inform the client when you want to run the graph. `Cron` jobs are run in the background and do not interfere with normal invocations of the graph.\n",
|
||||
"\n",
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
"First, let's setup our SDK client, assistant, and thread:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 110,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langgraph_sdk import get_client\n",
|
||||
"\n",
|
||||
"client = get_client()\n",
|
||||
"assistants = await client.assistants.search()\n",
|
||||
"assistants = [a for a in assistants if not a[\"config\"]]\n",
|
||||
"assistant = assistants[0]\n",
|
||||
"thread = await client.threads.create()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Cron job on a thread \n",
|
||||
"\n",
|
||||
"To create a cron job associated with a specific thread, you can write:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# This schedules a job to run at 15:27 (3:27PM) every day\n",
|
||||
"cron_1 = await client.crons.create_for_thread(\n",
|
||||
" thread[\"thread_id\"],\n",
|
||||
" assistant[\"assistant_id\"],\n",
|
||||
" schedule=\"27 15 * * *\",\n",
|
||||
" input={\"messages\": [{\"role\": \"user\", \"content\": \"What time is it?\"}]},\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Note that it is **very** important to delete `Cron` jobs that are no longer useful. Otherwise you could rack up unwanted API charges to the LLM! You can delete a `Cron` job using the following code:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"await client.crons.delete(cron_1[\"cron_id\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Cron job stateless\n",
|
||||
"\n",
|
||||
"You can also create stateless cron jobs by using the following code:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# This schedules a job to run at 15:27 (3:27PM) every day\n",
|
||||
"cron_2 = await client.crons.create(\n",
|
||||
" assistant[\"assistant_id\"],\n",
|
||||
" schedule=\"27 15 * * *\",\n",
|
||||
" input={\"messages\": [{\"role\": \"user\", \"content\": \"What time is it?\"}]},\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Again, remember to delete your job once you are done with it!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"await client.crons.delete(cron_2[\"cron_id\"])"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 432 KiB |
@@ -1,192 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "68c0837d-c40a-4209-9f88-5d08c00c31b0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# How to run multiple agents on the same thread\n",
|
||||
"\n",
|
||||
"In LangGraph Cloud, a thread is not explicitly associated with a particular agent.\n",
|
||||
"This means that you can run multiple agents on the same thread, which allows a different\n",
|
||||
"agent to continue from an initial agent's progress.\n",
|
||||
"\n",
|
||||
"In this example, we will create two agents and then call them both on the same thread.\n",
|
||||
"You'll see that the second agent will respond using information from the [checkpoint](https://langchain-ai.github.io/langgraph/concepts/low_level/#checkpointer-state) generated in the thread\n",
|
||||
"by the first agent as context."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "e06be1f6-07a5-4e93-8497-02473fc65d4f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langgraph_sdk import get_client\n",
|
||||
"\n",
|
||||
"client = get_client()\n",
|
||||
"\n",
|
||||
"openai_assistant = await client.assistants.create(\n",
|
||||
" graph_id=\"agent\", config={\"configurable\": {\"model_name\": \"openai\"}}\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# There should always be a default assistant with no configuration\n",
|
||||
"assistants = await client.assistants.search()\n",
|
||||
"default_assistant = [a for a in assistants if not a[\"config\"]][0]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4f10d346-69e6-44f4-8ff0-ef539ba938df",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We can see that these agents are different:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "3898ca35-eb2c-4b12-97ea-e0cc6a7c6a2e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'assistant_id': '13ecc353-a9a9-474b-a824-b6a343cd74b1',\n",
|
||||
" 'graph_id': 'agent',\n",
|
||||
" 'config': {'configurable': {'model_name': 'openai'}},\n",
|
||||
" 'created_at': '2024-05-21T16:22:59.258447+00:00',\n",
|
||||
" 'updated_at': '2024-05-21T16:22:59.258447+00:00',\n",
|
||||
" 'metadata': {}}"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"openai_assistant"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "a8fa67b2-cb4f-43d3-a1fc-f8b3936c16b6",
|
||||
"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": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"default_assistant"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5e655e61-c2ee-488a-90f6-6189c84841da",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We can now run the OpenAI assistant on the thread first."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "68ed7a1b-74be-4560-8c55-c76d49d3d348",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"StreamPart(event='metadata', data={'run_id': 'f90b3029-8669-4d70-976c-b70368e355d8'})\n",
|
||||
"StreamPart(event='updates', data={'agent': {'messages': [{'content': 'I was created by OpenAI, a research organization focused on developing and advancing artificial intelligence technology.', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop'}, 'type': 'ai', 'name': None, 'id': 'run-9801a5ba-2f3c-43de-89cf-c740debf36fc', 'example': False, 'tool_calls': [], 'invalid_tool_calls': []}]}})\n",
|
||||
"StreamPart(event='end', data=None)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"thread = await client.threads.create()\n",
|
||||
"input = {\"messages\": [{\"role\": \"user\", \"content\": \"who made you?\"}]}\n",
|
||||
"async for event in client.runs.stream(\n",
|
||||
" thread[\"thread_id\"],\n",
|
||||
" openai_assistant[\"assistant_id\"],\n",
|
||||
" input=input,\n",
|
||||
" stream_mode=\"updates\",\n",
|
||||
"):\n",
|
||||
" print(event)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c53709e9-ddb2-4429-9042-456eb6c91244",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now, we can run it on a second Anthropic-based assistant and see that this second assistant is aware of the initial question, and can answer the question, `and you?`:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "666d78f1-019a-433e-839e-52d2ebb3d9c8",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"StreamPart(event='metadata', data={'run_id': 'c3521302-48ae-4c29-a0f2-5eb865cbc6d7'})\n",
|
||||
"StreamPart(event='updates', data={'agent': {'messages': [{'content': \"I am an AI assistant created by Anthropic to be helpful, harmless, and honest. I don't actually have a physical form or visual representation - I exist as a language model trained to have natural conversations.\", 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'ai', 'name': None, 'id': 'run-4d05ffd7-0505-43e1-a068-0207c56b7665', 'example': False, 'tool_calls': [], 'invalid_tool_calls': []}]}})\n",
|
||||
"StreamPart(event='end', data=None)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"input = {\"messages\": [{\"role\": \"user\", \"content\": \"and you?\"}]}\n",
|
||||
"async for event in client.runs.stream(\n",
|
||||
" thread[\"thread_id\"],\n",
|
||||
" default_assistant[\"assistant_id\"],\n",
|
||||
" input=input,\n",
|
||||
" stream_mode=\"updates\",\n",
|
||||
"):\n",
|
||||
" print(event)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
@@ -1,152 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Stateless Runs\n",
|
||||
"\n",
|
||||
"Most of the time, you provide a `thread_id` to your client when you run your graph in order to keep track of prior runs through the persistent state implemented in LangGraph Cloud. However, if you have your own database to save runs and don't need to use the built in persistent state, you can create stateless runs.\n",
|
||||
"\n",
|
||||
"## Setup\n",
|
||||
"\n",
|
||||
"First, let's setup our client"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 106,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langgraph_sdk import get_client\n",
|
||||
"\n",
|
||||
"client = get_client()\n",
|
||||
"assistants = await client.assistants.search()\n",
|
||||
"assistants = [a for a in assistants if not a[\"config\"]]\n",
|
||||
"assistant = assistants[0]\n",
|
||||
"thread = await client.threads.create()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Stateless streaming\n",
|
||||
"\n",
|
||||
"We can stream the results of a stateless run in an almost identical fashion to how we stream from a run with the state attribute, but instead of passing a value to the `thread_id` parameter, we pass `None`:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 107,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{'agent': {'messages': [{'content': \"Hello Bagatur! It's nice to meet you. Thank you for introducing yourself and sharing your age. Is there anything specific you'd like to know or discuss? I'm here to help with any questions or topics you're interested in.\", 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'ai', 'name': None, 'id': 'run-489ec573-1645-4ce2-a3b8-91b391d50a71', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}]}}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"input = {\n",
|
||||
" \"messages\": [\n",
|
||||
" {\"role\": \"user\", \"content\": \"Hello! My name is Bagatur and I am 26 years old.\"}\n",
|
||||
" ]\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"async for chunk in client.runs.stream(\n",
|
||||
" # Don't pass in a thread_id and the stream will be stateless\n",
|
||||
" None,\n",
|
||||
" assistant[\"assistant_id\"], # graph_id\n",
|
||||
" input=input,\n",
|
||||
" stream_mode=\"updates\",\n",
|
||||
"):\n",
|
||||
" if chunk.data and \"run_id\" not in chunk.data:\n",
|
||||
" print(chunk.data)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Waiting for stateless results\n",
|
||||
"\n",
|
||||
"In addition to streaming, you can also wait for a stateless result by using the `.wait` function like follows:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 108,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"stateless_run_result = await client.runs.wait(\n",
|
||||
" None,\n",
|
||||
" assistant[\"assistant_id\"], # graph_id\n",
|
||||
" input=input,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 109,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'messages': [{'content': 'Hello! My name is Bagatur and I am 26 years old.',\n",
|
||||
" 'additional_kwargs': {},\n",
|
||||
" 'response_metadata': {},\n",
|
||||
" 'type': 'human',\n",
|
||||
" 'name': None,\n",
|
||||
" 'id': '5e088543-62c2-43de-9d95-6086ad7f8b48',\n",
|
||||
" 'example': False},\n",
|
||||
" {'content': \"Hello Bagatur! It's nice to meet you. Thank you for introducing yourself and sharing your age. Is there anything specific you'd like to know or discuss? I'm here to help with any questions or topics you'd like to explore.\",\n",
|
||||
" 'additional_kwargs': {},\n",
|
||||
" 'response_metadata': {},\n",
|
||||
" 'type': 'ai',\n",
|
||||
" 'name': None,\n",
|
||||
" 'id': 'run-d6361e8d-4d4c-45bd-ba47-39520257f773',\n",
|
||||
" 'example': False,\n",
|
||||
" 'tool_calls': [],\n",
|
||||
" 'invalid_tool_calls': [],\n",
|
||||
" 'usage_metadata': None}]}"
|
||||
]
|
||||
},
|
||||
"execution_count": 109,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"stateless_run_result"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Use Webhooks\n",
|
||||
"\n",
|
||||
"You may wish to use webhooks in your client, especially when using async streams in case you want to update something in your service once the API call to LangGraph Cloud has finished running. To do so, you will need to expose an endpoint that can accept POST requests, and then pass it to your API request in the \"webhook\" parameter.\n",
|
||||
"\n",
|
||||
"Currently, the SDK has not exposed this endpoint but you can access it through curl commands as follows.\n",
|
||||
"\n",
|
||||
"The following endpoints accept `webhook` as a parameter: \n",
|
||||
"\n",
|
||||
"- Create Run -> POST /thread/{thread_id}/runs\n",
|
||||
"- Create Thread Cron -> POST /thread/{thread_id}/runs/crons\n",
|
||||
"- Stream Run -> POST /thread/{thread_id}/runs/stream\n",
|
||||
"- Wait Run -> POST /thread/{thread_id}/runs/wait\n",
|
||||
"- Create Cron -> POST /runs/crons\n",
|
||||
"- Stream Run Stateless -> POST /runs/stream\n",
|
||||
"- Wait Run Stateless -> POST /runs/wait\n",
|
||||
"\n",
|
||||
"The following example uses a url from a public website that allows users to create free webhooks, but you should pass in the webhook that you wish to use. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"curl --request POST \\\n",
|
||||
" --url http://localhost:8123/threads/b76d1e94-f251-40e3-8933-796d775cdb4c/runs/stream \\\n",
|
||||
" --header 'Content-Type: application/json' \\\n",
|
||||
" --data '{\n",
|
||||
" \"assistant_id\": \"fe096781-5601-53d2-b2f6-0d3403f7e9ca\",\n",
|
||||
" \"input\" : {\"messages\":[{\"role\": \"user\", \"content\": \"Hello! My name is Bagatur and I am 26 years old.\"}]},\n",
|
||||
" \"metadata\": {},\n",
|
||||
" \"config\": {\n",
|
||||
" \"configurable\": {}\n",
|
||||
" },\n",
|
||||
" \"multitask_strategy\": \"reject\",\n",
|
||||
" \"stream_mode\": [\n",
|
||||
" \"values\"\n",
|
||||
" ],\n",
|
||||
" \"webhook\": \"https://webhook.site/6ca33471-dd65-4103-a851-0a252dae0f2a\"\n",
|
||||
"}'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"To check that this worked as intended, we can go to the website where our webhook was created and confirm that it received a POST request:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
""
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user