Time to practise. Riverside Books has customer reviews for a book. Build a chain that takes the reviews, and returns a short summary and an overall mood, side by side. Pause, and try it yourself first.
Here's one solution. The reviews are joined into one string. A runnable parallel runs two small chains on it. One writes a one sentence summary. The other gives a single word mood. Run it. Both results come back in one dictionary, ready to show on the book's page.
reviews.pypython
from langchain.chat_models import init_chat_modelfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParserfrom langchain_core.runnables import RunnableParallelmodel = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)ask = lambda t: ChatPromptTemplate.from_template(t) | model | StrOutputParser()reviews = ["Loved the characters!", "Slow start, great ending.", "Too long for me."]chain = RunnableParallel( summary=ask("Summarise these reviews in one sentence:\n{reviews}"), mood=ask("One word for the overall mood of these reviews:\n{reviews}"),)print(chain.invoke({"reviews": "\n".join(reviews)}))
Output
{'summary': 'While readers enjoyed the fantastic characters and rewarding ending, some
found the book too long and slow to start.', 'mood': 'Mixed'}
This small solution uses most of what we've learned. Prompt templates with blanks. Pipes, to make chains. A string parser, for clean text. And a runnable parallel, so both answers are made at the same time. That's the core of LangChain.