Skip to content

Commit 55b421f

Browse files
committed
Added test cases
1 parent c78d0f9 commit 55b421f

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

+llms/+utils/errorMessageCatalog.m

+2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@
4949
catalog("llms:mustBeMessagesOrTxt") = "Messages must be text with one or more characters or an openAIMessages objects.";
5050
catalog("llms:invalidOptionAndValueForModel") = "'{1}' with value '{2}' is not supported for ModelName '{3}'";
5151
catalog("llms:invalidOptionForModel") = "{1} is not supported for ModelName '{2}'";
52+
catalog("llms:invalidContentTypeForModel") = "{1} is not supported for ModelName '{2}'";
5253
catalog("llms:functionNotAvailableForModel") = "This function is not supported for ModelName '{1}'";
5354
catalog("llms:promptLimitCharacter") = "Prompt must have a maximum length of {1} characters for ModelName '{2}'";
5455
catalog("llms:pngExpected") = "Argument must be a PNG image.";
5556
catalog("llms:warningJsonInstruction") = "When using JSON mode, you must also prompt the model to produce JSON yourself via a system or user message.";
57+
catalog("llms:apiReturnedError") = "OpenAI API Error: {1}";
5658
end

openAIChat.m

+14
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@
216216
messagesStruct = messages.Messages;
217217
end
218218

219+
if iscell(messagesStruct{end}.content) && any(cellfun(@(x) isfield(x,"image_url"), messagesStruct{end}.content))
220+
if ~ismember(this.ModelName,["gpt-4-turbo","gpt-4-turbo-2024-04-09"])
221+
error("llms:invalidContentTypeForModel", ...
222+
llms.utils.errorMessageCatalog.getMessage("llms:invalidContentTypeForModel", "Image content", this.ModelName));
223+
end
224+
end
225+
219226
if ~isempty(this.SystemPrompt)
220227
messagesStruct = horzcat(this.SystemPrompt, messagesStruct);
221228
end
@@ -227,6 +234,13 @@
227234
PresencePenalty=this.PresencePenalty, FrequencyPenalty=this.FrequencyPenalty, ...
228235
ResponseFormat=this.ResponseFormat,Seed=nvp.Seed, ...
229236
ApiKey=this.ApiKey,TimeOut=this.TimeOut, StreamFun=this.StreamFun);
237+
238+
if isfield(response.Body.Data,"error")
239+
err = response.Body.Data.error.message;
240+
text = llms.utils.errorMessageCatalog.getMessage("llms:apiReturnedError",err);
241+
message = struct("role","assistant","content",text);
242+
end
243+
230244
end
231245

232246
function this = set.Temperature(this, temperature)

tests/topenAIChat.m

+15-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,21 @@ function assignValueToProperty(property, value)
100100
end
101101

102102
testCase.verifyError(@()assignValueToProperty(InvalidValuesSetters.Property,InvalidValuesSetters.Value), InvalidValuesSetters.Error);
103-
end
103+
end
104+
105+
function invalidGenerateInputforModel(testCase)
106+
chat = openAIChat(ApiKey="this-is-not-a-real-key");
107+
image_path = "peppers.png";
108+
emptyMessages = openAIMessages;
109+
inValidMessages = addUserMessageWithImages(emptyMessages,"What is in the image?",image_path);
110+
testCase.verifyError(@()generate(chat,inValidMessages), "llms:invalidContentTypeForModel")
111+
end
112+
113+
function noStopSequencesNoMaxNumTokens(testCase)
114+
chat = openAIChat(ApiKey="this-is-not-a-real-key");
115+
testCase.verifyWarningFree(@()generate(chat,"This is okay"));
116+
end
117+
104118
end
105119
end
106120

0 commit comments

Comments
 (0)