Shubham170793 commited on
Commit
0f5b8d7
·
verified ·
1 Parent(s): 59078ee

Update src/qa.py

Browse files
Files changed (1) hide show
  1. src/qa.py +26 -1
src/qa.py CHANGED
@@ -6,6 +6,7 @@ qa.py — GPT-4o (SAP Gen AI Hub) + ReRank Retrieval
6
  ✅ Embedding caching (per PDF)
7
  ✅ Smart factual mode (fast)
8
  ✅ Deep reasoning mode (ChatGPT-like)
 
9
  """
10
 
11
  import os
@@ -247,7 +248,31 @@ def generate_answer(query: str, retrieved_chunks: list, reasoning_mode: bool = F
247
  return "⚠️ Error: Could not generate an answer."
248
 
249
  # ==========================================================
250
- # 8️⃣ Local Test
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
  # ==========================================================
252
  if __name__ == "__main__":
253
  from vectorstore import build_faiss_index
 
6
  ✅ Embedding caching (per PDF)
7
  ✅ Smart factual mode (fast)
8
  ✅ Deep reasoning mode (ChatGPT-like)
9
+ ✅ genai_generate() helper for suggestions
10
  """
11
 
12
  import os
 
248
  return "⚠️ Error: Could not generate an answer."
249
 
250
  # ==========================================================
251
+ # 8️⃣ Generic Text Generation Helper (for AI suggestions)
252
+ # ==========================================================
253
+ def genai_generate(prompt: str) -> str:
254
+ """
255
+ Utility for single-turn GPT-4o generation (e.g., query suggestions, summaries).
256
+ Uses the same SAP Gen AI Hub connection as main assistant.
257
+ """
258
+ global chat_llm
259
+ if chat_llm is None:
260
+ raise RuntimeError("⚠️ GPT-4o not initialized. Check credentials or rebuild the Space.")
261
+
262
+ messages = [
263
+ {"role": "system", "content": "You are a concise, intelligent text generator."},
264
+ {"role": "user", "content": prompt.strip()},
265
+ ]
266
+
267
+ try:
268
+ response = chat_llm.invoke(messages)
269
+ return response.content.strip()
270
+ except Exception as e:
271
+ print(f"⚠️ genai_generate() failed: {e}")
272
+ return "⚠️ Unable to generate response."
273
+
274
+ # ==========================================================
275
+ # 9️⃣ Local Test
276
  # ==========================================================
277
  if __name__ == "__main__":
278
  from vectorstore import build_faiss_index