AI Hints
Home›LangChain Tutorial›Inside Chains and Practice
LangChain · Lesson 26 of 120

Practice: Review Summariser

A small chain.

⏱ 2 min read▶ Video at 27:37🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

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.

Practice: Review Summariser
From the video: Practice: Review Summariser
  • Customer reviews
  • A summary and a mood
  • Pause and try

One possible solution

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_model
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableParallel

model = 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'}

What we used

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.

keyvalue
Templateswith blanks
Pipesprompt | model | parser
Parserclean strings
Parallelboth at once

Summary

A few runnables piped together make a useful tool. Next, a quiz on chains.

Key takeaways
  • Join the inputs
  • Two chains in parallel
  • One dictionary out
Next up: Chains Quiz — test yourself