langchain.js
    Preparing search index...

    Class ConversationalRetrievalQAChain

    This class will be removed in 1.0.0. See below for an example implementation using createRetrievalChain.

    Class for conducting conversational question-answering tasks with a retrieval component. Extends the BaseChain class and implements the ConversationalRetrievalQAChainInput interface.

    import { ChatAnthropic } from "@langchain/anthropic";
    import {
    ChatPromptTemplate,
    MessagesPlaceholder,
    } from "@langchain/core/prompts";
    import { BaseMessage } from "@langchain/core/messages";
    import { createStuffDocumentsChain } from "langchain/chains/combine_documents";
    import { createHistoryAwareRetriever } from "langchain/chains/history_aware_retriever";
    import { createRetrievalChain } from "langchain/chains/retrieval";

    const retriever = ...your retriever;
    const llm = new ChatAnthropic();

    // Contextualize question
    const contextualizeQSystemPrompt = `
    Given a chat history and the latest user question
    which might reference context in the chat history,
    formulate a standalone question which can be understood
    without the chat history. Do NOT answer the question, just
    reformulate it if needed and otherwise return it as is.`;
    const contextualizeQPrompt = ChatPromptTemplate.fromMessages([
    ["system", contextualizeQSystemPrompt],
    new MessagesPlaceholder("chat_history"),
    ["human", "{input}"],
    ]);
    const historyAwareRetriever = await createHistoryAwareRetriever({
    llm,
    retriever,
    rephrasePrompt: contextualizeQPrompt,
    });

    // Answer question
    const qaSystemPrompt = `
    You are an assistant for question-answering tasks. Use
    the following pieces of retrieved context to answer the
    question. If you don't know the answer, just say that you
    don't know. Use three sentences maximum and keep the answer
    concise.
    \n\n
    {context}`;
    const qaPrompt = ChatPromptTemplate.fromMessages([
    ["system", qaSystemPrompt],
    new MessagesPlaceholder("chat_history"),
    ["human", "{input}"],
    ]);

    // Below we use createStuffDocuments_chain to feed all retrieved context
    // into the LLM. Note that we can also use StuffDocumentsChain and other
    // instances of BaseCombineDocumentsChain.
    const questionAnswerChain = await createStuffDocumentsChain({
    llm,
    prompt: qaPrompt,
    });

    const ragChain = await createRetrievalChain({
    retriever: historyAwareRetriever,
    combineDocsChain: questionAnswerChain,
    });

    // Usage:
    const chat_history: BaseMessage[] = [];
    const response = await ragChain.invoke({
    chat_history,
    input: "...",
    });

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    Properties

    chatHistoryKey: string = "chat_history"
    combineDocumentsChain: BaseChain
    inputKey: string = "question"
    memory?: any
    questionGeneratorChain: LLMChain
    retriever: BaseRetrieverInterface
    returnGeneratedQuestion: boolean = false
    returnSourceDocuments: boolean = false

    Accessors

    • get inputKeys(): string[]

      Returns string[]

    • get lc_namespace(): string[]

      Returns string[]

    • get outputKeys(): string[]

      Returns string[]

    Methods

    • Return the string type key uniquely identifying this class of chain.

      Returns string

    • Parameters

      • values: any

      Returns Promise<any>

    • Parameters

      • inputs: ChainValues[]
      • Optionalconfig: any[]

      Returns Promise<ChainValues[]>

      Use .batch() instead. Will be removed in 0.2.0.

      Call the chain on all inputs in the list

    • Parameters

      • values: any
      • Optionalconfig: any
      • Optionaltags: string[]

      Returns Promise<ChainValues>

      Use .invoke() instead. Will be removed in 0.2.0.

      Run the core logic of this chain and add to output if desired.

      Wraps _call and handles memory.

    • Invoke the chain with the provided input and returns the output.

      Parameters

      • input: ChainValues

        Input values for the chain run.

      • Optionaloptions: any

      Returns Promise<ChainValues>

      Promise that resolves with the output of the chain run.

    • Parameters

      • inputs: Record<string, unknown>
      • outputs: Record<string, unknown>
      • returnOnlyOutputs: boolean = false

      Returns Promise<Record<string, unknown>>

    • Parameters

      • input: any
      • Optionalconfig: any

      Returns Promise<string>

      Use .invoke() instead. Will be removed in 0.2.0.

    • Static method to create a new ConversationalRetrievalQAChain from a BaseLanguageModel and a BaseRetriever.

      Parameters

      • llm: BaseLanguageModelInterface

        BaseLanguageModelInterface instance used to generate a new question.

      • retriever: BaseRetrieverInterface

        BaseRetrieverInterface instance used to retrieve relevant documents.

      • options: {
            outputKey?: string;
            qaChainOptions?: QAChainParams;
            qaTemplate?: string;
            questionGeneratorChainOptions?: { llm?: any; template?: string };
            questionGeneratorTemplate?: string;
            returnSourceDocuments?: boolean;
        } & Omit<
            ConversationalRetrievalQAChainInput,
            "retriever"
            | "combineDocumentsChain"
            | "questionGeneratorChain",
        > = {}
        • OptionaloutputKey?: string
        • OptionalqaChainOptions?: QAChainParams

          QAChainParams used to initialize the QA chain used as the second internal step

        • OptionalqaTemplate?: string

          Pass in qaChainOptions.prompt instead

        • OptionalquestionGeneratorChainOptions?: { llm?: any; template?: string }

          Options to initialize the standalone question generation chain used as the first internal step

        • OptionalquestionGeneratorTemplate?: string

          Pass in questionGeneratorChainOptions.template instead

        • OptionalreturnSourceDocuments?: boolean

          Whether to return source documents in the final output

        • returnSourceDocuments

          Whether to return source documents in the final output

        • questionGeneratorChainOptions

          Options to initialize the standalone question generation chain used as the first internal step

        • qaChainOptions

          QAChainParams used to initialize the QA chain used as the second internal step

      Returns ConversationalRetrievalQAChain

      A new instance of ConversationalRetrievalQAChain.

    • Static method to convert the chat history input into a formatted string.

      Parameters

      • chatHistory: string | BaseMessage[] | string[][]

        Chat history input which can be a string, an array of BaseMessage instances, or an array of string arrays.

      Returns string

      A formatted string representing the chat history.

    • Returns string