-
Notifications
You must be signed in to change notification settings - Fork 2
Rename openai_api parameter to openai_api_key with backwards compatibility
#296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/fix-278
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| # do not pre-load | ||
|
|
||
| import unittest | ||
| from unittest.mock import patch, MagicMock | ||
| import os | ||
| from .llms import _get_openai_api_key, chat_completion | ||
|
|
||
|
|
||
| class TestLLMSConfig(unittest.TestCase): | ||
| """Test configuration lookup and parameter handling for LLM functions.""" | ||
|
|
||
| @patch("docassemble.ALToolbox.llms.get_config") | ||
| @patch.dict(os.environ, {}, clear=True) | ||
| def test_get_openai_api_key_priority_order(self, mock_get_config): | ||
| """Test that _get_openai_api_key follows the correct priority order.""" | ||
|
|
||
| # Test priority 1: direct key configuration "openai api key" | ||
| mock_get_config.side_effect = lambda key, default=None: { | ||
| "openai api key": "direct-key" | ||
| }.get(key, default) | ||
|
|
||
| result = _get_openai_api_key() | ||
| self.assertEqual(result, "direct-key") | ||
|
|
||
| # Test priority 2: standardized nested configuration "openai: key" | ||
| mock_get_config.side_effect = lambda key, default=None: { | ||
| "openai api key": None, | ||
| "openai": {"key": "standardized-key"}, | ||
| }.get(key, default) | ||
|
|
||
| result = _get_openai_api_key() | ||
| self.assertEqual(result, "standardized-key") | ||
|
|
||
| # Test priority 3: legacy nested configuration "open ai: key" | ||
| mock_get_config.side_effect = lambda key, default=None: { | ||
| "openai api key": None, | ||
| "openai": {}, | ||
| "open ai": {"key": "legacy-key"}, | ||
| }.get(key, default) | ||
|
|
||
| result = _get_openai_api_key() | ||
| self.assertEqual(result, "legacy-key") | ||
|
|
||
| @patch("docassemble.ALToolbox.llms.get_config") | ||
| @patch.dict(os.environ, {"OPENAI_API_KEY": "env-key"}, clear=True) | ||
| def test_get_openai_api_key_environment_fallback(self, mock_get_config): | ||
| """Test that environment variable is used as last resort.""" | ||
|
|
||
| # No config keys found, should fall back to environment | ||
| mock_get_config.side_effect = lambda key, default=None: default | ||
|
|
||
| result = _get_openai_api_key() | ||
| self.assertEqual(result, "env-key") | ||
|
|
||
| @patch("docassemble.ALToolbox.llms.get_config") | ||
| @patch.dict(os.environ, {}, clear=True) | ||
| def test_get_openai_api_key_no_key_found(self, mock_get_config): | ||
| """Test that None is returned when no key is found.""" | ||
|
|
||
| mock_get_config.side_effect = lambda key, default=None: default | ||
|
|
||
| result = _get_openai_api_key() | ||
| self.assertIsNone(result) | ||
|
|
||
| @patch("docassemble.ALToolbox.llms.get_config") | ||
| def test_get_openai_api_key_handles_non_dict_config(self, mock_get_config): | ||
| """Test that non-dict config values are handled gracefully.""" | ||
|
|
||
| # Test when config returns a string instead of dict | ||
| mock_get_config.side_effect = lambda key, default=None: { | ||
| "openai api key": None, | ||
| "openai": "not-a-dict", # This should be handled gracefully | ||
| "open ai": {"key": "fallback-key"}, | ||
| }.get(key, default) | ||
|
|
||
| result = _get_openai_api_key() | ||
| self.assertEqual(result, "fallback-key") | ||
|
|
||
| @patch("docassemble.ALToolbox.llms.OpenAI") | ||
| @patch("docassemble.ALToolbox.llms._get_openai_api_key") | ||
| def test_chat_completion_parameter_backwards_compatibility( | ||
| self, mock_get_key, mock_openai | ||
| ): | ||
| """Test that both openai_api and openai_api_key parameters work.""" | ||
|
|
||
| # Mock the OpenAI client | ||
| mock_client = MagicMock() | ||
| mock_openai.return_value = mock_client | ||
|
|
||
| # Mock successful response | ||
| mock_response = MagicMock() | ||
| mock_response.choices = [MagicMock()] | ||
| mock_response.choices[0].finish_reason = "stop" | ||
| mock_response.choices[0].message.content = "test response" | ||
| mock_client.chat.completions.create.return_value = mock_response | ||
|
|
||
| # Mock moderation | ||
| mock_moderation = MagicMock() | ||
| mock_moderation.results = [MagicMock()] | ||
| mock_moderation.results[0].flagged = False | ||
| mock_client.moderations.create.return_value = mock_moderation | ||
|
|
||
| mock_get_key.return_value = "config-key" | ||
|
|
||
| # Test new parameter name | ||
| result = chat_completion( | ||
| system_message="test", user_message="test", openai_api_key="new-param-key" | ||
| ) | ||
|
|
||
| # Should use new parameter value | ||
| mock_openai.assert_called_with( | ||
| base_url="https://api.openai.com/v1/", api_key="new-param-key" | ||
| ) | ||
|
|
||
| # Test old parameter name | ||
| result = chat_completion( | ||
| system_message="test", user_message="test", openai_api="old-param-key" | ||
| ) | ||
|
|
||
| # Should use old parameter value | ||
| mock_openai.assert_called_with( | ||
| base_url="https://api.openai.com/v1/", api_key="old-param-key" | ||
| ) | ||
|
|
||
| # Test both parameters (new should take priority) | ||
| result = chat_completion( | ||
| system_message="test", | ||
| user_message="test", | ||
| openai_api_key="new-param-key", | ||
| openai_api="old-param-key", | ||
| ) | ||
|
|
||
| # Should prioritize new parameter | ||
| mock_openai.assert_called_with( | ||
| base_url="https://api.openai.com/v1/", api_key="new-param-key" | ||
| ) | ||
|
|
||
| # Test neither parameter (should fall back to config) | ||
| result = chat_completion(system_message="test", user_message="test") | ||
|
|
||
| # Should use config key | ||
| mock_openai.assert_called_with( | ||
| base_url="https://api.openai.com/v1/", api_key="config-key" | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.