dipan004 commited on
Commit
35cd5eb
·
verified ·
1 Parent(s): 6150f80

Update agents/simple_tools.py

Browse files
Files changed (1) hide show
  1. agents/simple_tools.py +19 -22
agents/simple_tools.py CHANGED
@@ -3,6 +3,7 @@ Simplified LangChain tools for FinRyver financial statement generation
3
  Focus: Notes, Balance Sheet, P&L, Cash Flow generation only
4
  """
5
  from langchain_core.tools import tool
 
6
  import os
7
  import subprocess
8
  import json
@@ -424,7 +425,6 @@ def generate_llm_notes(file_path: str, note_numbers: str = "", user_api_key: str
424
  if user_api_key:
425
  env["OPENROUTER_API_KEY"] = user_api_key
426
  elif os.getenv("OPENROUTER_API_KEY"):
427
- # Fallback to environment variable if no user key provided
428
  env["OPENROUTER_API_KEY"] = os.getenv("OPENROUTER_API_KEY")
429
 
430
  cwd = os.getcwd()
@@ -436,7 +436,8 @@ def generate_llm_notes(file_path: str, note_numbers: str = "", user_api_key: str
436
  env=env,
437
  cwd=cwd,
438
  capture_output=True,
439
- text=True
 
440
  )
441
 
442
  if result1.returncode != 0:
@@ -445,22 +446,22 @@ def generate_llm_notes(file_path: str, note_numbers: str = "", user_api_key: str
445
  # Step 2: Run LLM notes generator
446
  logger.info("Step 2: Generating notes using LLM")
447
  if note_numbers:
448
- # Generate specific notes
449
  result2 = subprocess.run(
450
  ["python", "notes/llm_notes_generator.py", "specific", note_numbers],
451
  env=env,
452
  cwd=cwd,
453
  capture_output=True,
454
- text=True
 
455
  )
456
  else:
457
- # Generate all notes
458
  result2 = subprocess.run(
459
  ["python", "notes/llm_notes_generator.py", "all", ""],
460
  env=env,
461
  cwd=cwd,
462
  capture_output=True,
463
- text=True
 
464
  )
465
 
466
  if result2.returncode != 0:
@@ -522,27 +523,13 @@ def generate_llm_notes(file_path: str, note_numbers: str = "", user_api_key: str
522
  except Exception as e:
523
  logger.error(f"Error applying UDFs to JSON: {e}")
524
 
525
- # Check if JSON file has content
526
- try:
527
- with open(input_json, 'r', encoding='utf-8') as f:
528
- json_content = json.load(f)
529
- if isinstance(json_content, dict) and 'notes' in json_content and not json_content['notes']:
530
- logger.warning("JSON file exists but contains no notes")
531
- except Exception as e:
532
- execution_time = round(time.time() - start_time, 2)
533
- return {
534
- "status": "error",
535
- "error": f"Invalid JSON file generated: {str(e)}",
536
- "execution_id": execution_id,
537
- "execution_time": execution_time
538
- }
539
-
540
  result3 = subprocess.run(
541
  ["python", "notes/llm_notes_excel_converter.py", input_json, output_excel],
542
  env=env,
543
  cwd=cwd,
544
  capture_output=True,
545
- text=True
 
546
  )
547
 
548
  if result3.returncode == 0:
@@ -563,6 +550,16 @@ def generate_llm_notes(file_path: str, note_numbers: str = "", user_api_key: str
563
  "execution_time": execution_time
564
  }
565
 
 
 
 
 
 
 
 
 
 
 
566
  except Exception as e:
567
  execution_time = round(time.time() - start_time, 2)
568
  logger.error(f"LLM notes generation failed: {e}")
 
3
  Focus: Notes, Balance Sheet, P&L, Cash Flow generation only
4
  """
5
  from langchain_core.tools import tool
6
+ import subprocess
7
  import os
8
  import subprocess
9
  import json
 
425
  if user_api_key:
426
  env["OPENROUTER_API_KEY"] = user_api_key
427
  elif os.getenv("OPENROUTER_API_KEY"):
 
428
  env["OPENROUTER_API_KEY"] = os.getenv("OPENROUTER_API_KEY")
429
 
430
  cwd = os.getcwd()
 
436
  env=env,
437
  cwd=cwd,
438
  capture_output=True,
439
+ text=True,
440
+ timeout=120 # ADD THIS: 2 minute timeout
441
  )
442
 
443
  if result1.returncode != 0:
 
446
  # Step 2: Run LLM notes generator
447
  logger.info("Step 2: Generating notes using LLM")
448
  if note_numbers:
 
449
  result2 = subprocess.run(
450
  ["python", "notes/llm_notes_generator.py", "specific", note_numbers],
451
  env=env,
452
  cwd=cwd,
453
  capture_output=True,
454
+ text=True,
455
+ timeout=600 # ADD THIS: 10 minute timeout for LLM generation
456
  )
457
  else:
 
458
  result2 = subprocess.run(
459
  ["python", "notes/llm_notes_generator.py", "all", ""],
460
  env=env,
461
  cwd=cwd,
462
  capture_output=True,
463
+ text=True,
464
+ timeout=900 # ADD THIS: 15 minute timeout for all notes
465
  )
466
 
467
  if result2.returncode != 0:
 
523
  except Exception as e:
524
  logger.error(f"Error applying UDFs to JSON: {e}")
525
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
526
  result3 = subprocess.run(
527
  ["python", "notes/llm_notes_excel_converter.py", input_json, output_excel],
528
  env=env,
529
  cwd=cwd,
530
  capture_output=True,
531
+ text=True,
532
+ timeout=120 # ADD THIS: 2 minute timeout
533
  )
534
 
535
  if result3.returncode == 0:
 
550
  "execution_time": execution_time
551
  }
552
 
553
+ except subprocess.TimeoutExpired as te: # ADD THIS: Handle timeout
554
+ execution_time = round(time.time() - start_time, 2)
555
+ logger.error(f"LLM notes generation timed out after {execution_time}s")
556
+ return {
557
+ "status": "error",
558
+ "error": f"LLM notes generation timed out. The process took longer than expected. Try generating fewer notes or check your API key.",
559
+ "execution_id": execution_id,
560
+ "execution_time": execution_time,
561
+ "timeout": True
562
+ }
563
  except Exception as e:
564
  execution_time = round(time.time() - start_time, 2)
565
  logger.error(f"LLM notes generation failed: {e}")