How to assign a specific llm field to an output key #25896
Replies: 1 comment 2 replies
-
Hello there, @santiagovasquez1! 👋 I'm here to assist you with any bugs, questions, or contributions you might have. I'm a bot designed to help you while waiting for a human maintainer. To create a sequential chain where you first invoke
Here is an example of how you can achieve this: from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
# Define the RAG chain
RAG_TEMPLATE = """
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>
{context}
</context>
Answer the following question:
{question}"""
rag_prompt = ChatPromptTemplate.from_template(RAG_TEMPLATE)
llm = ChatOpenAI(model="gpt-4")
rag_chain = (
RunnablePassthrough.assign(context=lambda input: format_docs(input["context"]))
| rag_prompt
| llm
| StrOutputParser()
)
# Define the summary chain
summary_prompt = ChatPromptTemplate.from_template(
"Summarize the following answer: {answer}"
)
summary_chain = (
RunnablePassthrough.assign(answer=lambda input: input["answer"])
| summary_prompt
| llm
| StrOutputParser()
)
# Combine the chains
combined_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| rag_chain
| RunnablePassthrough.assign(answer=lambda output: output["answer"])
| summary_chain
)
# Invoke the combined chain
question = "What are the approaches to Task Decomposition?"
result = combined_chain.invoke(question)
print(result) In this example:
This approach leverages the concepts of Retrieval Augmented Generation (RAG) as described in the LangChain documentation, where the retrieval and generation steps are combined to answer questions based on specific source information [1][2][3]. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I want to create a sequential chain in which first invoke rag_chain and then pass to summary_chain, how can I pass the rag_chain[answer] field as an inpute of summary_chain, I have tried with
but when I do it in the output field system_message comes all the rag_chain dictionary and not only the answer
System Info
langchain==0.2.14
langchain-cli==0.0.30
langchain-community==0.2.12
langchain-core==0.2.35
langchain-experimental==0.0.64
langchain-openai==0.1.22
langchain-text-splitters==0.2.2
Beta Was this translation helpful? Give feedback.
All reactions