!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/)
            
            *   [Quick Start](/v0.1/docs/modules/model_io/prompts/quick_start/)
            *   [Example selectors](/v0.1/docs/modules/model_io/prompts/example_selector_types/)
                
            *   [Few Shot Prompt Templates](/v0.1/docs/modules/model_io/prompts/few_shot/)
            *   [Partial prompt templates](/v0.1/docs/modules/model_io/prompts/partial/)
            *   [Composition](/v0.1/docs/modules/model_io/prompts/pipeline/)
        *   [LLMs](/v0.1/docs/modules/model_io/llms/)
            
        *   [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/)
*   [Prompts](/v0.1/docs/modules/model_io/prompts/)
*   Quick Start

On this page

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

Language models take text as input - that text is commonly referred to as a prompt. Typically this is not simply a hardcoded string but rather a combination of a template, some examples, and user input. LangChain provides several classes and functions to make constructing and working with prompts easy.

What is a prompt template?[​](#what-is-a-prompt-template "Direct link to What is a prompt template?")
-----------------------------------------------------------------------------------------------------

A prompt template refers to a reproducible way to generate a prompt. It contains a text string ("the template"), that can take in a set of parameters from the end user and generates a prompt.

A prompt template can contain:

*   instructions to the language model,
*   a set of few shot examples to help the language model generate a better response,
*   a question to the language model.

Here's a simple example:

*   F-String
*   Mustache

    import { PromptTemplate } from "@langchain/core/prompts";// If a template is passed in, the input variables are inferred automatically from the template.const prompt = PromptTemplate.fromTemplate(  `You are a naming consultant for new companies.What is a good name for a company that makes {product}?`);const formattedPrompt = await prompt.format({  product: "colorful socks",});/*You are a naming consultant for new companies.What is a good name for a company that makes colorful socks?*/

#### API Reference:

*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`

    import { PromptTemplate } from "@langchain/core/prompts";// If a template is passed in, the input variables are inferred automatically from the template.const prompt = PromptTemplate.fromTemplate(  `You are a naming consultant for new companies.What is a good name for a company that makes {{product}}?`,  {    templateFormat: "mustache",  });const formattedPrompt = await prompt.format({  product: "colorful socks",});/*You are a naming consultant for new companies.What is a good name for a company that makes colorful socks?*/

#### API Reference:

*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`

Create a prompt template[​](#create-a-prompt-template "Direct link to Create a prompt template")
------------------------------------------------------------------------------------------------

You can create simple hardcoded prompts using the `PromptTemplate` class. Prompt templates can take any number of input variables, and can be formatted to generate a prompt.

*   F-String
*   Mustache

    import { PromptTemplate } from "@langchain/core/prompts";// An example prompt with no input variablesconst noInputPrompt = new PromptTemplate({  inputVariables: [],  template: "Tell me a joke.",});const formattedNoInputPrompt = await noInputPrompt.format({});console.log(formattedNoInputPrompt);// "Tell me a joke."// An example prompt with one input variableconst oneInputPrompt = new PromptTemplate({  inputVariables: ["adjective"],  template: "Tell me a {adjective} joke.",});const formattedOneInputPrompt = await oneInputPrompt.format({  adjective: "funny",});console.log(formattedOneInputPrompt);// "Tell me a funny joke."// An example prompt with multiple input variablesconst multipleInputPrompt = new PromptTemplate({  inputVariables: ["adjective", "content"],  template: "Tell me a {adjective} joke about {content}.",});const formattedMultipleInputPrompt = await multipleInputPrompt.format({  adjective: "funny",  content: "chickens",});console.log(formattedMultipleInputPrompt);// "Tell me a funny joke about chickens."

#### API Reference:

*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`

    import { PromptTemplate } from "@langchain/core/prompts";// An example prompt with no input variablesconst noInputPrompt = new PromptTemplate({  inputVariables: [],  template: "Tell me a joke.",});const formattedNoInputPrompt = await noInputPrompt.format({});console.log(formattedNoInputPrompt);// "Tell me a joke."// An example prompt with one input variableconst oneInputPrompt = new PromptTemplate({  inputVariables: ["adjective"],  template: "Tell me a {{adjective}} joke.",  templateFormat: "mustache",});const formattedOneInputPrompt = await oneInputPrompt.format({  adjective: "funny",});console.log(formattedOneInputPrompt);// "Tell me a funny joke."// An example prompt with multiple input variablesconst multipleInputPrompt = new PromptTemplate({  inputVariables: ["adjective", "content"],  template: "Tell me a {{adjective}} joke about {{content}}.",  templateFormat: "mustache",});const formattedMultipleInputPrompt = await multipleInputPrompt.format({  adjective: "funny",  content: "chickens",});console.log(formattedMultipleInputPrompt);// "Tell me a funny joke about chickens."

#### API Reference:

*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`

If you do not wish to specify `inputVariables` manually, you can also create a `PromptTemplate` using the `fromTemplate` class method. LangChain will automatically infer the `inputVariables` based on the `template` passed.

*   F-String
*   Mustache

    import { PromptTemplate } from "@langchain/core/prompts";const template = "Tell me a {adjective} joke about {content}.";const promptTemplate = PromptTemplate.fromTemplate(template);console.log(promptTemplate.inputVariables);// ['adjective', 'content']const formattedPromptTemplate = await promptTemplate.format({  adjective: "funny",  content: "chickens",});console.log(formattedPromptTemplate);// "Tell me a funny joke about chickens."

#### API Reference:

*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`

    import { PromptTemplate } from "@langchain/core/prompts";const template = "Tell me a {{adjective}} joke about {{content}}.";const promptTemplate = PromptTemplate.fromTemplate(template, {  templateFormat: "mustache",});console.log(promptTemplate.inputVariables);// ['adjective', 'content']const formattedPromptTemplate = await promptTemplate.format({  adjective: "funny",  content: "chickens",});console.log(formattedPromptTemplate);// "Tell me a funny joke about chickens."

#### API Reference:

*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`

You can create custom prompt templates that format the prompt in any way you want.

Chat prompt template[​](#chat-prompt-template "Direct link to Chat prompt template")
------------------------------------------------------------------------------------

[Chat Models](/v0.1/docs/modules/model_io/chat/) take a list of chat messages as input - this list is commonly referred to as a `prompt`. These chat messages differ from raw string (which you would pass into a [LLM](/v0.1/docs/modules/model_io/llms/)) in that every message is associated with a `role`.

For example, in OpenAI [Chat Completion API](https://platform.openai.com/docs/guides/chat/introduction), a chat message can be associated with an AI, human or system role. The model is supposed to follow instruction from system chat message more closely.

LangChain provides several prompt templates to make constructing and working with prompts easily. You are encouraged to use these chat related prompt templates instead of `PromptTemplate` when invoking chat models to fully explore the model's potential.

    import {  ChatPromptTemplate,  PromptTemplate,  SystemMessagePromptTemplate,  AIMessagePromptTemplate,  HumanMessagePromptTemplate,} from "@langchain/core/prompts";import {  AIMessage,  HumanMessage,  SystemMessage,} from "@langchain/core/messages";

To create a message template associated with a role, you would use the corresponding `<ROLE>MessagePromptTemplate`.

For convenience, you can also declare message prompt templates as tuples. These will be coerced to the proper prompt template types:

    const systemTemplate =  "You are a helpful assistant that translates {input_language} to {output_language}.";const humanTemplate = "{text}";const chatPrompt = ChatPromptTemplate.fromMessages([  ["system", systemTemplate],  ["human", humanTemplate],]);// Format the messagesconst formattedChatPrompt = await chatPrompt.formatMessages({  input_language: "English",  output_language: "French",  text: "I love programming.",});console.log(formattedChatPrompt);/*  [    SystemMessage {      content: 'You are a helpful assistant that translates English to French.'    },    HumanMessage {      content: 'I love programming.'    }  ]*/

You can also use `ChatPromptTemplate`'s `.formatPromptValue()` method -- this returns a `PromptValue`, which you can convert to a string or Message object, depending on whether you want to use the formatted value as input to an LLM or chat model.

If you prefer to use the message classes, there is a `fromTemplate` method exposed on these classes. This is what it would look like:

    const template =  "You are a helpful assistant that translates {input_language} to {output_language}.";const systemMessagePrompt = SystemMessagePromptTemplate.fromTemplate(template);const humanTemplate = "{text}";const humanMessagePrompt =  HumanMessagePromptTemplate.fromTemplate(humanTemplate);

If you wanted to construct the `MessagePromptTemplate` more directly, you could create a PromptTemplate externally and then pass it in, e.g.:

    const prompt = new PromptTemplate({  template:    "You are a helpful assistant that translates {input_language} to {output_language}.",  inputVariables: ["input_language", "output_language"],});const systemMessagePrompt2 = new SystemMessagePromptTemplate({  prompt,});

**Note:** If using TypeScript, you can add typing to prompts created with `.fromMessages` by passing a type parameter like this:

    const chatPrompt = ChatPromptTemplate.fromMessages<{  input_language: string;  output_language: string;  text: string;}>([systemMessagePrompt, humanMessagePrompt]);

Multi-modal prompts[​](#multi-modal-prompts "Direct link to Multi-modal prompts")
---------------------------------------------------------------------------------

    import { HumanMessage } from "@langchain/core/messages";import { ChatPromptTemplate } from "@langchain/core/prompts";import fs from "node:fs/promises";const hotdogImage = await fs.readFile("hotdog.jpg");const base64Image = hotdogImage.toString("base64");const imageURL = "https://avatars.githubusercontent.com/u/126733545?s=200&v=4";const langchainLogoMessage = new HumanMessage({  content: [    {      type: "image_url",      image_url: {        url: "{imageURL}",        detail: "high",      },    },  ],});const base64ImageMessage = new HumanMessage({  content: [    {      type: "image_url",      image_url: "data:image/jpeg;base64,{base64Image}",    },  ],});const multiModalPrompt = ChatPromptTemplate.fromMessages([  ["system", "You have 20:20 vision! Describe the user's image."],  langchainLogoMessage,  base64ImageMessage,]);const formattedPrompt = await multiModalPrompt.invoke({  imageURL,  base64Image,});console.log(JSON.stringify(formattedPrompt, null, 2));/**{  "kwargs": {    "messages": [      {        "kwargs": {          "content": "You have 20:20 vision! Describe the user's image.",        }      },      {        "kwargs": {          "content": [            {              "type": "image_url",              "image_url": {                "url": "https://avatars.githubusercontent.com/u/126733545?s=200&v=4",                "detail": "high"              }            }          ],        }      },      {        "kwargs": {          "content": [            {              "type": "image_url",              "image_url": "data:image/png;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/4Q..."            }          ],        }      }    ]  }} */

#### API Reference:

*   [HumanMessage](https://api.js.langchain.com/classes/langchain_core_messages.HumanMessage.html) from `@langchain/core/messages`
*   [ChatPromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`

tip

LangSmith will render your images inside traces!

See the LangSmith trace [here](https://smith.langchain.com/public/15f4b4e4-2b2f-476a-952c-b9abcb9ac278/r)

You can also pass multi-modal prompt templates inline:

    import { ChatPromptTemplate } from "@langchain/core/prompts";import fs from "node:fs/promises";const hotdogImage = await fs.readFile("hotdog.jpg");// Convert the image to base64const base64Image = hotdogImage.toString("base64");const imageURL = "https://avatars.githubusercontent.com/u/126733545?s=200&v=4";const multiModalPrompt = ChatPromptTemplate.fromMessages([  ["system", "You have 20:20 vision! Describe the user's image."],  [    "human",    [      {        type: "image_url",        image_url: {          url: "{imageURL}",          detail: "high",        },      },      {        type: "image_url",        image_url: "data:image/jpeg;base64,{base64Image}",      },    ],  ],]);const formattedPrompt = await multiModalPrompt.invoke({  imageURL,  base64Image,});console.log(JSON.stringify(formattedPrompt, null, 2));/**{  "kwargs": {    "messages": [      {        "kwargs": {          "content": "You have 20:20 vision! Describe the user's image.",        }      },      {        "kwargs": {          "content": [            {              "type": "image_url",              "image_url": {                "url": "https://avatars.githubusercontent.com/u/126733545?s=200&v=4",                "detail": "high"              }            },            {              "type": "image_url",              "image_url": {                "url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/4QBWRX...",              }            }          ],        }      }    ]  }} */

#### API Reference:

*   [ChatPromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`

tip

See the LangSmith trace [here](https://smith.langchain.com/public/66bf8258-fa1c-42a3-9e14-9b3eb5902435/r)

* * *

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

[

Previous

Prompts

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

Next

Example selectors

](/v0.1/docs/modules/model_io/prompts/example_selector_types/)

*   [What is a prompt template?](#what-is-a-prompt-template)
*   [Create a prompt template](#create-a-prompt-template)
*   [Chat prompt template](#chat-prompt-template)
*   [Multi-modal prompts](#multi-modal-prompts)

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.