docs: update structured output how-to guide (#2773)

Fixes #2760
This commit is contained in:
Vadym Barda
2024-12-16 09:51:12 -05:00
committed by GitHub
parent d9ec185e72
commit 2f0e3c66d1
@@ -225,9 +225,19 @@
"# Define the function that responds to the user\n",
"def respond(state: AgentState):\n",
" # Construct the final answer from the arguments of the last tool call\n",
" response = WeatherResponse(**state[\"messages\"][-1].tool_calls[0][\"args\"])\n",
" weather_tool_call = state[\"messages\"][-1].tool_calls[0]\n",
" response = WeatherResponse(**weather_tool_call[\"args\"])\n",
" # Since we're using tool calling to return structured output,\n",
" # we need to add a tool message corresponding to the WeatherResponse tool call,\n",
" # This is due to LLM providers' requirement that AI messages with tool calls\n",
" # need to be followed by a tool message for each tool call\n",
" tool_message = {\n",
" \"type\": \"tool\",\n",
" \"content\": \"Here is your structured response\",\n",
" \"tool_call_id\": weather_tool_call[\"id\"],\n",
" }\n",
" # We return the final answer\n",
" return {\"final_response\": response}\n",
" return {\"final_response\": response, \"messages\": [tool_message]}\n",
"\n",
"\n",
"# Define the function that determines whether to continue or not\n",
@@ -466,7 +476,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.12.3"
}
},
"nbformat": 4,