Skip to content

Commit 78279d2

Browse files
committed
Update topenAIChat.m
Changing createOpenAIChatWithStreamFunc as suggested by Christopher
1 parent 857fd45 commit 78279d2

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

tests/topenAIChat.m

+15-2
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,23 @@ function noStopSequencesNoMaxNumTokens(testCase)
126126
end
127127

128128
function createOpenAIChatWithStreamFunc(testCase)
129-
sf = @(x)fprintf("%s", x);
130-
chat = openAIChat(ApiKey="this-is-not-a-real-key", StreamFun=sf);
129+
130+
function seen = sf(str)
131+
persistent data;
132+
if isempty(data)
133+
data = strings(1, 0);
134+
end
135+
% Append streamed text to an empty string array of length 1
136+
data = [data, str];
137+
seen = data;
138+
end
139+
chat = openAIChat(ApiKey=getenv("OPENAI_KEY"), StreamFun=@sf);
131140

132141
testCase.verifyWarningFree(@()generate(chat, "Hello world."));
142+
% Checking that persistent data, which is still stored in
143+
% memory, is greater than 1. This would mean that the stream
144+
% function has been called and streamed some text.
145+
testCase.verifyGreaterThan(numel(sf("")), 1);
133146
end
134147

135148
function warningJSONResponseFormatGPT35(testCase)

0 commit comments

Comments
 (0)