-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parameterize getApiKeyFromNvpOrEnv, allowing different env variables …
…for API keys
- Loading branch information
Showing
8 changed files
with
25 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,23 +1,23 @@ | ||
function key = getApiKeyFromNvpOrEnv(nvp) | ||
function key = getApiKeyFromNvpOrEnv(nvp,envVarName) | ||
% This function is undocumented and will change in a future release | ||
|
||
%getApiKeyFromNvpOrEnv Retrieves an API key from a Name-Value Pair struct or environment variable. | ||
% | ||
% This function takes a struct nvp containing name-value pairs and checks | ||
% if it contains a field called "ApiKey". If the field is not found, | ||
% the function attempts to retrieve the API key from an environment | ||
% variable called "OPENAI_API_KEY". If both methods fail, the function | ||
% throws an error. | ||
% This function takes a struct nvp containing name-value pairs and checks if | ||
% it contains a field called "ApiKey". If the field is not found, the | ||
% function attempts to retrieve the API key from an environment variable | ||
% whose name is given as the second argument. If both methods fail, the | ||
% function throws an error. | ||
|
||
% Copyright 2023 The MathWorks, Inc. | ||
% Copyright 2023-2024 The MathWorks, Inc. | ||
|
||
if isfield(nvp, "ApiKey") | ||
key = nvp.ApiKey; | ||
else | ||
if isenv("OPENAI_API_KEY") | ||
key = getenv("OPENAI_API_KEY"); | ||
if isenv(envVarName) | ||
key = getenv(envVarName); | ||
else | ||
error("llms:keyMustBeSpecified", llms.utils.errorMessageCatalog.getMessage("llms:keyMustBeSpecified")); | ||
error("llms:keyMustBeSpecified", llms.utils.errorMessageCatalog.getMessage("llms:keyMustBeSpecified", envVarName)); | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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 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 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 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 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 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