Question about using LCEL RAG for Q&A with chat_history and returning sources #18115
-
Checked other resources
Commit to Help
Example CodeReturning sources: rag_chain_from_docs = (
RunnablePassthrough.assign(context=(lambda x: format_docs(x["context"])))
| prompt
| llm
| StrOutputParser()
)
rag_chain_with_source = RunnableParallel(
{"context": retriever, "question": RunnablePassthrough()}
).assign(answer=rag_chain_from_docs) Chat history: def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
def contextualized_question(input: dict):
if input.get("chat_history"):
return contextualize_q_chain
else:
return input["question"]
rag = (
RunnablePassthrough.assign(
context=contextualized_question | retriever | format_docs
)
|
RunnableParallel({
"context": lambda input: format_docs(input["documents"]),
"question": itemgetter("question"),
})
| qa_prompt
| llm
| StrOutputParser()
) DescriptionHello, Some context into what I've been doing. I've recently starting learning how lang chain works and in particular with RAG. I've got my code working really well with I've started with the (docs)[https://python.langchain.com/docs/use_cases/question_answering/sources] and I understand the overall flow for the basic one in the Quickstart but in the next section re returning sources I think this is the flow:
Essentially what I'd like to do is combine my code for returning_sources with chat_history - at present I have both customised well but Im not sure where the runnable need be modified to allow sources if using the chat_history implementation (2nd example above) Any info on this would be greatly appreciated! System Infopython-dotenv==1.0.0 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
🤖 Based on the code you've shared and the versions of the packages you're using, I can provide some suggestions that might help resolve the issues you're encountering. Firstly, you're using an older version of LangChain. The latest version is v0.1.9. Updating to the latest version might help resolve some of the issues you're experiencing. You can update LangChain by running the following command in your terminal: pip install --upgrade langchain Regarding the issues you're facing with the As for the In terms of returning sources, there was a similar issue in the LangChain repository titled Return Source Documents in LECL. The suggested solution was to define the documents as a global variable and modify the chain to store the documents and extract the source. After running the chain, you can access the # Define documents as a global variable
documents = []
# Define your chain
# ...
# Define your generate_response function
async def generate_response():
input = {"question": data.message}
response = ""
async for token in chain.astream(input=input):
yield token.content
response += token.content
memory.save_context(input, {"output": response})
# Access the documents after running the chain
for document in documents:
print(document) To extract just the # Modify your chain to store the documents and extract the source
context = (
RunnableMap(
{
"question": lambda x: x["question"],
"memory": memory.load_memory_variables,
}
)
| RunnableMap(
{
"context": (
retriever_chain
| _get_k_or_less_documents
| (lambda docs: [documents.append(doc.metadata['source']) for doc in docs] or docs) # Store the source of the documents
| reorder_documents
| format_docs
),
"question": lambda x: x["question"],
"chat_history": lambda x: x["memory"]["chat_history"],
}
)
) I hope this helps! If you're still encountering issues, could you please provide more details about the issues you're facing? For example, any error messages you're receiving would be very helpful. Sources
|
Beta Was this translation helpful? Give feedback.
🤖
To add document sources to your code without modifying the context, you can create a new function that formats the documents along with their sources. This function can then be used in the
RunnablePassthrough.assign
method. Here's an example of how you can do this:First, modify your
format_docs
function to include the document sources:In this function,
doc.source
should contain the source of the document. You'll need to ensure that your documents have asource
attribute that contains this information.Next, use this new function in your
RunnablePassthrough.assign
call: