Skip to content

Commit acaa901

Browse files
author
Dylan Huang
committed
Enhance environment variable loading in auth module
- Added functionality to load environment variables from .env.dev or .env as a fallback when the auth module is imported. - Updated the API key verification process to allow explicit base URL handling, defaulting to dev.api.fireworks.ai if not provided. - Removed redundant environment variable loading code from platform_api module.
1 parent 348bb58 commit acaa901

2 files changed

Lines changed: 29 additions & 24 deletions

File tree

eval_protocol/auth.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,30 @@
33
from typing import Optional
44

55
import requests
6+
from dotenv import find_dotenv, load_dotenv
67

78
logger = logging.getLogger(__name__)
89

10+
# --- Load .env files ---
11+
# Attempt to load .env.dev first, then .env as a fallback.
12+
# This happens when the module is imported.
13+
# We use override=False (default) so that existing environment variables
14+
# (e.g., set in the shell) are NOT overridden by .env files.
15+
_ENV_DEV_PATH = find_dotenv(filename=".env.dev", raise_error_if_not_found=False, usecwd=True)
16+
if _ENV_DEV_PATH:
17+
load_dotenv(dotenv_path=_ENV_DEV_PATH, override=False)
18+
logger.debug(f"eval_protocol.auth: Loaded environment variables from: {_ENV_DEV_PATH}")
19+
else:
20+
_ENV_PATH = find_dotenv(filename=".env", raise_error_if_not_found=False, usecwd=True)
21+
if _ENV_PATH:
22+
load_dotenv(dotenv_path=_ENV_PATH, override=False)
23+
logger.debug(f"eval_protocol.auth: Loaded environment variables from: {_ENV_PATH}")
24+
else:
25+
logger.debug(
26+
"eval_protocol.auth: No .env.dev or .env file found. Relying on shell/existing environment variables."
27+
)
28+
# --- End .env loading ---
29+
930

1031
def get_fireworks_api_key() -> Optional[str]:
1132
"""
@@ -73,6 +94,8 @@ def verify_api_key_and_get_account_id(
7394
Args:
7495
api_key: Optional explicit API key. When None, resolves via get_fireworks_api_key().
7596
api_base: Optional explicit API base. When None, resolves via get_fireworks_api_base().
97+
If api_base is api.fireworks.ai, it is used directly. Otherwise, defaults to
98+
dev.api.fireworks.ai for the verification call.
7699
77100
Returns:
78101
The resolved account id if verification succeeds and the header is present; otherwise None.
@@ -81,7 +104,12 @@ def verify_api_key_and_get_account_id(
81104
resolved_key = api_key or get_fireworks_api_key()
82105
if not resolved_key:
83106
return None
84-
resolved_base = api_base or get_fireworks_api_base()
107+
provided_base = api_base or get_fireworks_api_base()
108+
# Use api.fireworks.ai if explicitly provided, otherwise fall back to dev
109+
if "api.fireworks.ai" in provided_base:
110+
resolved_base = provided_base
111+
else:
112+
resolved_base = "https://dev.api.fireworks.ai"
85113

86114
from .common_utils import get_user_agent
87115

eval_protocol/platform_api.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import sys
44
from typing import Optional
55

6-
from dotenv import find_dotenv, load_dotenv
7-
86
from eval_protocol.auth import (
97
get_fireworks_account_id,
108
get_fireworks_api_base,
@@ -16,27 +14,6 @@
1614

1715
logger = logging.getLogger(__name__)
1816

19-
# --- Load .env files ---
20-
# Attempt to load .env.dev first, then .env as a fallback.
21-
# This happens when the module is imported.
22-
# We use override=False (default) so that existing environment variables
23-
# (e.g., set in the shell) are NOT overridden by .env files.
24-
ENV_DEV_PATH = find_dotenv(filename=".env.dev", raise_error_if_not_found=False, usecwd=True)
25-
if ENV_DEV_PATH:
26-
load_dotenv(dotenv_path=ENV_DEV_PATH, override=False)
27-
logger.info(f"eval_protocol.platform_api: Loaded environment variables from: {ENV_DEV_PATH}")
28-
else:
29-
ENV_PATH = find_dotenv(filename=".env", raise_error_if_not_found=False, usecwd=True)
30-
if ENV_PATH:
31-
load_dotenv(dotenv_path=ENV_PATH, override=False)
32-
logger.info(f"eval_protocol.platform_api: Loaded environment variables from: {ENV_PATH}")
33-
else:
34-
logger.info(
35-
"eval_protocol.platform_api: No .env.dev or .env file found. "
36-
"Relying on shell/existing environment variables."
37-
)
38-
# --- End .env loading ---
39-
4017

4118
class PlatformAPIError(Exception):
4219
"""Custom exception for platform API errors."""

0 commit comments

Comments
 (0)