Skip to content

Commit 26b1272

Browse files
committed
parameterize getApiKeyFromNvpOrEnv, allowing different env variables for API keys
1 parent ccd6961 commit 26b1272

File tree

8 files changed

+25
-22
lines changed

8 files changed

+25
-22
lines changed
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
function key = getApiKeyFromNvpOrEnv(nvp)
1+
function key = getApiKeyFromNvpOrEnv(nvp,envVarName)
22
% This function is undocumented and will change in a future release
33

44
%getApiKeyFromNvpOrEnv Retrieves an API key from a Name-Value Pair struct or environment variable.
55
%
6-
% This function takes a struct nvp containing name-value pairs and checks
7-
% if it contains a field called "ApiKey". If the field is not found,
8-
% the function attempts to retrieve the API key from an environment
9-
% variable called "OPENAI_API_KEY". If both methods fail, the function
10-
% throws an error.
6+
% This function takes a struct nvp containing name-value pairs and checks if
7+
% it contains a field called "ApiKey". If the field is not found, the
8+
% function attempts to retrieve the API key from an environment variable
9+
% whose name is given as the second argument. If both methods fail, the
10+
% function throws an error.
1111

12-
% Copyright 2023 The MathWorks, Inc.
12+
% Copyright 2023-2024 The MathWorks, Inc.
1313

1414
if isfield(nvp, "ApiKey")
1515
key = nvp.ApiKey;
1616
else
17-
if isenv("OPENAI_API_KEY")
18-
key = getenv("OPENAI_API_KEY");
17+
if isenv(envVarName)
18+
key = getenv(envVarName);
1919
else
20-
error("llms:keyMustBeSpecified", llms.utils.errorMessageCatalog.getMessage("llms:keyMustBeSpecified"));
20+
error("llms:keyMustBeSpecified", llms.utils.errorMessageCatalog.getMessage("llms:keyMustBeSpecified", envVarName));
2121
end
2222
end
23-
end
23+
end

+llms/+utils/errorMessageCatalog.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
catalog("llms:assistantMustHaveTextNameAndArguments") = "Fields 'name' and 'arguments' must be text with one or more characters.";
4444
catalog("llms:mustBeValidIndex") = "Value is larger than the number of elements in Messages ({1}).";
4545
catalog("llms:stopSequencesMustHaveMax4Elements") = "Number of elements must not be larger than 4.";
46-
catalog("llms:keyMustBeSpecified") = "API key not found as environment variable OPENAI_API_KEY and not specified via ApiKey parameter.";
46+
catalog("llms:keyMustBeSpecified") = "API key not found as environment variable {1} and not specified via ApiKey parameter.";
4747
catalog("llms:mustHaveMessages") = "Value must contain at least one message in Messages.";
4848
catalog("llms:mustSetFunctionsForCall") = "When no functions are defined, ToolChoice must not be specified.";
4949
catalog("llms:mustBeMessagesOrTxt") = "Messages must be text with one or more characters or an openAIMessages objects.";
@@ -58,4 +58,4 @@
5858
catalog("llms:invalidOptionsForAzureBackEnd") = "The parameter Model Name is not compatible with Azure.";
5959
catalog("llms:apiReturnedError") = "OpenAI API Error: {1}";
6060
catalog("llms:dimensionsMustBeSmallerThan") = "Dimensions must be less than or equal to {1}.";
61-
end
61+
end

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ jobs:
1515
- name: Run tests and generate artifacts
1616
env:
1717
OPENAI_KEY: ${{ secrets.OPENAI_KEY }}
18+
AZURE_OPENAI_DEPLOYMENT: ${{ secrets.AZURE_DEPLOYMENT }}
19+
AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_ENDPOINT }}
20+
AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_KEY }}
1821
uses: matlab-actions/run-tests@v2
1922
with:
2023
test-results-junit: test-results/results.xml

azureChat.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
this.StopSequences = nvp.StopSequences;
133133
this.PresencePenalty = nvp.PresencePenalty;
134134
this.FrequencyPenalty = nvp.FrequencyPenalty;
135-
this.ApiKey = llms.internal.getApiKeyFromNvpOrEnv(nvp);
135+
this.ApiKey = llms.internal.getApiKeyFromNvpOrEnv(nvp,"AZURE_OPENAI_API_KEY");
136136
this.TimeOut = nvp.TimeOut;
137137
end
138138

extractOpenAIEmbeddings.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
END_POINT = "https://api.openai.com/v1/embeddings";
3535

36-
key = llms.internal.getApiKeyFromNvpOrEnv(nvp);
36+
key = llms.internal.getApiKeyFromNvpOrEnv(nvp,"OPENAI_API_KEY");
3737

3838
parameters = struct("input",text,"model",nvp.ModelName);
3939

openAIChat.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124

125125
this.PresencePenalty = nvp.PresencePenalty;
126126
this.FrequencyPenalty = nvp.FrequencyPenalty;
127-
this.ApiKey = llms.internal.getApiKeyFromNvpOrEnv(nvp);
127+
this.ApiKey = llms.internal.getApiKeyFromNvpOrEnv(nvp,"OPENAI_API_KEY");
128128
this.TimeOut = nvp.TimeOut;
129129
end
130130

openAIImages.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
end
5252

5353
this.ModelName = nvp.ModelName;
54-
this.ApiKey = llms.internal.getApiKeyFromNvpOrEnv(nvp);
54+
this.ApiKey = llms.internal.getApiKeyFromNvpOrEnv(nvp,"OPENAI_API_KEY");
5555
this.TimeOut = nvp.TimeOut;
5656
end
5757

tests/tazureChat.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
methods (TestClassSetup)
77
function saveEnvVar(testCase)
88
% Ensures key is not in environment variable for tests
9-
openAIEnvVar = "OPENAI_API_KEY";
10-
if isenv(openAIEnvVar)
11-
key = getenv(openAIEnvVar);
12-
unsetenv(openAIEnvVar);
13-
testCase.addTeardown(@(x) setenv(openAIEnvVar, x), key);
9+
azureKeyVar = "AZURE_OPENAI_API_KEY";
10+
if isenv(azureKeyVar)
11+
key = getenv(azureKeyVar);
12+
unsetenv(azureKeyVar);
13+
testCase.addTeardown(@(x) setenv(azureKeyVar, x), key);
1414
end
1515
end
1616
end

0 commit comments

Comments
 (0)