Skip to content

Commit 56ac9b4

Browse files
committed
Merge branch 'bugfix' into 'main'
Bugfix See merge request dferreir/llms-with-matlab!7
2 parents 74da91d + ca5a30d commit 56ac9b4

File tree

3 files changed

+85
-63
lines changed

3 files changed

+85
-63
lines changed

.github/workflows/setup_matlab.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Run MATLAB Tests on GitHub-Hosted Runner
2+
on: [push]
3+
jobs:
4+
my-job:
5+
name: Run MATLAB Tests and Generate Artifacts
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Check out repository
9+
uses: actions/checkout@v3
10+
- name: Set up MATLAB
11+
uses: matlab-actions/setup-matlab@v1
12+
- name: Run tests and generate artifacts
13+
uses: matlab-actions/run-tests@v1
14+
with:
15+
test-results-junit: test-results/results.xml
16+
code-coverage-cobertura: code-coverage/coverage.xml

openAIChat.m

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@
5757

5858
% Copyright 2023 The MathWorks, Inc.
5959

60-
properties
61-
%MODELNAME Model name.
62-
ModelName
63-
60+
properties
6461
%TEMPERATURE Temperature of generation.
6562
Temperature
6663

@@ -74,15 +71,18 @@
7471
PresencePenalty
7572

7673
%FREQUENCYPENALTY Penalty for using a token that is frequent in the training data.
77-
FrequencyPenalty
78-
79-
%SYSTEMPROMPT System prompt.
80-
SystemPrompt = []
74+
FrequencyPenalty
8175
end
8276

8377
properties(SetAccess=private)
8478
%FUNCTIONNAMES Names of the functions that the model can request calls
8579
FunctionNames
80+
81+
%MODELNAME Model name.
82+
ModelName
83+
84+
%SYSTEMPROMPT System prompt.
85+
SystemPrompt = []
8686
end
8787

8888
properties(Access=private)
@@ -99,12 +99,12 @@
9999
nvp.ModelName (1,1) {mustBeMember(nvp.ModelName,["gpt-4", "gpt-4-0613", "gpt-4-32k", ...
100100
"gpt-3.5-turbo", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k",...
101101
"gpt-3.5-turbo-16k-0613"])} = "gpt-3.5-turbo"
102-
nvp.Temperature (1,1) {mustBeValidTemperature} = 1
103-
nvp.TopProbabilityMass (1,1) {mustBeValidTopP} = 1
104-
nvp.StopSequences (1,:) {mustBeValidStop} = {}
105-
nvp.ApiKey {mustBeNonzeroLengthTextScalar}
106-
nvp.PresencePenalty (1,1) {mustBeValidPenalty} = 0
107-
nvp.FrequencyPenalty (1,1) {mustBeValidPenalty} = 0
102+
nvp.Temperature {mustBeValidTemperature} = 1
103+
nvp.TopProbabilityMass {mustBeValidTopP} = 1
104+
nvp.StopSequences {mustBeValidStop} = {}
105+
nvp.ApiKey {mustBeNonzeroLengthTextScalar}
106+
nvp.PresencePenalty {mustBeValidPenalty} = 0
107+
nvp.FrequencyPenalty {mustBeValidPenalty} = 0
108108
end
109109

110110
if ~isempty(nvp.Functions)
@@ -180,40 +180,46 @@
180180
function this = set.Temperature(this, temperature)
181181
arguments
182182
this openAIChat
183-
temperature (1,1) {mustBeValidTemperature}
183+
temperature
184184
end
185+
mustBeValidTemperature(temperature);
186+
185187
this.Temperature = temperature;
186188
end
187189

188190
function this = set.TopProbabilityMass(this,topP)
189191
arguments
190192
this openAIChat
191-
topP (1,1) {mustBeValidTopP}
193+
topP
192194
end
195+
mustBeValidTopP(topP);
193196
this.TopProbabilityMass = topP;
194197
end
195198

196199
function this = set.StopSequences(this,stop)
197200
arguments
198201
this openAIChat
199-
stop (1,:) {mustBeValidStop}
202+
stop
200203
end
204+
mustBeValidStop(stop);
201205
this.StopSequences = stop;
202206
end
203207

204208
function this = set.PresencePenalty(this,penalty)
205209
arguments
206210
this openAIChat
207-
penalty (1,1) {mustBeValidPenalty}
211+
penalty
208212
end
213+
mustBeValidPenalty(penalty)
209214
this.PresencePenalty = penalty;
210215
end
211216

