Skip to content

Commit fce8bb8

Browse files
authored
Merge pull request #71 from matlab-deep-learning/avoid-content-error
Avoid bogus access to `json.choices.delta.content`
2 parents 062538c + 53c1df5 commit fce8bb8

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

+llms/+stream/responseStreamer.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
end
8585
this.StreamFun('');
8686
this.ResponseText = txt;
87-
else
87+
elseif isfield(json.choices,"delta") && ...
88+
isfield(json.choices.delta,"content")
8889
txt = json.choices.delta.content;
8990
this.StreamFun(txt);
9091
this.ResponseText = [this.ResponseText txt];

tests/topenAIChat.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ function invalidInputsConstructor(testCase, InvalidConstructorInput)
9797
testCase.verifyError(@()openAIChat(InvalidConstructorInput.Input{:}), InvalidConstructorInput.Error);
9898
end
9999

100+
function generateWithStreamFunAndMaxNumTokens(testCase)
101+
sf = @(x) fprintf("%s",x);
102+
chat = openAIChat(StreamFun=sf);
103+
result = generate(chat,"Why is a raven like a writing desk?",MaxNumTokens=5);
104+
testCase.verifyClass(result,"string");
105+
testCase.verifyLessThan(strlength(result), 100);
106+
end
107+
100108
function generateWithToolsAndStreamFunc(testCase)
101109
import matlab.unittest.constraints.HasField
102110

0 commit comments

Comments
 (0)