docs: anthropic forced tool calling (#21774)

This commit is contained in:
Erick Friis 2024-05-16 11:34:02 -07:00 committed by GitHub
parent f02e27b664
commit d53de65aca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -80,7 +80,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 1,
"id": "238bdbaa-526a-4130-89e9-523aa44bb196", "id": "238bdbaa-526a-4130-89e9-523aa44bb196",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -250,16 +250,7 @@
"execution_count": 3, "execution_count": 3,
"id": "42f87466-cb8e-490d-a9f8-aa0f8e9b4217", "id": "42f87466-cb8e-490d-a9f8-aa0f8e9b4217",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/bagatur/langchain/libs/core/langchain_core/_api/beta_decorator.py:87: LangChainBetaWarning: The function `bind_tools` is in beta. It is actively being worked on, so the API may change.\n",
" warn_beta(\n"
]
}
],
"source": [ "source": [
"from langchain_core.pydantic_v1 import BaseModel, Field\n", "from langchain_core.pydantic_v1 import BaseModel, Field\n",
"\n", "\n",
@ -369,13 +360,49 @@
"id": "90e015e0-c6e5-4ff5-8fb9-be0cd3c86395", "id": "90e015e0-c6e5-4ff5-8fb9-be0cd3c86395",
"metadata": {}, "metadata": {},
"source": [ "source": [
"::: {.callout-tip}\n", ":::tip\n",
"\n", "\n",
"ChatAnthropic model outputs are always a single AI message that can have either a single string or a list of content blocks. The content blocks can be text blocks or tool-duse blocks. There can be multiple of each and they can be interspersed.\n", "ChatAnthropic model outputs are always a single AI message that can have either a single string or a list of content blocks. The content blocks can be text blocks or tool-duse blocks. There can be multiple of each and they can be interspersed.\n",
"\n", "\n",
":::" ":::"
] ]
}, },
{
"cell_type": "markdown",
"id": "b5145dea-0183-4cab-b9e2-0e35fb8370cf",
"metadata": {},
"source": [
"### Forcing tool calls\n",
"\n",
"By default the model can choose whether to call any tools. To force the model to call at least one tool we can specify `bind_tools(..., tool_choice=\"any\")` and to force the model to call a specific tool we can pass in that tool name `bind_tools(..., tool_choice=\"GetWeather\")`"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "05993626-060c-449f-8069-e52d31442977",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'name': 'GetWeather',\n",
" 'args': {'location': '<UNKNOWN>'},\n",
" 'id': 'toolu_01DwWjKzHPs6EHCUPxsGm9bN'}]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"llm_with_force_tools = llm.bind_tools([GetWeather], tool_choice=\"GetWeather\")\n",
"# Notice the model will still return tool calls despite a message that\n",
"# doesn't have anything to do with the tools.\n",
"llm_with_force_tools.invoke(\"this doesn't really require tool use\").tool_calls"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "8652ee98-814c-4ed6-9def-275eeaa9651e", "id": "8652ee98-814c-4ed6-9def-275eeaa9651e",