!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/)
            
            *   [Quick Start](/v0.1/docs/modules/model_io/llms/quick_start/)
            *   [Streaming](/v0.1/docs/modules/model_io/llms/streaming_llm/)
            *   [Caching](/v0.1/docs/modules/model_io/llms/llm_caching/)
            *   [Custom LLM](/v0.1/docs/modules/model_io/llms/custom_llm/)
            *   [Tracking token usage](/v0.1/docs/modules/model_io/llms/token_usage_tracking/)
            *   [Cancelling requests](/v0.1/docs/modules/model_io/llms/cancelling_requests/)
            *   [Dealing with API Errors](/v0.1/docs/modules/model_io/llms/dealing_with_api_errors/)
            *   [Dealing with Rate Limits](/v0.1/docs/modules/model_io/llms/dealing_with_rate_limits/)
            *   [Subscribing to events](/v0.1/docs/modules/model_io/llms/subscribing_events/)
            *   [Adding a timeout](/v0.1/docs/modules/model_io/llms/timeouts/)
        *   [Chat Models](/v0.1/docs/modules/model_io/chat/)
            
        *   [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/)
*   [LLMs](/v0.1/docs/modules/model_io/llms/)
*   Quick Start

On this page

Quick Start
===========

Large Language Models (LLMs) are a core component of LangChain. LangChain does not serve its own LLMs, but rather provides a standard interface for interacting with many different LLMs.

There are lots of LLM providers (OpenAI, Cohere, Hugging Face, etc) - the `LLM` class is designed to provide a standard interface for all of them.

In this walkthrough we'll work with an OpenAI LLM wrapper, although the functionalities highlighted are generic for all LLM types.

Setup[​](#setup "Direct link to Setup")
---------------------------------------

*   OpenAI
*   Local (using Ollama)

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 by running:

    export 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 { OpenAI } from "@langchain/openai";const llm = new OpenAI({  apiKey: "YOUR_KEY_HERE",});

otherwise you can initialize with an empty object:

    import { OpenAI } from "@langchain/openai";const llm = new OpenAI({});

[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 { Ollama } from "@langchain/community/llms/ollama";const llm = new Ollama({  baseUrl: "http://localhost:11434", // Default value  model: "mistral",});

LCEL[​](#lcel "Direct link to LCEL")
------------------------------------

LLMs 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.

LLMs accept **strings** as inputs, or objects which can be coerced to string prompts, including `BaseMessage[]` and `PromptValue`.

    await llm.invoke(  "What are some theories about the relationship between unemployment and inflation?");

    '\n\n1. The Phillips Curve Theory: This suggests that there is an inverse relationship between unemployment and inflation, meaning that when unemployment is low, inflation will be higher, and when unemployment is high, inflation will be lower.\n\n2. The Monetarist Theory: This theory suggests that the relationship between unemployment and inflation is weak, and that changes in the money supply are more important in determining inflation.\n\n3. The Resource Utilization Theory: This suggests that when unemployment is low, firms are able to raise wages and prices in order to take advantage of the increased demand for their products and services. This leads to higher inflation.'

See the [Runnable interface](/v0.1/docs/expression_language/interface/) for more details on the available methods.

\[Legacy\] `generate`: batch calls, richer outputs[​](#legacy-generate-batch-calls-richer-outputs "Direct link to legacy-generate-batch-calls-richer-outputs")
--------------------------------------------------------------------------------------------------------------------------------------------------------------

`generate` lets you can call the model with a list of strings, getting back a more complete response than just the text. This complete response can include things like multiple top responses and other LLM provider-specific information:

    const llmResult = await llm.generate(  ["Tell me a joke", "Tell me a poem"],  ["Tell me a joke", "Tell me a poem"]);console.log(llmResult.generations.length);// 30console.log(llmResult.generations[0]);/*  [    {      text: "\n\nQ: What did the fish say when it hit the wall?\nA: Dam!",      generationInfo: { finishReason: "stop", logprobs: null }    }  ]*/console.log(llmResult.generations[1]);/*  [    {      text: "\n\nRoses are red,\nViolets are blue,\nSugar is sweet,\nAnd so are you.",      generationInfo: { finishReason: "stop", logprobs: null }    }  ]*/

You can also access provider specific information that is returned. This information is NOT standardized across providers.

    console.log(llmResult.llmOutput);/*  {    tokenUsage: { completionTokens: 46, promptTokens: 8, totalTokens: 54 }  }*/

Here's an example with additional parameters, which sets `-1` for `max_tokens` to turn on token size calculations:

    import { OpenAI } from "@langchain/openai";const model = new OpenAI({  // customize openai model that's used, `gpt-3.5-turbo-instruct` is the default  model: "gpt-3.5-turbo-instruct",  // `max_tokens` supports a magic -1 param where the max token length for the specified modelName  //  is calculated and included in the request to OpenAI as the `max_tokens` param  maxTokens: -1,  // use `modelKwargs` to pass params directly to the openai call  // note that OpenAI uses snake_case instead of camelCase  modelKwargs: {    user: "me",  },  // for additional logging for debugging purposes  verbose: true,});const resA = await model.invoke(  "What would be a good company name a company that makes colorful socks?");console.log({ resA });// { resA: '\n\nSocktastic Colors' }

#### API Reference:

*   [OpenAI](https://api.js.langchain.com/classes/langchain_openai.OpenAI.html) from `@langchain/openai`

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

LLMs

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

Next

Streaming

](/v0.1/docs/modules/model_io/llms/streaming_llm/)

*   [Setup](#setup)
*   [LCEL](#lcel)
*   [Legacy `generate`: batch calls, richer outputs](#legacy-generate-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.