A Retrieval Augmented Generation (RAG) system that generates newspaper-style articles using different knowledge sources and the Google Gemini API.
This project implements a RAG (Retrieval Augmented Generation) system that:
- Retrieves information from either Wikipedia or user-uploaded text files
- Processes and embeds the text using HuggingFace embeddings
- Stores the embeddings in a Chroma vector database
- Performs similarity search to find relevant context for a user query
- Generates a newspaper-style article using the Google Gemini API
The project includes two main implementations:
- Wikipedia-based RAG: Fetches information from Wikipedia based on user queries
- File-based RAG: Uses content from user-uploaded text files as the knowledge base
Both implementations are accessible through Streamlit web applications.
requirements.txt: Lists all Python dependencies required for the projectrag.ipynb: Jupyter notebook demonstrating the RAG implementation with Wikipediafetch.py: Utility script to download Wikipedia content about eco-friendly topics
rag_with_wiki.py: Implementation of RAG using Wikipedia as the knowledge sourcerag_with_uploaded.py: Implementation of RAG using user-uploaded files as the knowledge source
app_wiki.py: Streamlit web application for the Wikipedia-based RAGapp_upload.py: Streamlit web application for the file-based RAG
- Clone this repository
- Install the required dependencies:
pip install -r requirements.txt- Set up your environment variables:
- Create a
.envfile in the project root - Add your Google Gemini API key:
GEMINI_API_KEY=your_api_key_here
- Create a
Run the Wikipedia-based web application:
streamlit run app_wiki.py- Enter a topic or query in the text input field
- Click "Generate Article"
- The application will:
- Fetch relevant information from Wikipedia
- Process and embed the text
- Generate a newspaper-style article using the Gemini API
Run the file-based web application:
streamlit run app_upload.py- Upload one or more text files to provide context
- Enter a topic or query in the text input field
- Click "Generate Article"
- The application will:
- Process and embed the content from your uploaded files
- Generate a newspaper-style article using the Gemini API
To download Wikipedia content about eco-friendly topics:
python fetch.pyThis will create a directory called ecofriendly_activities containing text files with Wikipedia content on various eco-friendly topics.
The project implements a standard RAG architecture:
-
Retrieval:
- Wikipedia API or user-uploaded files provide the knowledge base
- Text is embedded using HuggingFace's "all-MiniLM-L6-v2" model
- Embeddings are stored in a Chroma vector database
-
Augmentation:
- User queries are used to perform similarity search in the vector database
- The most relevant documents are retrieved and combined to form the context
-
Generation:
- The context and query are sent to the Google Gemini API
- A prompt instructs the model to generate a newspaper-style article
- The generated article is returned to the user
Key dependencies include:
langchain: Framework for building applications with LLMslangchain_huggingface: HuggingFace integration for LangChainlangchain_community: Community components for LangChainchromadb: Vector database for storing embeddingsstreamlit: Web application frameworkwikipedia: Python library for accessing Wikipedia contentdotenv: For loading environment variablesrequests: For making HTTP requests to the Gemini API
Potential enhancements for this project:
- Add support for more file formats (PDF, DOCX, etc.)
- Implement caching to improve performance
- Add more customization options for the generated articles
- Implement error handling and retry mechanisms for API calls
- Add authentication for the web applications
- Expand to other LLM providers beyond Google Gemini