跳到主要内容
所有与 Anthropic 模型相关的功能。 Anthropic 是一家人工智能安全和研究公司,也是 Claude 的创建者。本页面涵盖了 Anthropic 模型和 LangChain 之间的所有集成。

提示词最佳实践

与 OpenAI 模型相比,Anthropic 模型有几项提示词最佳实践。 系统消息只能是第一条消息 Anthropic 模型要求任何系统消息必须是提示词中的第一条。

ChatAnthropic

ChatAnthropic 是 LangChain 的 ChatModel 的一个子类,这意味着它与 ChatPromptTemplate 配合使用效果最佳。您可以使用以下代码导入此包装器
有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
npm install @langchain/anthropic @langchain/core
import { ChatAnthropic } from "@langchain/anthropic";
const model = new ChatAnthropic({});
使用 ChatModels 时,最好将您的提示词设计为 ChatPromptTemplate。下面是一个示例
import { ChatPromptTemplate } from "@langchain/classic/prompts";

const prompt = ChatPromptTemplate.fromMessages([
  ["system", "You are a helpful chatbot"],
  ["human", "Tell me a joke about {topic}"],
]);
然后您可以在链中如下使用它
const chain = prompt.pipe(model);
await chain.invoke({ topic: "bears" });
有关更多示例,包括多模态输入,请参阅聊天模型集成页面
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.