Skip to content

Commit b2a3de6

Browse files
authored
feat: allow users to pass api_key (#35)
1 parent 14a806e commit b2a3de6

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

src/litai/llm.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ def __init__(
8282
fallback_models: Optional[List[str]] = None,
8383
teamspace: Optional[str] = None,
8484
max_retries: int = 3,
85-
lightning_api_key: Optional[str] = None,
86-
lightning_user_id: Optional[str] = None,
85+
api_key: Optional[str] = None,
8786
enable_async: Optional[bool] = False,
8887
verbose: int = 0,
8988
full_response: Optional[bool] = None,
@@ -96,26 +95,15 @@ def __init__(
9695
if the main model fails. Defaults to None.
9796
teamspace (Optional[List[str]]): Teamspace used for billing.
9897
max_retries (int): The maximum number of retries for API requests. Defaults to 3.
99-
lightning_api_key (Optional[str]): The API key for Lightning AI. Defaults to None.
100-
lightning_user_id (Optional[str]): The user ID for Lightning AI. Defaults to None.
98+
api_key (Optional[str]): The API key for Lightning AI. Defaults to None.
10199
enable_async (Optional[bool]): Enable async requests. Defaults to True.
102100
verbose (int): Verbosity level for logging. Defaults to 0. Must be 0, 1, or 2.
103101
full_response (bool): Whether the entire response should be returned from the chat
104102
"""
105-
if (lightning_api_key is None) != (lightning_user_id is None):
106-
missing_param = "lightning_api_key" if lightning_api_key is None else "lightning_user_id"
107-
raise ValueError(
108-
f"Missing required parameter: '{missing_param}'. "
109-
"Both 'lightning_api_key' and 'lightning_user_id' must be provided together. "
110-
"Either provide both or none.\n"
111-
"To find the API key and user ID, go to the Global Settings page in your Lightning account."
112-
)
113-
114-
if lightning_api_key is not None and lightning_user_id is not None:
115-
os.environ["LIGHTNING_API_KEY"] = lightning_api_key
116-
os.environ["LIGHTNING_USER_ID"] = lightning_user_id
103+
if api_key is not None:
104+
os.environ["LIGHTNING_API_KEY"] = api_key
117105

118-
if os.environ.get("LIGHTNING_API_KEY") is None and os.environ.get("LIGHTNING_USER_ID") is None:
106+
if os.environ.get("LIGHTNING_API_KEY") is None:
119107
self._authenticate()
120108

121109
if verbose not in [0, 1, 2]:

tests/test_llm.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ def test_initialization_with_config_file(monkeypatch):
2121
"""Test LitAI config."""
2222
mock_llm_instance = MagicMock()
2323
monkeypatch.setattr("litai.llm.SDKLLM", mock_llm_instance)
24-
LLM(model="openai/gpt-4", lightning_api_key="my-key", lightning_user_id="my-user-id")
24+
LLM(model="openai/gpt-4", api_key="my-key")
2525
assert os.getenv("LIGHTNING_API_KEY") == "my-key"
26-
assert os.getenv("LIGHTNING_USER_ID") == "my-user-id"
2726

2827

2928
@patch("litai.llm.SDKLLM")
@@ -296,14 +295,13 @@ def mock_auth_constructor():
296295
monkeypatch.setattr("litai.llm.login.Auth", mock_auth_constructor)
297296

298297
# Test case 1: Both api_key and user_id provided
299-
LLM(model="openai/gpt-4", lightning_api_key="my-key", lightning_user_id="my-user-id")
298+
LLM(model="openai/gpt-4", api_key="my-key")
300299

301300
# Verify that the authentication was not called
302301
mock_auth.authenticate.assert_not_called()
303302

304303
# Verify that environment variables were set
305304
assert os.getenv("LIGHTNING_API_KEY") == "my-key"
306-
assert os.getenv("LIGHTNING_USER_ID") == "my-user-id"
307305

308306
# Test case 2: Neither api_key nor user_id provided
309307
mock_auth.reset_mock()

0 commit comments

Comments
 (0)