This repository provides a simple FastAPI application that uses LangChain to summarize web pages. It leverages the power of Google Gemini Pro and offers language selection for Thai and English summaries. This project also demonstrates the use of LangChain's "stuff" chains for efficient text processing.
- Web Page Summarization: Provides concise summaries of web page content.
- Multilingual Support: Offers summaries in both Thai and English.
- FastAPI Framework: Uses FastAPI for a modern and performant API experience.
- Stuff Chains: Utilizes LangChain's "stuff" chains for streamlined text processing.
In LangChain, "stuff" chains are used to insert all the documents retrieved by a loader into a single prompt. This is particularly useful when you want to summarize or analyze multiple documents together.
In this project, the stuff_chain
takes all the content from a web page, formats it using the doc_prompt
, and then feeds it as a single input to the language model (Gemini Pro) along with the summarizing prompt. This allows the language model to consider the entire context of the web page when generating a summary.
read more: https://js.langchain.com/v0.1/docs/modules/chains/document/stuff/
-
Clone the Repository:
git clone https://github.com/bestculling/LangChainWebSummarizer.git cd LangChainWebSummarizer
-
Create a Virtual Environment:
python3 -m venv venv source venv/bin/activate
-
Install Dependencies:
pip install -r requirements.txt
-
Set Environment Variables:
- Create a
.env
file in the root directory. - Add your Google API key:
GOOGLE_API_KEY=your_google_api_key
- Create a
-
Run the Application:
uvicorn main:app --reload
- Send a POST request to
/summarize
with the following JSON payload:{ "url": "https://example.com/article-to-summarize", "language": "en" // "th" for Thai }
- The API will respond with the summary in JSON format:
{ "summary": "This is a concise summary of the web page content." }
curl -X POST -H "Content-Type: application/json" -d '{"url": "https://en.wikipedia.org/wiki/Artificial_intelligence", "language": "en"}' http://localhost:8000/summarize
Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or bug fixes.