ashishMenon05 commited on
Commit
3168d77
·
1 Parent(s): 43956b9

refactor: improve environment variable handling for API authentication - 18:47

Browse files
Files changed (2) hide show
  1. backend/models/model_manager.py +5 -3
  2. inference.py +4 -5
backend/models/model_manager.py CHANGED
@@ -17,10 +17,12 @@ class ModelManager:
17
  self.hf_client = HFClient(settings.HF_INFERENCE_URL, hf_token)
18
 
19
  def get_client(self, agent_id: str) -> Tuple[AsyncOpenAI, str]:
20
- if "API_BASE_URL" in os.environ and "API_KEY" in os.environ:
 
 
21
  client = AsyncOpenAI(
22
- base_url=os.environ["API_BASE_URL"],
23
- api_key=os.environ["API_KEY"]
24
  )
25
  model_name = os.environ.get("MODEL_NAME", "")
26
  if agent_id == "agent_a":
 
17
  self.hf_client = HFClient(settings.HF_INFERENCE_URL, hf_token)
18
 
19
  def get_client(self, agent_id: str) -> Tuple[AsyncOpenAI, str]:
20
+ api_base = os.environ.get("API_BASE_URL", "")
21
+ api_key = os.environ.get("API_KEY", "")
22
+ if api_base and api_key:
23
  client = AsyncOpenAI(
24
+ base_url=api_base,
25
+ api_key=api_key
26
  )
27
  model_name = os.environ.get("MODEL_NAME", "")
28
  if agent_id == "agent_a":
inference.py CHANGED
@@ -25,13 +25,12 @@ load_dotenv(ROOT / "default.env", override=False)
25
 
26
  API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
27
  MODEL_NAME = os.getenv("MODEL_NAME", "meta-llama/Llama-3.1-8B-Instruct")
28
- HF_TOKEN = os.getenv("HF_TOKEN")
29
 
30
- # Fallbacks to ensure os.environ has the keys before we strictly read them
31
- if "API_BASE_URL" not in os.environ:
32
  os.environ["API_BASE_URL"] = API_BASE_URL
33
- if "API_KEY" not in os.environ:
34
- os.environ["API_KEY"] = HF_TOKEN or "none"
35
 
36
  # The client should NOT be initialized here at the module level.
37
  # If the evaluator imports this file before patching os.environ, it will permanently bind to fallbacks.
 
25
 
26
  API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
27
  MODEL_NAME = os.getenv("MODEL_NAME", "meta-llama/Llama-3.1-8B-Instruct")
28
+ HF_TOKEN = os.getenv("HF_TOKEN", "")
29
 
30
+ if "API_BASE_URL" not in os.environ or not os.environ["API_BASE_URL"]:
 
31
  os.environ["API_BASE_URL"] = API_BASE_URL
32
+ if "API_KEY" not in os.environ or not os.environ["API_KEY"]:
33
+ os.environ["API_KEY"] = "none"
34
 
35
  # The client should NOT be initialized here at the module level.
36
  # If the evaluator imports this file before patching os.environ, it will permanently bind to fallbacks.