Spaces:
Sleeping
Sleeping
FastAPI Quick Start Guide
π― First Time Setup
Step 1: Create Vector Database (ONE TIME ONLY)
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_dbfolder - If you want to rebuild the database with different settings
π Start the API Server (Every Time)
Activate virtual environment:
.venv\Scripts\activateRun the application:
python app.pyThis now starts in seconds (not minutes!) because it loads the pre-built database.
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
python test_api.py
Option 2: Use the interactive docs
- Open http://127.0.0.1:8000/docs in your browser
- Click on "POST /query"
- Click "Try it out"
- Enter your question and click "Execute"
Option 3: Use cURL
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
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
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.pyfirst to create the database
API won't start?
- Check if port 8000 is already in use
- Verify
.envfile hasGROQ_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_dbfolder exists
Need to update data?
- Update
updated_data.csv - Delete
chroma_dbfolder - Run
python setup_db.pyagain
π Full Documentation
See API_DOCUMENTATION.md for complete API reference.