|
36 | 36 | str = erase(str,"data: ");
|
37 | 37 |
|
38 | 38 | for i = 1:length(str)
|
39 |
| - json = jsondecode(str{i}); |
40 |
| - if strcmp(json.choices.finish_reason,'stop') |
| 39 | + if strcmp(str{i},'[DONE]') |
41 | 40 | stop = true;
|
42 | 41 | return
|
43 | 42 | else
|
44 |
| - txt = json.choices.delta.content; |
45 |
| - this.StreamFun(txt); |
46 |
| - this.ResponseText = [this.ResponseText txt]; |
| 43 | + try |
| 44 | + json = jsondecode(str{i}); |
| 45 | + catch ME |
| 46 | + errID = 'llms:stream:responseStreamer:InvalidInput'; |
| 47 | + msg = "Input does not have the expected json format. " + str{i}; |
| 48 | + causeException = MException(errID,msg); |
| 49 | + ME = addCause(ME,causeException); |
| 50 | + rethrow(ME) |
| 51 | + end |
| 52 | + if ischar(json.choices.finish_reason) && ismember(json.choices.finish_reason,["stop","tool_calls"]) |
| 53 | + stop = true; |
| 54 | + return |
| 55 | + else |
| 56 | + if isfield(json.choices.delta,"tool_calls") |
| 57 | + if isfield(json.choices.delta.tool_calls,"id") |
| 58 | + id = json.choices.delta.tool_calls.id; |
| 59 | + type = json.choices.delta.tool_calls.type; |
| 60 | + fcn = json.choices.delta.tool_calls.function; |
| 61 | + s = struct('id',id,'type',type,'function',fcn); |
| 62 | + txt = jsonencode(s); |
| 63 | + else |
| 64 | + s = jsondecode(this.ResponseText); |
| 65 | + args = json.choices.delta.tool_calls.function.arguments; |
| 66 | + s.function.arguments = [s.function.arguments args]; |
| 67 | + txt = jsonencode(s); |
| 68 | + end |
| 69 | + this.StreamFun(''); |
| 70 | + this.ResponseText = txt; |
| 71 | + else |
| 72 | + txt = json.choices.delta.content; |
| 73 | + this.StreamFun(txt); |
| 74 | + this.ResponseText = [this.ResponseText txt]; |
| 75 | + end |
| 76 | + end |
47 | 77 | end
|
48 | 78 | end
|
49 | 79 | end
|
|
0 commit comments