!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

*   [Use cases](/v0.1/docs/use_cases/)
    
    *   [SQL](/v0.1/docs/use_cases/sql/)
        
    *   [Chatbots](/v0.1/docs/use_cases/chatbots/)
        
    *   [Extraction](/v0.1/docs/use_cases/extraction/)
        
    *   [Query Analysis](/v0.1/docs/use_cases/query_analysis/)
        
        *   [Quickstart](/v0.1/docs/use_cases/query_analysis/quickstart/)
        *   [Techniques](/v0.1/docs/use_cases/query_analysis/techniques/decomposition/)
            
        *   [How-To Guides](/v0.1/docs/use_cases/query_analysis/how_to/few_shot/)
            
            *   [Add Examples to the Prompt](/v0.1/docs/use_cases/query_analysis/how_to/few_shot/)
            *   [Handle Cases Where No Queries are Generated](/v0.1/docs/use_cases/query_analysis/how_to/no_queries/)
            *   [Handle Multiple Queries](/v0.1/docs/use_cases/query_analysis/how_to/multiple_queries/)
            *   [Handle Multiple Retrievers](/v0.1/docs/use_cases/query_analysis/how_to/multiple_retrievers/)
            *   [Construct Filters](/v0.1/docs/use_cases/query_analysis/how_to/constructing_filters/)
            *   [Deal with High Cardinality Categoricals](/v0.1/docs/use_cases/query_analysis/how_to/high_cardinality/)
    *   [Q&A with RAG](/v0.1/docs/use_cases/question_answering/)
        
    *   [Tool use](/v0.1/docs/use_cases/tool_use/)
        
    *   [Interacting with APIs](/v0.1/docs/use_cases/api/)
    *   [Tabular Question Answering](/v0.1/docs/use_cases/tabular/)
    *   [Graphs](/v0.1/docs/use_cases/graph/)
        
    *   [Summarization](/v0.1/docs/use_cases/summarization/)
    *   [Agent Simulations](/v0.1/docs/use_cases/agent_simulations/)
        
    *   [Autonomous Agents](/v0.1/docs/use_cases/autonomous_agents/)
        
    *   [Code Understanding](/v0.1/docs/use_cases/code_understanding/)
    *   [Audio/Video Structured Extraction](/v0.1/docs/use_cases/media/)

*   [](/v0.1/)
*   [Use cases](/v0.1/docs/use_cases/)
*   [Query Analysis](/v0.1/docs/use_cases/query_analysis/)
*   How-To Guides
*   Add Examples to the Prompt

On this page

Add Examples to the Prompt
==========================

As our query analysis becomes more complex, the LLM may struggle to understand how exactly it should respond in certain scenarios. In order to improve performance here, we can add examples to the prompt to guide the LLM.

Let’s take a look at how we can add examples for the LangChain YouTube video query analyzer we built in the [Quickstart](/v0.1/docs/use_cases/query_analysis/quickstart/).

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

#### Install dependencies[​](#install-dependencies "Direct link to Install dependencies")

tip

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

*   npm
*   yarn
*   pnpm

    npm i @langchain/core zod uuid

    yarn add @langchain/core zod uuid

    pnpm add @langchain/core zod uuid

#### Set environment variables[​](#set-environment-variables "Direct link to Set environment variables")

    # Optional, use LangSmith for best-in-class observabilityLANGSMITH_API_KEY=your-api-keyLANGCHAIN_TRACING_V2=true

