Hello there, AI enthusiasts! Today, we're going to have a closer look at LangChain Chat Models.
Models are an essential part of LangChain. Rather than being a provider of models, LangChain serves as an interface allowing you to interact with diverse language models from other places. Currently, LangChain supports LLMs, Chat Models, and Text Embedding models.
The primary distinction between LLMs and Chat Models lies in their input and output structure. LLMs operate on a text-based input and output, while Chat Models follow a message-based input and output system.
It's important to note that chat model APIs are a relatively new feature, and they're still determining the most effective abstractions.
Chat Models are a variation of language models. While they use language models underneath, the interface they expose is somewhat different. Instead of a "text in, text out" API, they expose an interface where "chat messages" are the inputs and outputs.
At LangChain, they refer to a ChatMessage as the modular unit of information for a chat model. This usually includes a "text" field, signifying the content of the chat message. There are currently four different classes of ChatMessage supported:
HumanChatMessage
: A chat message sent from a human's perspective.AIChatMessage
: A message sent from the point of view of the AI system the human is communicating with.SystemChatMessage
: A message that provides the AI system with some information about the conversation. Typically sent at the start of a conversation.ChatMessage
: A generic chat message, not only with a "text" field but also an arbitrary "role" field.
LangChain presently supports the ChatOpenAI model (with gpt-4 and gpt-3.5-turbo), but they anticipate adding more in the future. To get started, all you need to do is use the call method of an LLM implementation, passing in a string input. Here's a simple example of how to initiate a conversation using the ChatOpenAI implementation:
import { ChatOpenAI } from "langchain/chat_models/openai";
import { HumanChatMessage } from "langchain/schema";
export const run = async () => {
const chat = new ChatOpenAI();
const response = await chat.call([
new HumanChatMessage(
"What is a good name for a company that makes colorful socks?"
),
]);
console.log(response);
// AIChatMessage { text: '\n\nRainbow Sox Co.' }
};
To get a deeper understanding, you can refer to the API references:
LangChain offers several Chat Models implementations that integrate with various model providers. These include ChatOpenAI
, Azure ChatOpenAI
, and ChatAnthropic
. Each of these providers has a different way of getting instantiated. For example, to instantiate ChatOpenAI
:
import { ChatOpenAI } from "langchain/chat_models/openai";
const model = new ChatOpenAI({
temperature: 0.9,
apiKey: "your_openai_api_key"
});
The temperature
is a hyperparameter that adjusts the randomness of the model's responses. Higher values result in more diverse outputs, while lower values make the output more deterministic.
The apiKey
is your personal OpenAI API key, which authorizes you to use the model.
Similarly, you can instantiate Azure ChatOpenAI
and ChatAnthropic
:
// Azure ChatOpenAI
import { AzureChatOpenAI } from "langchain/chat_models/azure";
const model = new AzureChatOpenAI({
temperature: 0.9,
azureApiKey: "your_azure_api_key"
});
// ChatAnthropic
import { ChatAnthropic } from "langchain/chat_models/anthropic";
const model = new ChatAnthropic({
temperature: 0.9,
anthropicApiKey: "your_anthropic_api_key"
});
Note: Make sure to replace "your_openai_api_key"
, "your_azure_api_key"
, and "your_anthropic_api_key"
with your actual API keys.
Keep in mind that each provider may have specific parameters unique to their implementation. Always refer to the LangChain documentation or the provider's documentation for a comprehensive understanding of the parameters.
LangChain's Chat Models provide a unified approach to interacting with various AI language models. This versatility allows developers to switch between different providers seamlessly. While we've covered the basics of instantiating chat models from OpenAI, Azure, and Anthropic, there's a wealth of other functionality and customization available in LangChain. As an open-source project, LangChain encourages community contributions to drive its growth and evolution. With LangChain, the potential to create powerful language processing applications is at your fingertips.
Subscribe or follow me on
What is LangChain?
How can I contribute to LangChain?
What is the temperature
parameter in the instantiation code?
temperature
parameter controls the randomness of the model's responses. Higher values produce more random outputs, while lower values make the model's outputs more deterministic.
What is an API key, and why do I need it?
Where can I get my API key?
Is LangChain free to use?
Also published here.