A powerful Retrieval-Augmented Generation (RAG) tool that allows you to chat with YouTube channel transcripts using Google's Gemini API File Search feature.
- YouTube Channel Scraping: Automatically scrape video titles and transcripts from any YouTube channel using Playwright
- Gemini File Search Integration: Upload transcripts to Gemini's managed RAG system for semantic search
- Flexible Authentication: Supports both API key and gcloud CLI authentication
- Interactive & Non-Interactive Modes: Run as an interactive chat or execute scripted prompts
- Cost Tracking: Track and report API costs with detailed breakdowns
- Chat History: Maintain complete conversation history with metadata
- Idempotent Operations: Re-run safely without duplicating work
- Python 3.8 or higher
- pip (Python package manager)
- Google Cloud account with Gemini API access
- Either:
- Gemini API key, OR
- gcloud CLI installed and configured
-
Clone the repository:
git clone https://github.com/libertyteeth/gemini-api-rag.git cd gemini-api-rag -
Install Python dependencies:
pip install -r requirements.txt
-
Install Playwright browsers:
playwright install chromium
-
Configure authentication (choose ONE):
Option A: Using API Key
cp .env.example .env # Edit .env and add your Gemini API keyOption B: Using gcloud CLI
gcloud auth application-default login
Create a .env file in the project root (optional if using gcloud CLI):
# Optional: Gemini API Key
GEMINI_API_KEY=your_api_key_hereIf GEMINI_API_KEY is not set, the tool will automatically attempt to use gcloud CLI authentication.
- API Key: Checks
GEMINI_API_KEYenvironment variable first - gcloud CLI: Falls back to
gcloud auth application-defaultcredentials - Error: Exits with helpful message if both methods fail
Start an interactive chat session:
python main.pyYou'll be prompted for:
- YouTube channel URL
- Number of videos to process
Then you can chat with the transcripts:
You: What are the main topics discussed in these videos?
Assistant: Based on the transcripts, the main topics include...
You: Tell me more about topic X
Assistant: ...
You: quit
Interactive Commands:
quit,exit, orq- Exit the chatcost- Show cost summaryhistory- Show recent chat history
Specify all parameters via command line:
# Basic usage
python main.py --channel="https://youtube.com/@channelname" --numvideos=10
# With specific prompts (non-interactive)
python main.py \
--channel="https://youtube.com/@channelname" \
--numvideos=5 \
--prompt="Summarize the main topics discussed" \
--prompt="What insights are provided about AI?"| Parameter | Description | Default |
|---|---|---|
--channel=URL |
YouTube channel URL | (required for non-interactive) |
--numvideos=N |
Number of videos to retrieve | 5 |
--prompt="text" |
Prompt to execute (can repeat) | None |
--model=NAME |
Gemini model to use | gemini-2.0-flash-exp |
--skip-scraping |
Skip scraping, use existing transcripts | False |
--cost-report |
Show detailed cost report | - |
--cost-query="query" |
Query costs | - |
View detailed cost summary:
python main.py --cost-reportQuery specific cost information:
# Yesterday's costs
python main.py --cost-query="How much did yesterday cost?"
# Total costs since project began
python main.py --cost-query="Total cost since project began"
# This week's costs
python main.py --cost-query="This week's costs"
# This month's costs
python main.py --cost-query="This month's costs"
# Today's costs
python main.py --cost-query="What is today's cost?"Example 1: Quick test with 3 videos
python main.py \
--channel="https://youtube.com/@lexfridman" \
--numvideos=3 \
--prompt="What topics are covered?"Example 2: Deep dive with multiple prompts
python main.py \
--channel="https://youtube.com/@3blue1brown" \
--numvideos=10 \
--prompt="List all mathematical concepts discussed" \
--prompt="Explain the calculus topics in detail" \
--prompt="What visualizations are described?"Example 3: Use existing transcripts
python main.py \
--skip-scraping \
--prompt="Summarize everything we have so far"gemini-api-rag/
├── README.md # This file
├── requirements.txt # Python dependencies
├── .env.example # Environment variable template
├── .gitignore # Git ignore rules
├── main.py # Main entry point
│
├── src/
│ ├── __init__.py
│ ├── gemini_client.py # Gemini API authentication
│ ├── youtube_scraper.py # YouTube scraping with Playwright
│ ├── rag_manager.py # Gemini File Search RAG management
│ ├── cost_tracker.py # Cost tracking and reporting
│ └── chat_history.py # Chat history management
│
├── data/
│ ├── transcripts/ # Downloaded video transcripts
│ ├── costs.json # Cost tracking data
│ └── history.json # Chat history data
│
└── config/
└── store_config.json # Vector store configuration
| Operation | Cost |
|---|---|
| File Search Indexing | $0.15 per 1M tokens |
| File Search Storage | Free |
| Query Embeddings | Free |
| Context Tokens | $0.075 per 1M tokens* |
| Output Tokens | $0.30 per 1M tokens* |
*Varies by model
The tool automatically tracks:
- Indexing costs when uploading transcripts
- Query costs for each chat interaction
- Historical costs by day, week, month
- Token usage for input and output
All costs are stored in data/costs.json and can be queried at any time.
For a typical YouTube channel scrape:
- 10 videos × 5,000 words each = ~17,000 tokens per video
- Total indexing: 170,000 tokens = $0.026
- Storage: Free
- 10 chat queries: ~$0.002
Total for this workflow: ~$0.028
- Saved in
data/transcripts/ - Format:
{video_id}_{title}.txt - Includes video metadata (ID, title, URL)
- File:
data/costs.json - Includes: timestamp, transaction type, cost, metadata
- Supports historical queries and reporting
- File:
data/history.json - Includes: timestamp, prompt, response, cost, tokens, user, IP
- Searchable and exportable
- File:
config/store_config.json - Stores Gemini File Search store IDs
- Enables idempotent operations
Problem: "Authentication failed"
# Solution 1: Use API key
echo "GEMINI_API_KEY=your_key_here" > .env
# Solution 2: Configure gcloud
gcloud auth application-default loginProblem: "Browser not found"
# Solution: Install Playwright browsers
playwright install chromiumProblem: "No videos found"
- Verify the channel URL format:
https://youtube.com/@channelname - Check if the channel has public videos
- Try adding
/videosto the URL
Problem: "No transcript found"
- Not all YouTube videos have transcripts/subtitles
- The tool will skip videos without transcripts
- Try increasing
--numvideosto get more videos
Problem: "File Search not available"
- Verify you have access to Gemini API File Search feature
- Check if your API key has the necessary permissions
- Ensure you're using a supported model (gemini-2.0-flash-exp, gemini-2.5-pro, etc.)
Enable verbose output:
python -u main.py --channel="..." 2>&1 | tee debug.logUse a different Gemini model:
python main.py --model="gemini-2.5-pro" --channel="..."Create a script:
#!/bin/bash
CHANNELS=(
"https://youtube.com/@channel1"
"https://youtube.com/@channel2"
"https://youtube.com/@channel3"
)
for channel in "${CHANNELS[@]}"; do
python main.py --channel="$channel" --numvideos=10
donefrom src.chat_history import ChatHistory
history = ChatHistory()
history.export_to_file('my_conversations.txt', format='txt')
history.export_to_file('my_conversations.json', format='json')from src.gemini_client import GeminiClient
from src.rag_manager import RAGManager
# Initialize
client = GeminiClient()
client.authenticate()
rag = RAGManager()
# Query
result = rag.query("What topics are discussed?")
print(result['response'])Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
- Google Gemini API for File Search
- Playwright for web scraping
- youtube-transcript-api for transcript extraction
For issues, questions, or feature requests, please open an issue on GitHub: https://github.com/libertyteeth/gemini-api-rag/issues
- Support for playlist URLs
- Parallel video processing
- Web UI interface
- Export to different formats (PDF, Markdown)
- Integration with other LLMs
- Advanced filtering and search
- Automatic transcript summarization
- Multi-language support
MIT License - See LICENSE file for details