Skip to content

Commit fa61f1f

Browse files
committed
Update topenAIChat.m
Add extra test to increase code coverage in openAIChat
1 parent 751b348 commit fa61f1f

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

tests/topenAIChat.m

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function generateAcceptsMessagesAsInput(testCase)
3434
chat = openAIChat(ApiKey="this-is-not-a-real-key");
3535
messages = openAIMessages;
3636
messages = addUserMessage(messages, "This should be okay.");
37+
3738
testCase.verifyWarningFree(@()generate(chat,messages));
3839
end
3940

@@ -55,6 +56,7 @@ function constructChatWithAllNVP(testCase)
5556
chat = openAIChat(systemPrompt, Tools=functions, ModelName=modelName, ...
5657
Temperature=temperature, TopProbabilityMass=topP, StopSequences=stop, ApiKey=apiKey,...
5758
FrequencyPenalty=frequenceP, PresencePenalty=presenceP, TimeOut=timeout);
59+
5860
testCase.verifyEqual(chat.ModelName, modelName);
5961
testCase.verifyEqual(chat.Temperature, temperature);
6062
testCase.verifyEqual(chat.TopProbabilityMass, topP);
@@ -65,27 +67,37 @@ function constructChatWithAllNVP(testCase)
6567

6668
function verySmallTimeOutErrors(testCase)
6769
chat = openAIChat(TimeOut=0.0001, ApiKey="false-key");
70+
6871
testCase.verifyError(@()generate(chat, "hi"), "MATLAB:webservices:Timeout")
6972
end
7073

7174
function errorsWhenPassingToolChoiceWithEmptyTools(testCase)
7275
chat = openAIChat(ApiKey="this-is-not-a-real-key");
76+
7377
testCase.verifyError(@()generate(chat,"input", ToolChoice="bla"), "llms:mustSetFunctionsForCall");
7478
end
7579

7680
function settingToolChoiceWithNone(testCase)
7781
functions = openAIFunction("funName");
7882
chat = openAIChat(ApiKey="this-is-not-a-real-key",Tools=functions);
83+
7984
testCase.verifyWarningFree(@()generate(chat,"This is okay","ToolChoice","none"));
8085
end
8186

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+
8293
function invalidInputsConstructor(testCase, InvalidConstructorInput)
8394
testCase.verifyError(@()openAIChat(InvalidConstructorInput.Input{:}), InvalidConstructorInput.Error);
8495
end
8596

8697
function invalidInputsGenerate(testCase, InvalidGenerateInput)
8798
f = openAIFunction("validfunction");
8899
chat = openAIChat(Tools=f, ApiKey="this-is-not-a-real-key");
100+
89101
testCase.verifyError(@()generate(chat,InvalidGenerateInput.Input{:}), InvalidGenerateInput.Error);
90102
end
91103

@@ -103,22 +115,36 @@ function invalidGenerateInputforModel(testCase)
103115
image_path = "peppers.png";
104116
emptyMessages = openAIMessages;
105117
inValidMessages = addUserMessageWithImages(emptyMessages,"What is in the image?",image_path);
118+
106119
testCase.verifyError(@()generate(chat,inValidMessages), "llms:invalidContentTypeForModel")
107120
end
108121

109122
function noStopSequencesNoMaxNumTokens(testCase)
110123
chat = openAIChat(ApiKey="this-is-not-a-real-key");
124+
111125
testCase.verifyWarningFree(@()generate(chat,"This is okay"));
112126
end
113127

114128
function createOpenAIChatWithStreamFunc(testCase)
115129
sf = @(x)fprintf("%s", x);
116130
chat = openAIChat(ApiKey="this-is-not-a-real-key", StreamFun=sf);
131+
117132
testCase.verifyWarningFree(@()generate(chat, "Hello world."));
118133
end
119134

135+
function warningJSONResponseFormatGPT35(testCase)
136+
chat = @() openAIChat("You are a useful assistant", ...
137+
ApiKey="this-is-not-a-real-key", ...
138+
ResponseFormat="json", ...
139+
ModelName="gpt-3.5-turbo");
140+
141+
testCase.verifyWarning(@()chat(), "llms:warningJsonInstruction");
142+
end
143+
120144
function createOpenAIChatWithOpenAIKey(testCase)
121-
chat = openAIChat(ApiKey=getenv("OPENAI_KEY"));
145+
chat = openAIChat("You are a useful assistant", ...
146+
ApiKey=getenv("OPENAI_KEY"));
147+
122148
testCase.verifyWarningFree(@()generate(chat, "Hello world."));
123149
end
124150

@@ -420,5 +446,9 @@ function createOpenAIChatWithOpenAIKey(testCase)
420446
...
421447
"InvalidToolChoiceSize",struct( ...
422448
"Input",{{ validMessages "ToolChoice" ["validfunction", "validfunction"] }},...
423-
"Error","MATLAB:validators:mustBeTextScalar"));
449+
"Error","MATLAB:validators:mustBeTextScalar"),...
450+
...
451+
"InvalidSeed",struct( ...
452+
"Input",{{ validMessages "Seed" "2" }},...
453+
"Error","MATLAB:validators:mustBeNumericOrLogical"));
424454
end

0 commit comments

Comments
 (0)