Files
langgraph/examples/cloud_examples/background_run.ipynb
T
Vadym BardaandGitHub f741fbf92e docs: run codespell on all notebooks (#736)
* run codespell on all notebooks

* fix codespell
2024-06-20 21:39:13 -04:00

25 KiB

How to kick off background runs

This guide covers how to kick off background runs for your agent. This can be useful for long running jobs.

In [ ]:
# Initialize the client
from langgraph_sdk import get_client

client = get_client()
In [7]:
# List available assistants
assistants = await client.assistants.search()
assistants
Out [7]:
[{'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',
  'graph_id': 'agent',
  'config': {},
  'created_at': '2024-05-18T00:19:39.688822+00:00',
  'updated_at': '2024-05-18T00:19:39.688822+00:00',
  'metadata': {'created_by': 'system'}}]
In [9]:
# Get the first assistant, we will use this one
assistant = assistants[0]
assistant
Out [9]:
{'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',
 'graph_id': 'agent',
 'config': {},
 'created_at': '2024-05-18T00:19:39.688822+00:00',
 'updated_at': '2024-05-18T00:19:39.688822+00:00',
 'metadata': {'created_by': 'system'}}
In [10]:
# Create a new thread
thread = await client.threads.create()
thread
Out [10]:
{'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
 'created_at': '2024-05-18T00:50:26.367620+00:00',
 'updated_at': '2024-05-18T00:50:26.367620+00:00',
 'metadata': {}}
In [11]:
# If we list runs on this thread, we can see it is empty
runs = await client.runs.list(thread['thread_id'])
runs
Out [11]:
[]
In [ ]:
# Let's kick off a run
input = {"messages": [{"role": "human", "content": "what's the weather in sf"}]}
run = await client.runs.create(thread['thread_id'], assistant["assistant_id"], input=input)
In [13]:
# The first time we poll it, we can see `status=pending`
await client.runs.get(thread['thread_id'], run['run_id'])
Out [13]:
{'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',
 'created_at': '2024-05-18T00:50:27.618761+00:00',
 'updated_at': '2024-05-18T00:50:27.618761+00:00',
 'status': 'pending',
 'metadata': {}}
In [14]:
# We can list events for the run
await client.runs.list_events(thread['thread_id'], run['run_id'])
Out [14]:
[{'event_id': '3ac6d963-442f-481c-9fde-b8a27bc0e277',
  'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
  'received_at': '2024-05-18T00:50:29.570649+00:00',
  'span_id': 'd4e4a6ee-da2f-4b5f-b656-1a1f73065161',
  'event': 'on_tool_start',
  'name': 'tavily_search_results_json',
  'data': {'input': {'query': 'weather in san francisco'}},
  'metadata': {'graph_id': 'agent',
   'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
   'created_by': 'system',
   'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},
  'tags': ['seq:step:1']},
 {'event_id': 'f8961760-6f13-40e6-9eef-6d4d68e0ed19',
  'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
  'received_at': '2024-05-18T00:50:29.569708+00:00',
  'span_id': '73295bb1-6cd3-403d-abc1-4f9d96a63894',
  'event': 'on_chain_start',
  'name': 'action',
  'data': {},
  'metadata': {'graph_id': 'agent',
   'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
   'created_by': 'system',
   'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},
  'tags': ['graph:step:2']},
 {'event_id': '44b5f815-f60c-468f-8d23-96dfdaa0ed20',
  'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
  'received_at': '2024-05-18T00:50:29.568202+00:00',
  'span_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
  'event': 'on_chain_stream',
  'name': 'LangGraph',
  'data': {'chunk': {'messages': [{'id': '46b31c3a-01bf-4946-bd5a-fa6a7f6c97ce',
      'name': None,
      'type': 'human',
      'content': 'what's the weather in sf',
      'example': False,
      'additional_kwargs': {},
      'response_metadata': {}},
     {'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',
      'name': None,
      'type': 'ai',
      'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
        'name': 'tavily_search_results_json',
        'type': 'tool_use',
        'input': {'query': 'weather in san francisco'}}],
      'example': False,
      'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
        'args': {'query': 'weather in san francisco'},
        'name': 'tavily_search_results_json'}],
      'additional_kwargs': {},
      'response_metadata': {},
      'invalid_tool_calls': []}]}},
  'metadata': {'graph_id': 'agent',
   'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
   'created_by': 'system',
   'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},
  'tags': []},
 {'event_id': '0917b31d-f49d-43d4-a8ed-a9eebd8904e8',
  'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
  'received_at': '2024-05-18T00:50:29.566761+00:00',
  'span_id': 'f469ac2e-17a3-491c-bc03-0c56aa30a68b',
  'event': 'on_chain_end',
  'name': 'agent',
  'data': {'input': {'messages': [{'role': 'human',
      'content': 'what's the weather in sf'}]},
   'output': {'messages': [{'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',
      'name': None,
      'type': 'ai',
      'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
        'name': 'tavily_search_results_json',
        'type': 'tool_use',
        'input': {'query': 'weather in san francisco'}}],
      'example': False,
      'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
        'args': {'query': 'weather in san francisco'},
        'name': 'tavily_search_results_json'}],
      'additional_kwargs': {},
      'response_metadata': {},
      'invalid_tool_calls': []}]}},
  'metadata': {'graph_id': 'agent',
   'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
   'created_by': 'system',
   'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},
  'tags': ['graph:step:1']},
 {'event_id': 'bbe33ebf-04ba-43d5-8718-e2da23295675',
  'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
  'received_at': '2024-05-18T00:50:29.566076+00:00',
  'span_id': 'f469ac2e-17a3-491c-bc03-0c56aa30a68b',
  'event': 'on_chain_stream',
  'name': 'agent',
  'data': {'chunk': {'messages': [{'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',
      'name': None,
      'type': 'ai',
      'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
        'name': 'tavily_search_results_json',
        'type': 'tool_use',
        'input': {'query': 'weather in san francisco'}}],
      'example': False,
      'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
        'args': {'query': 'weather in san francisco'},
        'name': 'tavily_search_results_json'}],
      'additional_kwargs': {},
      'response_metadata': {},
      'invalid_tool_calls': []}]}},
  'metadata': {'graph_id': 'agent',
   'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
   'created_by': 'system',
   'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},
  'tags': ['graph:step:1']},
 {'event_id': '38333127-fa97-4830-8157-f76264778d81',
  'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
  'received_at': '2024-05-18T00:50:29.564195+00:00',
  'span_id': 'ef55c681-be15-4f3d-9aee-3a9ff05d8746',
  'event': 'on_chain_end',
  'name': 'should_continue',
  'data': {'input': {'messages': [{'id': 'abc3581e-417b-4ca1-ab31-de7108e64b3b',
      'name': None,
      'type': 'human',
      'content': 'what's the weather in sf',
      'example': False,
      'additional_kwargs': {},
      'response_metadata': {}},
     {'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',
      'name': None,
      'type': 'ai',
      'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
        'name': 'tavily_search_results_json',
        'type': 'tool_use',
        'input': {'query': 'weather in san francisco'}}],
      'example': False,
      'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
        'args': {'query': 'weather in san francisco'},
        'name': 'tavily_search_results_json'}],
      'additional_kwargs': {},
      'response_metadata': {},
      'invalid_tool_calls': []}]},
   'output': 'continue'},
  'metadata': {'graph_id': 'agent',
   'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
   'created_by': 'system',
   'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},
  'tags': ['seq:step:3']},
 {'event_id': '408a7785-c715-4bcb-a5aa-950f414baa77',
  'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
  'received_at': '2024-05-18T00:50:29.563289+00:00',
  'span_id': 'ef55c681-be15-4f3d-9aee-3a9ff05d8746',
  'event': 'on_chain_start',
  'name': 'should_continue',
  'data': {'input': {'messages': [{'id': 'abc3581e-417b-4ca1-ab31-de7108e64b3b',
      'name': None,
      'type': 'human',
      'content': 'what's the weather in sf',
      'example': False,
      'additional_kwargs': {},
      'response_metadata': {}},
     {'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',
      'name': None,
      'type': 'ai',
      'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
        'name': 'tavily_search_results_json',
        'type': 'tool_use',
        'input': {'query': 'weather in san francisco'}}],
      'example': False,
      'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
        'args': {'query': 'weather in san francisco'},
        'name': 'tavily_search_results_json'}],
      'additional_kwargs': {},
      'response_metadata': {},
      'invalid_tool_calls': []}]}},
  'metadata': {'graph_id': 'agent',
   'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
   'created_by': 'system',
   'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},
  'tags': ['seq:step:3']},
 {'event_id': '679c8ae7-5cd2-4462-936e-20f0ea45cfb8',
  'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
  'received_at': '2024-05-18T00:50:29.560628+00:00',
  'span_id': '4885d5a0-cd89-4f00-8558-e85b542a710c',
  'event': 'on_chat_model_end',
  'name': 'ChatAnthropic',
  'data': {'input': {'messages': [[{'id': None,
       'name': None,
       'type': 'human',
       'content': 'what's the weather in sf',
       'example': False,
       'additional_kwargs': {},
       'response_metadata': {}}]]},
   'output': {'run': None,
    'llm_output': None,
    'generations': [[{'text': '',
       'type': 'ChatGeneration',
       'message': {'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',
        'name': None,
        'type': 'ai',
        'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
          'name': 'tavily_search_results_json',
          'type': 'tool_use',
          'input': {'query': 'weather in san francisco'}}],
        'example': False,
        'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
          'args': {'query': 'weather in san francisco'},
          'name': 'tavily_search_results_json'}],
        'additional_kwargs': {},
        'response_metadata': {},
        'invalid_tool_calls': []},
       'generation_info': None}]]}},
  'metadata': {'graph_id': 'agent',
   'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
   'created_by': 'system',
   'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',
   'ls_model_type': 'chat'},
  'tags': ['seq:step:1']},
 {'event_id': '055fcf73-36e7-444b-990d-6263ec50925c',
  'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
  'received_at': '2024-05-18T00:50:29.559491+00:00',
  'span_id': '4885d5a0-cd89-4f00-8558-e85b542a710c',
  'event': 'on_chat_model_stream',
  'name': 'ChatAnthropic',
  'data': {'chunk': {'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',
    'name': None,
    'type': 'AIMessageChunk',
    'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
      'name': 'tavily_search_results_json',
      'type': 'tool_use',
      'input': {'query': 'weather in san francisco'}}],
    'example': False,
    'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
      'args': {'query': 'weather in san francisco'},
      'name': 'tavily_search_results_json'}],
    'tool_call_chunks': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
      'args': '{"query": "weather in san francisco"}',
      'name': 'tavily_search_results_json',
      'index': 0}],
    'additional_kwargs': {},
    'response_metadata': {},
    'invalid_tool_calls': []}},
  'metadata': {'graph_id': 'agent',
   'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
   'created_by': 'system',
   'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',
   'ls_model_type': 'chat'},
  'tags': ['seq:step:1']},
 {'event_id': 'cf90f755-5a12-46bd-8f60-adc862388635',
  'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
  'received_at': '2024-05-18T00:50:27.873602+00:00',
  'span_id': '4885d5a0-cd89-4f00-8558-e85b542a710c',
  'event': 'on_chat_model_start',
  'name': 'ChatAnthropic',
  'data': {'input': {'messages': [[{'id': None,
       'name': None,
       'type': 'human',
       'content': 'what's the weather in sf',
       'example': False,
       'additional_kwargs': {},
       'response_metadata': {}}]]}},
  'metadata': {'graph_id': 'agent',
   'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
   'created_by': 'system',
   'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',
   'ls_model_type': 'chat'},
  'tags': ['seq:step:1']}]
