[Deprecated] Experimental Anthropic Tools Wrapper
The Anthropic API officially supports tool-calling so this workaround is no longer needed. Please useChatAnthropic withlangchain-anthropic>=0.1.15
.
This notebook shows how to use an experimental wrapper around Anthropic that gives it tool calling and structured output capabilities. It follows Anthropic's guidehere
The wrapper is available from thelangchain-anthropic
package, and it also requires the optional dependencydefusedxml
for parsing XML output from the llm.
Note: this is a beta feature that will be replaced by Anthropic's formal implementation of tool calling, but it is useful for testing and experimentation in the meantime.
%pip install-qU langchain-anthropic defusedxml
from langchain_anthropic.experimentalimport ChatAnthropicTools
Tool Binding
ChatAnthropicTools
exposes abind_tools
method that allows you to pass in Pydantic models or BaseTools to the llm.
from pydanticimport BaseModel
classPerson(BaseModel):
name:str
age:int
model= ChatAnthropicTools(model="claude-3-opus-20240229").bind_tools(tools=[Person])
model.invoke("I am a 27 year old named Erick")
AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'name': 'Person', 'arguments': '{"name": "Erick", "age": "27"}'}, 'type': 'function'}]})
Structured Output
ChatAnthropicTools
also implements thewith_structured_output
spec for extracting values. Note: this may not be as stable as with models that explicitly offer tool calling.
chain= ChatAnthropicTools(model="claude-3-opus-20240229").with_structured_output(
Person
)
chain.invoke("I am a 27 year old named Erick")
Person(name='Erick', age=27)
Related
- Chat modelconceptual guide
- Chat modelhow-to guides