Skip to main content
所有与 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({});
在使用 ChatModel 时,建议您将提示设计为 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" });
有关更多示例(包括多模态输入),请参阅 聊天模型集成页面