liveBook · Manning

Chapter 4: Building a research summarization engine

What you will learn in chapter 4 of AI Agents and Applications

The introduction frames a research summarization engine as an LLM application that turns a user query and web search results into a summary.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction frames a research summarization engine as an LLM application that turns a user query and web search results into a summary.

Key terms: research summarization engine, user queries, web searches

4.1 Overview of a research summarization engine

The section contrasts manually using an LLM to summarize web pages with an engine that automates research searches, summaries, and a final report.

Key terms: semiautomated summarization, research summarization engine, consolidated summary, final report

4.2 Setting up the project

This section sets up a VS Code Python workspace and virtual environment for the chapter, then configures VS Code to run and debug code with that environment.

Key terms: VS Code, Python extension, ch04, terminal, virtual environment, required packages, run configuration, launch.json, env_ch04, python field

4.3 Implementing the core functionality

Identifies the capabilities the research summarization engine needs before its core functionality can be implemented.

Key terms: research summarization engine, web searches, extracting text, LLM client

4.3.1 Implementing web searching

This section introduces a DuckDuckGo web-search wrapper, shows how to test the search function, and explains why that wrapper was chosen.

Key terms: DuckDuckGo, results method, link, web_searching_try.py, DuckDuckGoSearchAPIWrapper, TavilySearchResults, GoogleSearchAPIWrapper, API key

4.3.2 Implementing web scraping

This section introduces scraping pages from web search results with Beautiful Soup and explains why irrelevant scraped content is acceptable at this stage.

Key terms: Beautiful Soup, web scraping, result list, LLM

4.3.3 Instantiating the LLM client

This subsection prepares the OpenAI API key and a client-instantiation function for the research summarization engine.

Key terms: OpenAI GPT-5 nano, OpenAI API key, .env, .env_example, llm_models.py

4.3.4 JSON to Python object converter

This subsection introduces a utility for converting LLM-generated JSON text into Python data, with a fallback for malformed JSON.

Key terms: JSON text, Python object, dictionary, list, utilities.py

4.4 Enhancing the architecture with query rewriting

This section adds LLM-generated search queries to the research engine and explains why the extra searches may improve results while increasing sequential response time.

Key terms: query rewriting, multiple query generation, targeted searches, sequential processing, response time, parallelization

4.5 Prompt engineering

This section previews prompt engineering for the research summarization engine before its building blocks are assembled.

4.5.1 Crafting web search prompts

The section explains how two prompts prepare a research assistant’s instructions and web search queries from a user’s question.

Key terms: persona, research assistant, few-shot prompt, JSON, {assistant_instructions}, web search prompt, user question

4.5.2 Crafting summarization prompts

4.5.3 Research report prompt

4.6 Initial implementation

Introduces the initial, easy-to-follow implementation of the research summarization engine and previews the workflow and later LCEL-based improvements.

4.6.1 Importing functions and prompt templates

4.6.2 Setting constants and input variables

This subsection sets the research engine’s search-volume configuration and identifies the user’s research question as its sole input variable.

Key terms: web searches, results per search, tokens, budget, input variable, research question

4.6.3 Instantiating the LLM client

4.6.4 Generating the web searches and collecting the results

This section generates web search queries from research-assistant instructions, collects result URLs, and flattens them into query–URL pairs for the next stage.

Key terms: assistant_instructions, content property, assistant_instructions_dict, web_search_queries_list, web_search(), searches_and_result_urls, search_query_and_result_url_list, flatten, breakpoint, Python Debugger: Current File

4.6.5 Scraping the web results

This section uses web_scrape() to collect text from pages linked in the search results and stores it for later summarization.

Key terms: web_scrape(), result_text_list, search results

4.6.6 Summarizing the web results

This section summarizes each scraped web page and preserves its search query and URL for the final research report.

Key terms: SUMMARY_PROMPT_TEMPLATE, result_text_summary_list, search queries, URLs

4.6.7 Generating the research report

This section combines web-result summaries and source URLs into a final research report, then notes a speed limitation of the sequential implementation.

Key terms: stringified_summary_list, RESEARCH_REPORT_INSTRUCTIONS, summaries, source URL, sequentially, web searches, results

4.7 Reimplementing the research summary engine in LCEL

This section explains how LCEL organizes the research summarization engine into composable chains and where that design enables parallel execution.

Key terms: LCEL, chain, Runnable protocol, invoke(), stream(), batch(), fallback, parallel execution, execution modes, LangSmith tracing

4.7.1 Assistant Instructions chain

This section builds an LCEL chain that selects a research assistant for a user question, then makes its input and output easier to use in subsequent steps.

Key terms: assistant_instructions_chain, selection prompt, research assistant, RunnablePassthrough(), user_question, ASSISTANT_SELECTION_PROMPT_TEMPLATE, StrOutputParser(), content property, to_obj(), Python dictionary

4.7.2 Web Searches chain

This section connects the Assistant Instructions chain to a Web Searches chain and shows how to test it with mock input.

Key terms: RunnableLambda, WEB_SEARCH_PROMPT_TEMPLATE, Assistant Instructions chain, web searches, assistant_instruction_str, mock input

4.7.3 Search and Summarization chain

This section builds an LCEL chain that finds web pages for a search query, summarizes each page, and joins the summaries.

Key terms: Search Result URLs chain, web_query, web_search(), Web Searches chain, Search Result Text and Summary chain, RunnableParallel, text_summary, source URL, Search and Summarization chain, map()

4.7.4 Web Research chain

This section explains how the LCEL Web Research chain turns multiple web searches into parallel summaries and combines them into a final research report.

Key terms: Web Research chain, Web Searches chain, Search and Summarization chain, map(), Research Report chain

4.8 Summary

The section recaps how a research summarization engine turns a question into a multisource report and how LCEL organizes its operations.

Where this is used later

Concepts explained in this chapter

← Chapter 3: Summarizing text using LangChain Chapter 5: Agentic workflows with LangGraph →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.