yoursdvniel commited on
Commit
ac1b7f1
Β·
verified Β·
1 Parent(s): b72b771

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +32 -25
main.py CHANGED
@@ -8,6 +8,7 @@ from prompt_instructions import build_system_message
8
  from role_access import get_allowed_collections
9
  from data_fetcher import fetch_data_from_firestore
10
  from data_planner import determine_data_requirements # 🧠 Gemini planner
 
11
 
12
  app = Flask(__name__)
13
  CORS(app)
@@ -15,23 +16,22 @@ CORS(app)
15
  db = get_firestore_client()
16
 
17
  # πŸ”§ Normalize Gemini plan into proper Firestore fetch format
18
- def normalize_plan(plan: dict) -> dict:
19
  filters = plan.get("filters", {})
20
  filter_list = []
21
 
22
  for k, v in filters.items():
23
- # πŸ”„ Adjust "running" to match actual Firestore value
 
 
24
  if k == "status" and v == "running":
25
  v = "active"
 
26
  filter_list.append({"field": k, "op": "==", "value": v})
27
 
28
  return {
29
  "collections": [
30
- {
31
- "name": col,
32
- "filters": filter_list,
33
- "limit": 50
34
- }
35
  for col in plan.get("collections", [])
36
  ]
37
  }
@@ -43,39 +43,46 @@ def chat():
43
  role = data.get('role')
44
  user_input = data.get('message')
45
  company_code = data.get('companyCode')
46
- user_id = data.get('userId') # βœ…
47
 
48
  if not role or not user_input or not company_code or not user_id:
49
  return jsonify({"error": "Missing role, message, companyCode, or userId"}), 400
50
 
51
- print("🧠 Incoming user message:", user_input)
52
-
53
- # 🧠 Step 1: Gemini plans what data to fetch
54
- planning_result = determine_data_requirements(user_input, company_code, user_id)
55
- print("πŸ“‹ Raw planning result:", planning_result)
 
 
56
 
 
 
 
 
 
 
57
  if "error" in planning_result:
58
  return jsonify({"reply": f"⚠️ Planning error: {planning_result['error']}"})
59
 
60
- try:
61
- normalized_plan = normalize_plan(planning_result)
62
- print("πŸ› οΈ Normalized Plan:", normalized_plan)
63
 
64
- firestore_data = fetch_data_from_firestore(normalized_plan)
65
- print("πŸ“¦ Fetched Firestore Data:", firestore_data)
66
- except Exception as e:
67
- return jsonify({"reply": f"⚠️ Firestore fetch error: {str(e)}"})
68
 
69
- # πŸ€– Step 2: Use Gemini to interpret and answer
70
  system_msg = build_system_message(company_code)
71
  data_msg = {
72
  "role": "system",
73
- "content": f"Here is the data from Firestore:\n{json.dumps(firestore_data)}"
 
 
 
74
  }
75
- user_msg = { "role": "user", "content": user_input }
76
-
77
  final_response = ask_gpt([system_msg, data_msg, user_msg])
78
- return jsonify({ "reply": final_response })
79
 
80
 
81
  if __name__ == "__main__":
 
8
  from role_access import get_allowed_collections
9
  from data_fetcher import fetch_data_from_firestore
10
  from data_planner import determine_data_requirements # 🧠 Gemini planner
11
+ from resolver import resolve_user_context
12
 
13
  app = Flask(__name__)
14
  CORS(app)
 
16
  db = get_firestore_client()
17
 
18
  # πŸ”§ Normalize Gemini plan into proper Firestore fetch format
19
+ def normalize_plan(plan: dict, token_map: dict) -> dict:
20
  filters = plan.get("filters", {})
21
  filter_list = []
22
 
23
  for k, v in filters.items():
24
+ if isinstance(v, str) and v in token_map:
25
+ v = token_map[v]
26
+
27
  if k == "status" and v == "running":
28
  v = "active"
29
+
30
  filter_list.append({"field": k, "op": "==", "value": v})
31
 
32
  return {
33
  "collections": [
34
+ {"name": col, "filters": filter_list, "limit": 50}
 
 
 
 
35
  for col in plan.get("collections", [])
36
  ]
37
  }
 
43
  role = data.get('role')
44
  user_input = data.get('message')
45
  company_code = data.get('companyCode')
46
+ user_id = data.get('userId')
47
 
48
  if not role or not user_input or not company_code or not user_id:
49
  return jsonify({"error": "Missing role, message, companyCode, or userId"}), 400
50
 
51
+ # πŸ”Ž Resolve current user's email + participantId
52
+ ctx = resolve_user_context(user_id, company_code)
53
+ token_map = {
54
+ "{{participantId}}": ctx.get("participantId"),
55
+ "{{userEmail}}": ctx.get("email"),
56
+ "{{userId}}": ctx.get("uid"),
57
+ }
58
 
59
+ # 🧠 Plan
60
+ planning_result = determine_data_requirements(
61
+ user_input, company_code, user_id,
62
+ participant_email=ctx.get("email"),
63
+ participant_id=ctx.get("participantId"),
64
+ )
65
  if "error" in planning_result:
66
  return jsonify({"reply": f"⚠️ Planning error: {planning_result['error']}"})
67
 
68
+ # πŸ› οΈ Normalize & replace tokens
69
+ normalized_plan = normalize_plan(planning_result, token_map)
 
70
 
71
+ # πŸ“₯ Fetch
72
+ firestore_data = fetch_data_from_firestore(normalized_plan)
 
 
73
 
74
+ # 🧩 Add current-user context to the model explicitly (helps answering)
75
  system_msg = build_system_message(company_code)
76
  data_msg = {
77
  "role": "system",
78
+ "content": (
79
+ f"CurrentUserContext: {json.dumps(ctx)}\n"
80
+ f"Here is the data from Firestore:\n{json.dumps(firestore_data)}"
81
+ )
82
  }
83
+ user_msg = {"role": "user", "content": user_input}
 
84
  final_response = ask_gpt([system_msg, data_msg, user_msg])
85
+ return jsonify({"reply": final_response})
86
 
87
 
88
  if __name__ == "__main__":