212217
function this = set.FrequencyPenalty(this,penalty)
213218
arguments
214219
this openAIChat
215-
penalty (1,1) {mustBeValidPenalty}
220+
penalty
216221
end
222+
mustBeValidPenalty(penalty)
217223
this.FrequencyPenalty = penalty;
218224
end
219225
end
@@ -271,24 +277,24 @@ function mustBeValidMsgs(value)
271277
end
272278

273279
function mustBeValidPenalty(value)
274-
mustBeLessThanOrEqual(value,2);
275-
mustBeGreaterThanOrEqual(value,-2);
280+
validateattributes(value, {'numeric'}, {'real', 'scalar', 'nonsparse', '<=', 2, '>=', -2})
276281
end
277282

278283
function mustBeValidTopP(value)
279-
mustBeNonnegative(value);
280-
mustBeLessThanOrEqual(value,1);
284+
validateattributes(value, {'numeric'}, {'real', 'scalar', 'nonnegative', 'nonsparse', '<=', 1})
281285
end
282286

283287
function mustBeValidTemperature(value)
284-
mustBeNonnegative(value);
285-
mustBeLessThanOrEqual(value,2)
288+
validateattributes(value, {'numeric'}, {'real', 'scalar', 'nonnegative', 'nonsparse', '<=', 2})
286289
end
287290

288291
function mustBeValidStop(value)
289-
mustBeNonzeroLengthText(value);
290-
% This restriction is set by the OpenAI API
291-
if numel(value)>4
292-
error("llms:stopSequencesMustHaveMax4Elements", llms.utils.errorMessageCatalog.getMessage("llms:stopSequencesMustHaveMax4Elements"));
292+
if ~isempty(value)
293+
mustBeVector(value);
294+
mustBeNonzeroLengthText(value);
295+
% This restriction is set by the OpenAI API
296+
if numel(value)>4
297+
error("llms:stopSequencesMustHaveMax4Elements", llms.utils.errorMessageCatalog.getMessage("llms:stopSequencesMustHaveMax4Elements"));
298+
end
293299
end
294300
end

