所有与 Anthropic 模型相关的功能。 Anthropic是一家 AI 安全与研究公司,也是 Claude 的创造者。 本页面涵盖了 Anthropic 模型与 LangChain 之间的所有集成。

提示最佳实践

与 OpenAI 模型相比,Anthropic 模型有几条提示最佳实践。 系统消息必须位于首位 Anthropic 模型要求任何系统消息都必须是你提示中的第一条消息。

ChatAnthropic

ChatAnthropic 是 LangChain 的 ChatModel 的子类,这意味着它最适合与 ChatPromptTemplate 搭配使用。 你可以通过以下代码导入此封装器:
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", "你是一个乐于助人的聊天机器人"],
  ["human", "给我讲一个关于{topic}的笑话"],
]);
然后,你可以像下面这样将它用于链中:
const chain = prompt.pipe(model);
await chain.invoke({ topic: "熊" });
有关更多示例,包括多模态输入,请参阅聊天模型集成页面