*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to handle multiple queries

On this page

How to handle multiple queries
==============================

Prerequisites

This guide assumes familiarity with the following:

*   [Query analysis](/v0.2/docs/tutorials/query_analysis)

Sometimes, a query analysis technique may allow for multiple queries to be generated. In these cases, we need to remember to run all queries and then to combine the results. We will show a simple example (using mock data) of how to do that.

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.2/docs/how_to/installation#installing-integration-packages).

*   npm
*   yarn
*   pnpm

    npm i @langchain/community @langchain/openai zod chromadb

    yarn add @langchain/community @langchain/openai zod chromadb

    pnpm add @langchain/community @langchain/openai zod chromadb

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

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

### Create Index[​](#create-index "Direct link to Create Index")

We will create a vectorstore over fake information.

    import { Chroma } from "@langchain/community/vectorstores/chroma";import { OpenAIEmbeddings } from "@langchain/openai";import "chromadb";const texts = ["Harrison worked at Kensho", "Ankush worked at Facebook"];const embeddings = new OpenAIEmbeddings({ model: "text-embedding-3-small" });const vectorstore = await Chroma.fromTexts(texts, {}, embeddings, {  collectionName: "multi_query",});const retriever = vectorstore.asRetriever(1);

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

We will use function calling to structure the output. We will let it return multiple queries.

    import { z } from "zod";const searchSchema = z  .object({    queries: z.array(z.string()).describe("Distinct queries to search for"),  })  .describe("Search over a database of job records.");

### Pick your chat model:

*   OpenAI
*   Anthropic
*   FireworksAI
*   MistralAI
*   Groq
*   VertexAI

#### 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",  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});

#### 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/groq 

    yarn add @langchain/groq 

    pnpm add @langchain/groq 

#### Add environment variables

    GROQ_API_KEY=your-api-key

#### Instantiate the model

    import { ChatGroq } from "@langchain/groq";const llm = new ChatGroq({  model: "mixtral-8x7b-32768",  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/google-vertexai 

    yarn add @langchain/google-vertexai 

    pnpm add @langchain/google-vertexai 

#### Add environment variables

    GOOGLE_APPLICATION_CREDENTIALS=credentials.json

#### Instantiate the model

    import { ChatVertexAI } from "@langchain/google-vertexai";const llm = new ChatVertexAI({  model: "gemini-1.5-pro",  temperature: 0});

    import { ChatPromptTemplate } from "@langchain/core/prompts";import {  RunnableSequence,  RunnablePassthrough,} from "@langchain/core/runnables";const system = `You have the ability to issue search queries to get information to help answer user information.If you need to look up two distinct pieces of information, you are allowed to do that!`;const prompt = ChatPromptTemplate.fromMessages([  ["system", system],  ["human", "{question}"],]);const llmWithTools = llm.withStructuredOutput(searchSchema, {  name: "Search",});const queryAnalyzer = RunnableSequence.from([  {    question: new RunnablePassthrough(),  },  prompt,  llmWithTools,]);

We can see that this allows for creating multiple queries

    await queryAnalyzer.invoke("where did Harrison Work");

    { queries: [ "Harrison" ] }

    await queryAnalyzer.invoke("where did Harrison and ankush Work");

    { queries: [ "Harrison work", "Ankush work" ] }

Retrieval with query analysis[​](#retrieval-with-query-analysis "Direct link to Retrieval with query analysis")
---------------------------------------------------------------------------------------------------------------

So how would we include this in a chain? One thing that will make this a lot easier is if we call our retriever asyncronously - this will let us loop over the queries and not get blocked on the response time.

    import { RunnableConfig, RunnableLambda } from "@langchain/core/runnables";const chain = async (question: string, config?: RunnableConfig) => {  const response = await queryAnalyzer.invoke(question, config);  const docs = [];  for (const query of response.queries) {    const newDocs = await retriever.invoke(query, config);    docs.push(...newDocs);  }  // You probably want to think about reranking or deduplicating documents here  // But that is a separate topic  return docs;};const customChain = new RunnableLambda({ func: chain });

    await customChain.invoke("where did Harrison Work");

    [ Document { pageContent: "Harrison worked at Kensho", metadata: {} } ]

    await customChain.invoke("where did Harrison and ankush Work");

    [  Document { pageContent: "Harrison worked at Kensho", metadata: {} },  Document { pageContent: "Ankush worked at Facebook", metadata: {} }]

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

You’ve now learned some techniques for handling multiple queries in a query analysis system.

Next, check out some of the other query analysis guides in this section, like [how to deal with cases where no query is generated](/v0.2/docs/how_to/query_no_queries).

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to deal with high cardinality categorical variables

](/v0.2/docs/how_to/query_high_cardinality)[

Next

How to handle multiple retrievers

](/v0.2/docs/how_to/query_multiple_retrievers)

*   [Setup](#setup)
    *   [Install dependencies](#install-dependencies)
    *   [Set environment variables](#set-environment-variables)
    *   [Create Index](#create-index)
*   [Query analysis](#query-analysis)
*   [Retrieval with query analysis](#retrieval-with-query-analysis)
*   [Next steps](#next-steps)