Gemini LangChain Summarization WebLoad
Copyright 2025 Google LLC.
Gemini API: Summarize large documents using LangChain
Overview
The Gemini models are a family of generative AI models that allow developers generate content and solve problems. These models are designed and trained to handle both text and images as input.
LangChain is a framework designed to make integration of Large Language Models (LLM) like Gemini easier for applications.
In this notebook, you'll learn how to create an application to summarize large documents using the Gemini API and LangChain.
Setup
First, you must install the packages and set the necessary environment variables.
Installation
Install LangChain's Python library, langchain and LangChain's integration package for the Gemini API, langchain-google-genai. Installing langchain-community allows you to use the WebBaseLoader tool shown later in this example.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 241.2/241.2 kB 2.9 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 55.4/55.4 kB 2.9 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 53.0/53.0 kB 3.8 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 802.4/802.4 kB 8.9 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 42.2 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 49.2/49.2 kB 3.4 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 302.9/302.9 kB 19.9 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 48.7 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 53.4 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 67.6 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.9/1.9 MB 39.5 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.9/1.9 MB 51.3 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.9/1.9 MB 37.3 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.9/1.9 MB 40.1 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.9/1.9 MB 45.7 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 56.8 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 56.0 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 60.9 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 78.1 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 80.7 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 76.1 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 80.6 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 69.3 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 76.3 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 83.8 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 146.9/146.9 kB 2.9 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 598.7/598.7 kB 6.7 MB/s eta 0:00:00
Configure your API key
To run the following cell, your API key must be stored in a Colab Secret named GOOGLE_API_KEY. If you don't already have an API key, or you're not sure how to create a Colab Secret, see Authentication for an example.
Summarize text
In this tutorial, you are going to summarize the text from a website using the Gemini model integrated through LangChain.
You'll perform the following steps to achieve the same:
-
Read and parse the website data using LangChain.
-
Chain together the following:
- A prompt for extracting the required input data from the parsed website data.
- A prompt for summarizing the text using LangChain.
- An LLM model (such as the Gemini model) for prompting.
-
Run the created chain to prompt the model for the summary of the website data.
Read and parse the website data
LangChain provides a wide variety of document loaders. To read the website data as a document, you will use the WebBaseLoader from LangChain.
To know more about how to read and parse input data from different sources using the document loaders of LangChain, read LangChain's document loaders guide.
Initialize the Gemini model
You must import the ChatGoogleGenerativeAI LLM from LangChain to initialize your model.
In this example you will use Gemini 2.5 Flash, (gemini-2.5-flash), as it supports text summarization. To know more about this model and the other models availabe, read Google AI's language documentation.
You can configure the model parameters such as temperature or top_p, by passing the appropriate values when creating the ChatGoogleGenerativeAI LLM. To learn more about the parameters and their uses, read Google AI's concepts guide.
Create prompt templates
You'll use LangChain's PromptTemplate to generate prompts for summarizing the text.
To summarize the text from the website, you will need the following prompts.
- Prompt to extract the data from the output of
WebBaseLoader, nameddoc_prompt - Prompt for the Gemini model to summarize the extracted text, named
llm_prompt.
In the llm_prompt, the variable text will be replaced later by the text from the website.
input_variables=['text'] template='Write a concise summary of the following:\n"{text}"\nCONCISE SUMMARY:'
Create a Stuff documents chain
LangChain provides Chains for chaining together LLMs with each other or other components for complex applications. You will create a Stuff documents chain for this application. A Stuff documents chain lets you combine all the documents, insert them into the prompt and pass that prompt to the LLM.
You can create a Stuff documents chain using the LangChain Expression Language (LCEL).
To learn more about different types of document chains, read LangChain's chains guide.
Prompt the model
To generate the summary of the the website data, pass the documents extracted using the WebBaseLoader (docs) to invoke().
"Google has introduced Gemini, its most capable AI model yet. Gemini is multimodal, meaning it can understand and interact with various forms of information, including text, code, audio, images, and video. It comes in three sizes: Ultra (for complex tasks), Pro (for a wide range of tasks), and Nano (for on-device tasks). Gemini surpasses existing models in performance benchmarks across various domains, including natural language understanding, reasoning, and coding. \n\nGoogle emphasizes Gemini's safety and responsibility features, including comprehensive bias and toxicity evaluation, adversarial testing, and collaboration with external experts. \n\nGemini is being integrated into various Google products, such as Bard, Pixel, Search, and Ads, and will be available to developers through APIs. \n\nThe release of Gemini marks a significant milestone in AI development, opening up new possibilities for innovation and enhancing human capabilities in various areas. \n"
Conclusion
That's it. You have successfully created an LLM application to summarize text using LangChain and the Gemini API.