Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
|
|
|
|
|
|
|
|
|
| 2 |
from firestore_client import get_firestore_client
|
| 3 |
from openai_client import ask_gpt
|
| 4 |
from prompt_instructions import build_system_message
|
| 5 |
from role_access import get_allowed_collections
|
| 6 |
-
from data_fetcher import fetch_data_from_firestore
|
| 7 |
-
from
|
| 8 |
-
import json
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
CORS(app)
|
|
@@ -22,34 +24,26 @@ def chat():
|
|
| 22 |
if not role or not user_input or not company_code:
|
| 23 |
return jsonify({"error": "Missing role, message, or companyCode"}), 400
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"content": (
|
| 29 |
-
"You are a planning agent. Given a user query and context, return a list of Firestore collections to query.\n"
|
| 30 |
-
"Respond ONLY in this JSON format:\n"
|
| 31 |
-
"{ \"collections\": [ { \"name\": \"collectionName\", \"filters\": [ {\"field\": \"\", \"op\": \"==\", \"value\": \"\"} ], \"limit\": 50 } ] }\n"
|
| 32 |
-
f"The companyCode is '{company_code}' and role is '{role}'.\n"
|
| 33 |
-
"Do not generate explanations. Only the raw JSON.\n\n"
|
| 34 |
-
f"User query: {user_input}"
|
| 35 |
-
)
|
| 36 |
-
}
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
except Exception as e:
|
| 45 |
-
return jsonify({
|
| 46 |
|
| 47 |
-
#
|
| 48 |
system_msg = build_system_message(company_code)
|
| 49 |
data_msg = {
|
| 50 |
"role": "system",
|
| 51 |
"content": f"Here is the data from Firestore:\n{json.dumps(firestore_data)}"
|
| 52 |
}
|
|
|
|
| 53 |
|
| 54 |
final_response = ask_gpt([system_msg, data_msg, user_msg])
|
| 55 |
return jsonify({ "reply": final_response })
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
from firestore_client import get_firestore_client
|
| 6 |
from openai_client import ask_gpt
|
| 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 # 🧠 Replaces inline planner
|
|
|
|
| 11 |
|
| 12 |
app = Flask(__name__)
|
| 13 |
CORS(app)
|
|
|
|
| 24 |
if not role or not user_input or not company_code:
|
| 25 |
return jsonify({"error": "Missing role, message, or companyCode"}), 400
|
| 26 |
|
| 27 |
+
# 🧠 Step 1: Use Gemini to plan the data to fetch
|
| 28 |
+
planning_result = determine_data_requirements(user_input, company_code)
|
| 29 |
+
print("📋 Data Plan:", planning_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
if "error" in planning_result:
|
| 32 |
+
return jsonify({"reply": f"⚠️ Planning error: {planning_result['error']}"})
|
| 33 |
|
| 34 |
try:
|
| 35 |
+
firestore_data = fetch_data_from_firestore(planning_result)
|
| 36 |
+
print("📦 Firestore Fetched Data:", firestore_data)
|
| 37 |
except Exception as e:
|
| 38 |
+
return jsonify({"reply": f"⚠️ Firestore fetch error: {str(e)}"})
|
| 39 |
|
| 40 |
+
# 💬 Step 2: Ask Gemini to answer using that data
|
| 41 |
system_msg = build_system_message(company_code)
|
| 42 |
data_msg = {
|
| 43 |
"role": "system",
|
| 44 |
"content": f"Here is the data from Firestore:\n{json.dumps(firestore_data)}"
|
| 45 |
}
|
| 46 |
+
user_msg = { "role": "user", "content": user_input }
|
| 47 |
|
| 48 |
final_response = ask_gpt([system_msg, data_msg, user_msg])
|
| 49 |
return jsonify({ "reply": final_response })
|