Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,12 +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 role_access import get_allowed_collections
|
|
|
|
| 5 |
from flask_cors import CORS
|
| 6 |
|
| 7 |
-
|
| 8 |
app = Flask(__name__)
|
| 9 |
-
CORS(app)
|
| 10 |
|
| 11 |
db = get_firestore_client()
|
| 12 |
|
|
@@ -15,22 +16,44 @@ def chat():
|
|
| 15 |
data = request.json
|
| 16 |
role = data.get('role')
|
| 17 |
user_input = data.get('message')
|
| 18 |
-
company_code = data.get('companyCode')
|
| 19 |
|
| 20 |
if not role or not user_input or not company_code:
|
| 21 |
return jsonify({"error": "Missing role, message, or companyCode"}), 400
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
system_msg = build_system_message(company_code)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
|
| 32 |
-
|
| 33 |
-
return jsonify({"reply":
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 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_collections # <-- new function we'll build
|
| 7 |
from flask_cors import CORS
|
| 8 |
|
|
|
|
| 9 |
app = Flask(__name__)
|
| 10 |
+
CORS(app)
|
| 11 |
|
| 12 |
db = get_firestore_client()
|
| 13 |
|
|
|
|
| 16 |
data = request.json
|
| 17 |
role = data.get('role')
|
| 18 |
user_input = data.get('message')
|
| 19 |
+
company_code = data.get('companyCode')
|
| 20 |
|
| 21 |
if not role or not user_input or not company_code:
|
| 22 |
return jsonify({"error": "Missing role, message, or companyCode"}), 400
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# 🔹 First step: instruct Gemini to tell us what to fetch
|
| 25 |
+
planner_prompt = {
|
| 26 |
+
"role": "system",
|
| 27 |
+
"content": (
|
| 28 |
+
"Your job is to act like a planner. Based on the user query, output a list of Firestore collections and filters needed to answer it."
|
| 29 |
+
"Return your result strictly in this JSON format:\n\n"
|
| 30 |
+
"{ collections: [ { name: 'collectionName', filters: { field: value } } ] }\n\n"
|
| 31 |
+
"Only include collections that are actually relevant. The user's companyCode is "
|
| 32 |
+
f"'{company_code}' and their role is '{role}'. Here is the question:\n\n{user_input}"
|
| 33 |
+
)
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
user_msg = { "role": "user", "content": user_input }
|
| 37 |
+
planning_response = ask_gpt([planner_prompt, user_msg])
|
| 38 |
+
|
| 39 |
+
# 🔹 Second step: fetch data based on the plan
|
| 40 |
+
try:
|
| 41 |
+
import json
|
| 42 |
+
plan = json.loads(planning_response)
|
| 43 |
+
collections = plan.get("collections", [])
|
| 44 |
+
fetched_data = fetch_data_from_collections(db, collections)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return jsonify({ "reply": f"⚠️ Failed to plan or fetch data: {str(e)}" })
|
| 47 |
|
| 48 |
+
# 🔹 Third step: answer the original question using real data
|
| 49 |
+
system_msg = build_system_message(company_code)
|
| 50 |
+
data_msg = {
|
| 51 |
+
"role": "system",
|
| 52 |
+
"content": f"The following data was retrieved from Firestore:\n\n{json.dumps(fetched_data)}"
|
| 53 |
}
|
| 54 |
|
| 55 |
+
final_response = ask_gpt([system_msg, data_msg, user_msg])
|
| 56 |
+
return jsonify({ "reply": final_response })
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
app.run(host="0.0.0.0", port=7860)
|