AI-powered chatbot API with knowledge about personal information, research papers, and interests. Built with FastAPI, Claude (Anthropic), and Pinecone for semantic search.
- RAG (Retrieval-Augmented Generation) - Semantic search over personal documents and research papers
- Rate Limiting - Configurable hourly limits and daily cost caps to control API usage
- Usage Tracking - Detailed logging of requests, tokens, and costs
- CORS Enabled - Ready for frontend integration
- Type-Safe - Full TypeScript-style typing with Pydantic
- Idempotent Ingestion - Update knowledge base without duplicates
- Emergency Kill Switch - Disable API via environment variable
- FastAPI - Modern Python web framework
- Claude (Anthropic) - LLM for generating responses
- Pinecone - Vector database for semantic search
- LangChain - Document processing and RAG orchestration
- OpenAI - Embedding generation
- Pydantic - Data validation and settings management
- Python 3.11 (not 3.13 - compatibility issues with some dependencies)
- API keys for:
- Anthropic Claude
- Pinecone
- OpenAI
# Clone the repository
git clone git@github.com:rweir4/mybot.git
cd personal-chatbot-api
# Create virtual environment with Python 3.11
python3.11 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Create .env file from template
cp .env.example .envEdit .env with your API keys and settings:
# API Keys (required)
ANTHROPIC_API_KEY=sk-ant-your-key-here
PINECONE_API_KEY=your-pinecone-key
OPENAI_API_KEY=sk-your-openai-key
# Pinecone Configuration
PINECONE_ENVIRONMENT=us-east-1-aws
PINECONE_INDEX_NAME=personal-chatbot
# Rate Limiting
RATE_LIMIT_PER_HOUR=30
MAX_OUTPUT_TOKENS=4000
MAX_DAILY_COST=5.0
# Emergency Kill Switch
API_ENABLED=true- Create personal info file:
# Create data directory
mkdir -p data/papers
# Create personal info JSON
cat > data/personal_info.json << 'EOF'
{
"name": "Your Name",
"bio": "Brief bio",
"interests": {
"hobbies": ["hobby1", "hobby2"],
"research_areas": ["area1", "area2"]
}
}
EOF- Add research papers:
# Copy your PDF papers
cp /path/to/your/papers/*.pdf data/papers/Run the ingestion script to populate Pinecone:
python3 -m scripts.ingestThis will:
- Load your personal info and papers
- Chunk documents into searchable pieces
- Generate embeddings using OpenAI
- Upload to Pinecone with idempotent upsert
You can re-run this anytime to update your knowledge base.
# Start the server
uvicorn app.main:app --reload
# API will be available at:
# - API: http://localhost:8000
# - Docs: http://localhost:8000/docs
# - OpenAPI spec: http://localhost:8000/openapi.jsonuvicorn app.main:app --host 0.0.0.0 --port 8000Ask a question and get an AI-generated response with sources.
Request:
{
"message": "What are your research interests?",
"top_k": 5 // optional, number of context chunks to retrieve
}Response:
{
"answer": "Based on my research...",
"sources": [
{
"content": "Excerpt from source...",
"source": "paper1.pdf",
"relevance_score": 0.95
}
],
"tokens_used": 1234,
"estimated_cost": 0.0185
}Check API health and configuration status.
Response:
{
"status": "healthy",
"api_enabled": true,
"config_valid": true
}Get usage statistics and rate limit status.
Response:
{
"rate_limit": {
"hourly_stats": {
"requests_used": 15,
"requests_limit": 30,
"requests_remaining": 15
},
"daily_stats": {
"tokens_used": 45000,
"estimated_cost": 0.23,
"cost_limit": 5.0
}
},
"usage": {
"total_requests": 120,
"successful_requests": 118,
"total_cost_usd": 2.45
}
}# Run all tests
pytest
# Run with coverage
pytest --cov=app
# Run specific test file
pytest tests/test_main.py -v
# Run specific test
pytest tests/test_main.py::test_chat_endpoint_success -v- Create a new Railway project
- Connect your GitHub repository
- Add environment variables in Railway dashboard (all values from
.env) - Railway will auto-deploy on push to main branch
The API will be available at: https://your-project.railway.app
Make sure to set all required environment variables:
ANTHROPIC_API_KEYPINECONE_API_KEYOPENAI_API_KEYPINECONE_ENVIRONMENTPINECONE_INDEX_NAMERATE_LIMIT_PER_HOURMAX_DAILY_COSTAPI_ENABLED
personal-chatbot-api/
├── app/
│ ├── config.py # Configuration management
│ ├── rate_limiter.py # Rate limiting logic
│ ├── logger.py # Usage logging
│ ├── rag.py # RAG implementation
│ └── main.py # FastAPI application
├── scripts/
│ └── ingest.py # Data ingestion script
├── tests/
│ ├── test_config.py
│ ├── test_rate_limiter.py
│ ├── test_logger.py
│ ├── test_rag.py
│ ├── test_ingest.py
│ └── test_main.py
├── data/
│ ├── personal_info.json # Your personal information
│ └── papers/ # PDF research papers
├── requirements.txt # Python dependencies
├── pytest.ini # Pytest configuration
├── .env.example # Environment variables template
└── README.md
Disable the API immediately:
# In Railway dashboard or .env
API_ENABLED=falseAll requests will return 503 Service Unavailable.
Update limits on the fly:
# Increase hourly limit
RATE_LIMIT_PER_HOUR=100
# Increase daily cost cap
MAX_DAILY_COST=10.0The API tracks costs automatically:
- Embeddings (one-time during ingestion): ~$0.02 per 1M tokens
- Claude API (per query):
- Input: $3 per 1M tokens
- Output: $15 per 1M tokens
- Daily cap: Configurable via
MAX_DAILY_COST
Expected costs for typical usage:
- Ingestion (one-time): <$0.01
- 100 queries/day: ~$0.50-2.00/day depending on complexity
To update your information:
- Edit
data/personal_info.jsonor add PDFs todata/papers/ - Re-run ingestion:
python3 -m scripts.ingestThe script uses idempotent upsert - only changed content is re-processed.
Use Python 3.11:
python3.11 -m venv venv
source venv/bin/activate
pip install -r requirements.txtMake sure virtual environment is activated and dependencies are installed:
source venv/bin/activate
pip install -r requirements.txtCheck your current usage:
curl http://localhost:8000/statsAdjust RATE_LIMIT_PER_HOUR if needed.
Verify your Pinecone environment matches your account region:
# Check Pinecone dashboard for your environment
# Update in .env:
PINECONE_ENVIRONMENT=us-east-1-aws # or your regionMIT
Contributions welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Submit a pull request