naman1102 commited on
Commit
98a9782
·
1 Parent(s): f7505a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -3
app.py CHANGED
@@ -200,7 +200,8 @@ class BasicAgent:
200
  messages=[
201
  {"role": "user", "content": prompt},
202
  ],
203
- temperature=0.3,
 
204
  max_tokens=max_tokens,
205
  )
206
  return resp.choices[0].message.content.strip()
@@ -208,6 +209,20 @@ class BasicAgent:
208
  print(f"\nLLM Error: {str(e)}")
209
  raise
210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  def _generate_answer(self, state: AgentState) -> AgentState:
212
  prompt = f"""
213
  Answer this question using the materials provided.
@@ -220,8 +235,7 @@ Return ONLY this exact JSON object:
220
  """
221
  try:
222
  raw = self._call_llm(prompt, 300)
223
- data = json.loads(raw)
224
- answer = data["ANSWER"]
225
  state["final_answer"] = answer
226
  except Exception as e:
227
  print(f"\nLLM Error in answer generation: {str(e)}")
 
200
  messages=[
201
  {"role": "user", "content": prompt},
202
  ],
203
+ temperature=0,
204
+ top_p=0.1,
205
  max_tokens=max_tokens,
206
  )
207
  return resp.choices[0].message.content.strip()
 
209
  print(f"\nLLM Error: {str(e)}")
210
  raise
211
 
212
+ def _safe_parse(self, raw: str) -> str:
213
+ try:
214
+ return json.loads(raw)["ANSWER"]
215
+ except Exception:
216
+ # grab the first {...} in the text
217
+ match = re.search(r'\{.*?\}', raw, re.S)
218
+ if match:
219
+ try:
220
+ return json.loads(match.group())["ANSWER"]
221
+ except Exception:
222
+ pass
223
+ # as a last resort, strip everything before the first colon
224
+ return raw.split(':', 1)[-1].strip()
225
+
226
  def _generate_answer(self, state: AgentState) -> AgentState:
227
  prompt = f"""
228
  Answer this question using the materials provided.
 
235
  """
236
  try:
237
  raw = self._call_llm(prompt, 300)
238
+ answer = self._safe_parse(raw)
 
239
  state["final_answer"] = answer
240
  except Exception as e:
241
  print(f"\nLLM Error in answer generation: {str(e)}")