In [15]:
# Eventually, it should finish and we should see `status=success`
await client.runs.get(thread['thread_id'], run['run_id'])
Out [15]:
{'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
 'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
 'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',
 'created_at': '2024-05-18T00:50:27.618761+00:00',
 'updated_at': '2024-05-18T00:50:27.618761+00:00',
 'status': 'success',
 'metadata': {}}
In [16]:
# We can get the final results
results = await client.runs.list_events(thread['thread_id'], run['run_id'])
In [21]:
# The results are sorted by time, so the most recent (final) step is the 0 index
final_result = results[0]
In [22]:
final_result
Out [22]:
{'event_id': '1af2076e-8ec7-4f2e-bc2c-6fbbf586397c',
 'run_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
 'received_at': '2024-05-18T00:50:35.557925+00:00',
 'span_id': 'e843abcb-e478-421b-91e1-a8ae171b14f4',
 'event': 'on_chain_end',
 'name': 'LangGraph',
 'data': {'output': {'messages': [{'id': '46b31c3a-01bf-4946-bd5a-fa6a7f6c97ce',
     'name': None,
     'type': 'human',
     'content': 'what's the weather in sf',
     'example': False,
     'additional_kwargs': {},
     'response_metadata': {}},
    {'id': 'run-4885d5a0-cd89-4f00-8558-e85b542a710c',
     'name': None,
     'type': 'ai',
     'content': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
       'name': 'tavily_search_results_json',
       'type': 'tool_use',
       'input': {'query': 'weather in san francisco'}}],
     'example': False,
     'tool_calls': [{'id': 'toolu_018yyEfJHihdVfWypRNLqDug',
       'args': {'query': 'weather in san francisco'},
       'name': 'tavily_search_results_json'}],
     'additional_kwargs': {},
     'response_metadata': {},
     'invalid_tool_calls': []},
    {'id': '045e936d-ee47-4236-95ff-793b6b32b590',
     'name': 'tavily_search_results_json',
     'type': 'tool',
     '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}}"}]',
     'tool_call_id': 'toolu_018yyEfJHihdVfWypRNLqDug',
     'additional_kwargs': {},
     'response_metadata': {}},
    {'id': 'run-069f1ddd-64b3-451d-bb84-75dec23a7286',
     'name': None,
     'type': 'ai',
     '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.",
     'example': False,
     'tool_calls': [],
     'additional_kwargs': {},
     'response_metadata': {},
     'invalid_tool_calls': []}]}},
 'metadata': {'graph_id': 'agent',
  'thread_id': '6ada015b-b47a-4c4f-a5cd-580893cb6d0c',
  'created_by': 'system',
  'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'},
 'tags': []}
In [28]:
# We can get the content of the final message
final_result['data']['output']['messages'][-1]['content']
Out [28]:
"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."
In [ ]: