mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-09-12 04:37:51 +02:00
Format docs (#752)
This commit is contained in:
@@ -108,7 +108,7 @@
|
||||
],
|
||||
"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 = await client.runs.list(thread[\"thread_id\"])\n",
|
||||
"runs"
|
||||
]
|
||||
},
|
||||
@@ -121,7 +121,7 @@
|
||||
"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)"
|
||||
"run = await client.runs.create(thread[\"thread_id\"], assistant_id, input=input)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -165,7 +165,7 @@
|
||||
],
|
||||
"source": [
|
||||
"# The first time we poll it, we can see `status=pending`\n",
|
||||
"await client.runs.get(thread['thread_id'], run['run_id'])"
|
||||
"await client.runs.get(thread[\"thread_id\"], run[\"run_id\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -176,7 +176,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Wait until the run finishes\n",
|
||||
"await client.runs.join(thread['thread_id'], run['run_id'])"
|
||||
"await client.runs.join(thread[\"thread_id\"], run[\"run_id\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -220,7 +220,7 @@
|
||||
],
|
||||
"source": [
|
||||
"# Eventually, it should finish and we should see `status=success`\n",
|
||||
"await client.runs.get(thread['thread_id'], run['run_id'])"
|
||||
"await client.runs.get(thread[\"thread_id\"], run[\"run_id\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -231,7 +231,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# We can get the final results\n",
|
||||
"final_result = await client.threads.get_state(thread['thread_id'])"
|
||||
"final_result = await client.threads.get_state(thread[\"thread_id\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -360,7 +360,7 @@
|
||||
],
|
||||
"source": [
|
||||
"# We can get the content of the final message\n",
|
||||
"final_result['values']['messages'][-1]['content']"
|
||||
"final_result[\"values\"][\"messages\"][-1][\"content\"]"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
@@ -68,7 +68,7 @@
|
||||
"# 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",
|
||||
"assistants = [a for a in assistants if not a[\"config\"]]\n",
|
||||
"base_assistant = assistants[0]"
|
||||
]
|
||||
},
|
||||
@@ -93,10 +93,12 @@
|
||||
],
|
||||
"source": [
|
||||
"# We can now call `.get_schemas` to get schemas associated with this graph\n",
|
||||
"schemas = await client.assistants.get_schemas(assistant_id=base_assistant[\"assistant_id\"])\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']"
|
||||
"schemas[\"config_schema\"][\"definitions\"][\"Configurable\"][\"properties\"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -106,7 +108,9 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"assistant = await client.assistants.create(graph_id=\"agent\", config={\"configurable\": {\"model_name\": \"openai\"}})"
|
||||
"assistant = await client.assistants.create(\n",
|
||||
" graph_id=\"agent\", config={\"configurable\": {\"model_name\": \"openai\"}}\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -163,7 +167,9 @@
|
||||
"source": [
|
||||
"thread = await client.threads.create()\n",
|
||||
"input = {\"messages\": [{\"role\": \"user\", \"content\": \"who made you?\"}]}\n",
|
||||
"async for event in client.runs.stream(thread['thread_id'], assistant['assistant_id'], input=input):\n",
|
||||
"async for event in client.runs.stream(\n",
|
||||
" thread[\"thread_id\"], assistant[\"assistant_id\"], input=input\n",
|
||||
"):\n",
|
||||
" print(event)"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -112,7 +112,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"runs = await client.runs.list(thread['thread_id'])\n",
|
||||
"runs = await client.runs.list(thread[\"thread_id\"])\n",
|
||||
"runs"
|
||||
]
|
||||
},
|
||||
@@ -158,7 +158,11 @@
|
||||
"source": [
|
||||
"input = {\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in sf\"}]}\n",
|
||||
"async for chunk in client.runs.stream(\n",
|
||||
" thread['thread_id'], assistant_id, input=input, stream_mode=\"updates\", interrupt_before=['action']\n",
|
||||
" thread[\"thread_id\"],\n",
|
||||
" assistant_id,\n",
|
||||
" input=input,\n",
|
||||
" stream_mode=\"updates\",\n",
|
||||
" interrupt_before=[\"action\"],\n",
|
||||
"):\n",
|
||||
" print(f\"Receiving new event of type: {chunk.event}...\")\n",
|
||||
" print(chunk.data)\n",
|
||||
@@ -209,7 +213,11 @@
|
||||
"source": [
|
||||
"input = None\n",
|
||||
"async for chunk in client.runs.stream(\n",
|
||||
" thread['thread_id'], assistant_id, input=input, stream_mode=\"updates\", interrupt_before=['action']\n",
|
||||
" thread[\"thread_id\"],\n",
|
||||
" assistant_id,\n",
|
||||
" input=input,\n",
|
||||
" stream_mode=\"updates\",\n",
|
||||
" interrupt_before=[\"action\"],\n",
|
||||
"):\n",
|
||||
" print(f\"Receiving new event of type: {chunk.event}...\")\n",
|
||||
" print(chunk.data)\n",
|
||||
@@ -259,7 +267,11 @@
|
||||
"source": [
|
||||
"input = {\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in la?\"}]}\n",
|
||||
"async for chunk in client.runs.stream(\n",
|
||||
" thread['thread_id'], assistant_id, input=input, stream_mode=\"updates\", interrupt_before=['action']\n",
|
||||
" thread[\"thread_id\"],\n",
|
||||
" assistant_id,\n",
|
||||
" input=input,\n",
|
||||
" stream_mode=\"updates\",\n",
|
||||
" interrupt_before=[\"action\"],\n",
|
||||
"):\n",
|
||||
" print(f\"Receiving new event of type: {chunk.event}...\")\n",
|
||||
" print(chunk.data)\n",
|
||||
@@ -281,7 +293,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"thread_state = await client.threads.get_state(thread['thread_id'])"
|
||||
"thread_state = await client.threads.get_state(thread[\"thread_id\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -299,7 +311,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"last_message = thread_state['values']['messages'][-1]"
|
||||
"last_message = thread_state[\"values\"][\"messages\"][-1]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -323,7 +335,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"last_message['content']"
|
||||
"last_message[\"content\"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -341,12 +353,14 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"last_message['tool_calls'] = [{\n",
|
||||
" 'id': last_message['tool_calls'][0]['id'],\n",
|
||||
" 'name': 'tavily_search_results_json',\n",
|
||||
" # We change the query to say temperature\n",
|
||||
" 'args': {'query': 'weather in Louisiana'}\n",
|
||||
"}]\n",
|
||||
"last_message[\"tool_calls\"] = [\n",
|
||||
" {\n",
|
||||
" \"id\": last_message[\"tool_calls\"][0][\"id\"],\n",
|
||||
" \"name\": \"tavily_search_results_json\",\n",
|
||||
" # We change the query to say temperature\n",
|
||||
" \"args\": {\"query\": \"weather in Louisiana\"},\n",
|
||||
" }\n",
|
||||
"]\n",
|
||||
"# last_message['content'] = [{\n",
|
||||
"# 'id': last_message['content'][0]['id'],\n",
|
||||
"# 'name': 'tavily_search_results_json',\n",
|
||||
@@ -383,7 +397,9 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"await client.threads.update_state(thread['thread_id'], values={\"messages\": [last_message]})"
|
||||
"await client.threads.update_state(\n",
|
||||
" thread[\"thread_id\"], values={\"messages\": [last_message]}\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -414,8 +430,8 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"thread_state = await client.threads.get_state(thread['thread_id'])\n",
|
||||
"thread_state['values']['messages'][-1]['tool_calls']"
|
||||
"thread_state = await client.threads.get_state(thread[\"thread_id\"])\n",
|
||||
"thread_state[\"values\"][\"messages\"][-1][\"tool_calls\"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -462,7 +478,11 @@
|
||||
"source": [
|
||||
"input = None\n",
|
||||
"async for chunk in client.runs.stream(\n",
|
||||
" thread['thread_id'], assistant_id, input=input, stream_mode=\"updates\", interrupt_before=['action']\n",
|
||||
" thread[\"thread_id\"],\n",
|
||||
" assistant_id,\n",
|
||||
" input=input,\n",
|
||||
" stream_mode=\"updates\",\n",
|
||||
" interrupt_before=[\"action\"],\n",
|
||||
"):\n",
|
||||
" print(f\"Receiving new event of type: {chunk.event}...\")\n",
|
||||
" print(chunk.data)\n",
|
||||
@@ -487,7 +507,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"thread_history = await client.threads.get_history(thread['thread_id'], limit=100)"
|
||||
"thread_history = await client.threads.get_history(thread[\"thread_id\"], limit=100)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -541,7 +561,7 @@
|
||||
],
|
||||
"source": [
|
||||
"rewind_state = thread_history[3]\n",
|
||||
"rewind_state['values']['messages'][-1]['tool_calls']"
|
||||
"rewind_state[\"values\"][\"messages\"][-1][\"tool_calls\"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -563,7 +583,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"rewind_state['config']"
|
||||
"rewind_state[\"config\"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -610,12 +630,12 @@
|
||||
"source": [
|
||||
"input = None\n",
|
||||
"async for chunk in client.runs.stream(\n",
|
||||
" thread['thread_id'], \n",
|
||||
" assistant_id, \n",
|
||||
" input=input, \n",
|
||||
" stream_mode=\"updates\", \n",
|
||||
" interrupt_before=['action'],\n",
|
||||
" config=rewind_state['config']\n",
|
||||
" thread[\"thread_id\"],\n",
|
||||
" assistant_id,\n",
|
||||
" input=input,\n",
|
||||
" stream_mode=\"updates\",\n",
|
||||
" interrupt_before=[\"action\"],\n",
|
||||
" config=rewind_state[\"config\"],\n",
|
||||
"):\n",
|
||||
" print(f\"Receiving new event of type: {chunk.event}...\")\n",
|
||||
" print(chunk.data)\n",
|
||||
|
||||
@@ -65,7 +65,9 @@
|
||||
" await client.runs.create(\n",
|
||||
" thread[\"thread_id\"],\n",
|
||||
" assistant_id,\n",
|
||||
" input={\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in nyc?\"}]},\n",
|
||||
" input={\n",
|
||||
" \"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in nyc?\"}]\n",
|
||||
" },\n",
|
||||
" multitask_strategy=\"reject\",\n",
|
||||
" )\n",
|
||||
"except httpx.HTTPStatusError as e:\n",
|
||||
|
||||
@@ -23,11 +23,13 @@
|
||||
"\n",
|
||||
"client = get_client()\n",
|
||||
"\n",
|
||||
"openai_assistant = await client.assistants.create(graph_id=\"agent\", config={\"configurable\": {\"model_name\": \"openai\"}})\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]"
|
||||
"default_assistant = [a for a in assistants if not a[\"config\"]][0]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -117,7 +119,12 @@
|
||||
"source": [
|
||||
"thread = await client.threads.create()\n",
|
||||
"input = {\"messages\": [{\"role\": \"user\", \"content\": \"who made you?\"}]}\n",
|
||||
"async for event in client.runs.stream(thread['thread_id'], openai_assistant['assistant_id'], input=input, stream_mode='updates'):\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)"
|
||||
]
|
||||
},
|
||||
@@ -147,7 +154,12 @@
|
||||
],
|
||||
"source": [
|
||||
"input = {\"messages\": [{\"role\": \"user\", \"content\": \"and you?\"}]}\n",
|
||||
"async for event in client.runs.stream(thread['thread_id'], default_assistant['assistant_id'], input=input, stream_mode='updates'):\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)"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -109,7 +109,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"runs = await client.runs.list(thread['thread_id'])\n",
|
||||
"runs = await client.runs.list(thread[\"thread_id\"])\n",
|
||||
"runs"
|
||||
]
|
||||
},
|
||||
@@ -122,11 +122,14 @@
|
||||
"source": [
|
||||
"# Helper function for formatting messages\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def format_tool_calls(tool_calls):\n",
|
||||
" if tool_calls:\n",
|
||||
" formatted_calls = []\n",
|
||||
" for call in tool_calls:\n",
|
||||
" formatted_calls.append(f\"Tool Call ID: {call['id']}, Function: {call['name']}, Arguments: {call['args']}\")\n",
|
||||
" formatted_calls.append(\n",
|
||||
" f\"Tool Call ID: {call['id']}, Function: {call['name']}, Arguments: {call['args']}\"\n",
|
||||
" )\n",
|
||||
" return \"\\n\".join(formatted_calls)\n",
|
||||
" return \"No tool calls\""
|
||||
]
|
||||
@@ -332,36 +335,41 @@
|
||||
"input = {\"messages\": [{\"role\": \"user\", \"content\": \"what's the weather in sf\"}]}\n",
|
||||
"config = {\"configurable\": {\"model_name\": \"openai\"}}\n",
|
||||
"\n",
|
||||
"async for event in client.runs.stream(thread['thread_id'], assistant_id, input=input, config=config, stream_mode='messages'):\n",
|
||||
" if event.event == 'metadata':\n",
|
||||
"async for event in client.runs.stream(\n",
|
||||
" thread[\"thread_id\"],\n",
|
||||
" assistant_id,\n",
|
||||
" input=input,\n",
|
||||
" config=config,\n",
|
||||
" stream_mode=\"messages\",\n",
|
||||
"):\n",
|
||||
" if event.event == \"metadata\":\n",
|
||||
" print(f\"Metadata: Run ID - {event.data['run_id']}\")\n",
|
||||
" print(\"-\" * 50)\n",
|
||||
" elif event.event == 'messages/partial':\n",
|
||||
" elif event.event == \"messages/partial\":\n",
|
||||
" for data_item in event.data:\n",
|
||||
" if 'role' in data_item and data_item['role'] == 'user':\n",
|
||||
" if \"role\" in data_item and data_item[\"role\"] == \"user\":\n",
|
||||
" print(f\"Human: {data_item['content']}\")\n",
|
||||
" else:\n",
|
||||
" tool_calls = data_item.get('tool_calls', [])\n",
|
||||
" invalid_tool_calls = data_item.get('invalid_tool_calls', [])\n",
|
||||
" content = data_item.get('content', \"\")\n",
|
||||
" response_metadata = data_item.get('response_metadata', {})\n",
|
||||
" tool_calls = data_item.get(\"tool_calls\", [])\n",
|
||||
" invalid_tool_calls = data_item.get(\"invalid_tool_calls\", [])\n",
|
||||
" content = data_item.get(\"content\", \"\")\n",
|
||||
" response_metadata = data_item.get(\"response_metadata\", {})\n",
|
||||
"\n",
|
||||
" if content:\n",
|
||||
" print(f\"AI: {content}\")\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" if tool_calls:\n",
|
||||
" print(\"Tool Calls:\")\n",
|
||||
" print(format_tool_calls(tool_calls))\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" if invalid_tool_calls:\n",
|
||||
" print(\"Invalid Tool Calls:\")\n",
|
||||
" print(format_tool_calls(invalid_tool_calls))\n",
|
||||
"\n",
|
||||
" if response_metadata:\n",
|
||||
" finish_reason = response_metadata.get('finish_reason', 'N/A')\n",
|
||||
" finish_reason = response_metadata.get(\"finish_reason\", \"N/A\")\n",
|
||||
" print(f\"Response Metadata: Finish Reason - {finish_reason}\")\n",
|
||||
" print(\"-\" * 50)\n",
|
||||
" "
|
||||
" print(\"-\" * 50)"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
@@ -90,7 +90,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"runs = await client.runs.list(thread['thread_id'])\n",
|
||||
"runs = await client.runs.list(thread[\"thread_id\"])\n",
|
||||
"runs"
|
||||
]
|
||||
},
|
||||
@@ -134,7 +134,12 @@
|
||||
],
|
||||
"source": [
|
||||
"input = {\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in la\"}]}\n",
|
||||
"async for chunk in client.runs.stream(thread['thread_id'], assistant_id, input=input, stream_mode=\"updates\", ):\n",
|
||||
"async for chunk in client.runs.stream(\n",
|
||||
" thread[\"thread_id\"],\n",
|
||||
" assistant_id,\n",
|
||||
" input=input,\n",
|
||||
" stream_mode=\"updates\",\n",
|
||||
"):\n",
|
||||
" print(f\"Receiving new event of type: {chunk.event}...\")\n",
|
||||
" print(chunk.data)\n",
|
||||
" print(\"\\n\\n\")"
|
||||
|
||||
@@ -93,7 +93,7 @@
|
||||
"source": [
|
||||
"input = {\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in la\"}]}\n",
|
||||
"thread = await client.threads.create()\n",
|
||||
"async for chunk in client.runs.stream(thread['thread_id'], assistant_id, input=input):\n",
|
||||
"async for chunk in client.runs.stream(thread[\"thread_id\"], assistant_id, input=input):\n",
|
||||
" print(f\"Receiving new event of type: {chunk.event}...\")\n",
|
||||
" print(chunk.data)\n",
|
||||
" print(\"\\n\\n\")"
|
||||
@@ -117,7 +117,7 @@
|
||||
"input = {\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in la\"}]}\n",
|
||||
"thread = await client.threads.create()\n",
|
||||
"final_answer = None\n",
|
||||
"async for chunk in client.runs.stream(thread['thread_id'], assistant_id, input=input):\n",
|
||||
"async for chunk in client.runs.stream(thread[\"thread_id\"], assistant_id, input=input):\n",
|
||||
" if chunk.event == \"values\":\n",
|
||||
" final_answer = chunk.data"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user