Skip to main content

Anthropic

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

¥All functionality related to Anthropic models.

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

¥Anthropic is an AI safety and research company, and is the creator of Claude. This page covers all integrations between Anthropic models and LangChain.

提示最佳实践

¥Prompting Best Practices

与 OpenAI 模型相比,人为模型有几种提示最佳实践。

¥Anthropic models have several prompting best practices compared to OpenAI models.

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

¥System Messages may only be the first message

人为模型要求所有系统消息都必须是提示中的第一个消息。

¥Anthropic models require any system messages to be the first one in your prompts.

ChatAnthropic

ChatAnthropic 是 LangChain ChatModel 的子类,这意味着它与 ChatPromptTemplate 配合使用效果最佳。你可以使用以下代码导入此封装器:

¥ChatAnthropic is a subclass of LangChain's ChatModel, meaning it works best with ChatPromptTemplate. You can import this wrapper with the following code:

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

使用 ChatModel 时,最好将提示设计为 ChatPromptTemplate。下面是一个执行此操作的示例:

¥When working with ChatModels, it is preferred that you design your prompts as ChatPromptTemplates. Here is an example below of doing that:

import { ChatPromptTemplate } from "langchain/prompts";

const prompt = ChatPromptTemplate.fromMessages([
["system", "You are a helpful chatbot"],
["human", "Tell me a joke about {topic}"],
]);

然后,你可以在链中使用它,如下所示:

¥You can then use this in a chain as follows:

const chain = prompt.pipe(model);
await chain.invoke({ topic: "bears" });

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

¥See the chat model integration page for more examples, including multimodal inputs.