Skip to main content

ChatAnthropic

LangChain supports Anthropic's Claude family of chat models.

You'll first need to install the @langchain/anthropic package:

npm install @langchain/anthropic

You'll also need to sign up and obtain an Anthropic API key. Set it as an environment variable named ANTHROPIC_API_KEY, or pass it into the constructor as shown below.

Usage

You can initialize an instance like this:

import { ChatAnthropic } from "@langchain/anthropic";

const model = new ChatAnthropic({
temperature: 0.9,
modelName: "claude-2.1",
// In Node.js defaults to process.env.ANTHROPIC_API_KEY,
// anthropicApiKey: "YOUR-API-KEY",
maxTokens: 1024,
});

const res = await model.invoke("Why is the sky blue?");

console.log(res);

/*
AIMessage {
content: "The sky appears blue because of how air in Earth's atmosphere interacts with sunlight. As sunlight passes through the atmosphere, light waves get scattered by gas molecules and airborne particles. Blue light waves scatter more easily than other color light waves. Since blue light gets scattered across the sky, we perceive the sky as having a blue color.",
name: undefined,
additional_kwargs: {
id: 'msg_01JuukTnjoXHuzQaPiSVvZQ1',
type: 'message',
role: 'assistant',
model: 'claude-2.1',
stop_reason: 'end_turn',
stop_sequence: null,
usage: { input_tokens: 15, output_tokens: 70 }
}
}
*/

API Reference: