Deprecated

This class will be removed in 0.3.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.

Example

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 full)

Implements

Constructors

Properties

chatHistoryKey: string = "chat_history"
combineDocumentsChain: BaseChain<ChainValues, ChainValues>
inputKey: string = "question"
questionGeneratorChain: LLMChain<string, LLMType>
retriever: BaseRetrieverInterface<Record<string, any>>
returnGeneratedQuestion: boolean = false
returnSourceDocuments: boolean = false
memory?: BaseMemory

Accessors

Methods

  • Parameters

    • inputs: ChainValues[]
    • Optional config: (RunnableConfig | CallbackManager | (BaseCallbackHandler | BaseCallbackHandlerMethodsClass)[])[]

    Returns Promise<ChainValues[]>

    ⚠️ Deprecated ⚠️

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

    This feature is deprecated and will be removed in the future.

    It is not recommended for use.

    Call the chain on all inputs in the list

  • Parameters

    • values: ChainValues & {
          signal?: AbortSignal;
          timeout?: number;
      }
    • Optional config: RunnableConfig | CallbackManager | (BaseCallbackHandler | BaseCallbackHandlerMethodsClass)[]
    • Optional tags: string[]

      Deprecated

    Returns Promise<ChainValues>

    Deprecated

    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.

    • Optional options: RunnableConfig

    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
    • Optional config: RunnableConfig | CallbackManager | (BaseCallbackHandler | BaseCallbackHandlerMethodsClass)[]

    Returns Promise<string>

    Deprecated

    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<any, BaseLanguageModelCallOptions>

      BaseLanguageModelInterface instance used to generate a new question.

    • retriever: BaseRetrieverInterface<Record<string, any>>

      BaseRetrieverInterface instance used to retrieve relevant documents.

    • options: {
          outputKey?: string;
          qaChainOptions?: {
              type?: "stuff";
          } & StuffQAChainParams | {
              type?: "map_reduce";
          } & MapReduceQAChainParams | {
              type?: "refine";
          } & RefineQAChainParams;
          qaTemplate?: string;
          questionGeneratorChainOptions?: {
              llm?: BaseLanguageModelInterface<any, BaseLanguageModelCallOptions>;
              template?: string;
          };
          questionGeneratorTemplate?: string;
          returnSourceDocuments?: boolean;
      } & Omit<ConversationalRetrievalQAChainInput, "retriever" | "combineDocumentsChain" | "questionGeneratorChain"> = {}

    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.

Generated using TypeDoc