Shubham170793 commited on
Commit
0dc8e87
·
verified ·
1 Parent(s): cb020cf

Update src/ingestion.py

Browse files
Files changed (1) hide show
  1. src/ingestion.py +19 -5
src/ingestion.py CHANGED
@@ -131,19 +131,20 @@ from gen_ai_hub.proxy.langchain.openai import ChatOpenAI
131
  def adaptive_fallback_toc(text: str, model_name: str = "gpt-4o"):
132
  """
133
  Uses SAP GenAI Hub proxy (same as QA pipeline) to infer a Table of Contents.
134
- This avoids manual auth and ensures consistent credentials across the app.
 
135
  """
136
  snippet = text[:7000]
137
 
138
- # Read GenAI proxy credentials (same JSON used by QA)
139
- creds_path = os.path.join(os.path.dirname(__file__), "GEN AI HUB PROXY.json")
140
  base_url = ""
141
 
 
 
142
  if os.path.exists(creds_path):
143
  try:
144
  with open(creds_path, "r") as f:
145
  creds = json.load(f)
146
- # Try all known locations for base URL
147
  base_url = (
148
  creds.get("base_url")
149
  or creds.get("serviceurls", {}).get("AI_API_URL", "")
@@ -151,11 +152,23 @@ def adaptive_fallback_toc(text: str, model_name: str = "gpt-4o"):
151
  )
152
  except Exception as e:
153
  print(f"⚠️ Could not read GenAI proxy credentials: {e}")
 
 
 
154
 
155
  if not base_url:
156
  print("⚠️ Missing AI_API_URL or base_url in credentials — skipping fallback.")
157
  return []
158
 
 
 
 
 
 
 
 
 
 
159
  try:
160
  print(f"⚙️ Invoking GenAI proxy for TOC inference using model: {model_name}")
161
  proxy_client = get_proxy_client("gen-ai-hub", base_url=base_url)
@@ -179,7 +192,7 @@ def adaptive_fallback_toc(text: str, model_name: str = "gpt-4o"):
179
  response = llm.invoke(prompt)
180
  response_text = getattr(response, "content", str(response))
181
 
182
- # Extract clean TOC-like lines
183
  lines = [
184
  re.sub(r"^[0-9.\-•\s]+", "", l.strip())
185
  for l in response_text.splitlines()
@@ -195,6 +208,7 @@ def adaptive_fallback_toc(text: str, model_name: str = "gpt-4o"):
195
  return []
196
 
197
 
 
198
  # ==========================================================
199
  # 3B️⃣ UNIFIED WRAPPER (Heuristic + AI Hybrid)
200
  # ==========================================================
 
131
  def adaptive_fallback_toc(text: str, model_name: str = "gpt-4o"):
132
  """
133
  Uses SAP GenAI Hub proxy (same as QA pipeline) to infer a Table of Contents.
134
+ This ensures consistent credentials, no manual token handling, and safe reuse
135
+ of your existing GEN AI HUB PROXY.json configuration.
136
  """
137
  snippet = text[:7000]
138
 
139
+ creds = {}
 
140
  base_url = ""
141
 
142
+ # ✅ Load credentials from same JSON as QA pipeline
143
+ creds_path = os.path.join(os.path.dirname(__file__), "GEN AI HUB PROXY.json")
144
  if os.path.exists(creds_path):
145
  try:
146
  with open(creds_path, "r") as f:
147
  creds = json.load(f)
 
148
  base_url = (
149
  creds.get("base_url")
150
  or creds.get("serviceurls", {}).get("AI_API_URL", "")
 
152
  )
153
  except Exception as e:
154
  print(f"⚠️ Could not read GenAI proxy credentials: {e}")
155
+ else:
156
+ print("⚠️ No SAP GenAI credentials file found — skipping AI fallback.")
157
+ return []
158
 
159
  if not base_url:
160
  print("⚠️ Missing AI_API_URL or base_url in credentials — skipping fallback.")
161
  return []
162
 
163
+ # ✅ Inject credentials into environment (matches QA setup)
164
+ os.environ.update({
165
+ "AICORE_AUTH_URL": creds.get("url", ""),
166
+ "AICORE_CLIENT_ID": creds.get("clientid") or creds.get("client_id", ""),
167
+ "AICORE_CLIENT_SECRET": creds.get("clientsecret") or creds.get("client_secret", ""),
168
+ "AICORE_RESOURCE_GROUP": "default",
169
+ "AICORE_BASE_URL": base_url
170
+ })
171
+
172
  try:
173
  print(f"⚙️ Invoking GenAI proxy for TOC inference using model: {model_name}")
174
  proxy_client = get_proxy_client("gen-ai-hub", base_url=base_url)
 
192
  response = llm.invoke(prompt)
193
  response_text = getattr(response, "content", str(response))
194
 
195
+ # Extract clean TOC-like lines
196
  lines = [
197
  re.sub(r"^[0-9.\-•\s]+", "", l.strip())
198
  for l in response_text.splitlines()
 
208
  return []
209
 
210
 
211
+
212
  # ==========================================================
213
  # 3B️⃣ UNIFIED WRAPPER (Heuristic + AI Hybrid)
214
  # ==========================================================