Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create pull request to to update activate-code-coverage with new content from main #25

Merged
merged 11 commits into from
Apr 24, 2024
1 change: 1 addition & 0 deletions +llms/+utils/errorMessageCatalog.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@
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}";
catalog("llms:dimensionsMustBeSmallerThan") = "Dimensions must be less than or equal to {1}.";
end
Binary file modified examples/AnalyzeScientificPapersUsingFunctionCalls.mlx
Binary file not shown.
Binary file not shown.
Binary file removed examples/ExampleParallelFunctionCalls.mlx
Binary file not shown.
20 changes: 17 additions & 3 deletions extractOpenAIEmbeddings.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
% [emb, response] = EXTRACTOPENAIEMBEDDINGS(...) also returns the full
% response from the OpenAI API call.
%
% Copyright 2023 The MathWorks, Inc.
% Copyright 2023-2024 The MathWorks, Inc.

arguments
text (1,:) {mustBeText}
text (1,:) {mustBeNonzeroLengthText}
nvp.ModelName (1,1) {mustBeMember(nvp.ModelName,["text-embedding-ada-002", ...
"text-embedding-3-large", "text-embedding-3-small"])} = "text-embedding-ada-002"
nvp.TimeOut (1,1) {mustBeReal,mustBePositive} = 10
nvp.Dimensions (1,1) {mustBeInteger}
nvp.Dimensions (1,1) {mustBeInteger,mustBePositive}
nvp.ApiKey {llms.utils.mustBeNonzeroLengthTextScalar}
end

Expand All @@ -42,6 +42,7 @@
error("llms:invalidOptionForModel", ...
llms.utils.errorMessageCatalog.getMessage("llms:invalidOptionForModel", "Dimensions", nvp.ModelName));
end
mustBeCorrectDimensions(nvp.Dimensions,nvp.ModelName);
parameters.dimensions = nvp.Dimensions;
end

Expand All @@ -53,4 +54,17 @@
emb = emb';
else
emb = [];
end
end

function mustBeCorrectDimensions(dimensions,modelName)
model2dim = ....
dictionary(["text-embedding-3-large", "text-embedding-3-small"], ...
[3072,1536]);

if dimensions>model2dim(modelName)
error("llms:dimensionsMustBeSmallerThan", ...
llms.utils.errorMessageCatalog.getMessage("llms:dimensionsMustBeSmallerThan", ...
string(model2dim(modelName))));
end
end
10 changes: 6 additions & 4 deletions openAIImages.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
% Only "dall-e-3" supports this parameter.