tests/topenAIChat.m

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,42 +94,42 @@ function assignValueToProperty(property, value)
9494
"InvalidTemperatureType", struct( ...
9595
"Property", "Temperature", ...
9696
"Value", "2", ...
97-
"Error", "MATLAB:validators:mustBeNumericOrLogical"), ...
97+
"Error", "MATLAB:invalidType"), ...
9898
...
9999
"InvalidTemperatureSize", struct( ...
100100
"Property", "Temperature", ...
101101
"Value", [1 1 1], ...
102-
"Error", "MATLAB:validation:IncompatibleSize"), ...
102+
"Error", "MATLAB:expectedScalar"), ...
103103
...
104104
"TemperatureTooLarge", struct( ...
105105
"Property", "Temperature", ...
106106
"Value", 20, ...
107-
"Error", "MATLAB:validators:mustBeLessThanOrEqual"), ...
107+
"Error", "MATLAB:notLessEqual"), ...
108108
...
109109
"TemperatureTooSmall", struct( ...
110110
"Property", "Temperature", ...
111111
"Value", -20, ...
112-
"Error", "MATLAB:validators:mustBeNonnegative"), ...
112+
"Error", "MATLAB:expectedNonnegative"), ...
113113
...
114114
"InvalidTopProbabilityMassType", struct( ...
115115
"Property", "TopProbabilityMass", ...
116116
"Value", "2", ...
117-
"Error", "MATLAB:validators:mustBeNumericOrLogical"), ...
117+
"Error", "MATLAB:invalidType"), ...
118118
...
119119
"InvalidTopProbabilityMassSize", struct( ...
120120
"Property", "TopProbabilityMass", ...
121121
"Value", [1 1 1], ...
122-
"Error", "MATLAB:validation:IncompatibleSize"), ...
122+
"Error", "MATLAB:expectedScalar"), ...
123123
...
124124
"TopProbabilityMassTooLarge", struct( ...
125125
"Property", "TopProbabilityMass", ...
126126
"Value", 20, ...
127-
"Error", "MATLAB:validators:mustBeLessThanOrEqual"), ...
127+
"Error", "MATLAB:notLessEqual"), ...
128128
...
129129
"TopProbabilityMassTooSmall", struct( ...
130130
"Property", "TopProbabilityMass", ...
131131
"Value", -20, ...
132-
"Error", "MATLAB:validators:mustBeNonnegative"), ...
132+
"Error", "MATLAB:expectedNonnegative"), ...
133133
...
134134
"WrongTypeStopSequences", struct( ...
135135
"Property", "StopSequences", ...
@@ -139,7 +139,7 @@ function assignValueToProperty(property, value)
139139
"WrongSizeStopNonVector", struct( ...
140140
"Property", "StopSequences", ...
141141
"Value", repmat("stop", 4), ...
142-
"Error", "MATLAB:validation:IncompatibleSize"), ...
142+
"Error", "MATLAB:validators:mustBeVector"), ...
143143
...
144144
"EmptyStopSequences", struct( ...
145145
"Property", "StopSequences", ...
@@ -154,42 +154,42 @@ function assignValueToProperty(property, value)
154154
"InvalidPresencePenalty", struct( ...
155155
"Property", "PresencePenalty", ...
156156
"Value", "2", ...
157-
"Error", "MATLAB:validators:mustBeNumericOrLogical"), ...
157+
"Error", "MATLAB:invalidType"), ...
158158
...
159159
"InvalidPresencePenaltySize", struct( ...
160160
"Property", "PresencePenalty", ...
161161
"Value", [1 1 1], ...
162-
"Error", "MATLAB:validation:IncompatibleSize"), ...
162+
"Error", "MATLAB:expectedScalar"), ...
163163
...
164164
"PresencePenaltyTooLarge", struct( ...
165165
"Property", "PresencePenalty", ...
166166
"Value", 20, ...
167-
"Error", "MATLAB:validators:mustBeLessThanOrEqual"), ...
167+
"Error", "MATLAB:notLessEqual"), ...
168168
...
169169
"PresencePenaltyTooSmall", struct( ...
170170
"Property", "PresencePenalty", ...
171171
"Value", -20, ...
172-
"Error", "MATLAB:validators:mustBeGreaterThanOrEqual"), ...
172+
"Error", "MATLAB:notGreaterEqual"), ...
173173
...
174174
"InvalidFrequencyPenalty", struct( ...
175175
"Property", "FrequencyPenalty", ...
176176
"Value", "2", ...
177-
"Error", "MATLAB:validators:mustBeNumericOrLogical"), ...
177+
"Error", "MATLAB:invalidType"), ...
178178
...
179179
"InvalidFrequencyPenaltySize", struct( ...
180180
"Property", "FrequencyPenalty", ...
181181
"Value", [1 1 1], ...
182-
"Error", "MATLAB:validation:IncompatibleSize"), ...
182+
"Error", "MATLAB:expectedScalar"), ...
183183
...
184184
"FrequencyPenaltyTooLarge", struct( ...
185185
"Property", "FrequencyPenalty", ...
186186
"Value", 20, ...
187-
"Error", "MATLAB:validators:mustBeLessThanOrEqual"), ...
187+
"Error", "MATLAB:notLessEqual"), ...
188188
...
189189
"FrequencyPenaltyTooSmall", struct( ...
190190
"Property", "FrequencyPenalty", ...
191191
"Value", -20, ...
192-
"Error", "MATLAB:validators:mustBeGreaterThanOrEqual"));
192+
"Error", "MATLAB:notGreaterEqual"));
193193
end
194194

