SchemeSync / Documentation /FASTAPI_QUICKSTART.md
suryateja008's picture
Upload 22 files
4093408 verified
# FastAPI Quick Start Guide
## 🎯 First Time Setup
### Step 1: Create Vector Database (ONE TIME ONLY)
```bash
python setup_db.py
```
This will:
- Load 3400+ schemes from `updated_data.csv`
- Convert them into embeddings
- Create and save vector database to `./chroma_db/`
- Takes 2-5 minutes (only need to do this once!)
**When to run `setup_db.py` again:**
- When you have new/updated CSV data
- If you delete the `chroma_db` folder
- If you want to rebuild the database with different settings
---
## πŸš€ Start the API Server (Every Time)
1. **Activate virtual environment:**
```bash
.venv\Scripts\activate
```
2. **Run the application:**
```bash
python app.py
```
This now starts in **seconds** (not minutes!) because it loads the pre-built database.
3. **Access the API:**
- API Server: http://127.0.0.1:8000
- Interactive Docs: http://127.0.0.1:8000/docs
- Alternative Docs: http://127.0.0.1:8000/redoc
---
## πŸ§ͺ Test the API
### Option 1: Use the test script
```bash
python test_api.py
```
### Option 2: Use the interactive docs
1. Open http://127.0.0.1:8000/docs in your browser
2. Click on "POST /query"
3. Click "Try it out"
4. Enter your question and click "Execute"
### Option 3: Use cURL
```bash
curl -X POST http://127.0.0.1:8000/query -H "Content-Type: application/json" -d "{\"question\": \"What schemes are available for students?\"}"
```
---
## πŸ“ Sample API Calls
### Python Example
```python
import requests
response = requests.post(
"http://127.0.0.1:8000/query",
json={
"question": "My daughter is studying in 9th standard. What schemes are applicable?",
"state": "Maharashtra"
}
)
print(response.json()["answer"])
```
### JavaScript Example
```javascript
const response = await fetch('http://127.0.0.1:8000/query', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
question: 'What scholarships are available for SC students?'
})
});
const data = await response.json();
console.log(data.answer);
```
---
## πŸ“š Available Endpoints
| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | `/` | API information |
| GET | `/health` | Health check |
| GET | `/states` | List of Indian states |
| POST | `/query` | Query government schemes |
---
## πŸ› οΈ Troubleshooting
**"Vector database not found" error?**
- Run `python setup_db.py` first to create the database
**API won't start?**
- Check if port 8000 is already in use
- Verify `.env` file has `GROQ_API_KEY`
**Slow first query?**
- Normal! First query loads the model
- Subsequent queries will be faster
**Error 500?**
- Check your Groq API key is valid
- Ensure `chroma_db` folder exists
**Need to update data?**
- Update `updated_data.csv`
- Delete `chroma_db` folder
- Run `python setup_db.py` again
---
## πŸ“– Full Documentation
See `API_DOCUMENTATION.md` for complete API reference.