arguments
this (1,1) openAIImages
prompt {mustBeTextScalar}
this (1,1) openAIImages
prompt {mustBeNonzeroLengthTextScalar}
nvp.NumImages (1,1) {mustBePositive, mustBeInteger,...
mustBeLessThanOrEqual(nvp.NumImages,10)} = 1
nvp.Size (1,1) string {mustBeMember(nvp.Size, ["256x256", "512x512", ...
Expand Down Expand Up @@ -176,7 +176,7 @@
arguments
this (1,1) openAIImages
imagePath {mustBeValidFileType(imagePath)}
prompt {mustBeTextScalar}
prompt {mustBeNonzeroLengthTextScalar}
nvp.MaskImagePath {mustBeValidFileType(nvp.MaskImagePath)}
nvp.NumImages (1,1) {mustBePositive, mustBeInteger,...
mustBeLessThanOrEqual(nvp.NumImages,10)} = 1
Expand Down Expand Up @@ -345,7 +345,9 @@ function validatePromptSize(model, prompt)
function mustBeValidFileType(filePath)
mustBeFile(filePath);
s = dir(filePath);
if ~endsWith(s.name, ".png")
imgDetails = imfinfo(filePath);
imgFormat = imgDetails.Format;
if ~(imgFormat=="png")
error("llms:pngExpected", ...
llms.utils.errorMessageCatalog.getMessage("llms:pngExpected"));
end
Expand Down
Binary file added tests/test_files/solar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 24 additions & 2 deletions tests/textractOpenAIEmbeddings.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
classdef textractOpenAIEmbeddings < matlab.unittest.TestCase
% Tests for extractOpenAIEmbeddings

% Copyright 2023 The MathWorks, Inc.
% Copyright 2023-2024 The MathWorks, Inc.

methods (TestClassSetup)
function saveEnvVar(testCase)
Expand Down Expand Up @@ -56,6 +56,14 @@ function testInvalidInputs(testCase, InvalidInput)

function invalidInput = iGetInvalidInput
invalidInput = struct( ...
"InvalidEmptyText", struct( ...
"Input",{{ "" }},...
"Error", "MATLAB:validators:mustBeNonzeroLengthText"), ...
...
"InvalidEmptyTextArray", struct( ...
"Input",{{ ["", ""] }},...
"Error", "MATLAB:validators:mustBeNonzeroLengthText"), ...
...
"InvalidTimeOutType", struct( ...
"Input",{{ "bla", "TimeOut", "2" }},...
"Error", "MATLAB:validators:mustBeReal"), ...
Expand All @@ -66,7 +74,7 @@ function testInvalidInputs(testCase, InvalidInput)
...
"WrongTypeText",struct( ...
"Input",{{ 123 }},...
"Error","MATLAB:validators:mustBeText"),...
"Error","MATLAB:validators:mustBeNonzeroLengthText"),...
...
"InvalidModelNameType",struct( ...
"Input",{{"bla", "ModelName", 0 }},...
Expand All @@ -84,6 +92,20 @@ function testInvalidInputs(testCase, InvalidInput)
"Input",{{"bla", "Dimensions", "123" }},...
"Error","MATLAB:validators:mustBeNumericOrLogical"),...
...
"InvalidDimensionValue",struct( ...
"Input",{{"bla", "Dimensions", "-11" }},...
"Error","MATLAB:validators:mustBeNumericOrLogical"),...
...
"LargeDimensionValueForModelLarge",struct( ...
"Input",{{"bla", "ModelName", "text-embedding-3-large", ...
"Dimensions", 3073, "ApiKey", "fake-key" }},...
"Error","llms:dimensionsMustBeSmallerThan"),...
...
"LargeDimensionValueForModelSmall",struct( ...
"Input",{{"bla", "ModelName", "text-embedding-3-small", ...
"Dimensions", 1537, "ApiKey", "fake-key" }},...
"Error","llms:dimensionsMustBeSmallerThan"),...
...
"InvalidDimensionSize",struct( ...
"Input",{{"bla", "Dimensions", [123, 123] }},...
"Error","MATLAB:validation:IncompatibleSize"),...
Expand Down
22 changes: 18 additions & 4 deletions tests/topenAIImages.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ function constructModelWithAllNVP(testCase)
testCase.verifyEqual(mdl.ModelName, modelName);
end

function fakePNGImage(testCase)
mdl = openAIImages(ApiKey="this-is-not-a-real-key");
fakePng = fullfile("test_files", "solar.png");
testCase.verifyError(@()edit(mdl,fakePng, "bla"), "llms:pngExpected");
end

function invalidInputsConstructor(testCase, InvalidConstructorInput)
testCase.verifyError(@()openAIImages(InvalidConstructorInput.Input{:}), InvalidConstructorInput.Error);
end
Expand Down Expand Up @@ -157,11 +163,15 @@ function invalidInputsVariation(testCase, InvalidVariationInput)
invalidGenerateInput = struct( ...
"EmptyInput",struct( ...
"Input",{{ [] }},...
"Error","MATLAB:validators:mustBeTextScalar"),...
"Error","MATLAB:validators:mustBeNonzeroLengthText"),...
...
"InvalidInputType",struct( ...
"Input",{{ 123 }},...
"Error","MATLAB:validators:mustBeTextScalar"),...
"Error","MATLAB:validators:mustBeNonzeroLengthText"),...
...
"InvalidPromptLen",struct( ...
"Input",{{ "" }},...
"Error","MATLAB:validators:mustBeNonzeroLengthText"),...
...
"InvalidNumImagesType",struct( ...
"Input",{{ "prompt" "NumImages" "2" }},...
Expand Down Expand Up @@ -233,17 +243,21 @@ function invalidInputsVariation(testCase, InvalidVariationInput)
"Input",{{ 123, "prompt" }},...
"Error","MATLAB:validators:mustBeNonzeroLengthText"),...
...
"InvalidPromptLen",struct( ...
"Input",{{ validImage, "" }},...
"Error","MATLAB:validators:mustBeNonzeroLengthText"),...
...
"InvalidImageExtension",struct( ...
"Input",{{ nonPNGImage, "prompt" }},...
"Error","llms:pngExpected"),...
...
"EmptyPrompt",struct( ...
"Input",{{ validImage, [] }},...
"Error","MATLAB:validators:mustBeTextScalar"),...
"Error","MATLAB:validators:mustBeNonzeroLengthText"),...
...
"InvalidPromptType",struct( ...
"Input",{{ validImage, 123 }},...
"Error","MATLAB:validators:mustBeTextScalar"),...
"Error","MATLAB:validators:mustBeNonzeroLengthText"),...
...
"InvalidMaskImage",struct( ...
"Input",{{ validImage, "foo", "MaskImagePath", 123 }},...
Expand Down
Loading