Skip to main content

Anthropic

与Anthropic模型相关的所有功能。

Anthropic 是一家人工智能安全与研究公司,也是Claude的创建者。
本页面涵盖了Anthropic模型与LangChain之间的所有集成。

提示最佳实践

与OpenAI模型相比,Anthropic模型有一些不同的提示最佳实践。

系统消息只能是第一条消息

Anthropic模型要求任何系统消息必须是提示中的第一条消息。

ChatAnthropic

ChatAnthropic 是LangChain的 ChatModel 的子类,这意味着它最适合与 ChatPromptTemplate 一起使用。
你可以使用以下代码导入此包装器:

:::提示 请参阅安装集成包的一般说明部分。 :::

npm install @langchain/anthropic @langchain/core
import { ChatAnthropic } from "@langchain/anthropic";
const model = new ChatAnthropic({});

在使用ChatModels时,建议将提示设计为 ChatPromptTemplate 格式。
以下是一个示例:

import { ChatPromptTemplate } from "langchain/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" });

有关更多示例(包括多模态输入),请参阅 聊天模型集成页面


Was this page helpful?


You can also leave detailed feedback on GitHub.