20
20
gpt35Model
21
21
end
22
22
23
- methods (Test )
24
- % Test methods
25
- function generateAcceptsSingleStringAsInput(testCase ,StringInputs )
26
- response = testCase .verifyWarningFree(...
27
- @()generate(testCase .defaultModel ,StringInputs ));
28
- testCase .verifyClass(response ,' string' );
29
- testCase .verifyGreaterThan(strlength(response ),0 );
30
- end
31
-
32
- function generateMultipleResponses(testCase )
33
- [~ ,~ ,response ] = generate(testCase .defaultModel ," What is a cat?" ,NumCompletions= 3 );
34
- testCase .verifySize(response .Body .Data .choices ,[3 ,1 ]);
35
- end
36
-
37
- function generateAcceptsMessagesAsInput(testCase )
38
- messages = messageHistory ;
39
- messages = addUserMessage(messages , " This should be okay." );
40
-
41
- testCase .verifyWarningFree(...
42
- @()generate(testCase .defaultModel ,messages ));
43
- end
23
+ methods (Abstract )
24
+ responseMessage
25
+ end
44
26
27
+ methods (Test ) % not calling the server
45
28
function validConstructorCalls(testCase ,ValidConstructorInput )
46
29
if isempty(ValidConstructorInput .ExpectedWarning )
47
30
chat = testCase .verifyWarningFree(...
@@ -60,17 +43,87 @@ function validConstructorCalls(testCase,ValidConstructorInput)
60
43
function fixedSeedFixesResult(testCase )
61
44
% Seed is "beta" in OpenAI documentation
62
45
% and not reliable at this time.
63
- testCase .assumeFail(" disabled since the server is unreliable in honoring the Seed parameter" );
46
+ import matlab .unittest .constraints .HasField
47
+ [sendRequestMock ,sendRequestBehaviour ] = ...
48
+ createMock(testCase , AddedMethods= " sendRequest" );
49
+ testCase .assignOutputsWhen( ...
50
+ withAnyInputs(sendRequestBehaviour .sendRequest ),...
51
+ testCase .responseMessage(" Hello" )," This output is unused with Stream=false" );
52
+
53
+ chat = testCase .defaultModel ;
54
+ chat.sendRequestFcn = @(varargin ) sendRequestMock .sendRequest(varargin{: });
55
+
56
+ generate(chat ," This is okay" , " Seed" , 2 );
57
+
58
+ calls = testCase .getMockHistory(sendRequestMock );
59
+
60
+ testCase .verifySize(calls ,[1 ,1 ]);
61
+ sentHistory = calls.Inputs{2 };
62
+ testCase .assertThat(sentHistory ,HasField(" seed" ));
63
+ testCase .verifyEqual(sentHistory .seed ,2 );
64
+ end
65
+
66
+ function generateOverridesProperties(testCase )
67
+ import matlab .unittest .constraints .HasField
68
+ [sendRequestMock ,sendRequestBehaviour ] = ...
69
+ createMock(testCase , AddedMethods= " sendRequest" );
70
+ testCase .assignOutputsWhen( ...
71
+ withAnyInputs(sendRequestBehaviour .sendRequest ),...
72
+ testCase .responseMessage(" Hello" )," This output is unused with Stream=false" );
73
+
74
+ chat = testCase .defaultModel ;
75
+ chat.sendRequestFcn = @(varargin ) sendRequestMock .sendRequest(varargin{: });
64
76
65
- result1 = generate(testCase .defaultModel ," This is okay" , " Seed" , 2 );
66
- result2 = generate(testCase .defaultModel ," This is okay" , " Seed" , 2 );
67
- testCase .verifyEqual(result1 ,result2 );
77
+ generate(chat , " Please count from 1 to 10." , Temperature= 0 , StopSequences= " 4" );
78
+
79
+ calls = testCase .getMockHistory(sendRequestMock );
80
+
81
+ testCase .assertSize(calls ,[1 ,1 ]);
82
+ sentHistory = calls.Inputs{2 };
83
+ testCase .assertThat(sentHistory ,HasField(" temperature" ));
84
+ testCase .verifyEqual(sentHistory .temperature , 0 );
85
+ testCase .assertThat(sentHistory ,HasField(" stop" ));
86
+ testCase .verifyEqual(sentHistory .stop , " 4" );
68
87
end
69
88
70
89
function invalidInputsConstructor(testCase , InvalidConstructorInput )
71
90
testCase .verifyError(@()testCase .constructor(InvalidConstructorInput.Input{: }), InvalidConstructorInput .Error );
72
91
end
73
92
93
+ function keyNotFound(testCase )
94
+ % to verify the error, we need to unset the environment variable
95
+ % OPENAI_API_KEY, if given. Use a fixture to restore the
96
+ % value on leaving the test point:
97
+ import matlab .unittest .fixtures .EnvironmentVariableFixture
98
+ testCase .applyFixture(EnvironmentVariableFixture(" OPENAI_API_KEY" ," dummy" ));
99
+ unsetenv(" OPENAI_API_KEY" );
100
+ testCase .applyFixture(EnvironmentVariableFixture(" AZURE_OPENAI_API_KEY" ," dummy" ));
101
+ unsetenv(" AZURE_OPENAI_API_KEY" );
102
+ testCase .verifyError(testCase .constructor , " llms:keyMustBeSpecified" );
103
+ end
104
+ end
105
+
106
+ methods (Test ) % end-to-end, calling the server
107
+ function generateAcceptsSingleStringAsInput(testCase ,StringInputs )
108
+ response = testCase .verifyWarningFree(...
109
+ @()generate(testCase .defaultModel ,StringInputs ));
110
+ testCase .verifyClass(response ,' string' );
111
+ testCase .verifyGreaterThan(strlength(response ),0 );
112
+ end
113
+
114
+ function generateMultipleResponses(testCase )
115
+ [~ ,~ ,response ] = generate(testCase .defaultModel ," What is a cat?" ,NumCompletions= 3 );
116
+ testCase .verifySize(response .Body .Data .choices ,[3 ,1 ]);
117
+ end
118
+
119
+ function generateAcceptsMessagesAsInput(testCase )
120
+ messages = messageHistory ;
121
+ messages = addUserMessage(messages , " This should be okay." );
122
+
123
+ testCase .verifyWarningFree(...
124
+ @()generate(testCase .defaultModel ,messages ));
125
+ end
126
+
74
127
function generateWithStreamFunAndMaxNumTokens(testCase )
75
128
sf = @(x ) x ;
76
129
chat = testCase .constructor(StreamFun = sf );
@@ -101,12 +154,6 @@ function generateWithMultipleImages(testCase)
101
154
ContainsSubstring(" very similar" ));
102
155
end
103
156
104
- function generateOverridesProperties(testCase )
105
- import matlab .unittest .constraints .EndsWithSubstring
106
- text = generate(testCase .defaultModel , " Please count from 1 to 10." , Temperature = 0 , StopSequences = " 4" );
107
- testCase .verifyThat(text , EndsWithSubstring(" 3, " ));
108
- end
109
-
110
157
function invalidInputsGenerate(testCase , InvalidGenerateInput )
111
158
f = openAIFunction(" validfunction" );
112
159
chat = testCase .constructor(Tools = f , APIKey= " this-is-not-a-real-key" );
@@ -161,18 +208,6 @@ function jsonFormatWithPrompt(testCase)
161
208
" string" );
162
209
end
163
210
164
- function keyNotFound(testCase )
165
- % to verify the error, we need to unset the environment variable
166
- % OPENAI_API_KEY, if given. Use a fixture to restore the
167
- % value on leaving the test point:
168
- import matlab .unittest .fixtures .EnvironmentVariableFixture
169
- testCase .applyFixture(EnvironmentVariableFixture(" OPENAI_API_KEY" ," dummy" ));
170
- unsetenv(" OPENAI_API_KEY" );
171
- testCase .applyFixture(EnvironmentVariableFixture(" AZURE_OPENAI_API_KEY" ," dummy" ));
172
- unsetenv(" AZURE_OPENAI_API_KEY" );
173
- testCase .verifyError(testCase .constructor , " llms:keyMustBeSpecified" );
174
- end
175
-
176
211
function toolCallingAndStructuredOutput(testCase )
177
212
import matlab .unittest .constraints .HasField
178
213
0 commit comments