Skip to main content
所有与Anthropic模型相关的功能。 Anthropic 是一家专注于人工智能安全和研究的公司,同时也是 Claude 的创造者。 本页面涵盖了 Anthropic 模型与 LangChain 之间的所有集成。

提示最佳实践

与 OpenAI 模型相比,Anthropic 模型在提示(prompting)方面有多个最佳实践。 系统消息可能仅为第一条消息 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" });
查看聊天模型集成页面以获取更多示例,包括多模态输入。