@@ -16,9 +16,9 @@ function saveEnvVar(testCase)
16
16
end
17
17
18
18
properties (TestParameter )
19
- InvalidConstructorInput = iGetInvalidConstructorInput;
20
- InvalidGenerateInput = iGetInvalidGenerateInput;
21
- InvalidValuesSetters = iGetInvalidValuesSetters;
19
+ InvalidConstructorInput = iGetInvalidConstructorInput() ;
20
+ InvalidGenerateInput = iGetInvalidGenerateInput() ;
21
+ InvalidValuesSetters = iGetInvalidValuesSetters() ;
22
22
end
23
23
24
24
methods (Test )
@@ -34,11 +34,8 @@ function generateAcceptsMessagesAsInput(testCase)
34
34
chat = openAIChat(ApiKey = " this-is-not-a-real-key" );
35
35
messages = openAIMessages ;
36
36
messages = addUserMessage(messages , " This should be okay." );
37
- testCase .verifyWarningFree(@()generate(chat ,messages ));
38
- end
39
37
40
- function constructMdlWithInvalidParameters(testCase )
41
- testCase .verifyError(@()openAIChat(ApiKey = " this-is-not-a-real-key" , ModelName= " gpt-4" , ResponseFormat= " json" ), " llms:invalidOptionAndValueForModel" );
38
+ testCase .verifyWarningFree(@()generate(chat ,messages ));
42
39
end
43
40
44
41
function keyNotFound(testCase )
@@ -59,6 +56,7 @@ function constructChatWithAllNVP(testCase)
59
56
chat = openAIChat(systemPrompt , Tools= functions , ModelName= modelName , ...
60
57
Temperature= temperature , TopProbabilityMass= topP , StopSequences= stop , ApiKey= apiKey ,...
61
58
FrequencyPenalty= frequenceP , PresencePenalty= presenceP , TimeOut= timeout );
59
+
62
60
testCase .verifyEqual(chat .ModelName , modelName );
63
61
testCase .verifyEqual(chat .Temperature , temperature );
64
62
testCase .verifyEqual(chat .TopProbabilityMass , topP );
@@ -69,27 +67,37 @@ function constructChatWithAllNVP(testCase)
69
67
70
68
function verySmallTimeOutErrors(testCase )
71
69
chat = openAIChat(TimeOut = 0.0001 , ApiKey= " false-key" );
70
+
72
71
testCase .verifyError(@()generate(chat , " hi" ), " MATLAB:webservices:Timeout" )
73
72
end
74
73
75
74
function errorsWhenPassingToolChoiceWithEmptyTools(testCase )
76
75
chat = openAIChat(ApiKey = " this-is-not-a-real-key" );
76
+
77
77
testCase .verifyError(@()generate(chat ," input" , ToolChoice= " bla" ), " llms:mustSetFunctionsForCall" );
78
78
end
79
79
80
80
function settingToolChoiceWithNone(testCase )
81
81
functions = openAIFunction(" funName" );
82
82
chat = openAIChat(ApiKey = " this-is-not-a-real-key" ,Tools= functions );
83
+
83
84
testCase .verifyWarningFree(@()generate(chat ," This is okay" ," ToolChoice" ," none" ));
84
85
end
85
86
87
+ function settingSeedToInteger(testCase )
88
+ chat = openAIChat(ApiKey = " this-is-not-a-real-key" );
89
+
90
+ testCase .verifyWarningFree(@()generate(chat ," This is okay" , " Seed" , 2 ));
91
+ end
92
+
86
93
function invalidInputsConstructor(testCase , InvalidConstructorInput )
87
94
testCase .verifyError(@()openAIChat(InvalidConstructorInput.Input{: }), InvalidConstructorInput .Error );
88
95
end
89
96
90
97
function invalidInputsGenerate(testCase , InvalidGenerateInput )
91
98
f = openAIFunction(" validfunction" );
92
99
chat = openAIChat(Tools = f , ApiKey= " this-is-not-a-real-key" );
100
+
93
101
testCase .verifyError(@()generate(chat ,InvalidGenerateInput.Input{: }), InvalidGenerateInput .Error );
94
102
end
95
103
@@ -107,18 +115,56 @@ function invalidGenerateInputforModel(testCase)
107
115
image_path = " peppers.png" ;
108
116
emptyMessages = openAIMessages ;
109
117
inValidMessages = addUserMessageWithImages(emptyMessages ," What is in the image?" ,image_path );
118
+
110
119
testCase .verifyError(@()generate(chat ,inValidMessages ), " llms:invalidContentTypeForModel" )
111
120
end
112
121
113
122
function noStopSequencesNoMaxNumTokens(testCase )
114
123
chat = openAIChat(ApiKey = " this-is-not-a-real-key" );
124
+
115
125
testCase .verifyWarningFree(@()generate(chat ," This is okay" ));
116
126
end
117
127
128
+ function createOpenAIChatWithStreamFunc(testCase )
129
+
130
+ function seen = sf(str )
131
+ persistent data ;
132
+ if isempty(data )
133
+ data = strings(1 , 0 );
134
+ end
135
+ % Append streamed text to an empty string array of length 1
136
+ data = [data , str ];
137
+ seen = data ;
138
+ end
139
+ chat = openAIChat(ApiKey = getenv(" OPENAI_KEY" ), StreamFun= @sf );
140
+
141
+ testCase .verifyWarningFree(@()generate(chat , " Hello world." ));
142
+ % Checking that persistent data, which is still stored in
143
+ % memory, is greater than 1. This would mean that the stream
144
+ % function has been called and streamed some text.
145
+ testCase .verifyGreaterThan(numel(sf(" " )), 1 );
146
+ end
147
+
148
+ function warningJSONResponseFormatGPT35(testCase )
149
+ chat = @() openAIChat(" You are a useful assistant" , ...
150
+ ApiKey= " this-is-not-a-real-key" , ...
151
+ ResponseFormat= " json" , ...
152
+ ModelName= " gpt-3.5-turbo" );
153
+
154
+ testCase .verifyWarning(@()chat(), " llms:warningJsonInstruction" );
155
+ end
156
+
157
+ function createOpenAIChatWithOpenAIKey(testCase )
158
+ chat = openAIChat(" You are a useful assistant" , ...
159
+ ApiKey= getenv(" OPENAI_KEY" ));
160
+
161
+ testCase .verifyWarningFree(@()generate(chat , " Hello world." ));
162
+ end
163
+
118
164
end
119
165
end
120
166
121
- function invalidValuesSetters = iGetInvalidValuesSetters
167
+ function invalidValuesSetters = iGetInvalidValuesSetters()
122
168
123
169
invalidValuesSetters = struct( ...
124
170
" InvalidTemperatureType" , struct( ...
@@ -222,7 +268,7 @@ function noStopSequencesNoMaxNumTokens(testCase)
222
268
" Error" , " MATLAB:notGreaterEqual" ));
223
269
end
224
270
225
- function invalidConstructorInput = iGetInvalidConstructorInput
271
+ function invalidConstructorInput = iGetInvalidConstructorInput()
226
272
validFunction = openAIFunction(" funName" );
227
273
invalidConstructorInput = struct( ...
228
274
" InvalidResponseFormatValue" , struct( ...
@@ -233,6 +279,10 @@ function noStopSequencesNoMaxNumTokens(testCase)
233
279
" Input" ,{{" ResponseFormat" , [" text" " text" ] }},...
234
280
" Error" , " MATLAB:validation:IncompatibleSize" ), ...
235
281
...
282
+ " InvalidResponseFormatModelCombination" , struct( ...
283
+ " Input" , {{" ApiKey" , " this-is-not-a-real-key" , " ModelName" , " gpt-4" , " ResponseFormat" , " json" }}, ...
284
+ " Error" , " llms:invalidOptionAndValueForModel" ), ...
285
+ ...
236
286
" InvalidStreamFunType" , struct( ...
237
287
" Input" ,{{" StreamFun" , " 2" }},...
238
288
" Error" , " MATLAB:validators:mustBeA" ), ...
@@ -366,7 +416,7 @@ function noStopSequencesNoMaxNumTokens(testCase)
366
416
" Error" ," MATLAB:validators:mustBeTextScalar" ));
367
417
end
368
418
369
- function invalidGenerateInput = iGetInvalidGenerateInput
419
+ function invalidGenerateInput = iGetInvalidGenerateInput()
370
420
emptyMessages = openAIMessages ;
371
421
validMessages = addUserMessage(emptyMessages ," Who invented the telephone?" );
372
422
@@ -409,5 +459,9 @@ function noStopSequencesNoMaxNumTokens(testCase)
409
459
...
410
460
" InvalidToolChoiceSize" ,struct( ...
411
461
" Input" ,{{ validMessages " ToolChoice" [" validfunction" , " validfunction" ] }},...
412
- " Error" ," MATLAB:validators:mustBeTextScalar" ));
462
+ " Error" ," MATLAB:validators:mustBeTextScalar" ),...
463
+ ...
464
+ " InvalidSeed" ,struct( ...
465
+ " Input" ,{{ validMessages " Seed" " 2" }},...
466
+ " Error" ," MATLAB:validators:mustBeNumericOrLogical" ));
413
467
end
0 commit comments