!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Get started](/v0.1/docs/get_started/)
    
    *   [Introduction](/v0.1/docs/get_started/introduction/)
    *   [Installation](/v0.1/docs/get_started/installation/)
    *   [Quickstart](/v0.1/docs/get_started/quickstart/)
*   [LangChain Expression Language](/v0.1/docs/expression_language/)
    
    *   [Get started](/v0.1/docs/expression_language/get_started/)
    *   [Why use LCEL?](/v0.1/docs/expression_language/why/)
    *   [Interface](/v0.1/docs/expression_language/interface/)
    *   [Streaming](/v0.1/docs/expression_language/streaming/)
    *   [How to](/v0.1/docs/expression_language/how_to/routing/)
        
    *   [Cookbook](/v0.1/docs/expression_language/cookbook/)
        
    *   [LangChain Expression Language (LCEL)](/v0.1/docs/expression_language/)
*   [Modules](/v0.1/docs/modules/)
    
    *   [Model I/O](/v0.1/docs/modules/model_io/)
        
        *   [Quickstart](/v0.1/docs/modules/model_io/quick_start/)
        *   [Concepts](/v0.1/docs/modules/model_io/concepts/)
        *   [Prompts](/v0.1/docs/modules/model_io/prompts/)
            
        *   [LLMs](/v0.1/docs/modules/model_io/llms/)
            
        *   [Chat Models](/v0.1/docs/modules/model_io/chat/)
            
            *   [Quick Start](/v0.1/docs/modules/model_io/chat/quick_start/)
            *   [Streaming](/v0.1/docs/modules/model_io/chat/streaming/)
            *   [Caching](/v0.1/docs/modules/model_io/chat/caching/)
            *   [Custom chat models](/v0.1/docs/modules/model_io/chat/custom_chat/)
            *   [Tracking token usage](/v0.1/docs/modules/model_io/chat/token_usage_tracking/)
            *   [Cancelling requests](/v0.1/docs/modules/model_io/chat/cancelling_requests/)
            *   [Dealing with API Errors](/v0.1/docs/modules/model_io/chat/dealing_with_api_errors/)
            *   [Dealing with rate limits](/v0.1/docs/modules/model_io/chat/dealing_with_rate_limits/)
            *   [Tool/function calling](/v0.1/docs/modules/model_io/chat/function_calling/)
            *   [Response metadata](/v0.1/docs/modules/model_io/chat/response_metadata/)
            *   [Subscribing to events](/v0.1/docs/modules/model_io/chat/subscribing_events/)
            *   [Adding a timeout](/v0.1/docs/modules/model_io/chat/timeouts/)
        *   [Model I/O](/v0.1/docs/modules/model_io/)
        *   [Output Parsers](/v0.1/docs/modules/model_io/output_parsers/)
            
    *   [Retrieval](/v0.1/docs/modules/data_connection/)
        
    *   [Chains](/v0.1/docs/modules/chains/)
    *   [Agents](/v0.1/docs/modules/agents/)
        
    *   [More](/v0.1/docs/modules/memory/)
        
*   [Security](/v0.1/docs/security/)
*   [Guides](/v0.1/docs/guides/)
    
*   [Ecosystem](/v0.1/docs/ecosystem/)
    
*   [LangGraph](/v0.1/docs/langgraph/)
*   * * *
    

*   [](/v0.1/)
*   [Modules](/v0.1/docs/modules/)
*   [Model I/O](/v0.1/docs/modules/model_io/)
*   [Chat Models](/v0.1/docs/modules/model_io/chat/)
*   Quick Start

On this page

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[​](#setup "Direct link to Setup")
---------------------------------------

tip

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

*   OpenAI
*   Local (using Ollama)
*   Anthropic
*   Google GenAI

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

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/openai

    yarn add @langchain/openai

    pnpm add @langchain/openai

Accessing the API requires an API key, which you can get by creating an account and heading [here](https://platform.openai.com/account/api-keys). 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();

[Ollama](https://ollama.ai/) allows you to run open-source large language models, such as Llama 2 and Mistral, locally.

First, follow [these instructions](https://github.com/jmorganca/ollama) to set up and run a local Ollama instance:

*   [Download](https://ollama.ai/download)
*   Fetch a model via e.g. `ollama pull mistral`

Then, make sure the Ollama server is running. Next, you'll need to install the LangChain community package:

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/community

    yarn add @langchain/community

    pnpm add @langchain/community

And then you can do:

    import { ChatOllama } from "@langchain/community/chat_models/ollama";const chatModel = new ChatOllama({  baseUrl: "http://localhost:11434", // Default value  model: "mistral",});

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

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/anthropic

    yarn add @langchain/anthropic

    pnpm add @langchain/anthropic

Accessing the API requires an API key, which you can get by creating an account [here](https://console.anthropic.com/). Once we have a key we'll want to set it as an environment variable:

    ANTHROPIC_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 `ChatAnthropic` class:

    import { ChatAnthropic } from "@langchain/anthropic";const chatModel = new ChatAnthropic({  apiKey: "...",});

Otherwise you can initialize without any params:

    import { ChatAnthropic } from "@langchain/anthropic";const chatModel = new ChatAnthropic();

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

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/google-genai

    yarn add @langchain/google-genai

    pnpm add @langchain/google-genai

Accessing the API requires an API key, which you can get by creating an account [here](https://ai.google.dev/tutorials/setup). Once we have a key we'll want to set it as an environment variable:

    GOOGLE_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 `ChatGoogleGenerativeAI` class:

    import { ChatGoogleGenerativeAI } from "@langchain/google-genai";const chatModel = new ChatGoogleGenerativeAI({  apiKey: "...",});

Otherwise you can initialize without any params:

    import { ChatGoogleGenerativeAI } from "@langchain/google-genai";const chatModel = new ChatGoogleGenerativeAI();

Messages[​](#messages "Direct link to 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[​](#lcel "Direct link to LCEL")
------------------------------------

Chat models implement the [Runnable interface](/v0.1/docs/expression_language/interface/), the basic building block of the [LangChain Expression Language (LCEL)](/v0.1/docs/expression_language/). 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](/v0.1/docs/expression_language/interface/) for more details on the available methods.

[LangSmith](https://docs.smith.langchain.com/)[​](#langsmith "Direct link to langsmith")
----------------------------------------------------------------------------------------

All `ChatModel`s 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](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`[​](#legacy-generate "Direct link to legacy-generate")
---------------------------------------------------------------------------

### Batch calls, richer outputs[​](#batch-calls-richer-outputs "Direct link to 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:

[

Previous

Chat Models

](/v0.1/docs/modules/model_io/chat/)[

Next

Streaming

](/v0.1/docs/modules/model_io/chat/streaming/)

*   [Setup](#setup)
*   [Messages](#messages)
*   [LCEL](#lcel)
*   [LangSmith](#langsmith)
*   [Legacy `generate`](#legacy-generate)
    *   [Batch calls, richer outputs](#batch-calls-richer-outputs)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.