From d53de65aca55422857b2c9ef02f64fef4ef970ce Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Thu, 16 May 2024 11:34:02 -0700 Subject: [PATCH] docs: anthropic forced tool calling (#21774) --- docs/docs/integrations/chat/anthropic.ipynb | 51 ++++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/docs/docs/integrations/chat/anthropic.ipynb b/docs/docs/integrations/chat/anthropic.ipynb index 2308c1063fb..6ffd22a46d3 100644 --- a/docs/docs/integrations/chat/anthropic.ipynb +++ b/docs/docs/integrations/chat/anthropic.ipynb @@ -80,7 +80,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "id": "238bdbaa-526a-4130-89e9-523aa44bb196", "metadata": {}, "outputs": [], @@ -250,16 +250,7 @@ "execution_count": 3, "id": "42f87466-cb8e-490d-a9f8-aa0f8e9b4217", "metadata": {}, - "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" - ] - } - ], + "outputs": [], "source": [ "from langchain_core.pydantic_v1 import BaseModel, Field\n", "\n", @@ -369,13 +360,49 @@ "id": "90e015e0-c6e5-4ff5-8fb9-be0cd3c86395", "metadata": {}, "source": [ - "::: {.callout-tip}\n", + ":::tip\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", "\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': ''},\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", "id": "8652ee98-814c-4ed6-9def-275eeaa9651e",