Dipan Giri commited on
Commit
2e84f0a
·
unverified ·
1 Parent(s): c00e175

Update llm_notes_generator.py

Browse files
Files changed (1) hide show
  1. notes/llm_notes_generator.py +18 -16
notes/llm_notes_generator.py CHANGED
@@ -34,8 +34,8 @@ logger = logging.getLogger(__name__)
34
 
35
  class Settings(BaseSettings):
36
  """Application settings loaded from environment variables or .env file."""
37
- mistral_api_key: str = os.getenv('MISTRAL_API_KEY', '')
38
- api_url: str = "https://api.mistral.ai/v1/chat/completions"
39
  output_dir: str = "data/generated_notes"
40
  trial_balance_json: str = "data/output1/parsed_trial_balance.json"
41
 
@@ -61,20 +61,22 @@ class GeneratedNote(BaseModel):
61
 
62
  class FlexibleFinancialNoteGenerator:
63
  def __init__(self):
64
- self.mistral_api_key = settings.mistral_api_key
65
- if not self.mistral_api_key:
66
- logger.error("MISTRAL_API_KEY not found in .env file")
67
- raise ValueError("MISTRAL_API_KEY not found in .env file")
68
  self.api_url = settings.api_url
69
  self.headers = {
70
- "Authorization": f"Bearer {self.mistral_api_key}",
71
- "Content-Type": "application/json"
 
 
72
  }
73
  self.note_templates = self.load_note_templates()
74
  self.account_patterns = self._init_account_patterns()
75
  self.recommended_models = [
76
- "mistral-large-latest",
77
- "mistral-medium-latest"
78
  ]
79
 
80
  def _init_account_patterns(self) -> Dict[str, Dict[str, Any]]:
@@ -338,8 +340,8 @@ class FlexibleFinancialNoteGenerator:
338
 
339
  return prompt
340
 
341
- def call_mistral_api(self, prompt: str) -> Optional[str]:
342
- """Make API call to Mistral with model fallback"""
343
  for model in self.recommended_models:
344
  logger.info(f"Trying model: {model}")
345
  payload = {
@@ -357,7 +359,7 @@ class FlexibleFinancialNoteGenerator:
357
  self.api_url,
358
  headers=self.headers,
359
  json=payload,
360
- timeout=30
361
  )
362
  response.raise_for_status()
363
  result = response.json()
@@ -448,7 +450,7 @@ class FlexibleFinancialNoteGenerator:
448
  logger.error("Failed to build prompt")
449
  return False
450
 
451
- response = self.call_mistral_api(prompt)
452
  if not response:
453
  logger.error("Failed to get API response")
454
  return False
@@ -473,7 +475,7 @@ class FlexibleFinancialNoteGenerator:
473
  if not prompt:
474
  results[note_number] = False
475
  continue
476
- response = self.call_mistral_api(prompt)
477
  if not response:
478
  results[note_number] = False
479
  continue
@@ -586,4 +588,4 @@ def main() -> None:
586
  sys.exit(1)
587
 
588
  if __name__ == "__main__":
589
- main()
 
34
 
35
  class Settings(BaseSettings):
36
  """Application settings loaded from environment variables or .env file."""
37
+ openrouter_api_key: str = os.getenv('OPENROUTER_API_KEY', '')
38
+ api_url: str = "https://openrouter.ai/api/v1/chat/completions"
39
  output_dir: str = "data/generated_notes"
40
  trial_balance_json: str = "data/output1/parsed_trial_balance.json"
41
 
 
61
 
62
  class FlexibleFinancialNoteGenerator:
63
  def __init__(self):
64
+ self.openrouter_api_key = settings.openrouter_api_key
65
+ if not self.openrouter_api_key:
66
+ logger.error("OPENROUTER_API_KEY not found in .env file")
67
+ raise ValueError("OPENROUTER_API_KEY not found in .env file")
68
  self.api_url = settings.api_url
69
  self.headers = {
70
+ "Authorization": f"Bearer {self.openrouter_api_key}",
71
+ "Content-Type": "application/json",
72
+ "HTTP-Referer": "https://localhost:3000",
73
+ "X-Title": "Financial Note Generator"
74
  }
75
  self.note_templates = self.load_note_templates()
76
  self.account_patterns = self._init_account_patterns()
77
  self.recommended_models = [
78
+ "mistralai/mixtral-8x7b-instruct",
79
+ "mistralai/mistral-7b-instruct-v0.2"
80
  ]
81
 
82
  def _init_account_patterns(self) -> Dict[str, Dict[str, Any]]:
 
340
 
341
  return prompt
342
 
343
+ def call_openrouter_api(self, prompt: str) -> Optional[str]:
344
+ """Make API call to OpenRouter with model fallback"""
345
  for model in self.recommended_models:
346
  logger.info(f"Trying model: {model}")
347
  payload = {
 
359
  self.api_url,
360
  headers=self.headers,
361
  json=payload,
362
+ timeout=30 # <-- Add timeout here!
363
  )
364
  response.raise_for_status()
365
  result = response.json()
 
450
  logger.error("Failed to build prompt")
451
  return False
452
 
453
+ response = self.call_openrouter_api(prompt)
454
  if not response:
455
  logger.error("Failed to get API response")
456
  return False
 
475
  if not prompt:
476
  results[note_number] = False
477
  continue
478
+ response = self.call_openrouter_api(prompt)
479
  if not response:
480
  results[note_number] = False
481
  continue
 
588
  sys.exit(1)
589
 
590
  if __name__ == "__main__":
591
+ main()