Skip to main content
所有与 Anthropic 模型相关的功能。 Anthropic 是一家人工智能安全与研究公司,也是 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", "You are a helpful chatbot"],
  ["human", "Tell me a joke about {topic}"],
]);
然后,您可以在链中使用它,如下所示:
const chain = prompt.pipe(model);
await chain.invoke({ topic: "bears" });
请参阅聊天模型集成页面获取更多示例,包括多模态输入。