File size: 3,079 Bytes
4093408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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.