Overview

4 Building a research summarization engine

This chapter walks through building an end-to-end research summarization engine that turns an open-ended question into a structured report. The system automates the manual workflow of searching the web, reading sources, extracting key points, and synthesizing findings. It starts from a simple, sequential prototype and evolves into a robust, chain-based solution using LangChain and its Expression Language (LCEL), highlighting an architecture that includes query rewriting, web search, page scraping, per-page summarization, and final report generation. Along the way, it emphasizes clarity of prompts, modularity, and practical considerations like performance, deployment options, and cost awareness.

After a brief project setup in a Python environment, the chapter implements core capabilities: a web search function (via a DuckDuckGo wrapper), a web scraper using requests and Beautiful Soup, an OpenAI chat model client initializer, and a small JSON-to-object utility. Prompt engineering underpins the system’s accuracy and structure. A few-shot assistant selector returns a JSON blob describing the most relevant expert persona and its instructions; a web-search prompt rewrites the user’s question into multiple targeted queries to broaden coverage and disambiguate intent; a page-summary prompt extracts concise, factual answers or summaries from scraped text; and a final report prompt synthesizes all summaries into a detailed, well-structured response. Configuration constants (for number of queries, results per query, and text truncation) let developers balance depth, runtime, and token costs.

The initial sequential engine follows a clear flow: select the assistant, generate multiple queries, fetch result URLs, scrape each page, summarize each page with respect to its query, merge the summaries, and produce the final report. To address latency and scale, the chapter then reimplements the pipeline with LCEL as a set of composable mini-chains: Assistant Instructions, Web Searches, Search and Summarization (itself three sub-chains for URLs, text-and-summary, and merged summaries), and a final Research Report chain. Using LCEL operators—such as RunnableLambda, RunnableParallel, StrOutputParser, and map()—the design parallelizes work at two levels: across search queries and across URLs within each query. The result is a faster, cleaner, and more maintainable research engine inspired by established open-source patterns, and ready for experimentation or extension.

Semi-automated summarization with LLM: You prompt ChatGPT to summarize each web search result and then compile them into a consolidated summary.
Automated research summarization engine: Ask a question, and the engine performs a web search, returns URLs, scrapes and summarizes web pages, and compiles a research report for you.
Automated research summarization engine: Ask a question, and the engine performs a web search, returns URLs, scrapes and summarizes web pages, and compiles a research report for you.
Revised system architecture diagram, incorporating query rewriting: The process begins by tasking the LLM to generate a specified number of queries based on the user's research question. These queries are then submitted to the search engine. The subsequent processing remains consistent with the illustration in figure 4.2.
Architecture of the chain-based research summarization engine: Each step of the process is re-implemented as a mini-chain; all mini-chains are assembled into a master Web Research chain.
The Search and Summarization chain is made up of three chains: a web search chain, a scraping and summarization chain, and a summary compilation chain.
Chain architectural diagram showing how parallelization is applied in the research summarization engine, highlighting the execution of separate chain instances in parallel for efficiency.
Enhanced Search and Summarization chain diagram: The Search Result URLs chain generates multiple URLs; each URL initiates an instance of the Result Text and Summary chain, which in turn produces a corresponding summary. These summaries are then consolidated into a single text string by the Joined Summary chain.
Web Research chain: This illustrates how each web search initiated by the Web Searches chain triggers a separate instance of the Search and Summarization chain. These instances operate concurrently, each producing a summary. These summaries are then compiled by the Research Report chain into the comprehensive final research report.

Summary

  • A research summarization engine is built around an LLM for efficient processing.
  • The engine's workflow involves:
    • Receiving a research question from the user.
    • Generating web searches to gather relevant information with LLM assistance.
    • Summarizing the content from each search result using the LLM.
    • Combining these summaries into a detailed report.
  • This engine can be developed using Visual Studio Code.
  • Key components of the engine include web searching, web scraping, and LLM interaction.
  • Prompt engineering is critical in three areas:
    • Crafting web searches from the user's question.
    • Summarizing search results.
    • Creating the final report.
  • LangChain Expression Language (LCEL) helps organize the workflow into a main chain with secondary chains.
  • Various techniques can parallelize the execution of these secondary chains for efficiency.

FAQ

What is a research summarization engine and what problems does it solve?An LLM-powered application that takes a user question, performs web searches, scrapes relevant pages, summarizes each, and compiles a final report. It automates what you’d normally do manually: search, read, take notes, and synthesize findings, improving speed and consistency for a wide range of research tasks.
What are the core components I need to implement first?Three essentials: a web searching function (DuckDuckGo wrapper in LangChain), a web scraping function (requests + BeautifulSoup) to extract page text, and an LLM client initializer (e.g., ChatOpenAI via langchain_openai). A small utility to safely parse JSON from model outputs into Python objects (to_obj) is also used frequently.
How does the web search integration work in this chapter?It uses LangChain’s DuckDuckGoSearchAPIWrapper to run a query and return result URLs from the link field. You control the number of results per query and later flatten and pair each URL with its originating search query for downstream scraping and summarization.
How is web scraping implemented and how do we handle noisy content?Scraping is done with requests and BeautifulSoup, sending a realistic User-Agent header and a timeout. The raw page text can be noisy (menus, navigation, etc.), but the summarization prompt is designed to extract relevant facts from that noise, so heavy pre-cleaning isn’t required for the basic version.
What is “query rewriting” and why is it added to the architecture?Query rewriting (multiple-query generation) uses the LLM to expand or clarify the original question into several targeted web search queries. It improves coverage and relevance, disambiguates short or vague questions, and can decompose complex topics into simpler searches—resulting in better sources and higher-quality summaries.
How are prompts structured for this engine?Four prompt types: assistant selection (chooses a persona and returns JSON with assistant_type, assistant_instructions, user_question), web search generation (produces multiple queries as JSON), per-page summarization (answers the search query from scraped text or summarizes if not answerable), and final research report (synthesizes all summaries into a long, structured, source-cited report).
Why return JSON from the LLM and how is it parsed?JSON enforces predictable, machine-readable outputs that are easy to route into subsequent steps. The chapter uses a lightweight to_obj helper (json.loads with fallback) and, in LCEL chains, StrOutputParser to extract the content string before conversion to dicts/lists.
How does the initial sequential implementation work end-to-end?Flow: select assistant and instructions → generate multiple search queries → fetch result URLs for each query → scrape each URL → summarize each page → merge summaries and sources → produce the final report. It’s straightforward to understand and debug but slower because every step runs in sequence.
What is LCEL and how does it speed up the engine?LCEL (LangChain Expression Language) composes pipelines from runnables and enables parallelism, streaming, and clean composition. The chapter builds mini-chains (assistant instructions, web searches, search-and-summarization) and a master Web Research chain, using RunnableLambda, RunnableParallel, and the map operator to parallelize per-query and per-URL work for significant speed gains.
How do I set up, configure, and control cost?Set up a Python project (VS Code, virtual environment), install packages (langchain, langchain_openai, requests, bs4, duckduckgo-search, etc.), and put your OpenAI API key in a .env file. Control cost and latency by tuning NUM_SEARCH_QUERIES, results per query, and max characters per page; more pages and tokens yield better coverage but higher cost and longer runtime. You can start with a CLI script and later expose the chain as a REST API if desired.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • AI Agents and Applications ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • AI Agents and Applications ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • AI Agents and Applications ebook for free