naman1102 commited on
Commit
302c628
·
1 Parent(s): 4a3e964

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -26
app.py CHANGED
@@ -34,20 +34,28 @@ os.makedirs(LOGS_DIR, exist_ok=True)
34
 
35
  def log_to_file(task_id: str, question: str, log_data: Dict[str, Any]):
36
  """Store logs for a question in a JSON file."""
37
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
38
- filename = f"{LOGS_DIR}/question_{task_id}_{timestamp}.json"
39
-
40
- log_entry = {
41
- "task_id": task_id,
42
- "question": question,
43
- "timestamp": timestamp,
44
- "logs": log_data
45
- }
46
-
47
- with open(filename, 'w', encoding='utf-8') as f:
48
- json.dump(log_entry, f, indent=2, ensure_ascii=False)
49
-
50
- print(f"Logs saved to {filename}")
 
 
 
 
 
 
 
 
51
 
52
  class AgentState(TypedDict):
53
  question: Annotated[str, override]
@@ -154,10 +162,12 @@ Return ONLY a Python dictionary in this exact format, with no other text or expl
154
  print(f"LLM Response: {llm_response}")
155
 
156
  # Log the analysis step
157
- state["logs"]["analyze"] = {
158
- "prompt": prompt,
159
- "response": llm_response,
160
- "timestamp": datetime.now().isoformat()
 
 
161
  }
162
 
163
  analysis = ast.literal_eval(llm_response)
@@ -176,9 +186,11 @@ Return ONLY a Python dictionary in this exact format, with no other text or expl
176
  state["current_step"] = 'search'
177
 
178
  # Log the error
179
- state["logs"]["analyze_error"] = {
180
- "error": str(e),
181
- "timestamp": datetime.now().isoformat()
 
 
182
  }
183
 
184
  return state
@@ -300,12 +312,18 @@ Return ONLY the direct answer to the question. Do not include any explanations,
300
  # Run the workflow
301
  final_state = self.workflow.invoke(initial_state)
302
 
 
 
 
303
  # Save logs to file
304
- log_to_file(
305
- task_id=final_state["task_id"],
306
- question=final_state["question"],
307
- log_data=final_state["logs"]
308
- )
 
 
 
309
 
310
  return final_state["final_answer"]
311
 
 
34
 
35
  def log_to_file(task_id: str, question: str, log_data: Dict[str, Any]):
36
  """Store logs for a question in a JSON file."""
37
+ try:
38
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
39
+ filename = f"{LOGS_DIR}/question_{task_id}_{timestamp}.json"
40
+
41
+ log_entry = {
42
+ "task_id": task_id,
43
+ "question": question,
44
+ "timestamp": timestamp,
45
+ "logs": log_data
46
+ }
47
+
48
+ print(f"\n=== Saving Logs ===")
49
+ print(f"Task ID: {task_id}")
50
+ print(f"Question: {question}")
51
+ print(f"Log Data: {json.dumps(log_data, indent=2)}")
52
+
53
+ with open(filename, 'w', encoding='utf-8') as f:
54
+ json.dump(log_entry, f, indent=2, ensure_ascii=False)
55
+
56
+ print(f"Logs saved to {filename}")
57
+ except Exception as e:
58
+ print(f"Error saving logs: {e}")
59
 
60
  class AgentState(TypedDict):
61
  question: Annotated[str, override]
 
162
  print(f"LLM Response: {llm_response}")
163
 
164
  # Log the analysis step
165
+ state["logs"] = {
166
+ "analyze": {
167
+ "prompt": prompt,
168
+ "response": llm_response,
169
+ "timestamp": datetime.now().isoformat()
170
+ }
171
  }
172
 
173
  analysis = ast.literal_eval(llm_response)
 
186
  state["current_step"] = 'search'
187
 
188
  # Log the error
189
+ state["logs"] = {
190
+ "analyze_error": {
191
+ "error": str(e),
192
+ "timestamp": datetime.now().isoformat()
193
+ }
194
  }
195
 
196
  return state
 
312
  # Run the workflow
313
  final_state = self.workflow.invoke(initial_state)
314
 
315
+ # Ensure logs directory exists
316
+ os.makedirs(LOGS_DIR, exist_ok=True)
317
+
318
  # Save logs to file
319
+ if final_state["logs"]: # Only save if we have logs
320
+ log_to_file(
321
+ task_id=final_state["task_id"],
322
+ question=final_state["question"],
323
+ log_data=final_state["logs"]
324
+ )
325
+ else:
326
+ print("No logs to save in final state")
327
 
328
  return final_state["final_answer"]
329