Comment
03-18-2025
08:05 AM
03-18-2025
08:05 AM
Thanks for the blog post @s-udhaya and @jiayi-wu . This has been very helpful to add filters for our RAG application. But I am having a hard time combining this chain with a chain that includes the conversation history. I originally used the example notebook here 03-advanced-app, 02-advanced-chatbot-chain to build the chain that tracks user message history. Below is the chain I came up with that combines filters and message history. It works when I run it in the notebook with chain.invoke(model_config.get("input_example")) but fails when I try to deploy it to the review app. Do you have an example that combines both the filters and conversation history? Thanks.
# RAG Chain
chain = (
{
"question": itemgetter("messages") | RunnableLambda(extract_user_query_string),
"chat_history": itemgetter("messages") | RunnableLambda(extract_chat_history),
"formatted_chat_history": itemgetter("messages") | RunnableLambda(format_chat_history_for_prompt),
}
| RunnablePassthrough()
| {
"context": RunnableBranch(
(
lambda x: len(x["chat_history"]) > 0,
query_rewrite_prompt | model | StrOutputParser(),
),
itemgetter("question"),
)
| RunnableBranch(
(
# First path: Use configurable_vs_retriever with filters when applicable
lambda input: "configurable" in input.lower(),
RunnableLambda(
lambda input: configurable_vs_retriever.invoke(
input,
config=create_configurable_with_filters({"messages": input}, retriever_config),
)
)
),
# Second path: Default to vector_search_as_retriever
vector_search_as_retriever,
)
| RunnableLambda(format_context),
"formatted_chat_history": itemgetter("formatted_chat_history"),
"question": itemgetter("question"),
}
| prompt
| model
| StrOutputParser()
)