Skip to content

Commit

Permalink
Added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
toshiakit committed Apr 15, 2024
1 parent c78d0f9 commit 55b421f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions +llms/+utils/errorMessageCatalog.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
catalog("llms:mustBeMessagesOrTxt") = "Messages must be text with one or more characters or an openAIMessages objects.";
catalog("llms:invalidOptionAndValueForModel") = "'{1}' with value '{2}' is not supported for ModelName '{3}'";
catalog("llms:invalidOptionForModel") = "{1} is not supported for ModelName '{2}'";
catalog("llms:invalidContentTypeForModel") = "{1} is not supported for ModelName '{2}'";
catalog("llms:functionNotAvailableForModel") = "This function is not supported for ModelName '{1}'";
catalog("llms:promptLimitCharacter") = "Prompt must have a maximum length of {1} characters for ModelName '{2}'";
catalog("llms:pngExpected") = "Argument must be a PNG image.";
catalog("llms:warningJsonInstruction") = "When using JSON mode, you must also prompt the model to produce JSON yourself via a system or user message.";
catalog("llms:apiReturnedError") = "OpenAI API Error: {1}";
end
14 changes: 14 additions & 0 deletions openAIChat.m
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@
messagesStruct = messages.Messages;
end

if iscell(messagesStruct{end}.content) && any(cellfun(@(x) isfield(x,"image_url"), messagesStruct{end}.content))
if ~ismember(this.ModelName,["gpt-4-turbo","gpt-4-turbo-2024-04-09"])
error("llms:invalidContentTypeForModel", ...
llms.utils.errorMessageCatalog.getMessage("llms:invalidContentTypeForModel", "Image content", this.ModelName));
end
end

if ~isempty(this.SystemPrompt)
messagesStruct = horzcat(this.SystemPrompt, messagesStruct);
end
Expand All @@ -227,6 +234,13 @@
PresencePenalty=this.PresencePenalty, FrequencyPenalty=this.FrequencyPenalty, ...
ResponseFormat=this.ResponseFormat,Seed=nvp.Seed, ...
ApiKey=this.ApiKey,TimeOut=this.TimeOut, StreamFun=this.StreamFun);

if isfield(response.Body.Data,"error")
err = response.Body.Data.error.message;
text = llms.utils.errorMessageCatalog.getMessage("llms:apiReturnedError",err);
message = struct("role","assistant","content",text);
end

end

function this = set.Temperature(this, temperature)
Expand Down
16 changes: 15 additions & 1 deletion tests/topenAIChat.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,21 @@ function assignValueToProperty(property, value)
end

testCase.verifyError(@()assignValueToProperty(InvalidValuesSetters.Property,InvalidValuesSetters.Value), InvalidValuesSetters.Error);
end
end

function invalidGenerateInputforModel(testCase)
chat = openAIChat(ApiKey="this-is-not-a-real-key");
image_path = "peppers.png";
emptyMessages = openAIMessages;
inValidMessages = addUserMessageWithImages(emptyMessages,"What is in the image?",image_path);
testCase.verifyError(@()generate(chat,inValidMessages), "llms:invalidContentTypeForModel")
end

function noStopSequencesNoMaxNumTokens(testCase)
chat = openAIChat(ApiKey="this-is-not-a-real-key");
testCase.verifyWarningFree(@()generate(chat,"This is okay"));
end

end
end

Expand Down

0 comments on commit 55b421f

Please sign in to comment.