mirror of
https://github.com/langchain-ai/langgraph-example.git
synced 2026-07-22 17:45:24 -04:00
626 lines
25 KiB
Plaintext
626 lines
25 KiB
Plaintext
{
|
|
"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": null,
|
|
"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": 7,
|
|
"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": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"# List available assistants\n",
|
|
"assistants = await client.assistants.search()\n",
|
|
"assistants"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"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": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"# Get the first assistant, we will use this one\n",
|
|
"assistant = assistants[0]\n",
|
|
"assistant"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "56aa5159-5583-4134-9210-709b969bda6f",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'created_at': '2024-05-18T00:50:26.367620+00:00',\n",
|
|
" 'updated_at': '2024-05-18T00:50:26.367620+00:00',\n",
|
|
" 'metadata': {}}"
|
|
]
|
|
},
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"# Create a new thread\n",
|
|
"thread = await client.threads.create()\n",
|
|
"thread"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"id": "147c3f98-f889-4f05-a090-6b31f2a0b291",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[]"
|
|
]
|
|
},
|
|
"execution_count": 11,
|
|
"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": null,
|
|
"id": "8c7b44ef-4816-496d-88a1-2f7327cf576d",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Let's kick off a run\n",
|
|
"input = {\"messages\": [{\"role\": \"human\", \"content\": \"whats the weather in sf\"}]}\n",
|
|
"run = await client.runs.create(thread['thread_id'], assistant[\"assistant_id\"], input=input)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"id": "d84b4d80-b0aa-4d9f-a05d-0744b2fe8f72",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n",
|
|
" 'created_at': '2024-05-18T00:50:27.618761+00:00',\n",
|
|
" 'updated_at': '2024-05-18T00:50:27.618761+00:00',\n",
|
|
" 'status': 'pending',\n",
|
|
" 'metadata': {}}"
|
|
]
|
|
},
|
|
"execution_count": 13,
|
|
"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": 14,
|
|
"id": "ce124bd3-f197-4b73-9ff6-bb36730dd003",
|
|
"metadata": {
|
|
"scrolled": true
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[{'event_id': '3ac6d963-442f-481c-9fde-b8a27bc0e277',\n",
|
|
" 'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'received_at': '2024-05-18T00:50:29.570649+00:00',\n",
|
|
" 'span_id': 'd4e4a6ee-da2f-4b5f-b656-1a1f73065161',\n",
|
|
" 'event': 'on_tool_start',\n",
|
|
" 'name': 'tavily_search_results_json',\n",
|
|
" 'data': {'input': {'query': 'weather in san francisco'}},\n",
|
|
" 'metadata': {'graph_id': 'agent',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'created_by': 'system',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},\n",
|
|
" 'tags': ['seq:step:1']},\n",
|
|
" {'event_id': 'f8961760-6f13-40e6-9eef-6d4d68e0ed19',\n",
|
|
" 'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'received_at': '2024-05-18T00:50:29.569708+00:00',\n",
|
|
" 'span_id': '73295bb1-6cd3-403d-abc1-4f9d96a63894',\n",
|
|
" 'event': 'on_chain_start',\n",
|
|
" 'name': 'action',\n",
|
|
" 'data': {},\n",
|
|
" 'metadata': {'graph_id': 'agent',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'created_by': 'system',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},\n",
|
|
" 'tags': ['graph:step:2']},\n",
|
|
" {'event_id': '44b5f815-f60c-468f-8d23-96dfdaa0ed20',\n",
|
|
" 'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'received_at': '2024-05-18T00:50:29.568202+00:00',\n",
|
|
" 'span_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'event': 'on_chain_stream',\n",
|
|
" 'name': 'LangGraph',\n",
|
|
" 'data': {'chunk': {'messages': [{'id': '46b31c3a-01bf-4946-bd5a-fa6a7f6c97ce',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'human',\n",
|
|
" 'content': 'whats the weather in sf',\n",
|
|
" 'example': False,\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {}},\n",
|
|
" {'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'ai',\n",
|
|
" 'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'name': 'tavily_search_results_json',\n",
|
|
" 'type': 'tool_use',\n",
|
|
" 'input': {'query': 'weather in san francisco'}}],\n",
|
|
" 'example': False,\n",
|
|
" 'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'args': {'query': 'weather in san francisco'},\n",
|
|
" 'name': 'tavily_search_results_json'}],\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {},\n",
|
|
" 'invalid_tool_calls': []}]}},\n",
|
|
" 'metadata': {'graph_id': 'agent',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'created_by': 'system',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},\n",
|
|
" 'tags': []},\n",
|
|
" {'event_id': '0917b31d-f49d-43d4-a8ed-a9eebd8904e8',\n",
|
|
" 'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'received_at': '2024-05-18T00:50:29.566761+00:00',\n",
|
|
" 'span_id': 'f469ac2e-17a3-491c-bc03-0c56aa30a68b',\n",
|
|
" 'event': 'on_chain_end',\n",
|
|
" 'name': 'agent',\n",
|
|
" 'data': {'input': {'messages': [{'role': 'human',\n",
|
|
" 'content': 'whats the weather in sf'}]},\n",
|
|
" 'output': {'messages': [{'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'ai',\n",
|
|
" 'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'name': 'tavily_search_results_json',\n",
|
|
" 'type': 'tool_use',\n",
|
|
" 'input': {'query': 'weather in san francisco'}}],\n",
|
|
" 'example': False,\n",
|
|
" 'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'args': {'query': 'weather in san francisco'},\n",
|
|
" 'name': 'tavily_search_results_json'}],\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {},\n",
|
|
" 'invalid_tool_calls': []}]}},\n",
|
|
" 'metadata': {'graph_id': 'agent',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'created_by': 'system',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},\n",
|
|
" 'tags': ['graph:step:1']},\n",
|
|
" {'event_id': 'bbe33ebf-04ba-43d5-8718-e2da23295675',\n",
|
|
" 'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'received_at': '2024-05-18T00:50:29.566076+00:00',\n",
|
|
" 'span_id': 'f469ac2e-17a3-491c-bc03-0c56aa30a68b',\n",
|
|
" 'event': 'on_chain_stream',\n",
|
|
" 'name': 'agent',\n",
|
|
" 'data': {'chunk': {'messages': [{'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'ai',\n",
|
|
" 'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'name': 'tavily_search_results_json',\n",
|
|
" 'type': 'tool_use',\n",
|
|
" 'input': {'query': 'weather in san francisco'}}],\n",
|
|
" 'example': False,\n",
|
|
" 'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'args': {'query': 'weather in san francisco'},\n",
|
|
" 'name': 'tavily_search_results_json'}],\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {},\n",
|
|
" 'invalid_tool_calls': []}]}},\n",
|
|
" 'metadata': {'graph_id': 'agent',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'created_by': 'system',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},\n",
|
|
" 'tags': ['graph:step:1']},\n",
|
|
" {'event_id': '38333127-fa97-4830-8157-f76264778d81',\n",
|
|
" 'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'received_at': '2024-05-18T00:50:29.564195+00:00',\n",
|
|
" 'span_id': 'ef55c681-be15-4f3d-9aee-3a9ff05d8746',\n",
|
|
" 'event': 'on_chain_end',\n",
|
|
" 'name': 'should_continue',\n",
|
|
" 'data': {'input': {'messages': [{'id': 'abc3581e-417b-4ca1-ab31-de7108e64b3b',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'human',\n",
|
|
" 'content': 'whats the weather in sf',\n",
|
|
" 'example': False,\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {}},\n",
|
|
" {'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'ai',\n",
|
|
" 'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'name': 'tavily_search_results_json',\n",
|
|
" 'type': 'tool_use',\n",
|
|
" 'input': {'query': 'weather in san francisco'}}],\n",
|
|
" 'example': False,\n",
|
|
" 'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'args': {'query': 'weather in san francisco'},\n",
|
|
" 'name': 'tavily_search_results_json'}],\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {},\n",
|
|
" 'invalid_tool_calls': []}]},\n",
|
|
" 'output': 'continue'},\n",
|
|
" 'metadata': {'graph_id': 'agent',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'created_by': 'system',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},\n",
|
|
" 'tags': ['seq:step:3']},\n",
|
|
" {'event_id': '408a7785-c715-4bcb-a5aa-950f414baa77',\n",
|
|
" 'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'received_at': '2024-05-18T00:50:29.563289+00:00',\n",
|
|
" 'span_id': 'ef55c681-be15-4f3d-9aee-3a9ff05d8746',\n",
|
|
" 'event': 'on_chain_start',\n",
|
|
" 'name': 'should_continue',\n",
|
|
" 'data': {'input': {'messages': [{'id': 'abc3581e-417b-4ca1-ab31-de7108e64b3b',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'human',\n",
|
|
" 'content': 'whats the weather in sf',\n",
|
|
" 'example': False,\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {}},\n",
|
|
" {'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'ai',\n",
|
|
" 'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'name': 'tavily_search_results_json',\n",
|
|
" 'type': 'tool_use',\n",
|
|
" 'input': {'query': 'weather in san francisco'}}],\n",
|
|
" 'example': False,\n",
|
|
" 'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'args': {'query': 'weather in san francisco'},\n",
|
|
" 'name': 'tavily_search_results_json'}],\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {},\n",
|
|
" 'invalid_tool_calls': []}]}},\n",
|
|
" 'metadata': {'graph_id': 'agent',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'created_by': 'system',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},\n",
|
|
" 'tags': ['seq:step:3']},\n",
|
|
" {'event_id': '679c8ae7-5cd2-4462-936e-20f0ea45cfb8',\n",
|
|
" 'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'received_at': '2024-05-18T00:50:29.560628+00:00',\n",
|
|
" 'span_id': '4885d5a0-cd89-4f00-8558-e85b542a710c',\n",
|
|
" 'event': 'on_chat_model_end',\n",
|
|
" 'name': 'ChatAnthropic',\n",
|
|
" 'data': {'input': {'messages': [[{'id': None,\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'human',\n",
|
|
" 'content': 'whats the weather in sf',\n",
|
|
" 'example': False,\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {}}]]},\n",
|
|
" 'output': {'run': None,\n",
|
|
" 'llm_output': None,\n",
|
|
" 'generations': [[{'text': '',\n",
|
|
" 'type': 'ChatGeneration',\n",
|
|
" 'message': {'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'ai',\n",
|
|
" 'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'name': 'tavily_search_results_json',\n",
|
|
" 'type': 'tool_use',\n",
|
|
" 'input': {'query': 'weather in san francisco'}}],\n",
|
|
" 'example': False,\n",
|
|
" 'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'args': {'query': 'weather in san francisco'},\n",
|
|
" 'name': 'tavily_search_results_json'}],\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {},\n",
|
|
" 'invalid_tool_calls': []},\n",
|
|
" 'generation_info': None}]]}},\n",
|
|
" 'metadata': {'graph_id': 'agent',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'created_by': 'system',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n",
|
|
" 'ls_model_type': 'chat'},\n",
|
|
" 'tags': ['seq:step:1']},\n",
|
|
" {'event_id': '055fcf73-36e7-444b-990d-6263ec50925c',\n",
|
|
" 'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'received_at': '2024-05-18T00:50:29.559491+00:00',\n",
|
|
" 'span_id': '4885d5a0-cd89-4f00-8558-e85b542a710c',\n",
|
|
" 'event': 'on_chat_model_stream',\n",
|
|
" 'name': 'ChatAnthropic',\n",
|
|
" 'data': {'chunk': {'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'AIMessageChunk',\n",
|
|
" 'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'name': 'tavily_search_results_json',\n",
|
|
" 'type': 'tool_use',\n",
|
|
" 'input': {'query': 'weather in san francisco'}}],\n",
|
|
" 'example': False,\n",
|
|
" 'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'args': {'query': 'weather in san francisco'},\n",
|
|
" 'name': 'tavily_search_results_json'}],\n",
|
|
" 'tool_call_chunks': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'args': '{\"query\": \"weather in san francisco\"}',\n",
|
|
" 'name': 'tavily_search_results_json',\n",
|
|
" 'index': 0}],\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {},\n",
|
|
" 'invalid_tool_calls': []}},\n",
|
|
" 'metadata': {'graph_id': 'agent',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'created_by': 'system',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n",
|
|
" 'ls_model_type': 'chat'},\n",
|
|
" 'tags': ['seq:step:1']},\n",
|
|
" {'event_id': 'cf90f755-5a12-46bd-8f60-adc862388635',\n",
|
|
" 'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'received_at': '2024-05-18T00:50:27.873602+00:00',\n",
|
|
" 'span_id': '4885d5a0-cd89-4f00-8558-e85b542a710c',\n",
|
|
" 'event': 'on_chat_model_start',\n",
|
|
" 'name': 'ChatAnthropic',\n",
|
|
" 'data': {'input': {'messages': [[{'id': None,\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'human',\n",
|
|
" 'content': 'whats the weather in sf',\n",
|
|
" 'example': False,\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {}}]]}},\n",
|
|
" 'metadata': {'graph_id': 'agent',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'created_by': 'system',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n",
|
|
" 'ls_model_type': 'chat'},\n",
|
|
" 'tags': ['seq:step:1']}]"
|
|
]
|
|
},
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"# We can list events for the run\n",
|
|
"await client.runs.list_events(thread['thread_id'], run['run_id'])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"id": "8fa206ed-515e-4607-9a80-bebafe76cc24",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n",
|
|
" 'created_at': '2024-05-18T00:50:27.618761+00:00',\n",
|
|
" 'updated_at': '2024-05-18T00:50:27.618761+00:00',\n",
|
|
" 'status': 'success',\n",
|
|
" 'metadata': {}}"
|
|
]
|
|
},
|
|
"execution_count": 15,
|
|
"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": 16,
|
|
"id": "8de4495f-7873-487c-b1a8-ad2a78a1ff35",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# We can get the final results\n",
|
|
"results = await client.runs.list_events(thread['thread_id'], run['run_id'])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 21,
|
|
"id": "9da76fce-66e4-4f1b-8c24-09759889e50e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# The results are sorted by time, so the most recent (final) step is the 0 index\n",
|
|
"final_result = results[0]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 22,
|
|
"id": "02279ff3-c153-4ec4-be4d-1613a0dff4ee",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'event_id': '1af2076e-8ec7-4f2e-bc2c-6fbbf586397c',\n",
|
|
" 'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'received_at': '2024-05-18T00:50:35.557925+00:00',\n",
|
|
" 'span_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',\n",
|
|
" 'event': 'on_chain_end',\n",
|
|
" 'name': 'LangGraph',\n",
|
|
" 'data': {'output': {'messages': [{'id': '46b31c3a-01bf-4946-bd5a-fa6a7f6c97ce',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'human',\n",
|
|
" 'content': 'whats the weather in sf',\n",
|
|
" 'example': False,\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {}},\n",
|
|
" {'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'ai',\n",
|
|
" 'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'name': 'tavily_search_results_json',\n",
|
|
" 'type': 'tool_use',\n",
|
|
" 'input': {'query': 'weather in san francisco'}}],\n",
|
|
" 'example': False,\n",
|
|
" 'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'args': {'query': 'weather in san francisco'},\n",
|
|
" 'name': 'tavily_search_results_json'}],\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {},\n",
|
|
" 'invalid_tool_calls': []},\n",
|
|
" {'id': '045e936d-ee47-4236-95ff-793b6b32b590',\n",
|
|
" 'name': 'tavily_search_results_json',\n",
|
|
" 'type': 'tool',\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\\': 1715993410, \\'localtime\\': \\'2024-05-17 17:50\\'}, \\'current\\': {\\'last_updated_epoch\\': 1715993100, \\'last_updated\\': \\'2024-05-17 17:45\\', \\'temp_c\\': 17.8, \\'temp_f\\': 64.0, \\'is_day\\': 1, \\'condition\\': {\\'text\\': \\'Partly cloudy\\', \\'icon\\': \\'//cdn.weatherapi.com/weather/64x64/day/116.png\\', \\'code\\': 1003}, \\'wind_mph\\': 15.0, \\'wind_kph\\': 24.1, \\'wind_degree\\': 300, \\'wind_dir\\': \\'WNW\\', \\'pressure_mb\\': 1013.0, \\'pressure_in\\': 29.9, \\'precip_mm\\': 0.0, \\'precip_in\\': 0.0, \\'humidity\\': 65, \\'cloud\\': 25, \\'feelslike_c\\': 17.8, \\'feelslike_f\\': 64.0, \\'vis_km\\': 16.0, \\'vis_miles\\': 9.0, \\'uv\\': 5.0, \\'gust_mph\\': 16.2, \\'gust_kph\\': 26.1}}\"}]',\n",
|
|
" 'tool_call_id': 'toolu_018yyEfJHihdVfWypRNLqDug',\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {}},\n",
|
|
" {'id': 'run-069f1ddd-64b3-451d-bb84-75dec23a7286',\n",
|
|
" 'name': None,\n",
|
|
" 'type': 'ai',\n",
|
|
" 'content': \"The search results provide the current weather conditions in San Francisco. According to the data, as of 5:45pm on May 17, 2024, the weather in San Francisco is partly cloudy with a temperature of around 64°F (17.8°C). The wind is blowing from the west-northwest at 15 mph (24 km/h) with gusts up to 16 mph (26 km/h). The humidity is 65% and visibility is 9 miles (16 km). The UV index is 5.\\n\\nSo in summary, it's a partly cloudy spring day in San Francisco with mild temperatures and moderate winds. The weather seems pleasant for being outdoors during the daytime hours.\",\n",
|
|
" 'example': False,\n",
|
|
" 'tool_calls': [],\n",
|
|
" 'additional_kwargs': {},\n",
|
|
" 'response_metadata': {},\n",
|
|
" 'invalid_tool_calls': []}]}},\n",
|
|
" 'metadata': {'graph_id': 'agent',\n",
|
|
" 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',\n",
|
|
" 'created_by': 'system',\n",
|
|
" 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},\n",
|
|
" 'tags': []}"
|
|
]
|
|
},
|
|
"execution_count": 22,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"final_result"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 28,
|
|
"id": "ddd6e698-4609-4389-b84a-bb8939fff08b",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\"The search results provide the current weather conditions in San Francisco. According to the data, as of 5:45pm on May 17, 2024, the weather in San Francisco is partly cloudy with a temperature of around 64°F (17.8°C). The wind is blowing from the west-northwest at 15 mph (24 km/h) with gusts up to 16 mph (26 km/h). The humidity is 65% and visibility is 9 miles (16 km). The UV index is 5.\\n\\nSo in summary, it's a partly cloudy spring day in San Francisco with mild temperatures and moderate winds. The weather seems pleasant for being outdoors during the daytime hours.\""
|
|
]
|
|
},
|
|
"execution_count": 28,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"# We can get the content of the final message\n",
|
|
"final_result['data']['output']['messages'][-1]['content']"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "535638f7-48a9-49bb-9a0b-57a5b36d0696",
|
|
"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
|
|
}
|