Michael Siebenmann commited on
Commit
1febffc
·
1 Parent(s): 874a688

add budget/auth msg for API key

Browse files
generation/iterative_local_analyzer.py CHANGED
@@ -21,6 +21,8 @@ from generation.analyzer import Analyzer, CodeAct, CodeAction
21
  from retrieval.retriever import Metadata
22
  from utils import (
23
  clean,
 
 
24
  generate_iterative_system_prompt,
25
  get_file_from_title,
26
  get_path_from_title,
@@ -454,7 +456,8 @@ class IterativeLocalAnalyzer(Analyzer):
454
  thought_msg.metadata["status"] = "done"
455
  thought_msg.metadata["title"] = "Error during analysis"
456
  thought_msg.metadata["duration"] = time.time() - start_time
457
- yield [thought_msg, "Unfortunately, there was an error during the LLM invocation. Please try again."]
 
458
  return
459
 
460
  success, response, is_final_answer = self.run_ai_generated_code(code)
 
21
  from retrieval.retriever import Metadata
22
  from utils import (
23
  clean,
24
+ is_budget_error,
25
+ API_MSG_BUDGET,
26
  generate_iterative_system_prompt,
27
  get_file_from_title,
28
  get_path_from_title,
 
456
  thought_msg.metadata["status"] = "done"
457
  thought_msg.metadata["title"] = "Error during analysis"
458
  thought_msg.metadata["duration"] = time.time() - start_time
459
+ error_msg = API_MSG_BUDGET if is_budget_error(e) else "Unfortunately, there was an error during the LLM invocation. Please try again."
460
+ yield [thought_msg, error_msg]
461
  return
462
 
463
  success, response, is_final_answer = self.run_ai_generated_code(code)
generation/simple_local_analyzer_v2.py CHANGED
@@ -20,6 +20,8 @@ from generation.analyzer import Analyzer, CodeAct, CodeAction
20
  from retrieval.retriever import Metadata
21
  from utils import (
22
  clean,
 
 
23
  generate_system_prompt_v2,
24
  get_file_from_title,
25
  get_path_from_title,
@@ -424,7 +426,8 @@ class SimpleLocalAnalyzerV2(Analyzer):
424
  thought_msg.metadata["status"] = "done"
425
  thought_msg.metadata["title"] = "Error during analysis"
426
  thought_msg.metadata["duration"] = time.time() - start_time
427
- yield [thought_msg, "Unfortunately, there was an error during the LLM invocation. Please try again."]
 
428
  return
429
 
430
  success, response = self.run_ai_generated_code(code)
 
20
  from retrieval.retriever import Metadata
21
  from utils import (
22
  clean,
23
+ is_budget_error,
24
+ API_MSG_BUDGET,
25
  generate_system_prompt_v2,
26
  get_file_from_title,
27
  get_path_from_title,
 
426
  thought_msg.metadata["status"] = "done"
427
  thought_msg.metadata["title"] = "Error during analysis"
428
  thought_msg.metadata["duration"] = time.time() - start_time
429
+ error_msg = API_MSG_BUDGET if is_budget_error(e) else "Unfortunately, there was an error during the LLM invocation. Please try again."
430
+ yield [thought_msg, error_msg]
431
  return
432
 
433
  success, response = self.run_ai_generated_code(code)
main.py CHANGED
@@ -31,7 +31,7 @@ from retrieval.agentic_retriever import AgenticRetriever
31
  from retrieval.knn_retriever import KNNRetriever
32
  from retrieval.retriever import Retriever
33
  from retrieval.verified_retriever import VerifiedRetriever
34
- from utils import SUPPORTED_LLMS, get_llm_client, init_mappings, download_dataset_file, get_file_from_title, get_path_from_title
35
 
36
 
37
  class RetrievalCheck(BaseModel):
@@ -107,7 +107,7 @@ class OGD4All():
107
  thought_msg_retrieval.content = "An error occurred while retrieving datasets."
108
  thought_msg_retrieval.metadata["status"] = "done"
109
  thought_msg_retrieval.metadata["title"] = "Retrieval failed"
110
- error_msg = "I'm sorry, an error occurred during the retrieval of relevant datasets. Please try again."
111
  self.reset = True
112
  yield [thought_msg_retrieval, error_msg], updated_map
113
  return
@@ -195,7 +195,8 @@ class OGD4All():
195
  retrieval_check = self.retrieval_check_client.invoke(messages)
196
  except Exception as e:
197
  log.error("Error during retrieval check:", exc_info=True)
198
- yield "An error occurred while checking whether additional datasets are required.", updated_map
 
199
  return
200
 
201
  if retrieval_check.retrievalRequired:
@@ -344,7 +345,8 @@ class OGD4All():
344
  except Exception as e:
345
  log.error("Caught an exception in chat_fn: %s", e, exc_info=True, backtrace=True, diagnose=True)
346
  self.finalize()
347
- yield gr.ChatMessage(role="assistant", content="I am sorry, there has been an error processing your request. Please try again."), updated_map
 
348
  self.reset = True
349
  return
350
 
 
31
  from retrieval.knn_retriever import KNNRetriever
32
  from retrieval.retriever import Retriever
33
  from retrieval.verified_retriever import VerifiedRetriever
34
+ from utils import SUPPORTED_LLMS, get_llm_client, init_mappings, download_dataset_file, get_file_from_title, get_path_from_title, is_budget_error, API_MSG_BUDGET
35
 
36
 
37
  class RetrievalCheck(BaseModel):
 
107
  thought_msg_retrieval.content = "An error occurred while retrieving datasets."
108
  thought_msg_retrieval.metadata["status"] = "done"
109
  thought_msg_retrieval.metadata["title"] = "Retrieval failed"
110
+ error_msg = API_MSG_BUDGET if is_budget_error(e) else "I'm sorry, an error occurred during the retrieval of relevant datasets. Please try again."
111
  self.reset = True
112
  yield [thought_msg_retrieval, error_msg], updated_map
113
  return
 
195
  retrieval_check = self.retrieval_check_client.invoke(messages)
196
  except Exception as e:
197
  log.error("Error during retrieval check:", exc_info=True)
198
+ error_msg = API_MSG_BUDGET if is_budget_error(e) else "An error occurred while checking whether additional datasets are required."
199
+ yield error_msg, updated_map
200
  return
201
 
202
  if retrieval_check.retrievalRequired:
 
345
  except Exception as e:
346
  log.error("Caught an exception in chat_fn: %s", e, exc_info=True, backtrace=True, diagnose=True)
347
  self.finalize()
348
+ error_msg = API_MSG_BUDGET if is_budget_error(e) else "I am sorry, there has been an error processing your request. Please try again."
349
+ yield gr.ChatMessage(role="assistant", content=error_msg), updated_map
350
  self.reset = True
351
  return
352
 
utils.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import shutil
4
  import pandas as pd
5
  import logging
 
6
  import base64
7
  import mimetypes
8
  import pymupdf4llm
@@ -554,6 +555,22 @@ class ChatOpenRouter(ChatOpenAI):
554
  super().__init__(base_url="https://openrouter.ai/api/v1", openai_api_key=openai_api_key, **kwargs)
555
 
556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557
  def get_llm_client(llm_name: str, temperature: float = 0.0):
558
  """
559
  Returns an LLM client based on the provided name.
 
3
  import shutil
4
  import pandas as pd
5
  import logging
6
+ import openai
7
  import base64
8
  import mimetypes
9
  import pymupdf4llm
 
555
  super().__init__(base_url="https://openrouter.ai/api/v1", openai_api_key=openai_api_key, **kwargs)
556
 
557
 
558
+ API_MSG_BUDGET = "The LLM API key powering this demo has exceeded its budget or is no longer valid. "
559
+
560
+
561
+ def is_budget_error(e: Exception) -> bool:
562
+ """Return True if the exception is a permanent API failure (quota exhausted or key invalid/expired)."""
563
+ if isinstance(e, (openai.AuthenticationError, openai.PermissionDeniedError)):
564
+ return True
565
+
566
+ if isinstance(e, openai.RateLimitError):
567
+ body = getattr(e, "body", None) or {}
568
+ code = body.get("error", {}).get("code", "") if isinstance(body, dict) else ""
569
+ return code == "insufficient_quota" or "insufficient_quota" in str(e)
570
+
571
+ return False
572
+
573
+
574
  def get_llm_client(llm_name: str, temperature: float = 0.0):
575
  """
576
  Returns an LLM client based on the provided name.