Reputation: 6150
Using the Anthropic messsages API directly and providing multiple tools to the model, it's possible to instruct the model to only invoke one tool at a time by setting disable_parallel_tool_use=true
in the tool_choice field.
I'm using LangGraph, and I'd like to be able to do the same thing - instruct the model to only choose one tool at a time.
Below is the heart of my code. I'm leaving out the tool method definitions and prompts, because they're not directly relevant.
model = ChatAnthropic(model="claude-3-5-sonnet-20241022")
tools = [search_word_forms, search_dictionaries, WordDetermination]
model_with_tools = model.bind_tools(tools, tool_choice="any")
response = model_with_tools.invoke(state["messages"])
Using this framework, how can I instruct the model to disable parallel tool use?
Upvotes: 0
Views: 247
Reputation: 11
As of the latest version of langchain-anthropic
you can specify parallel_tools_calls
when calling bind_tools
:
model = ChatAnthropic(model="claude-3-5-sonnet-20241022")
model_with_tools = model.bind_tools(tools, parallel_tool_calls=False)
Upvotes: 1
Reputation: 21
Check out Langchain's document of ChatAnthropic: https://python.langchain.com/api_reference/anthropic/chat_models/langchain_anthropic.chat_models.ChatAnthropic.html#langchain_anthropic.chat_models.ChatAnthropic.bind_tools
You can use the dictionary containing options like type
or disable_parallel_tool_use
as tool_choice
argument of bind_tools
.
So the example code would be like:
model = ChatAnthropic(model="claude-3-5-sonnet-20241022")
tool_choice = {
"type": "any" if tool_call_only else "auto",
"disable_parallel_tool_use": not enable_parallel_tool_calls,
}
model_with_tools = model.bind_tools(tools, tool_choice=tool_choice)
Upvotes: 2