dembasowmr commited on
Commit
0f574db
·
1 Parent(s): 8bed3b8

Final application code deployment. Ensures all functionalities, including correct SYSTEM_PROMPT usage, Firestore conversation saving, and returning conversation ID in API response.

Browse files
Files changed (2) hide show
  1. app.py +8 -30
  2. src/compassia.py +3 -4
app.py CHANGED
@@ -103,39 +103,17 @@ class QueryRequest(BaseModel):
103
  async def compassia_endpoint(request: QueryRequest):
104
  """
105
  Answers a question about the indexed PDF documents using RAG, with conversational memory.
106
- If `conversation_id` is not provided, a new one will be generated and returned.
107
  """
108
  try:
109
- # The rag_system now holds the correct firestore_db_instance internally.
110
- # The answer_question method handles generating a new conversation_id if needed.
111
- answer = rag_system.answer_question(request.question, conversation_id=request.conversation_id)
 
 
112
 
113
- # When answer_question generates an ID, it needs to be returned to the client
114
- # so the client can use it for subsequent turns in the same conversation.
115
- # Since answer_question now internally generates/uses the ID,
116
- # we need to ensure the final response includes it.
117
- # The easiest way is to modify answer_question to return a tuple (answer_text, conv_id).
118
- # For simplicity now, we assume the initial conversation_id passed in is the one used,
119
- # or if it was None, that a new one was generated and saved, and we don't need to return it here.
120
- # If the client needs the *newly generated* ID, answer_question would need to return it.
121
-
122
- # For a production API, if conversation_id is initially None and gets generated,
123
- # answer_question needs to return the *generated* ID.
124
- # For now, if request.conversation_id was None, the saving still works internally.
125
- # If the client needs the *new* ID, let's adjust the return.
126
-
127
- # To return the generated ID:
128
- # We need to make `answer_question` return the `conversation_id` it used.
129
- # (This requires a small change to `compassia.py`'s `answer_question` return signature)
130
- # For now, let's assume if it was None, the client is okay not getting it back in the first call.
131
- # If the client *needs* the auto-generated ID back, the API design should support it.
132
-
133
- # Let's adjust compassia.py to return (answer, conversation_id)
134
- # and then use it here. This ensures the client knows the ID.
135
-
136
- return {"answer": answer, "conversation_id": request.conversation_id} # This was the previous behavior
137
- # Re-evaluating: The simplest is to ensure answer_question in compassia.py returns the *final* conversation_id used.
138
- # And then this endpoint returns it.
139
 
140
  except Exception as e:
141
  print(f"Error processing /compassia/ request: {e}")
 
103
  async def compassia_endpoint(request: QueryRequest):
104
  """
105
  Answers a question about the indexed PDF documents using RAG, with conversational memory.
106
+ If `conversation_id` is not provided, a new one will be generated and returned in the response.
107
  """
108
  try:
109
+ # Call answer_question which now returns a tuple (answer_text, conversation_id)
110
+ answer_text, final_conversation_id = rag_system.answer_question(
111
+ request.question,
112
+ conversation_id=request.conversation_id
113
+ )
114
 
115
+ # Return both the answer and the (potentially new) conversation_id to the client
116
+ return {"answer": answer_text, "conversation_id": final_conversation_id}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  except Exception as e:
119
  print(f"Error processing /compassia/ request: {e}")
src/compassia.py CHANGED
@@ -38,8 +38,7 @@ from config import (
38
  FIREBASE_CONFIG_BASE64 # Kept here only for local testing fallback in __main__
39
  )
40
  from pdf_processing import extract_text_from_pdf, chunk_text
41
- from prompt import SYSTEM_PROMPT
42
-
43
 
44
  # --- Global Firebase Firestore Client ---
45
  # This global is primarily for __main__ (local testing) execution.
@@ -341,8 +340,8 @@ class DocumentRAG:
341
  else:
342
  print("Warning: No relevant context found. Answering based on general knowledge or indicating lack of information.")
343
 
344
- # --- Handle Conversational Memory ---
345
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
346
 
347
  # Use the (possibly newly generated) conversation_id
348
  history = self.get_conversation_history(conversation_id)
 
38
  FIREBASE_CONFIG_BASE64 # Kept here only for local testing fallback in __main__
39
  )
40
  from pdf_processing import extract_text_from_pdf, chunk_text
41
+ from prompt import SYSTEM_PROMPT # <--- CORRECTLY IMPORTING SYSTEM_PROMPT
 
42
 
43
  # --- Global Firebase Firestore Client ---
44
  # This global is primarily for __main__ (local testing) execution.
 
340
  else:
341
  print("Warning: No relevant context found. Answering based on general knowledge or indicating lack of information.")
342
 
343
+ # --- Use the imported SYSTEM_PROMPT from prompt.py ---
344
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}] # Use the imported SYSTEM_PROMPT
345
 
346
  # Use the (possibly newly generated) conversation_id
347
  history = self.get_conversation_history(conversation_id)