Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: Conversational RAG example skips document retrieval, if chat_history is not provided #6829

Open
2 tasks done
anorderh opened this issue Sep 17, 2024 · 1 comment
Open
2 tasks done
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@anorderh
Copy link

Checklist

  • I added a very descriptive title to this issue.
  • I included a link to the documentation page I am referring to (if applicable).

Issue with current documentation:

https://github.com/langchain-ai/langchainjs/blob/666dee7b6519877df094b3ef49cbc6f84078e8bf/docs/core_docs/docs/how_to/qa_chat_history_how_to.ipynb
Lines 323 to 341

The code below is from documentation on how to implement conversational RAG at https://js.langchain.com/docs/tutorials/qa_chat_history/#contextualizing-the-question. This specific section is related to managing chat history.

Just for reference:

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", "{question}"],
]);
const contextualizeQChain = contextualizeQPrompt
  .pipe(llm)
  .pipe(new StringOutputParser());

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.

{context}`;

const qaPrompt = ChatPromptTemplate.fromMessages([
  ["system", qaSystemPrompt],
  new MessagesPlaceholder("chat_history"),
  ["human", "{question}"],
]);

The issue:

const contextualizedQuestion = (input: Record<string, unknown>) => {
  if ("chat_history" in input) {
    return contextualizeQChain;
  }
  return input.question;
};

const ragChain = RunnableSequence.from([
  RunnablePassthrough.assign({
    context: async (input: Record<string, unknown>) => {
      if ("chat_history" in input) {
        const chain = contextualizedQuestion(input);
        return chain.pipe(retriever).pipe(formatDocumentsAsString);
      }
      return "";
    },
  }),
  qaPrompt,
  llm
]);

Please refer to the multiple if statements for chat_history and then to the assigning of context inside the RunnableSequence. chain.pipe(retreiver) is pulling Documents to be used for RAG, but these documents are only pulled so long as chat_history is provided.

This seems incorrect, bc even if input provided did not have any chat history, I would still expect it to use RAG and pull documents from our retriever. But here, if there's no history, I assume RAG is not being orchestrated.

Idea or request for content:

Change RunnablePassthrough assignment so that context is atleast assigned the most related documents, even if chat_history is not present.

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Sep 17, 2024
@anorderh
Copy link
Author

anorderh commented Sep 17, 2024

This is my attempt for a fix, allowing input.question to be piped and removing the embedded chat_history check.

const contextualizedQuestion = (input: Record<string, unknown>) => {
  if ("chat_history" in input) {
    return contextualizeQChain;
  }
  return new RunnablePassthrough().bind(input.question);
};

const ragChain = RunnableSequence.from([
  RunnablePassthrough.assign({
    context: async (input: Record<string, unknown>) => {
      return contextualizedQuestion(input)
         .pipe(retriever)
         .pipe(formatDocumentsAsString)
    },
  }),
  qaPrompt,
  llm
]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant