05. RunnableParallel

RunnableParallel

Input and output manipulation

RunnableParallel One within the silver sequence Runnable Next output Runnable It can be useful for manipulating to match the input format of.

The input to the prompt here is expected in the form of a map with the keys "context" and "question".

User input is simply a question. So you need to use retriever to get the context, and pass the user input under the "question" key.

# Configuration file for managing API keys as environment variables.
from dotenv import load_dotenv

# Load API key information
load_dotenv()
 True 
# LangSmith set up tracking. https://smith.langchain.com
# !pip install langchain-teddynote
from langchain_teddynote import logging

# Enter a project name.
logging.langsmith("LCEL-Advanced")
 Start tracking LangSmith. 
[Project name] 
LCEL-Advanced 

Different Runnable with RunnableParallel When configuring, type conversion is handled automatically RunnableParallel Please note that there is no need to separately wrap the dict input injected as input in the class.

The trivalent methods below all handle the same.

Using itemgetter as shortcut

Python when combined with RunnableParallel itemgetter You can extract data from map using shortcuts.

In the example below itemgetter Extract a specific key from the map using

Step by step understanding of parallelism

RunnableParallel Using multiple Runnable Run in parallel, Runnable It becomes easier to return the output of to the map.

As shown below, it can be run regardless of the variable in the input template with chain stars.

Parallel processing

RunnableParallel Each on a silver map Runnable Because it runs in parallel, it is also useful for running independent processes in parallel.

For example, we looked ahead area_chain , capital_chain , map_chain silver map_chain Despite running both of these different chains Almost the same run time You can check that there is.

Last updated