195195
function invalidConstructorInput = iGetInvalidConstructorInput
@@ -225,43 +225,43 @@ function assignValueToProperty(property, value)
225225
...
226226
"InvalidTemperatureType",struct( ...
227227
"Input",{{ "Temperature" "2" }},...
228-
"Error","MATLAB:validators:mustBeNumericOrLogical"),...
228+
"Error","MATLAB:invalidType"),...
229229
...
230230
"InvalidTemperatureSize",struct( ...
231231
"Input",{{ "Temperature" [1 1 1] }},...
232-
"Error","MATLAB:validation:IncompatibleSize"),...
232+
"Error","MATLAB:expectedScalar"),...
233233
...
234234
"TemperatureTooLarge",struct( ...
235235
"Input",{{ "Temperature" 20 }},...
236-
"Error","MATLAB:validators:mustBeLessThanOrEqual"),...
236+
"Error","MATLAB:notLessEqual"),...
237237
...
238238
"TemperatureTooSmall",struct( ...
239239
"Input",{{ "Temperature" -20 }},...
240-
"Error","MATLAB:validators:mustBeNonnegative"),...
240+
"Error","MATLAB:expectedNonnegative"),...
241241
...
242242
"InvalidTopProbabilityMassType",struct( ...
243243
"Input",{{ "TopProbabilityMass" "2" }},...
244-
"Error","MATLAB:validators:mustBeNumericOrLogical"),...
244+
"Error","MATLAB:invalidType"),...
245245
...
246246
"InvalidTopProbabilityMassSize",struct( ...
247247
"Input",{{ "TopProbabilityMass" [1 1 1] }},...
248-
"Error","MATLAB:validation:IncompatibleSize"),...
248+
"Error","MATLAB:expectedScalar"),...
249249
...
250250
"TopProbabilityMassTooLarge",struct( ...
251251
"Input",{{ "TopProbabilityMass" 20 }},...
252-
"Error","MATLAB:validators:mustBeLessThanOrEqual"),...
252+
"Error","MATLAB:notLessEqual"),...
253253
...
254254
"TopProbabilityMassTooSmall",struct( ...
255255
"Input",{{ "TopProbabilityMass" -20 }},...
256-
"Error","MATLAB:validators:mustBeNonnegative"),...
256+
"Error","MATLAB:expectedNonnegative"),...
257257
...
258258
"WrongTypeStopSequences",struct( ...
259259
"Input",{{ "StopSequences" 123}},...
260260
"Error","MATLAB:validators:mustBeNonzeroLengthText"),...
261261
...
262262
"WrongSizeStopNonVector",struct( ...
263263
"Input",{{ "StopSequences" repmat("stop", 4) }},...
264-
"Error","MATLAB:validation:IncompatibleSize"),...
264+
"Error","MATLAB:validators:mustBeVector"),...
265265
...
266266
"EmptyStopSequences",struct( ...
267267
"Input",{{ "StopSequences" ""}},...
@@ -273,35 +273,35 @@ function assignValueToProperty(property, value)
273273
...
274274
"InvalidPresencePenalty",struct( ...
275275
"Input",{{ "PresencePenalty" "2" }},...
276-
"Error","MATLAB:validators:mustBeNumericOrLogical"),...
276+
"Error","MATLAB:invalidType"),...
277277
...
278278
"InvalidPresencePenaltySize",struct( ...
279279
"Input",{{ "PresencePenalty" [1 1 1] }},...
280-
"Error","MATLAB:validation:IncompatibleSize"),...
280+
"Error","MATLAB:expectedScalar"),...
281281
...
282282
"PresencePenaltyTooLarge",struct( ...
283283
"Input",{{ "PresencePenalty" 20 }},...
284-
"Error","MATLAB:validators:mustBeLessThanOrEqual"),...
284+
"Error","MATLAB:notLessEqual"),...
285285
...
286286
"PresencePenaltyTooSmall",struct( ...
287287
"Input",{{ "PresencePenalty" -20 }},...
288-
"Error","MATLAB:validators:mustBeGreaterThanOrEqual"),...
288+
"Error","MATLAB:notGreaterEqual"),...
289289
...
290290
"InvalidFrequencyPenalty",struct( ...
291291
"Input",{{ "FrequencyPenalty" "2" }},...
292-
"Error","MATLAB:validators:mustBeNumericOrLogical"),...
292+
"Error","MATLAB:invalidType"),...
293293
...
294294
"InvalidFrequencyPenaltySize",struct( ...
295295
"Input",{{ "FrequencyPenalty" [1 1 1] }},...
296-
"Error","MATLAB:validation:IncompatibleSize"),...
296+
"Error","MATLAB:expectedScalar"),...
297297
...
298298
"FrequencyPenaltyTooLarge",struct( ...
299299
"Input",{{ "FrequencyPenalty" 20 }},...
300-
"Error","MATLAB:validators:mustBeLessThanOrEqual"),...
300+
"Error","MATLAB:notLessEqual"),...
301301
...
302302
"FrequencyPenaltyTooSmall",struct( ...
303303
"Input",{{ "FrequencyPenalty" -20 }},...
304-
"Error","MATLAB:validators:mustBeGreaterThanOrEqual"),...
304+
"Error","MATLAB:notGreaterEqual"),...
305305
...
306306
"InvalidApiKeyType",struct( ...
307307
"Input",{{ "ApiKey" 123 }},...

0 commit comments

Comments
 (0)