yoursdvniel commited on
Commit
1483182
·
verified ·
1 Parent(s): b643904

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +13 -14
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 fetch_data_from_collections # <-- new function we'll build
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
- # 🔹 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])
 
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])