VOOZH about

URL: https://thenewstack.io/tutorial-using-langchain-and-gemini-to-summarize-articles/

⇱ Tutorial: Using LangChain and Gemini to Summarize Articles - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-03-27 05:00:59
Tutorial: Using LangChain and Gemini to Summarize Articles
tutorial,
AI / Software Development

Tutorial: Using LangChain and Gemini to Summarize Articles

We demonstrate how to combine LangChain and Google's Gemini LLM to summarize blog posts and articles on the internet.
Mar 27th, 2024 5:00am by Janakiram MSV
👁 Featued image for: Tutorial: Using LangChain and Gemini to Summarize Articles
Photo by Maarten van den Heuvel on Unsplash.

In this tutorial, we’ll look at how to combine LangChain — a programming framework for using large language models (LLMs) in applications — and Google’s Gemini LLM to summarize blog posts or articles on the internet.

Make sure you have an API key from Google AI Studio before continuing with the tutorial.

This application aims to summarize web-based articles, providing concise overviews of their content. This is particularly useful for quickly understanding the gist of long articles without reading through the entire text.

Step 1: Initializing the Environment

Create a Python virtual environment and install the required modules from the requirements.txt file.

python -m venv venv
source venv/bin/activate

Create therequirements.txt file with the below content:

google.generativeai
langchain-google-genai
langchain
langchain_community
jupyter
pip install -r requirements.txt

Set an environment variable to access the API key implicitly from the code.

export GOOGLE_API_KEY="YOUR_GOOGLE_API_KEY"

Launch the Jupyter Notebook and get started with the code.

Step 2: Import Modules

Begin by importing the necessary Python modules. These imports include classes and functions from LangChain and Google Generative AI, which are essential for building our application. Ensure these libraries are installed in your Python environment before proceeding.

from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_community.document_loaders import WebBaseLoader
from langchain.prompts import PromptTemplate
from langchain.chains import StuffDocumentsChain
from langchain.chains.llm import LLMChain

Step 3: Load the Blog

Load content from a web-based article. The URL provided in the code snippet below is just an example; feel free to replace it with any article URL you wish to summarize. The content is fetched and stored for further processing.

loader = WebBaseLoader("https://thenewstack.io/the-building-blocks-of-llms-vectors-tokens-and-embeddings/")
docs = loader.load()

Step 4: Define the Summarize Chain

In this critical step, we’ll define the summarization template and configure the LangChain model to generate summaries. The template instructs the model on how to structure its output, focusing on creating a concise summary of the input text.

template = "Write a concise summary of the following:\n\"{text}\"\nCONCISE SUMMARY:"
prompt = PromptTemplate.from_template(template)
llm_chain = LLMChain(llm=llm, prompt=prompt)
stuff_chain = StuffDocumentsChain(llm_chain=llm_chain, document_variable_name="text")

Step 5: Invoke Chain

The final step is to invoke the chain, which triggers the summarization process on the loaded document. The output is the model’s attempt at summarizing the article, providing you with a condensed version of its content.

response=stuff_chain.invoke(docs)
print(response["output_text"])

👁 Image

More About LangChain and LLMs

The question-and-answering (Q&A) use case, as demonstrated in the previous tutorial, and the summarization use case, as outlined above, harness the power of LangChain and Gemini, but serve distinct purposes and employ different methodologies.

The Q&A application focuses on extracting specific answers from a given text (such as a PDF document), requiring the system to understand the context and retrieve accurate information in response to queries. This process involves loading and splitting the document into manageable chunks, converting these chunks into embeddings and using a retrieval mechanism to find the most relevant sections of text for answering the questions posed.

On the other hand, the summarization use case is designed to condense long web-based articles into concise summaries. This application emphasizes the ability to grasp the overall theme and key points of an article, requiring a summarization chain that instructs the model to produce a brief overview of the content. Unlike the Q&A use case, summarization involves loading web content directly, applying a summarization template and generating a condensed version of the article — highlighting its core message without needing to dive into the specifics.

Both applications showcase LangChain’s versatility in handling natural language processing tasks, but they each cater to different needs. One focuses on pinpointing specific information within a document, while the other aims to provide a quick, digestible summary of lengthy articles.

You can find the complete code for summarization below:

TRENDING STORIES
Janakiram MSV (Jani) is a practicing architect, research analyst, and advisor to Silicon Valley startups. He focuses on the convergence of modern infrastructure powered by cloud-native technology and machine intelligence driven by generative AI. Before becoming an entrepreneur, he spent...
Read more from Janakiram MSV
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.