Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,8 +3,9 @@ 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
|
| 7 |
from flask_cors import CORS
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
CORS(app)
|
|
@@ -21,35 +22,33 @@ def chat():
|
|
| 21 |
if not role or not user_input or not company_code:
|
| 22 |
return jsonify({"error": "Missing role, message, or companyCode"}), 400
|
| 23 |
|
| 24 |
-
#
|
| 25 |
planner_prompt = {
|
| 26 |
"role": "system",
|
| 27 |
"content": (
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
"{ collections: [ { name:
|
| 31 |
-
"
|
| 32 |
-
|
|
|
|
| 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 |
-
|
| 44 |
-
fetched_data = fetch_data_from_collections(db, collections)
|
| 45 |
except Exception as e:
|
| 46 |
-
return jsonify({ "reply": f"⚠️
|
| 47 |
|
| 48 |
-
#
|
| 49 |
system_msg = build_system_message(company_code)
|
| 50 |
data_msg = {
|
| 51 |
"role": "system",
|
| 52 |
-
"content": f"
|
| 53 |
}
|
| 54 |
|
| 55 |
final_response = ask_gpt([system_msg, data_msg, user_msg])
|
|
|
|
| 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 # 🔄 using your function
|
| 7 |
from flask_cors import CORS
|
| 8 |
+
import json
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
CORS(app)
|
|
|
|
| 22 |
if not role or not user_input or not company_code:
|
| 23 |
return jsonify({"error": "Missing role, message, or companyCode"}), 400
|
| 24 |
|
| 25 |
+
# ✨ Ask Gemini to generate the Firestore fetch plan
|
| 26 |
planner_prompt = {
|
| 27 |
"role": "system",
|
| 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 |
user_msg = { "role": "user", "content": user_input }
|
| 39 |
planning_response = ask_gpt([planner_prompt, user_msg])
|
| 40 |
|
|
|
|
| 41 |
try:
|
|
|
|
| 42 |
plan = json.loads(planning_response)
|
| 43 |
+
firestore_data = fetch_data_from_firestore(plan)
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
+
return jsonify({ "reply": f"⚠️ Planning or Firestore error: {str(e)}" })
|
| 46 |
|
| 47 |
+
# ✨ Ask Gemini to analyze data + answer user
|
| 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])
|