*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to handle cases where no queries are generated

On this page

How to handle cases where no queries are generated
==================================================

Prerequisites

This guide assumes familiarity with the following:

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

Sometimes, a query analysis technique may allow for any number of queries to be generated - including no queries! In this case, our overall chain will need to inspect the result of the query analysis before deciding whether to call the retriever or not.

We will use mock data for this example.

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"];const embeddings = new OpenAIEmbeddings({ model: "text-embedding-3-small" });const vectorstore = await Chroma.fromTexts(texts, {}, embeddings, {  collectionName: "harrison",});const retriever = vectorstore.asRetriever(1);

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

We will use function calling to structure the output. However, we will configure the LLM such that is doesn’t NEED to call the function representing a search query (should it decide not to). We will also then use a prompt to do query analysis that explicitly lays when it should and shouldn’t make a search.

    import { z } from "zod";const searchSchema = z.object({  query: z.string().describe("Similarity search query applied to job record."),});

### 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 { zodToJsonSchema } from "zod-to-json-schema";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.You do not NEED to look things up. If you don't need to, then just respond normally.`;const prompt = ChatPromptTemplate.fromMessages([  ["system", system],  ["human", "{question}"],]);const llmWithTools = llm.bind({  tools: [    {      type: "function" as const,      function: {        name: "search",        description: "Search over a database of job records.",        parameters: zodToJsonSchema(searchSchema),      },    },  ],});const queryAnalyzer = RunnableSequence.from([  {    question: new RunnablePassthrough(),  },  prompt,  llmWithTools,]);

We can see that by invoking this we get an message that sometimes - but not always - returns a tool call.

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

    AIMessage {  lc_serializable: true,  lc_kwargs: {    content: "",    additional_kwargs: {      function_call: undefined,      tool_calls: [        {          id: "call_uqHm5OMbXBkmqDr7Xzj8EMmd",          type: "function",          function: [Object]        }      ]    }  },  lc_namespace: [ "langchain_core", "messages" ],  content: "",  name: undefined,  additional_kwargs: {    function_call: undefined,    tool_calls: [      {        id: "call_uqHm5OMbXBkmqDr7Xzj8EMmd",        type: "function",        function: { name: "search", arguments: '{"query":"Harrison"}' }      }    ]  }}

    await queryAnalyzer.invoke("hi!");

    AIMessage {  lc_serializable: true,  lc_kwargs: {    content: "Hello! How can I assist you today?",    additional_kwargs: { function_call: undefined, tool_calls: undefined }  },  lc_namespace: [ "langchain_core", "messages" ],  content: "Hello! How can I assist you today?",  name: undefined,  additional_kwargs: { function_call: undefined, tool_calls: undefined }}

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

So how would we include this in a chain? Let’s look at an example below.

    import { JsonOutputKeyToolsParser } from "@langchain/core/output_parsers/openai_tools";const outputParser = new JsonOutputKeyToolsParser({  keyName: "search",});

    import { RunnableConfig, RunnableLambda } from "@langchain/core/runnables";const chain = async (question: string, config?: RunnableConfig) => {  const response = await queryAnalyzer.invoke(question, config);  if (    "tool_calls" in response.additional_kwargs &&    response.additional_kwargs.tool_calls !== undefined  ) {    const query = await outputParser.invoke(response, config);    return retriever.invoke(query[0].query, config);  } else {    return response;  }};const customChain = new RunnableLambda({ func: chain });

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

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

    await customChain.invoke("hi!");

    AIMessage {  lc_serializable: true,  lc_kwargs: {    content: "Hello! How can I assist you today?",    additional_kwargs: { function_call: undefined, tool_calls: undefined }  },  lc_namespace: [ "langchain_core", "messages" ],  content: "Hello! How can I assist you today?",  name: undefined,  additional_kwargs: { function_call: undefined, tool_calls: undefined }}

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

You’ve now learned some techniques for handling irrelevant questions in query analysis systems.

Next, check out some of the other query analysis guides in this section, like [how to use few-shot examples](/v0.2/docs/how_to/query_few_shot).

* * *

#### 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 handle multiple retrievers

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

Next

How to recursively split text by characters

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

*   [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)