Skip to main content

Quick Start

Chat models are a variation on language models. While chat models use language models under the hood, the interface they use is a bit different. Rather than using a "text in, text out" API, they use an interface where "chat messages" are the inputs and outputs.

Setup

tip

We're unifying model params across all packages. We now suggest using model instead of modelName, and apiKey for API keys.

First we'll need to install the LangChain OpenAI integration package:

npm install @langchain/openai

Accessing the API requires an API key, which you can get by creating an account and heading here. Once we have a key we'll want to set it as an environment variable:

OPENAI_API_KEY="..."

If you'd prefer not to set an environment variable you can pass the key in directly via the apiKey named parameter when initiating the OpenAI Chat Model class:

import { ChatOpenAI } from "@langchain/openai";

const chatModel = new ChatOpenAI({
apiKey: "...",
});

Otherwise you can initialize without any params:

import { ChatOpenAI } from "@langchain/openai";

const chatModel = new ChatOpenAI();

Messages

The chat model interface is based around messages rather than raw text. The types of messages currently supported in LangChain are AIMessage, HumanMessage, SystemMessage, FunctionMessage, and ChatMessage -- ChatMessage takes in an arbitrary role parameter. Most of the time, you'll just be dealing with HumanMessage, AIMessage, and SystemMessage

LCEL

Chat models implement the Runnable interface, the basic building block of the LangChain Expression Language (LCEL). This means they support invoke, stream, batch, and streamLog calls.

Chat models accept BaseMessage[] as inputs, or objects which can be coerced to messages, including string (converted to HumanMessage) and PromptValue.

import { HumanMessage, SystemMessage } from "@langchain/core/messages";

const messages = [
new SystemMessage("You're a helpful assistant"),
new HumanMessage("What is the purpose of model regularization?"),
];
await chatModel.invoke(messages);
AIMessage { content: 'The purpose of model regularization is to prevent overfitting in machine learning models. Overfitting occurs when a model becomes too complex and starts to fit the noise in the training data, leading to poor generalization on unseen data. Regularization techniques introduce additional constraints or penalties to the model's objective function, discouraging it from becoming overly complex and promoting simpler and more generalizable models. Regularization helps to strike a balance between fitting the training data well and avoiding overfitting, leading to better performance on new, unseen data.' }

See the Runnable interface for more details on the available methods.

LangSmith

All ChatModels come with built-in LangSmith tracing. Just set the following environment variables:

export LANGCHAIN_TRACING_V2="true"
export LANGCHAIN_API_KEY=<your-api-key>

and any ChatModel invocation (whether it's nested in a chain or not) will automatically be traced. A trace will include inputs, outputs, latency, token usage, invocation params, environment params, and more. See an example here: https://smith.langchain.com/public/a54192ae-dd5c-4f7a-88d1-daa1eaba1af7/r.

In LangSmith you can then provide feedback for any trace, compile annotated datasets for evals, debug performance in the playground, and more.

[Legacy] generate

Batch calls, richer outputs

You can go one step further and generate completions for multiple sets of messages using generate. This returns an LLMResult with an additional message parameter.

const response3 = await chatModel.generate([
[
new SystemMessage(
"You are a helpful assistant that translates English to French."
),
new HumanMessage(
"Translate this sentence from English to French. I love programming."
),
],
[
new SystemMessage(
"You are a helpful assistant that translates English to French."
),
new HumanMessage(
"Translate this sentence from English to French. I love artificial intelligence."
),
],
]);
console.log(response3);
/*
{
generations: [
[
{
text: "J'aime programmer.",
message: AIMessage { text: "J'aime programmer." },
}
],
[
{
text: "J'aime l'intelligence artificielle.",
message: AIMessage { text: "J'aime l'intelligence artificielle." }
}
]
]
}
*/

You can recover things like token usage from this LLMResult:

console.log(response3.llmOutput);
/*
{
tokenUsage: { completionTokens: 20, promptTokens: 69, totalTokens: 89 }
}
*/

Help us out by providing feedback on this documentation page: