ChatAnthropic
这本笔记本提供了一个快速概述,帮助你开始使用 Anthropic 聊天模型。要查看所有 ChatAnthropic 功能和配置的详细文档,请参阅 API 参考。
Anthropic 有几个聊天模型。您可以在 Anthropic 文档 中找到有关它们最新版本的信息,包括成本、上下文窗口大小和支持的输入类型。
请注意,某些Anthropic模型还可以通过AWS Bedrock和Google VertexAI访问。请参阅ChatBedrock和ChatVertexAI集成以使用这些服务中的Anthropic模型。
概览
集成细节
| Class | 包 | 本地 | 序列化 | JS支持 | Package downloads | Package 最新版本 |
|---|---|---|---|---|---|---|
| ChatAnthropic | langchain-anthropic | ❌ | beta | ✅ |
模型特性
| 工具调用 | 结构化输出 | JSON 模式 | 图像输入 | 音频输入 | 视频输入 | Token级流式传输 | 原生异步 | Token 使用 | 对数概率 |
|---|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ |
设置
要访问Anthropic模型,您需要创建一个Anthropic账户,获取API密钥,并安装langchain-anthropic集成包。
Credentials
请访问 https://console.anthropic.com/ 注册 Anthropic 并生成 API 密钥。完成这些步骤后,请设置 ANTHROPIC_API_KEY 环境变量:
import getpass
import os
if "ANTHROPIC_API_KEY" not in os.environ:
os.environ["ANTHROPIC_API_KEY"] = getpass.getpass("Enter your Anthropic API key: ")
要启用对您的模型调用的自动跟踪,请设置您的LangSmith API密钥:
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"
安装
LangChain Anthropic 整合内容位于 langchain-anthropic 包中:
%pip install -qU langchain-anthropic
langchain-anthropic>=0.3.10Instantiation
现在我们就可以实例化我们的模型对象并生成聊天完成内容:
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(
model="claude-3-5-sonnet-20240620",
temperature=0,
max_tokens=1024,
timeout=None,
max_retries=2,
# other params...
)
Invocation
messages = [
(
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
),
("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
AIMessage(content="J'adore la programmation.", response_metadata={'id': 'msg_018Nnu76krRPq8HvgKLW4F8T', 'model': 'claude-3-5-sonnet-20240620', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 29, 'output_tokens': 11}}, id='run-57e9295f-db8a-48dc-9619-babd2bedd891-0', usage_metadata={'input_tokens': 29, 'output_tokens': 11, 'total_tokens': 40})
print(ai_msg.content)
J'adore la programmation.
链式调用
我们可以通过以下方式将模型与提示模板进行链接:
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
),
("human", "{input}"),
]
)
chain = prompt | llm
chain.invoke(
{
"input_language": "English",
"output_language": "German",
"input": "I love programming.",
}
)
AIMessage(content="Here's the German translation:\n\nIch liebe Programmieren.", response_metadata={'id': 'msg_01GhkRtQZUkA5Ge9hqmD8HGY', 'model': 'claude-3-5-sonnet-20240620', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 23, 'output_tokens': 18}}, id='run-da5906b4-b200-4e08-b81a-64d4453643b6-0', usage_metadata={'input_tokens': 23, 'output_tokens': 18, 'total_tokens': 41})
内容块
Content from a single Anthropic AI message can either be a single string or a list of content blocks. For example when an Anthropic model invokes a tool, the tool invocation is part of the message content (as well as being exposed in the standardized AIMessage.tool_calls):
from pydantic import BaseModel, Field
class GetWeather(BaseModel):
"""Get the current weather in a given location"""
location: str = Field(..., description="The city and state, e.g. San Francisco, CA")
llm_with_tools = llm.bind_tools([GetWeather])
ai_msg = llm_with_tools.invoke("Which city is hotter today: LA or NY?")
ai_msg.content
[{'text': "To answer this question, we'll need to check the current weather in both Los Angeles (LA) and New York (NY). I'll use the GetWeather function to retrieve this information for both cities.",
'type': 'text'},
{'id': 'toolu_01Ddzj5PkuZkrjF4tafzu54A',
'input': {'location': 'Los Angeles, CA'},
'name': 'GetWeather',
'type': 'tool_use'},
{'id': 'toolu_012kz4qHZQqD4qg8sFPeKqpP',
'input': {'location': 'New York, NY'},
'name': 'GetWeather',
'type': 'tool_use'}]
ai_msg.tool_calls
[{'name': 'GetWeather',
'args': {'location': 'Los Angeles, CA'},
'id': 'toolu_01Ddzj5PkuZkrjF4tafzu54A'},
{'name': 'GetWeather',
'args': {'location': 'New York, NY'},
'id': 'toolu_012kz4qHZQqD4qg8sFPeKqpP'}]
扩展思考
Claude 3.7 Sonnet 支持一个 扩展思维 功能,这将输出其最终答案所依据的逐步推理过程。
使用它时,在初始化thinking时指定ChatAnthropic参数。也可以在调用期间作为关键字参数传入。
您需要指定一个令牌预算才能使用此功能。请参阅下方的使用示例:
import json
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(
model="claude-3-7-sonnet-latest",
max_tokens=5000,
thinking={"type": "enabled", "budget_tokens": 2000},
)
response = llm.invoke("What is the cube root of 50.653?")
print(json.dumps(response.content, indent=2))
[
{
"signature": "ErUBCkYIARgCIkCx7bIPj35jGPHpoVOB2y5hvPF8MN4lVK75CYGftmVNlI4axz2+bBbSexofWsN1O/prwNv8yPXnIXQmwT6zrJsKEgwJzvks0yVRZtaGBScaDOm9xcpOxbuhku1zViIw9WDgil/KZL8DsqWrhVpC6TzM0RQNCcsHcmgmyxbgG9g8PR0eJGLxCcGoEw8zMQu1Kh1hQ1/03hZ2JCOgigpByR9aNPTwwpl64fQUe6WwIw==",
"thinking": "To find the cube root of 50.653, I need to find the value of $x$ such that $x^3 = 50.653$.\n\nI can try to estimate this first. \n$3^3 = 27$\n$4^3 = 64$\n\nSo the cube root of 50.653 will be somewhere between 3 and 4, but closer to 4.\n\nLet me try to compute this more precisely. I can use the cube root function:\n\ncube root of 50.653 = 50.653^(1/3)\n\nLet me calculate this:\n50.653^(1/3) \u2248 3.6998\n\nLet me verify:\n3.6998^3 \u2248 50.6533\n\nThat's very close to 50.653, so I'm confident that the cube root of 50.653 is approximately 3.6998.\n\nActually, let me compute this more precisely:\n50.653^(1/3) \u2248 3.69981\n\nLet me verify once more:\n3.69981^3 \u2248 50.652998\n\nThat's extremely close to 50.653, so I'll say that the cube root of 50.653 is approximately 3.69981.",
"type": "thinking"
},
{
"text": "The cube root of 50.653 is approximately 3.6998.\n\nTo verify: 3.6998\u00b3 = 50.6530, which is very close to our original number.",
"type": "text"
}
]
Prompt caching
Anthropic 支持缓存您提示中的部分元素,包括消息、工具定义、工具结果、图像和文档。这允许您重用大型文档、指令、少量示例文件和其他数据以减少延迟和成本。
要启用提示元素的缓存,请使用cache_control键标记其关联的内容块。请参见以下示例:
消息
import requests
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(model="claude-3-7-sonnet-20250219")
# Pull LangChain readme
get_response = requests.get(
"https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md"
)
readme = get_response.text
messages = [
{
"role": "system",
"content": [
{
"type": "text",
"text": "You are a technology expert.",
},
{
"type": "text",
"text": f"{readme}",
"cache_control": {"type": "ephemeral"},
},
],
},
{
"role": "user",
"content": "What's LangChain, according to its README?",
},
]
response_1 = llm.invoke(messages)
response_2 = llm.invoke(messages)
usage_1 = response_1.usage_metadata["input_token_details"]
usage_2 = response_2.usage_metadata["input_token_details"]
print(f"First invocation:\n{usage_1}")
print(f"\nSecond:\n{usage_2}")
First invocation:
{'cache_read': 0, 'cache_creation': 1458}
Second:
{'cache_read': 1458, 'cache_creation': 0}
工具
from langchain_anthropic import convert_to_anthropic_tool
from langchain_core.tools import tool
# For demonstration purposes, we artificially expand the
# tool description.
description = (
f"Get the weather at a location. By the way, check out this readme: {readme}"
)
@tool(description=description)
def get_weather(location: str) -> str:
return "It's sunny."
# Enable caching on the tool
weather_tool = convert_to_anthropic_tool(get_weather)
weather_tool["cache_control"] = {"type": "ephemeral"}
llm = ChatAnthropic(model="claude-3-7-sonnet-20250219")
llm_with_tools = llm.bind_tools([weather_tool])
query = "What's the weather in San Francisco?"
response_1 = llm_with_tools.invoke(query)
response_2 = llm_with_tools.invoke(query)
usage_1 = response_1.usage_metadata["input_token_details"]
usage_2 = response_2.usage_metadata["input_token_details"]
print(f"First invocation:\n{usage_1}")
print(f"\nSecond:\n{usage_2}")
First invocation:
{'cache_read': 0, 'cache_creation': 1809}
Second:
{'cache_read': 1809, 'cache_creation': 0}
增量缓存对话应用程序
提示缓存可以在多轮对话中使用来保持早期消息的上下文,而不需要冗余处理。
我们可以通过在最终消息标记为cache_control来启用增量缓存。Claude将会自动使用之前缓存的最长前缀作为后续消息的预缓存。
以下,我们将实现一个简单的聊天机器人,该聊天机器人包含此功能。我们遵循LangChain 聊天机器人教程,但在每个用户消息的消息块中自动将最后一个内容块标记为cache_control。请参见下方:
import requests
from langchain_anthropic import ChatAnthropic
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, StateGraph, add_messages
from typing_extensions import Annotated, TypedDict
llm = ChatAnthropic(model="claude-3-7-sonnet-20250219")
# Pull LangChain readme
get_response = requests.get(
"https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md"
)
readme = get_response.text
def messages_reducer(left: list, right: list) -> list:
# Update last user message
for i in range(len(right) - 1, -1, -1):
if right[i].type == "human":
right[i].content[-1]["cache_control"] = {"type": "ephemeral"}
break
return add_messages(left, right)
class State(TypedDict):
messages: Annotated[list, messages_reducer]
workflow = StateGraph(state_schema=State)
# Define the function that calls the model
def call_model(state: State):
response = llm.invoke(state["messages"])
return {"messages": [response]}
# Define the (single) node in the graph
workflow.add_edge(START, "model")
workflow.add_node("model", call_model)
# Add memory
memory = MemorySaver()
app = workflow.compile(checkpointer=memory)
from langchain_core.messages import HumanMessage
config = {"configurable": {"thread_id": "abc123"}}
query = "Hi! I'm Bob."
input_message = HumanMessage([{"type": "text", "text": query}])
output = app.invoke({"messages": [input_message]}, config)
output["messages"][-1].pretty_print()
print(f'\n{output["messages"][-1].usage_metadata["input_token_details"]}')
==================================[1m Ai Message [0m==================================
Hello, Bob! It's nice to meet you. How are you doing today? Is there something I can help you with?
{'cache_read': 0, 'cache_creation': 0}
query = f"Check out this readme: {readme}"
input_message = HumanMessage([{"type": "text", "text": query}])
output = app.invoke({"messages": [input_message]}, config)
output["messages"][-1].pretty_print()
print(f'\n{output["messages"][-1].usage_metadata["input_token_details"]}')
==================================[1m Ai Message [0m==================================
I can see you've shared the README from the LangChain GitHub repository. This is the documentation for LangChain, which is a popular framework for building applications powered by Large Language Models (LLMs). Here's a summary of what the README contains:
LangChain is:
- A framework for developing LLM-powered applications
- Helps chain together components and integrations to simplify AI application development
- Provides a standard interface for models, embeddings, vector stores, etc.
Key features/benefits:
- Real-time data augmentation (connect LLMs to diverse data sources)
- Model interoperability (swap models easily as needed)
- Large ecosystem of integrations
The LangChain ecosystem includes:
- LangSmith - For evaluations and observability
- LangGraph - For building complex agents with customizable architecture
- LangGraph Platform - For deployment and scaling of agents
The README also mentions installation instructions (`pip install -U langchain`) and links to various resources including tutorials, how-to guides, conceptual guides, and API references.
Is there anything specific about LangChain you'd like to know more about, Bob?
{'cache_read': 0, 'cache_creation': 1498}
query = "What was my name again?"
input_message = HumanMessage([{"type": "text", "text": query}])
output = app.invoke({"messages": [input_message]}, config)
output["messages"][-1].pretty_print()
print(f'\n{output["messages"][-1].usage_metadata["input_token_details"]}')
==================================[1m Ai Message [0m==================================
Your name is Bob. You introduced yourself at the beginning of our conversation.
{'cache_read': 1498, 'cache_creation': 269}
在LangSmith 跟踪中,切换 "原始输出" 将显示发送给聊天模型的确切消息,包括cache_control个密钥。
Token-efficient tool use
Anthropic 支持一个(Beta)高效使用的工具功能。要使用该功能,请在实例化模型时指定相关的 Beta-headers。
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
llm = ChatAnthropic(
model="claude-3-7-sonnet-20250219",
temperature=0,
model_kwargs={
"extra_headers": {"anthropic-beta": "token-efficient-tools-2025-02-19"}
},
)
@tool
def get_weather(location: str) -> str:
"""Get the weather at a location."""
return "It's sunny."
llm_with_tools = llm.bind_tools([get_weather])
response = llm_with_tools.invoke("What's the weather in San Francisco?")
print(response.tool_calls)
print(f'\nTotal tokens: {response.usage_metadata["total_tokens"]}')
[{'name': 'get_weather', 'args': {'location': 'San Francisco'}, 'id': 'toolu_01EoeE1qYaePcmNbUvMsWtmA', 'type': 'tool_call'}]
Total tokens: 408
引用
Anthropic 支持一个"citations": {"enabled": True}citations
简单示例
在本示例中,我们传递了一个纯文本文档。在后台,Claude 自动将输入文本切分为句子,这些句子用于生成引文。
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(model="claude-3-5-haiku-latest")
messages = [
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": "The grass is green. The sky is blue.",
},
"title": "My Document",
"context": "This is a trustworthy document.",
"citations": {"enabled": True},
},
{"type": "text", "text": "What color is the grass and sky?"},
],
}
]
response = llm.invoke(messages)
response.content
[{'text': 'Based on the document, ', 'type': 'text'},
{'text': 'the grass is green',
'type': 'text',
'citations': [{'type': 'char_location',
'cited_text': 'The grass is green. ',
'document_index': 0,
'document_title': 'My Document',
'start_char_index': 0,
'end_char_index': 20}]},
{'text': ', and ', 'type': 'text'},
{'text': 'the sky is blue',
'type': 'text',
'citations': [{'type': 'char_location',
'cited_text': 'The sky is blue.',
'document_index': 0,
'document_title': 'My Document',
'start_char_index': 20,
'end_char_index': 36}]},
{'text': '.', 'type': 'text'}]
使用带有文本分割器
Anthropic 还允许您使用自定义文档类型 指定自己的拆分。LangChain 的 文本拆分器 可以用于为此目的生成有意义的拆分。请参见以下示例,在此我们将 LangChain README(一个 markdown 文档)进行拆分并传递给 Claude 作为上下文:
import requests
from langchain_anthropic import ChatAnthropic
from langchain_text_splitters import MarkdownTextSplitter
def format_to_anthropic_documents(documents: list[str]):
return {
"type": "document",
"source": {
"type": "content",
"content": [{"type": "text", "text": document} for document in documents],
},
"citations": {"enabled": True},
}
# Pull readme
get_response = requests.get(
"https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md"
)
readme = get_response.text
# Split into chunks
splitter = MarkdownTextSplitter(
chunk_overlap=0,
chunk_size=50,
)
documents = splitter.split_text(readme)
# Construct message
message = {
"role": "user",
"content": [
format_to_anthropic_documents(documents),
{"type": "text", "text": "Give me a link to LangChain's tutorials."},
],
}
# Query LLM
llm = ChatAnthropic(model="claude-3-5-haiku-latest")
response = llm.invoke([message])
response.content
[{'text': "You can find LangChain's tutorials at https://python.langchain.com/docs/tutorials/\n\nThe tutorials section is recommended for those looking to build something specific or who prefer a hands-on learning approach. It's considered the best place to get started with LangChain.",
'type': 'text',
'citations': [{'type': 'content_block_location',
'cited_text': "[Tutorials](https://python.langchain.com/docs/tutorials/):If you're looking to build something specific orare more of a hands-on learner, check out ourtutorials. This is the best place to get started.",
'document_index': 0,
'document_title': None,
'start_block_index': 243,
'end_block_index': 248}]}]
内置工具
Anthropic 支持各种内置工具(参见此处),这些工具可以通过常规方式绑定到模型中。Claude 将根据其内部模式生成对工具的调用:
网搜索
Claude 可以使用一个网络搜索工具进行搜索,并通过引用将其响应基于搜索结果。
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(model="claude-3-5-sonnet-latest")
tool = {"type": "web_search_20250305", "name": "web_search", "max_uses": 3}
llm_with_tools = llm.bind_tools([tool])
response = llm_with_tools.invoke("How do I update a web app to TypeScript 5.5?")
文本编辑器
The text editor tool can be used to view and modify text files. See docs 这里 for details.
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(model="claude-3-7-sonnet-20250219")
tool = {"type": "text_editor_20250124", "name": "str_replace_editor"}
llm_with_tools = llm.bind_tools([tool])
response = llm_with_tools.invoke(
"There's a syntax error in my primes.py file. Can you help me fix it?"
)
print(response.text())
response.tool_calls
I'd be happy to help you fix the syntax error in your primes.py file. First, let's look at the current content of the file to identify the error.
[{'name': 'str_replace_editor',
'args': {'command': 'view', 'path': '/repo/primes.py'},
'id': 'toolu_01VdNgt1YV7kGfj9LFLm6HyQ',
'type': 'tool_call'}]
API 参考
详细文档涵盖了所有ChatAnthropic功能和配置,请参阅API参考:https://python.langchain.com/api_reference/anthropic/chat_models/langchain_anthropic.chat_models.ChatAnthropic.html