Update src/qa.py
Browse files
src/qa.py
CHANGED
|
@@ -48,34 +48,50 @@ except Exception as e:
|
|
| 48 |
_query_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2", cache_folder=CACHE_DIR)
|
| 49 |
|
| 50 |
# ==========================================================
|
| 51 |
-
# 3️⃣ GPT-4o via SAP Gen AI Hub
|
| 52 |
# ==========================================================
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
CRED_PATH = os.path.join(os.path.dirname(__file__), "GEN AI HUB PROXY.json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
try:
|
| 57 |
-
with open(CRED_PATH, "r") as key_file:
|
| 58 |
-
svcKey = json.load(key_file)
|
| 59 |
-
|
| 60 |
-
os.environ.update({
|
| 61 |
-
"AICORE_AUTH_URL": svcKey["url"],
|
| 62 |
-
"AICORE_CLIENT_ID": svcKey["clientid"],
|
| 63 |
-
"AICORE_CLIENT_SECRET": svcKey["clientsecret"],
|
| 64 |
-
"AICORE_RESOURCE_GROUP": "default",
|
| 65 |
-
"AICORE_BASE_URL": svcKey["serviceurls"]["AI_API_URL"]
|
| 66 |
-
})
|
| 67 |
-
|
| 68 |
-
proxy_client = get_proxy_client("gen-ai-hub")
|
| 69 |
-
chat_llm = ChatOpenAI(
|
| 70 |
-
proxy_model_name="gpt-4o",
|
| 71 |
-
proxy_client=proxy_client,
|
| 72 |
-
temperature=0.3,
|
| 73 |
-
max_tokens=1500
|
| 74 |
-
)
|
| 75 |
-
print("✅ GPT-4o (via Gen AI Hub) ready for generation.")
|
| 76 |
-
except Exception as e:
|
| 77 |
-
print(f"⚠️ Gen AI Hub setup failed: {e}")
|
| 78 |
-
chat_llm = None
|
| 79 |
|
| 80 |
# ==========================================================
|
| 81 |
# 4️⃣ Embedding Generator (batch-optimized)
|
|
@@ -234,14 +250,19 @@ def retrieve_chunks(query: str, index, chunks: list, top_k: int = 5,
|
|
| 234 |
return []
|
| 235 |
|
| 236 |
# ==========================================================
|
| 237 |
-
# 8️⃣ Answer Generation
|
| 238 |
# ==========================================================
|
| 239 |
def generate_answer(query: str, retrieved_chunks: list, reasoning_mode: bool = False):
|
| 240 |
if not retrieved_chunks:
|
| 241 |
return "Sorry, I couldn’t find relevant information in the document."
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
return "⚠️ GPT-4o not initialized. Check credentials or rebuild the Space."
|
| 244 |
|
|
|
|
| 245 |
context = "\n".join(f"[Chunk {i+1}] {chunk.strip()}" for i, chunk in enumerate(retrieved_chunks))
|
| 246 |
prompt = (REASONING_PROMPT if reasoning_mode else STRICT_PROMPT).format(context=context, query=query)
|
| 247 |
|
|
@@ -256,18 +277,23 @@ def generate_answer(query: str, retrieved_chunks: list, reasoning_mode: bool = F
|
|
| 256 |
{"role": "user", "content": prompt},
|
| 257 |
]
|
| 258 |
|
|
|
|
| 259 |
try:
|
| 260 |
-
response =
|
| 261 |
return response.content.strip()
|
| 262 |
except Exception as e:
|
| 263 |
print(f"⚠️ GPT-4o generation failed: {e}")
|
| 264 |
return "⚠️ Error: Could not generate an answer."
|
| 265 |
|
|
|
|
| 266 |
# ==========================================================
|
| 267 |
# 9️⃣ Generic Text Generation Helper (for AI suggestions)
|
| 268 |
# ==========================================================
|
| 269 |
def genai_generate(prompt: str) -> str:
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
| 271 |
raise RuntimeError("⚠️ GPT-4o not initialized. Check credentials or rebuild the Space.")
|
| 272 |
|
| 273 |
messages = [
|
|
@@ -276,7 +302,7 @@ def genai_generate(prompt: str) -> str:
|
|
| 276 |
]
|
| 277 |
|
| 278 |
try:
|
| 279 |
-
response =
|
| 280 |
return response.content.strip()
|
| 281 |
except Exception as e:
|
| 282 |
print(f"⚠️ genai_generate() failed: {e}")
|
|
|
|
| 48 |
_query_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2", cache_folder=CACHE_DIR)
|
| 49 |
|
| 50 |
# ==========================================================
|
| 51 |
+
# 3️⃣ GPT-4o via SAP Gen AI Hub — Lazy / On-demand initialization
|
| 52 |
# ==========================================================
|
| 53 |
+
from gen_ai_hub.proxy.core.proxy_clients import get_proxy_client
|
| 54 |
+
from gen_ai_hub.proxy.langchain.openai import ChatOpenAI
|
| 55 |
+
|
| 56 |
CRED_PATH = os.path.join(os.path.dirname(__file__), "GEN AI HUB PROXY.json")
|
| 57 |
+
_chat_llm = None # cached instance
|
| 58 |
+
|
| 59 |
+
def get_chat_llm(model_name: str = "gpt-4o", temperature: float = 0.3, max_tokens: int = 1500):
|
| 60 |
+
"""
|
| 61 |
+
Lazily initializes ChatOpenAI via Gen AI Hub proxy.
|
| 62 |
+
Only runs when first needed; cached afterward.
|
| 63 |
+
"""
|
| 64 |
+
global _chat_llm
|
| 65 |
+
if _chat_llm is not None:
|
| 66 |
+
return _chat_llm
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
# Optional: set environment variables from service key if present
|
| 70 |
+
if os.path.exists(CRED_PATH):
|
| 71 |
+
with open(CRED_PATH, "r") as key_file:
|
| 72 |
+
svcKey = json.load(key_file)
|
| 73 |
+
os.environ.update({
|
| 74 |
+
"AICORE_AUTH_URL": svcKey.get("url", ""),
|
| 75 |
+
"AICORE_CLIENT_ID": svcKey.get("clientid", ""),
|
| 76 |
+
"AICORE_CLIENT_SECRET": svcKey.get("clientsecret", ""),
|
| 77 |
+
"AICORE_BASE_URL": svcKey.get("serviceurls", {}).get("AI_API_URL", ""),
|
| 78 |
+
})
|
| 79 |
+
|
| 80 |
+
proxy_client = get_proxy_client("gen-ai-hub")
|
| 81 |
+
_chat_llm = ChatOpenAI(
|
| 82 |
+
proxy_model_name=model_name,
|
| 83 |
+
proxy_client=proxy_client,
|
| 84 |
+
temperature=temperature,
|
| 85 |
+
max_tokens=max_tokens,
|
| 86 |
+
)
|
| 87 |
+
print(f"✅ GPT-4o (via Gen AI Hub) initialized lazily for model: {model_name}")
|
| 88 |
+
return _chat_llm
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print(f"⚠️ Gen AI Hub lazy init failed: {e}")
|
| 92 |
+
_chat_llm = None
|
| 93 |
+
raise
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# ==========================================================
|
| 97 |
# 4️⃣ Embedding Generator (batch-optimized)
|
|
|
|
| 250 |
return []
|
| 251 |
|
| 252 |
# ==========================================================
|
| 253 |
+
# 8️⃣ Answer Generation (Lazy GPT-4o Initialization)
|
| 254 |
# ==========================================================
|
| 255 |
def generate_answer(query: str, retrieved_chunks: list, reasoning_mode: bool = False):
|
| 256 |
if not retrieved_chunks:
|
| 257 |
return "Sorry, I couldn’t find relevant information in the document."
|
| 258 |
+
|
| 259 |
+
# Try lazy initialization
|
| 260 |
+
try:
|
| 261 |
+
chat_llm_local = get_chat_llm()
|
| 262 |
+
except Exception:
|
| 263 |
return "⚠️ GPT-4o not initialized. Check credentials or rebuild the Space."
|
| 264 |
|
| 265 |
+
# Build context and prompt
|
| 266 |
context = "\n".join(f"[Chunk {i+1}] {chunk.strip()}" for i, chunk in enumerate(retrieved_chunks))
|
| 267 |
prompt = (REASONING_PROMPT if reasoning_mode else STRICT_PROMPT).format(context=context, query=query)
|
| 268 |
|
|
|
|
| 277 |
{"role": "user", "content": prompt},
|
| 278 |
]
|
| 279 |
|
| 280 |
+
# Invoke GPT-4o
|
| 281 |
try:
|
| 282 |
+
response = chat_llm_local.invoke(messages)
|
| 283 |
return response.content.strip()
|
| 284 |
except Exception as e:
|
| 285 |
print(f"⚠️ GPT-4o generation failed: {e}")
|
| 286 |
return "⚠️ Error: Could not generate an answer."
|
| 287 |
|
| 288 |
+
|
| 289 |
# ==========================================================
|
| 290 |
# 9️⃣ Generic Text Generation Helper (for AI suggestions)
|
| 291 |
# ==========================================================
|
| 292 |
def genai_generate(prompt: str) -> str:
|
| 293 |
+
# Try lazy initialization
|
| 294 |
+
try:
|
| 295 |
+
chat_llm_local = get_chat_llm()
|
| 296 |
+
except Exception:
|
| 297 |
raise RuntimeError("⚠️ GPT-4o not initialized. Check credentials or rebuild the Space.")
|
| 298 |
|
| 299 |
messages = [
|
|
|
|
| 302 |
]
|
| 303 |
|
| 304 |
try:
|
| 305 |
+
response = chat_llm_local.invoke(messages)
|
| 306 |
return response.content.strip()
|
| 307 |
except Exception as e:
|
| 308 |
print(f"⚠️ genai_generate() failed: {e}")
|