Spaces:
Runtime error
Runtime error
Nyha15 commited on
Commit ·
c54f7e6
1
Parent(s): 5c95ea1
Refactored
Browse files
app.py
CHANGED
|
@@ -145,6 +145,16 @@ class InternationalStudentDataCollector:
|
|
| 145 |
f"5 facts on credit building for {country} students: cards, history, pitfalls."
|
| 146 |
)
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
# =======================================
|
| 149 |
# RAG Knowledge Base
|
| 150 |
# =======================================
|
|
@@ -207,11 +217,10 @@ class KnowledgeBase:
|
|
| 207 |
docs = retr.get_relevant_documents(query)
|
| 208 |
return [d.page_content for d in docs]
|
| 209 |
|
| 210 |
-
# Preload KBs
|
| 211 |
-
COMMON_COUNTRIES = ["India","China"]
|
| 212 |
-
DOMAINS = ["banking","credit","tax"]
|
| 213 |
for dom in DOMAINS:
|
| 214 |
kb = KnowledgeBase(dom)
|
|
|
|
| 215 |
for c in COMMON_COUNTRIES:
|
| 216 |
threading.Thread(target=kb._init_country, args=(c,), daemon=True).start()
|
| 217 |
|
|
@@ -222,7 +231,7 @@ for dom in DOMAINS:
|
|
| 222 |
class SpecialistAgent:
|
| 223 |
def __init__(self, name: str, domain: str):
|
| 224 |
self.name = name
|
| 225 |
-
self.kb =
|
| 226 |
self.llm = ChatOpenAI(temperature=0.2)
|
| 227 |
|
| 228 |
def run(self, query: str, country: str) -> str:
|
|
@@ -234,11 +243,10 @@ class SpecialistAgent:
|
|
| 234 |
log_workflow(f"{self.name} done")
|
| 235 |
return resp.content
|
| 236 |
|
| 237 |
-
# Instantiate specialists
|
| 238 |
BankingAdvisor = lambda: SpecialistAgent("Banking Advisor","banking")
|
| 239 |
-
CreditBuilder
|
| 240 |
TaxSpecialist = lambda: SpecialistAgent("Tax Specialist","tax")
|
| 241 |
-
# Add more as needed
|
| 242 |
|
| 243 |
# =======================================
|
| 244 |
# Coordinator Agent
|
|
@@ -249,25 +257,34 @@ class CoordinatorAgent:
|
|
| 249 |
self.llm = ChatOpenAI(temperature=0.3)
|
| 250 |
self.specialists = {
|
| 251 |
"banking": BankingAdvisor(),
|
| 252 |
-
"credit":
|
| 253 |
-
"tax":
|
| 254 |
}
|
| 255 |
|
| 256 |
def run(self, query: str, profile: Dict[str,Any]) -> str:
|
| 257 |
clear_workflow_log()
|
| 258 |
country = profile.get("home_country","unknown")
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
plans = {"conservative":"...","balanced":"...","growth":"..."}
|
| 263 |
-
# 3. Synthesis & formatting
|
| 264 |
lines = ["# Your Personalized Financial Advice\n"]
|
| 265 |
-
for
|
| 266 |
-
lines.append(f"## {
|
| 267 |
-
for para in
|
| 268 |
lines.append(" "+para.replace("\n","\n "))
|
| 269 |
lines.append("")
|
| 270 |
-
lines.append("## Multi-Path
|
| 271 |
lines.append(json.dumps(plans,indent=2))
|
| 272 |
lines.append("```")
|
| 273 |
formatted = "\n".join(lines)
|
|
|
|
| 145 |
f"5 facts on credit building for {country} students: cards, history, pitfalls."
|
| 146 |
)
|
| 147 |
|
| 148 |
+
# =======================================
|
| 149 |
+
# Shared RAG Knowledge Base Instances
|
| 150 |
+
# =======================================
|
| 151 |
+
|
| 152 |
+
KB_INSTANCES: Dict[str, 'KnowledgeBase'] = {}
|
| 153 |
+
COMMON_COUNTRIES = ["India", "China"]
|
| 154 |
+
DOMAINS = ["banking", "credit", "tax"]
|
| 155 |
+
|
| 156 |
+
# Placeholder for class KnowledgeBase declaration
|
| 157 |
+
|
| 158 |
# =======================================
|
| 159 |
# RAG Knowledge Base
|
| 160 |
# =======================================
|
|
|
|
| 217 |
docs = retr.get_relevant_documents(query)
|
| 218 |
return [d.page_content for d in docs]
|
| 219 |
|
| 220 |
+
# Preload and register shared KBs
|
|
|
|
|
|
|
| 221 |
for dom in DOMAINS:
|
| 222 |
kb = KnowledgeBase(dom)
|
| 223 |
+
KB_INSTANCES[dom] = kb
|
| 224 |
for c in COMMON_COUNTRIES:
|
| 225 |
threading.Thread(target=kb._init_country, args=(c,), daemon=True).start()
|
| 226 |
|
|
|
|
| 231 |
class SpecialistAgent:
|
| 232 |
def __init__(self, name: str, domain: str):
|
| 233 |
self.name = name
|
| 234 |
+
self.kb = KB_INSTANCES[domain] # use shared, preloaded KB
|
| 235 |
self.llm = ChatOpenAI(temperature=0.2)
|
| 236 |
|
| 237 |
def run(self, query: str, country: str) -> str:
|
|
|
|
| 243 |
log_workflow(f"{self.name} done")
|
| 244 |
return resp.content
|
| 245 |
|
| 246 |
+
# Instantiate specialists using shared KB
|
| 247 |
BankingAdvisor = lambda: SpecialistAgent("Banking Advisor","banking")
|
| 248 |
+
CreditBuilder = lambda: SpecialistAgent("Credit Builder","credit")
|
| 249 |
TaxSpecialist = lambda: SpecialistAgent("Tax Specialist","tax")
|
|
|
|
| 250 |
|
| 251 |
# =======================================
|
| 252 |
# Coordinator Agent
|
|
|
|
| 257 |
self.llm = ChatOpenAI(temperature=0.3)
|
| 258 |
self.specialists = {
|
| 259 |
"banking": BankingAdvisor(),
|
| 260 |
+
"credit": CreditBuilder(),
|
| 261 |
+
"tax": TaxSpecialist()
|
| 262 |
}
|
| 263 |
|
| 264 |
def run(self, query: str, profile: Dict[str,Any]) -> str:
|
| 265 |
clear_workflow_log()
|
| 266 |
country = profile.get("home_country","unknown")
|
| 267 |
+
advice_map: Dict[str,str] = {}
|
| 268 |
+
q = query.lower()
|
| 269 |
+
# domain-based filtering
|
| 270 |
+
if "bank" in q or "account" in q:
|
| 271 |
+
advice_map["banking"] = self.specialists["banking"].run(query,country)
|
| 272 |
+
if "credit" in q:
|
| 273 |
+
advice_map["credit"] = self.specialists["credit"].run(query,country)
|
| 274 |
+
if "tax" in q or "treaty" in q:
|
| 275 |
+
advice_map["tax"] = self.specialists["tax"].run(query,country)
|
| 276 |
+
# fallback: if no specialist matched, run banking by default
|
| 277 |
+
if not advice_map:
|
| 278 |
+
advice_map["banking"] = self.specialists["banking"].run(query,country)
|
| 279 |
+
# synthesize
|
| 280 |
plans = {"conservative":"...","balanced":"...","growth":"..."}
|
|
|
|
| 281 |
lines = ["# Your Personalized Financial Advice\n"]
|
| 282 |
+
for d,t in advice_map.items():
|
| 283 |
+
lines.append(f"## {d.capitalize()}\n")
|
| 284 |
+
for para in t.strip().split("\n\n"):
|
| 285 |
lines.append(" "+para.replace("\n","\n "))
|
| 286 |
lines.append("")
|
| 287 |
+
lines.append("## Multi-Path Plans\n```json")
|
| 288 |
lines.append(json.dumps(plans,indent=2))
|
| 289 |
lines.append("```")
|
| 290 |
formatted = "\n".join(lines)
|