-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
221 lines (192 loc) · 6.89 KB
/
tools.py
File metadata and controls
221 lines (192 loc) · 6.89 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
Deterministic tool functions for the SchemeEligibilityAgent.
These are plain Python functions — no LLM calls.
Each tool performs one focused task and returns structured data.
"""
import json
import os
# ---------------------------------------------------------------------------
# Constants for rule evaluation
# ---------------------------------------------------------------------------
INCOME_UPPER_BOUNDS = {
"Below 1 Lakh": 100000,
"1-2.5 Lakh": 250000,
"2.5-5 Lakh": 500000,
"5-8 Lakh": 800000,
"8-10 Lakh": 1000000,
"Above 10 Lakh": float("inf"),
}
EDUCATION_RANK = {
"Below 10th": 1,
"10th Pass": 2,
"12th Pass": 3,
"Diploma/ITI": 4,
"Graduate": 5,
"Post Graduate": 6,
"PhD": 7,
}
# ---------------------------------------------------------------------------
# Tool 1: Load schemes from JSON database
# ---------------------------------------------------------------------------
def load_schemes(filepath: str = "schemes.json") -> list[dict]:
"""
Load all government schemes from the local JSON database.
Reads the JSON file and returns a list of scheme dictionaries.
Uses the script's directory for path resolution so it works
regardless of the working directory.
"""
base_dir = os.path.dirname(os.path.abspath(__file__))
full_path = os.path.join(base_dir, filepath)
with open(full_path, "r", encoding="utf-8") as f:
data = json.load(f)
return data["schemes"]
# ---------------------------------------------------------------------------
# Tool 2: Check eligibility for a single scheme
# ---------------------------------------------------------------------------
def check_eligibility(user_profile: dict, scheme: dict) -> dict:
"""
Compare a user profile against a single scheme's eligibility rules.
Evaluates 7 dimensions:
1. Age range
2. Income threshold
3. Education level
4. Social category
5. Employment status
6. Gender
7. State
Returns a dict with:
- scheme_id: str
- scheme_name: str
- eligible: bool
- reasons: list[str] — one entry per dimension checked
"""
rules = scheme["rules"]
reasons = []
eligible = True
# --- 1. Age check ---
age = user_profile["age"]
age_min = rules.get("age_min")
age_max = rules.get("age_max")
if age_min is not None and age < age_min:
eligible = False
reasons.append(f"Rejected: Age {age} is below minimum {age_min}")
elif age_max is not None and age > age_max:
eligible = False
reasons.append(f"Rejected: Age {age} exceeds maximum {age_max}")
else:
lo = age_min if age_min is not None else "any"
hi = age_max if age_max is not None else "any"
reasons.append(f"Passed: Age {age} is within range [{lo}-{hi}]")
# --- 2. Income check ---
income_max = rules.get("income_max")
if income_max is not None:
user_income_upper = INCOME_UPPER_BOUNDS.get(
user_profile["income_range"], 0
)
if user_income_upper > income_max:
eligible = False
reasons.append(
f"Rejected: Income range '{user_profile['income_range']}' "
f"exceeds limit of Rs {income_max:,}"
)
else:
reasons.append(
f"Passed: Income range '{user_profile['income_range']}' "
f"is within limit of Rs {income_max:,}"
)
else:
reasons.append("Passed: No income restriction")
# --- 3. Education check ---
min_edu = rules.get("min_education")
if min_edu is not None:
user_rank = EDUCATION_RANK.get(user_profile["education"], 0)
required_rank = EDUCATION_RANK.get(min_edu, 0)
if user_rank < required_rank:
eligible = False
reasons.append(
f"Rejected: Education '{user_profile['education']}' "
f"is below minimum '{min_edu}'"
)
else:
reasons.append(
f"Passed: Education '{user_profile['education']}' "
f"meets minimum '{min_edu}'"
)
else:
reasons.append("Passed: No education restriction")
# --- 4. Category check ---
eligible_cats = rules.get("eligible_categories", ["All"])
if "All" not in eligible_cats and user_profile["category"] not in eligible_cats:
eligible = False
reasons.append(
f"Rejected: Category '{user_profile['category']}' "
f"not in {eligible_cats}"
)
else:
reasons.append(
f"Passed: Category '{user_profile['category']}' is eligible"
)
# --- 5. Employment status check ---
eligible_emp = rules.get("eligible_employment", ["All"])
if "All" not in eligible_emp and user_profile["employment_status"] not in eligible_emp:
eligible = False
reasons.append(
f"Rejected: Employment status '{user_profile['employment_status']}' "
f"not in {eligible_emp}"
)
else:
reasons.append(
f"Passed: Employment status '{user_profile['employment_status']}' "
f"is eligible"
)
# --- 6. Gender check ---
eligible_gender = rules.get("eligible_gender", ["All"])
if "All" not in eligible_gender and user_profile["gender"] not in eligible_gender:
eligible = False
reasons.append(
f"Rejected: Gender '{user_profile['gender']}' "
f"not in {eligible_gender}"
)
else:
reasons.append(
f"Passed: Gender '{user_profile['gender']}' is eligible"
)
# --- 7. State check ---
eligible_states = rules.get("eligible_states", ["All"])
user_state = user_profile["state"]
if (
"All" not in eligible_states
and user_state != "Any"
and user_state not in eligible_states
):
eligible = False
reasons.append(
f"Rejected: State '{user_state}' not in eligible states"
)
else:
reasons.append(f"Passed: State '{user_state}' is eligible")
return {
"scheme_id": scheme["id"],
"scheme_name": scheme["name"],
"eligible": eligible,
"reasons": reasons,
}
# ---------------------------------------------------------------------------
# Tool 3: Summarize benefits in human-friendly language
# ---------------------------------------------------------------------------
def summarize_benefits(scheme: dict) -> str:
"""
Convert a scheme's benefits list into a simple, human-friendly
bullet-point summary string.
No LLM — pure string formatting.
"""
name = scheme["name"]
description = scheme.get("description", "")
benefits = scheme.get("benefits", [])
lines = [name]
if description:
lines.append(f" {description}")
lines.append(" Key Benefits:")
for benefit in benefits:
lines.append(f" - {benefit}")
return "\n".join(lines)