05. 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.

Copy

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

# Load API key information
load_dotenv()

Copy

 True 

Copy

# 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")

Copy

Copy

Copy

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.

Copy

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

Copy

Copy

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.

Copy

Copy

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

Copy

Copy

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.

Copy

Copy

Copy

Copy

Copy

Copy

Last updated