Spaces:
Sleeping
Sleeping
Update main.py
Browse files
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 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
if "error" in planning_result:
|
| 58 |
return jsonify({"reply": f"β οΈ Planning error: {planning_result['error']}"})
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
print("π οΈ Normalized Plan:", normalized_plan)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
except Exception as e:
|
| 67 |
-
return jsonify({"reply": f"β οΈ Firestore fetch error: {str(e)}"})
|
| 68 |
|
| 69 |
-
#
|
| 70 |
system_msg = build_system_message(company_code)
|
| 71 |
data_msg = {
|
| 72 |
"role": "system",
|
| 73 |
-
"content":
|
|
|
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
-
user_msg = {
|
| 76 |
-
|
| 77 |
final_response = ask_gpt([system_msg, data_msg, user_msg])
|
| 78 |
-
return jsonify({
|
| 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__":
|