Spaces:
Sleeping
title: Portfolio Backend
emoji: π€
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
app_port: 7860
Portfolio Chatbot API
A RAG (Retrieval-Augmented Generation) based chatbot API for Mrigank Singh's portfolio, powered by FastAPI, Google Gemini, and Pinecone.
π Features
- FastAPI Backend: High-performance async API with automatic OpenAPI documentation
- RAG Architecture: Combines semantic search with LLM generation for accurate, context-aware responses
- Vector Search: Uses Pinecone for efficient similarity search over portfolio knowledge base
- Google Gemini Integration: Leverages Gemini 2.0 Flash for responses and embedding generation
- CORS Enabled: Ready for frontend integration from any origin
- Production Ready: Deployed on Hugging Face Spaces with health check endpoints
π Prerequisites
- Python 3.9+
- Pinecone account and API key
- Google AI Studio API key
- (Optional) Hugging Face account for deployment
π οΈ Installation
Clone the repository
git clone <repository-url> cd Portfolio-BackendCreate a virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activateInstall dependencies
pip install -r requirements.txtSet up environment variables
Create a
.envfile in the root directory:GOOGLE_API_KEY=your_google_api_key_here PINECONE_API_KEY=your_pinecone_api_key_here
π Project Structure
Portfolio-Backend/
βββ app/
β βββ __init__.py # Application initialization
β βββ main.py # FastAPI app and endpoints
β βββ utils.py # RAG pipeline and helper functions
βββ data/
β βββ knowledge_base.txt # Portfolio information source
βββ scripts/
β βββ ingest.py # Data ingestion script for Pinecone
βββ requirements.txt # Python dependencies
βββ README.md # This file
π§ Configuration
Pinecone Setup
- Create a Pinecone index named
portfolio-chat - Configure the index with:
- Dimension: 768 (matches Gemini embedding model)
- Metric: Cosine similarity
Knowledge Base
The knowledge base is stored in data/knowledge_base.txt. The file should contain information about the portfolio owner, structured in chunks separated by double newlines.
π€ Data Ingestion
Before running the API, you need to ingest the knowledge base into Pinecone:
python scripts/ingest.py
This script will:
- Load and chunk the knowledge base text file
- Generate embeddings using Google Gemini
- Upsert vectors to Pinecone in batches
π Running the Application
Development
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
The API will be available at http://localhost:8000
Production
uvicorn app.main:app --host 0.0.0.0 --port 10000
π‘ API Endpoints
Health Check
GET /
Returns the API status.
Response:
{
"status": "ok"
}
Chat
POST /chat
Send a message to the chatbot and receive a context-aware response.
Request Body:
{
"message": "Tell me about Mrigank's projects"
}
Response:
{
"response": "Mrigank has worked on several impressive projects including..."
}
Error Responses:
400 Bad Request: Empty message500 Internal Server Error: Error generating response
π§ How RAG Works
- Query Embedding: User's question is converted to a 768-dimensional vector using Gemini
- Semantic Search: Top 5 most relevant chunks are retrieved from Pinecone
- Context Assembly: Retrieved chunks are combined into a context string
- LLM Generation: Context and query are sent to Gemini 2.0 Flash for response generation
- Response: Contextually accurate answer is returned to the user
π Deployment
This project is configured for deployment on Hugging Face Spaces.
Deploy to Hugging Face Spaces
- Connect your GitHub repository to Hugging Face Spaces
- Add environment variables in Hugging Face Spaces dashboard:
GOOGLE_API_KEYPINECONE_API_KEY
- Deploy!
π Environment Variables
| Variable | Description | Required |
|---|---|---|
GOOGLE_API_KEY |
Google AI Studio API key | Yes |
PINECONE_API_KEY |
Pinecone API key | Yes |
π¦ Dependencies
- FastAPI (0.115.0): Modern web framework for building APIs
- Uvicorn (0.30.6): ASGI server for running FastAPI
- Google GenAI (1.0.0): Google's generative AI client library
- Pinecone Client (5.0.1): Vector database client
- Pydantic (2.9.2): Data validation using Python type annotations
- Python-dotenv (1.0.1): Environment variable management
π API Documentation
Once the server is running, visit:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
π‘οΈ Security Considerations
- Store API keys securely in environment variables, never commit them to version control
- In production, restrict CORS origins to trusted domains
- Consider rate limiting for the
/chatendpoint - Implement authentication for sensitive deployments
π Troubleshooting
Common Issues
Issue: PINECONE_API_KEY not found
- Solution: Ensure
.envfile exists and contains the required API keys
Issue: Index 'portfolio-chat' not found
- Solution: Create the Pinecone index before running the ingestion script
Issue: No context chunks found
- Solution: Run the ingestion script to populate the Pinecone index
π€ Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
π License
This project is part of a personal portfolio. All rights reserved.
π§ Contact
For questions or collaboration opportunities, reach out to Mrigank Singh through his portfolio website.
Built with β€οΈ using FastAPI, Google Gemini, and Pinecone