3
3
4
4
% Copyright 2024 The MathWorks, Inc.
5
5
6
- methods (TestClassSetup )
7
- function saveEnvVar(testCase )
8
- % Ensures key is not in environment variable for tests
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 );
14
- end
15
- end
16
- end
17
-
18
6
properties (TestParameter )
19
7
InvalidConstructorInput = iGetInvalidConstructorInput;
20
8
InvalidGenerateInput = iGetInvalidGenerateInput;
@@ -24,11 +12,14 @@ function saveEnvVar(testCase)
24
12
methods (Test )
25
13
% Test methods
26
14
function keyNotFound(testCase )
27
- testCase .verifyError(@()azureChat(" My_resource" , " Deployment" ), " llms:keyMustBeSpecified" );
15
+ import matlab .unittest .fixtures .EnvironmentVariableFixture
16
+ testCase .applyFixture(EnvironmentVariableFixture(" AZURE_OPENAI_API_KEY" ," dummy" ));
17
+ unsetenv(" AZURE_OPENAI_API_KEY" );
18
+ testCase .verifyError(@()azureChat(getenv(" AZURE_OPENAI_ENDPOINT" ), getenv(" AZURE_OPENAI_DEPLOYMENT" )), " llms:keyMustBeSpecified" );
28
19
end
29
20
30
21
function constructChatWithAllNVP(testCase )
31
- resourceName = " resource " ;
22
+ endpoint = getenv( " AZURE_OPENAI_ENDPOINT " ) ;
32
23
deploymentID = " hello" ;
33
24
functions = openAIFunction(" funName" );
34
25
temperature = 0 ;
@@ -39,7 +30,7 @@ function constructChatWithAllNVP(testCase)
39
30
frequenceP = 2 ;
40
31
systemPrompt = " This is a system prompt" ;
41
32
timeout = 3 ;
42
- chat = azureChat(resourceName , deploymentID , systemPrompt , Tools= functions , ...
33
+ chat = azureChat(endpoint , deploymentID , systemPrompt , Tools= functions , ...
43
34
Temperature= temperature , TopProbabilityMass= topP , StopSequences= stop , ApiKey= apiKey ,...
44
35
FrequencyPenalty= frequenceP , PresencePenalty= presenceP , TimeOut= timeout );
45
36
testCase .verifyEqual(chat .Temperature , temperature );
@@ -49,28 +40,36 @@ function constructChatWithAllNVP(testCase)
49
40
testCase .verifyEqual(chat .PresencePenalty , presenceP );
50
41
end
51
42
43
+ function doGenerate(testCase )
44
+ testCase .assumeTrue(isenv(" AZURE_OPENAI_API_KEY" )," end-to-end test requires environment variables AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, and AZURE_OPENAI_DEPLOYMENT." );
45
+ chat = azureChat(getenv(" AZURE_OPENAI_ENDPOINT" ), getenv(" AZURE_OPENAI_DEPLOYMENT" ));
46
+ response = testCase .verifyWarningFree(@() generate(chat ," hi" ));
47
+ testCase .verifyClass(response ,' string' );
48
+ testCase .verifyGreaterThan(strlength(response ),0 );
49
+ end
50
+
52
51
function verySmallTimeOutErrors(testCase )
53
- chat = azureChat(" My_resource " , " Deployment " , TimeOut= 0.0001 , ApiKey= " false-key" );
52
+ chat = azureChat(getenv( " AZURE_OPENAI_ENDPOINT " ), getenv( " AZURE_OPENAI_DEPLOYMENT " ) , TimeOut= 0.0001 , ApiKey= " false-key" );
54
53
testCase .verifyError(@()generate(chat , " hi" ), " MATLAB:webservices:Timeout" )
55
54
end
56
55
57
56
function errorsWhenPassingToolChoiceWithEmptyTools(testCase )
58
- chat = azureChat(" My_resource " , " Deployment " , ApiKey= " this-is-not-a-real-key" );
57
+ chat = azureChat(getenv( " AZURE_OPENAI_ENDPOINT " ), getenv( " AZURE_OPENAI_DEPLOYMENT " ) , ApiKey= " this-is-not-a-real-key" );
59
58
testCase .verifyError(@()generate(chat ," input" , ToolChoice= " bla" ), " llms:mustSetFunctionsForCall" );
60
59
end
61
60
62
61
function invalidInputsConstructor(testCase , InvalidConstructorInput )
63
- testCase .verifyError(@()azureChat(" My_resource " , " Deployment " , InvalidConstructorInput.Input{: }), InvalidConstructorInput .Error );
62
+ testCase .verifyError(@()azureChat(getenv( " AZURE_OPENAI_ENDPOINT " ), getenv( " AZURE_OPENAI_DEPLOYMENT " ) , InvalidConstructorInput.Input{: }), InvalidConstructorInput .Error );
64
63
end
65
64
66
65
function invalidInputsGenerate(testCase , InvalidGenerateInput )
67
66
f = openAIFunction(" validfunction" );
68
- chat = azureChat(" My_resource " , " Deployment " , Tools= f , ApiKey= " this-is-not-a-real-key" );
67
+ chat = azureChat(getenv( " AZURE_OPENAI_ENDPOINT " ), getenv( " AZURE_OPENAI_DEPLOYMENT " ) , Tools= f , ApiKey= " this-is-not-a-real-key" );
69
68
testCase .verifyError(@()generate(chat ,InvalidGenerateInput.Input{: }), InvalidGenerateInput .Error );
70
69
end
71
70
72
71
function invalidSetters(testCase , InvalidValuesSetters )
73
- chat = azureChat(" My_resource " , " Deployment " , ApiKey= " this-is-not-a-real-key" );
72
+ chat = azureChat(getenv( " AZURE_OPENAI_ENDPOINT " ), getenv( " AZURE_OPENAI_DEPLOYMENT " ) , ApiKey= " this-is-not-a-real-key" );
74
73
function assignValueToProperty(property , value )
75
74
chat.(property ) = value ;
76
75
end
0 commit comments