diff --git a/.gitignore b/.gitignore index a8925435..e108c16e 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ venv *.egg-info .DS_Store .idea/ +.direnv/ +.envrc \ No newline at end of file diff --git a/llm/__init__.py b/llm/__init__.py index 0ea6c242..d1b70a80 100644 --- a/llm/__init__.py +++ b/llm/__init__.py @@ -173,12 +173,12 @@ def get_key( if explicit_key: # User specified a key that's not an alias, use that return explicit_key - # Stored key over-rides environment variables over-ride the default key - if key_alias in stored_keys: - return stored_keys[key_alias] # Finally try environment variable if env_var and os.environ.get(env_var): return os.environ[env_var] + # Stored key over-rides environment variables over-ride the default key + if key_alias in stored_keys: + return stored_keys[key_alias] # Couldn't find it return None diff --git a/tests/test_keys.py b/tests/test_keys.py index 5a5649a0..74815d85 100644 --- a/tests/test_keys.py +++ b/tests/test_keys.py @@ -65,7 +65,6 @@ def test_uses_correct_key(mocked_openai_chat, monkeypatch, tmpdir): } keys_path.write_text(json.dumps(KEYS), "utf-8") monkeypatch.setenv("LLM_USER_PATH", str(user_dir)) - monkeypatch.setenv("OPENAI_API_KEY", "from-env") def assert_key(key): request = mocked_openai_chat.get_requests()[-1] @@ -73,18 +72,28 @@ def assert_key(key): runner = CliRunner() - # Called without --key uses stored key - result = runner.invoke(cli, ["hello", "--no-stream"], catch_exceptions=False) + # Called without --key an no environment variable uses stored key + result = runner.invoke( + cli, ["hello", "--no-stream"], catch_exceptions=False) assert result.exit_code == 0 assert_key("from-keys-file") + monkeypatch.setenv("OPENAI_API_KEY", "from-env") + # Called without --key and without keys.json uses environment variable keys_path.write_text("{}", "utf-8") - result2 = runner.invoke(cli, ["hello", "--no-stream"], catch_exceptions=False) + result2 = runner.invoke( + cli, ["hello", "--no-stream"], catch_exceptions=False) assert result2.exit_code == 0 assert_key("from-env") keys_path.write_text(json.dumps(KEYS), "utf-8") + # Called without --key and with keys.json still uses environment variable + result = runner.invoke( + cli, ["hello", "--no-stream"], catch_exceptions=False) + assert result.exit_code == 0 + assert_key("from-env") + # Called with --key name-in-keys.json uses that value result3 = runner.invoke( cli, ["hello", "--key", "other", "--no-stream"], catch_exceptions=False