2020 gpt35Model
2121 end
2222
23- methods (Test )
24- % Test methods
25- function generateAcceptsSingleStringAsInput(testCase ,StringInputs )
26- response = testCase .verifyWarningFree(...
27- @()generate(testCase .defaultModel ,StringInputs ));
28- testCase .verifyClass(response ,' string' );
29- testCase .verifyGreaterThan(strlength(response ),0 );
30- end
31-
32- function generateMultipleResponses(testCase )
33- [~ ,~ ,response ] = generate(testCase .defaultModel ," What is a cat?" ,NumCompletions= 3 );
34- testCase .verifySize(response .Body .Data .choices ,[3 ,1 ]);
35- end
36-
37- function generateAcceptsMessagesAsInput(testCase )
38- messages = messageHistory ;
39- messages = addUserMessage(messages , " This should be okay." );
40-
41- testCase .verifyWarningFree(...
42- @()generate(testCase .defaultModel ,messages ));
43- end
23+ methods (Abstract )
24+ responseMessage
25+ end
4426
27+ methods (Test ) % not calling the server
4528 function validConstructorCalls(testCase ,ValidConstructorInput )
4629 if isempty(ValidConstructorInput .ExpectedWarning )
4730 chat = testCase .verifyWarningFree(...
@@ -60,17 +43,87 @@ function validConstructorCalls(testCase,ValidConstructorInput)
6043 function fixedSeedFixesResult(testCase )
6144 % Seed is "beta" in OpenAI documentation
6245 % and not reliable at this time.
63- testCase .assumeFail(" disabled since the server is unreliable in honoring the Seed parameter" );
46+ import matlab .unittest .constraints .HasField
47+ [sendRequestMock ,sendRequestBehaviour ] = ...
48+ createMock(testCase , AddedMethods= " sendRequest" );
49+ testCase .assignOutputsWhen( ...
50+ withAnyInputs(sendRequestBehaviour .sendRequest ),...
51+ testCase .responseMessage(" Hello" )," This output is unused with Stream=false" );
52+
53+ chat = testCase .defaultModel ;
54+ chat.sendRequestFcn = @(varargin ) sendRequestMock .sendRequest(varargin{: });
55+
56+ generate(chat ," This is okay" , " Seed" , 2 );
57+
58+ calls = testCase .getMockHistory(sendRequestMock );
59+
60+ testCase .verifySize(calls ,[1 ,1 ]);
61+ sentHistory = calls.Inputs{2 };
62+ testCase .assertThat(sentHistory ,HasField(" seed" ));
63+ testCase .verifyEqual(sentHistory .seed ,2 );
64+ end
65+
66+ function generateOverridesProperties(testCase )
67+ import matlab .unittest .constraints .HasField
68+ [sendRequestMock ,sendRequestBehaviour ] = ...
69+ createMock(testCase , AddedMethods= " sendRequest" );
70+ testCase .assignOutputsWhen( ...
71+ withAnyInputs(sendRequestBehaviour .sendRequest ),...
72+ testCase .responseMessage(" Hello" )," This output is unused with Stream=false" );
73+
74+ chat = testCase .defaultModel ;
75+ chat.sendRequestFcn = @(varargin ) sendRequestMock .sendRequest(varargin{: });
6476
65- result1 = generate(testCase .defaultModel ," This is okay" , " Seed" , 2 );
66- result2 = generate(testCase .defaultModel ," This is okay" , " Seed" , 2 );
67- testCase .verifyEqual(result1 ,result2 );
77+ generate(chat , " Please count from 1 to 10." , Temperature= 0 , StopSequences= " 4" );
78+
79+ calls = testCase .getMockHistory(sendRequestMock );
80+
81+ testCase .assertSize(calls ,[1 ,1 ]);
82+ sentHistory = calls.Inputs{2 };
83+ testCase .assertThat(sentHistory ,HasField(" temperature" ));
84+ testCase .verifyEqual(sentHistory .temperature , 0 );
85+ testCase .assertThat(sentHistory ,HasField(" stop" ));
86+ testCase .verifyEqual(sentHistory .stop , " 4" );
6887 end
6988
7089 function invalidInputsConstructor(testCase , InvalidConstructorInput )
7190 testCase .verifyError(@()testCase .constructor(InvalidConstructorInput.Input{: }), InvalidConstructorInput .Error );
7291 end
7392
93+ function keyNotFound(testCase )
94+ % to verify the error, we need to unset the environment variable
95+ % OPENAI_API_KEY, if given. Use a fixture to restore the
96+ % value on leaving the test point:
97+ import matlab .unittest .fixtures .EnvironmentVariableFixture
98+ testCase .applyFixture(EnvironmentVariableFixture(" OPENAI_API_KEY" ," dummy" ));
99+ unsetenv(" OPENAI_API_KEY" );
100+ testCase .applyFixture(EnvironmentVariableFixture(" AZURE_OPENAI_API_KEY" ," dummy" ));
101+ unsetenv(" AZURE_OPENAI_API_KEY" );
102+ testCase .verifyError(testCase .constructor , " llms:keyMustBeSpecified" );
103+ end
104+ end
105+
106+ methods (Test ) % end-to-end, calling the server
107+ function generateAcceptsSingleStringAsInput(testCase ,StringInputs )
108+ response = testCase .verifyWarningFree(...
109+ @()generate(testCase .defaultModel ,StringInputs ));
110+ testCase .verifyClass(response ,' string' );
111+ testCase .verifyGreaterThan(strlength(response ),0 );
112+ end
113+
114+ function generateMultipleResponses(testCase )
115+ [~ ,~ ,response ] = generate(testCase .defaultModel ," What is a cat?" ,NumCompletions= 3 );
116+ testCase .verifySize(response .Body .Data .choices ,[3 ,1 ]);
117+ end
118+
119+ function generateAcceptsMessagesAsInput(testCase )
120+ messages = messageHistory ;
121+ messages = addUserMessage(messages , " This should be okay." );
122+
123+ testCase .verifyWarningFree(...
124+ @()generate(testCase .defaultModel ,messages ));
125+ end
126+
74127 function generateWithStreamFunAndMaxNumTokens(testCase )
75128 sf = @(x ) x ;
76129 chat = testCase .constructor(StreamFun = sf );
@@ -101,12 +154,6 @@ function generateWithMultipleImages(testCase)
101154 ContainsSubstring(" very similar" ));
102155 end
103156
104- function generateOverridesProperties(testCase )
105- import matlab .unittest .constraints .EndsWithSubstring
106- text = generate(testCase .defaultModel , " Please count from 1 to 10." , Temperature = 0 , StopSequences = " 4" );
107- testCase .verifyThat(text , EndsWithSubstring(" 3, " ));
108- end
109-
110157 function invalidInputsGenerate(testCase , InvalidGenerateInput )
111158 f = openAIFunction(" validfunction" );
112159 chat = testCase .constructor(Tools = f , APIKey= " this-is-not-a-real-key" );
@@ -161,18 +208,6 @@ function jsonFormatWithPrompt(testCase)
161208 " string" );
162209 end
163210
164- function keyNotFound(testCase )
165- % to verify the error, we need to unset the environment variable
166- % OPENAI_API_KEY, if given. Use a fixture to restore the
167- % value on leaving the test point:
168- import matlab .unittest .fixtures .EnvironmentVariableFixture
169- testCase .applyFixture(EnvironmentVariableFixture(" OPENAI_API_KEY" ," dummy" ));
170- unsetenv(" OPENAI_API_KEY" );
171- testCase .applyFixture(EnvironmentVariableFixture(" AZURE_OPENAI_API_KEY" ," dummy" ));
172- unsetenv(" AZURE_OPENAI_API_KEY" );
173- testCase .verifyError(testCase .constructor , " llms:keyMustBeSpecified" );
174- end
175-
176211 function toolCallingAndStructuredOutput(testCase )
177212 import matlab .unittest .constraints .HasField
178213
0 commit comments