A production-ready RAG (Retrieval-Augmented Generation) chatbot powered by Google's Gemini FileSearch API, designed to be integrated with Kajabi's paywall system using token-based authentication.
- 🤖 Gemini FileSearch Integration: Uses Google's Gemini FileSearch for intelligent document retrieval
- 🔐 Token Authentication: Secure token-based access control
- 🎨 Modern UI: Beautiful, responsive chatbot interface
- 🔗 Kajabi Integration: Webhook support for automatic token management
- 📝 Conversation History: Maintains context across chat sessions
- 🚀 FastAPI Backend: High-performance async API
- Python 3.9+
- Google Cloud Storage bucket with your files uploaded
- Gemini API enabled and API key
- File Search Store created (use
create_store.py)
# Activate your virtual environment
source venv/bin/activate # On macOS/Linux
# or
venv\Scripts\activate # On Windows
# Install required packages
pip install -r requirements.txtCopy the example environment file and fill in your values:
cp env.example .envEdit .env and set the following:
# Required: Your Gemini API key
GEMINI_API_KEY=your_gemini_api_key_here
# Required: Your File Search Store ID (from create_store.py)
GEMINI_STORE_ID=your_file_search_store_id_here
# Optional: Model to use (default: gemini-2.0-flash-exp)
GEMINI_MODEL=gemini-2.0-flash-exp
# Optional: Secret key for additional security
SECRET_KEY=your_secret_key_here
# Optional: Server configuration
HOST=0.0.0.0
PORT=8000python create_store.pyCopy the Store ID that's printed and add it to your .env file as GEMINI_STORE_ID.
You'll need to upload files from your Google Cloud Storage bucket to the File Search Store. You can do this via:
- Google AI Studio UI
- Or use the Gemini API to upload files programmatically
Use the token manager to create tokens for your users:
# Create a token for a user (expires in 30 days by default)
python token_manager.py create user@example.com
# Create a token with custom expiration
python token_manager.py create user@example.com 90
# List all tokens
python token_manager.py list
# Revoke a token
python token_manager.py revoke <token>python app.pyOr using uvicorn directly:
uvicorn app:app --host 0.0.0.0 --port 8000 --reloadThe chatbot will be available at http://localhost:8000
- POST
/api/chat - Headers:
Authorization: Bearer <token> - Body:
{ "message": "Your question here", "conversation_history": [ {"role": "user", "content": "Previous message"}, {"role": "assistant", "content": "Previous response"} ] } - Response:
{ "response": "AI response text", "sources": [ {"file_uri": "...", "chunk_index": 0} ] }
- POST
/api/tokens- Create a new token (admin only) - GET
/api/tokens- List all tokens (admin only)
- GET
/api/health- Check API health and configuration
- Create tokens for each paying customer using
token_manager.py - Distribute tokens through Kajabi's email automation or custom fields
- Users enter their token in the chatbot interface
-
Set up the webhook handler:
python kajabi_webhook.py
-
Configure Kajabi webhook:
- Go to Kajabi Settings → Integrations → Webhooks
- Create a new webhook pointing to:
https://your-domain.com/webhook/kajabi - Set webhook secret in
.envasKAJABI_WEBHOOK_SECRET - Subscribe to events:
purchase.createdororder.completedpurchase.cancelledorsubscription.cancelledpurchase.refunded
-
The webhook will automatically:
- Create tokens when users purchase
- Revoke tokens when users cancel or get refunded
You can embed the chatbot directly in your Kajabi site:
- Add a custom HTML block in Kajabi
- Include the chatbot interface (modify the HTML in
app.pyor create a separate frontend) - Use Kajabi's Liquid variables to pass user information
Example Kajabi integration snippet:
<script>
// Get user info from Kajabi
const userId = "{{ customer.id }}";
const userEmail = "{{ customer.email }}";
// Make API call to get/create token
fetch('/api/tokens', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({user_id: userId})
})
.then(res => res.json())
.then(data => {
// Set token in chatbot
localStorage.setItem('chatbot_token', data.token);
});
</script>The chatbot UI is embedded in app.py. To customize:
- Modify the HTML/CSS/JavaScript in the
root()endpoint - Or create a separate frontend application that calls
/api/chat - Update CORS settings in
app.pyto allow your domain
- Set
SECRET_KEYto a strong random value - Configure CORS to only allow your Kajabi domain
- Use HTTPS for all API calls
- Implement rate limiting
- Use a database instead of JSON file for tokens
- Set up proper logging and monitoring
- Configure firewall rules
- Use environment variables for all secrets
- Google Cloud Run: Serverless, auto-scaling
- AWS Lambda + API Gateway: Serverless
- DigitalOcean App Platform: Simple deployment
- Heroku: Easy deployment
- Railway: Modern platform
Make sure to set these in your hosting platform:
GEMINI_API_KEY=your_key
GEMINI_STORE_ID=your_store_id
SECRET_KEY=strong_random_key
KAJABI_WEBHOOK_SECRET=your_webhook_secret- Check that
GEMINI_API_KEYis set correctly - Verify the API key is valid in Google AI Studio
- Run
create_store.pyto create a store - Copy the Store ID to your
.envfile
- Verify the token exists:
python token_manager.py list - Check if token was revoked
- Ensure token hasn't expired
- Verify files are uploaded to the File Search Store
- Check file permissions in Google Cloud Storage
- Ensure the store ID matches your store
.
├── app.py # Main FastAPI application
├── create_store.py # Script to create File Search Store
├── token_manager.py # Token management utility
├── kajabi_webhook.py # Kajabi webhook handler
├── requirements.txt # Python dependencies
├── env.example # Environment variables template
├── tokens.json # Token storage (created automatically)
└── README.md # This file
- ✅ Set up your environment variables
- ✅ Create your File Search Store
- ✅ Upload files to the store
- ✅ Create tokens for test users
- ✅ Test the chatbot locally
- ✅ Deploy to production
- ✅ Configure Kajabi integration
- ✅ Set up monitoring and logging
For issues related to:
- Gemini API: Check Google AI Studio
- FileSearch: See Gemini FileSearch Documentation
- Kajabi: Check Kajabi Webhooks Documentation
This project is provided as-is for your use case.