How to use runnable map in current version of langchain #26761
Replies: 1 comment 10 replies
-
Hey @aa31089907! 👋 I'm here to help you with any bugs, questions, or contributions you have. I'm a bot designed to assist you while waiting for a human maintainer. Let's tackle this together! To use Here is the updated implementation with the correct imports: from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from langchain.schema.runnable import RunnableParallel
def map_reduce_chain_function():
# Map Template with input variable
map_template = """Write a concise summary of the following content:
{content}
Summary:
"""
map_prompt = PromptTemplate(
input_variables=["content"],
template=map_template
)
# LLM - ChatOpenAI
llm = ChatOpenAI()
# Map Chain (Runnable)
map_chain = map_prompt | llm | StrOutputParser()
# Reduce Template with input variable
reduce_template = """The following is a set of summaries:
{doc_summaries}
Summarize the above summaries with all the key details.
Summary:
"""
reduce_prompt = PromptTemplate(
input_variables=["doc_summaries"],
template=reduce_template
)
# Reduce Chain (Runnable)
reduce_chain = reduce_prompt | llm | StrOutputParser()
# Combine Map and Reduce Chains
map_reduce_chain = RunnableParallel(
{
"map": map_chain,
"reduce": reduce_chain
}
)
return map_reduce_chain This code snippet demonstrates how to use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
Is the above implementation is correct for latest version of langchain, also mention all the latest import like this below
from langchain.runnables import RunnableMap, RunnableLambda
also the code is for summarizer, is it correct?
System Info
langchain==0.3.0
langchain-community==0.3.0
langchain-core==0.3.5
langchain-openai==0.2.0
langchain-text-splitters==0.3.0
langchainhub==0.1.21
ubuntu
Beta Was this translation helpful? Give feedback.
All reactions