Query schema[​](#query-schema "Direct link to Query schema")
------------------------------------------------------------

We’ll define a query schema that we want our model to output. To make our query analysis a bit more interesting, we’ll add a `subQueries` field that contains more narrow questions derived from the top level question.

    import { z } from "zod";const subQueriesDescription = `If the original question contains multiple distinct sub-questions,or if there are more generic questions that would be helpful to answer inorder to answer the original question, write a list of all relevant sub-questions.Make sure this list is comprehensive and covers all parts of the original question.It's ok if there's redundancy in the sub-questions, it's better to cover all the bases than to miss some.Make sure the sub-questions are as narrowly focused as possible in order to get the most relevant results.`;const searchSchema = z.object({  query: z    .string()    .describe("Primary similarity search query applied to video transcripts."),  subQueries: z.array(z.string()).optional().describe(subQueriesDescription),  publishYear: z.number().optional().describe("Year video was published"),});

Query generation[​](#query-generation "Direct link to Query generation")
------------------------------------------------------------------------

### Pick your chat model:

*   OpenAI
*   Anthropic
*   FireworksAI
*   MistralAI

#### Install dependencies

tip

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

*   npm
*   yarn
*   pnpm

    npm i @langchain/openai 

    yarn add @langchain/openai 

    pnpm add @langchain/openai 

#### Add environment variables

    OPENAI_API_KEY=your-api-key

#### Instantiate the model

    import { ChatOpenAI } from "@langchain/openai";const llm = new ChatOpenAI({  model: "gpt-3.5-turbo-0125",  temperature: 0});

#### Install dependencies

tip

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

*   npm
*   yarn
*   pnpm

    npm i @langchain/anthropic 

    yarn add @langchain/anthropic 

    pnpm add @langchain/anthropic 

#### Add environment variables

    ANTHROPIC_API_KEY=your-api-key

#### Instantiate the model

    import { ChatAnthropic } from "@langchain/anthropic";const llm = new ChatAnthropic({  model: "claude-3-sonnet-20240229",  temperature: 0});

#### Install dependencies

tip

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

*   npm
*   yarn
*   pnpm

    npm i @langchain/community 

    yarn add @langchain/community 

    pnpm add @langchain/community 

#### Add environment variables

    FIREWORKS_API_KEY=your-api-key

#### Instantiate the model

    import { ChatFireworks } from "@langchain/community/chat_models/fireworks";const llm = new ChatFireworks({  model: "accounts/fireworks/models/firefunction-v1",  temperature: 0});

#### Install dependencies

tip

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

*   npm
*   yarn
*   pnpm

    npm i @langchain/mistralai 

    yarn add @langchain/mistralai 

    pnpm add @langchain/mistralai 

#### Add environment variables

    MISTRAL_API_KEY=your-api-key

#### Instantiate the model

    import { ChatMistralAI } from "@langchain/mistralai";const llm = new ChatMistralAI({  model: "mistral-large-latest",  temperature: 0});

    import {  ChatPromptTemplate,  MessagesPlaceholder,} from "@langchain/core/prompts";import {  RunnablePassthrough,  RunnableSequence,} from "@langchain/core/runnables";const system = `You are an expert at converting user questions into database queries.You have access to a database of tutorial videos about a software library for building LLM-powered applications.Given a question, return a list of database queries optimized to retrieve the most relevant results.If there are acronyms or words you are not familiar with, do not try to rephrase them.`;const prompt = ChatPromptTemplate.fromMessages([  ["system", system],  new MessagesPlaceholder({    variableName: "examples",    optional: true,  }),  ["human", "{question}"],]);const llmWithTools = llm.withStructuredOutput(searchSchema, {  name: "Search",});const queryAnalyzer = RunnableSequence.from([  {    question: new RunnablePassthrough(),  },  prompt,  llmWithTools,]);

Let’s try out our query analyzer without any examples in the prompt:

    await queryAnalyzer.invoke(  "what's the difference between web voyager and reflection agents? do both use langgraph?");

    {  query: "difference between Web Voyager and Reflection Agents",  subQueries: [ "Do Web Voyager and Reflection Agents use LangGraph?" ]}

Adding examples and tuning the prompt[​](#adding-examples-and-tuning-the-prompt "Direct link to Adding examples and tuning the prompt")
---------------------------------------------------------------------------------------------------------------------------------------

This works pretty well, but we probably want it to decompose the question even further to separate the queries about Web Voyager and Reflection Agents.

To tune our query generation results, we can add some examples of inputs questions and gold standard output queries to our prompt.

    const examples = [];

    const question = "What's chat langchain, is it a langchain template?";const query = {  query: "What is chat langchain and is it a langchain template?",  subQueries: ["What is chat langchain", "What is a langchain template"],};examples.push({ input: question, toolCalls: [query] });

    1

    const question =  "How to build multi-agent system and stream intermediate steps from it";const query = {  query:    "How to build multi-agent system and stream intermediate steps from it",  subQueries: [    "How to build multi-agent system",    "How to stream intermediate steps from multi-agent system",    "How to stream intermediate steps",  ],};examples.push({ input: question, toolCalls: [query] });

    2

    const question = "LangChain agents vs LangGraph?";const query = {  query:    "What's the difference between LangChain agents and LangGraph? How do you deploy them?",  subQueries: [    "What are LangChain agents",    "What is LangGraph",    "How do you deploy LangChain agents",    "How do you deploy LangGraph",  ],};examples.push({ input: question, toolCalls: [query] });

    3

Now we need to update our prompt template and chain so that the examples are included in each prompt. Since we’re working with LLM model function-calling, we’ll need to do a bit of extra structuring to send example inputs and outputs to the model. We’ll create a `toolExampleToMessages` helper function to handle this for us:

    import {  AIMessage,  BaseMessage,  HumanMessage,  SystemMessage,  ToolMessage,} from "@langchain/core/messages";import { v4 as uuidV4 } from "uuid";const toolExampleToMessages = (  example: Example | Record<string, any>): Array<BaseMessage> => {  const messages: Array<BaseMessage> = [    new HumanMessage({ content: example.input }),  ];  const openaiToolCalls = example.toolCalls.map((toolCall) => {    return {      id: uuidV4(),      type: "function" as const,      function: {        name: "search",        arguments: JSON.stringify(toolCall),      },    };  });  messages.push(    new AIMessage({      content: "",      additional_kwargs: { tool_calls: openaiToolCalls },    })  );  const toolOutputs =    "toolOutputs" in example      ? example.toolOutputs      : Array(openaiToolCalls.length).fill(          "You have correctly called this tool."        );  toolOutputs.forEach((output, index) => {    messages.push(      new ToolMessage({        content: output,        tool_call_id: openaiToolCalls[index].id,      })    );  });  return messages;};const exampleMessages = examples.map((ex) => toolExampleToMessages(ex)).flat();

    import {  ChatPromptTemplate,  MessagesPlaceholder,} from "@langchain/core/prompts";import { RunnableSequence } from "@langchain/core/runnables";const queryAnalyzerWithExamples = RunnableSequence.from([  {    question: new RunnablePassthrough(),    examples: () => exampleMessages,  },  prompt,  llmWithTools,]);

    await queryAnalyzerWithExamples.invoke(  "what's the difference between web voyager and reflection agents? do both use langgraph?");

    {  query: "Difference between Web Voyager and Reflection agents, do they both use LangGraph?",  subQueries: [    "Difference between Web Voyager and Reflection agents",    "Do Web Voyager and Reflection agents use LangGraph"  ]}

Thanks to our examples we get a slightly more decomposed search query. With some more prompt engineering and tuning of our examples we could improve query generation even more.

You can see that the examples are passed to the model as messages in the [LangSmith trace](https://smith.langchain.com/public/102829c3-69fc-4cb7-b28b-399ae2c9c008/r).

* * *

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

[

Previous

Structuring

](/v0.1/docs/use_cases/query_analysis/techniques/structuring/)[

Next

Handle Cases Where No Queries are Generated

](/v0.1/docs/use_cases/query_analysis/how_to/no_queries/)

*   [Setup](#setup)
*   [Query schema](#query-schema)
*   [Query generation](#query-generation)
*   [Adding examples and tuning the prompt](#adding-examples-and-tuning-the-prompt)

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.