|
1 |
| -classdef responseStreamer < matlab.net.http.io.StringConsumer |
| 1 | +classdef responseStreamer < matlab.net.http.io.BinaryConsumer |
2 | 2 | %responseStreamer Responsible for obtaining the streaming results from the
|
3 | 3 | %API
|
4 | 4 |
|
|
7 | 7 | properties
|
8 | 8 | ResponseText
|
9 | 9 | StreamFun
|
| 10 | + Incomplete = "" |
10 | 11 | end
|
11 | 12 |
|
12 | 13 | methods
|
|
20 | 21 | if this.Response.StatusCode ~= matlab.net.http.StatusCode.OK
|
21 | 22 | length = 0;
|
22 | 23 | else
|
23 |
| - length = this.start@matlab.net.http.io.StringConsumer; |
| 24 | + length = this.start@matlab.net.http.io.BinaryConsumer; |
24 | 25 | end
|
25 | 26 | end
|
26 | 27 | end
|
27 | 28 |
|
28 | 29 | methods
|
29 | 30 | function [len,stop] = putData(this, data)
|
30 |
| - [len,stop] = this.putData@matlab.net.http.io.StringConsumer(data); |
| 31 | + [len,stop] = this.putData@matlab.net.http.io.BinaryConsumer(data); |
31 | 32 |
|
32 | 33 | % Extract out the response text from the message
|
33 | 34 | str = native2unicode(data','UTF-8');
|
| 35 | + str = this.Incomplete + string(str); |
| 36 | + this.Incomplete = ""; |
34 | 37 | str = split(str,newline);
|
35 | 38 | str = str(strlength(str)>0);
|
36 | 39 | str = erase(str,"data: ");
|
|
43 | 46 | try
|
44 | 47 | json = jsondecode(str{i});
|
45 | 48 | catch ME
|
| 49 | + if i == length(str) |
| 50 | + this.Incomplete = str{i}; |
| 51 | + return; |
| 52 | + end |
46 | 53 | errID = 'llms:stream:responseStreamer:InvalidInput';
|
47 | 54 | msg = "Input does not have the expected json format. " + str{i};
|
48 | 55 | ME = MException(errID,msg);
|
49 | 56 | throw(ME)
|
50 | 57 | end
|
51 |
| - if ischar(json.choices.finish_reason) && ismember(json.choices.finish_reason,["stop","tool_calls"]) |
52 |
| - stop = true; |
53 |
| - return |
54 |
| - else |
55 |
| - if isfield(json.choices.delta,"tool_calls") |
56 |
| - if isfield(json.choices.delta.tool_calls,"id") |
57 |
| - id = json.choices.delta.tool_calls.id; |
58 |
| - type = json.choices.delta.tool_calls.type; |
59 |
| - fcn = json.choices.delta.tool_calls.function; |
60 |
| - s = struct('id',id,'type',type,'function',fcn); |
61 |
| - txt = jsonencode(s); |
| 58 | + if isfield(json,'choices') |
| 59 | + if isempty(json.choices) |
| 60 | + continue; |
| 61 | + end |
| 62 | + if isfield(json.choices,'finish_reason') && ... |
| 63 | + ischar(json.choices.finish_reason) && ismember(json.choices.finish_reason,["stop","tool_calls"]) |
| 64 | + stop = true; |
| 65 | + return |
| 66 | + else |
| 67 | + if isfield(json.choices,"delta") && ... |
| 68 | + isfield(json.choices.delta,"tool_calls") |
| 69 | + if isfield(json.choices.delta.tool_calls,"id") |
| 70 | + id = json.choices.delta.tool_calls.id; |
| 71 | + type = json.choices.delta.tool_calls.type; |
| 72 | + fcn = json.choices.delta.tool_calls.function; |
| 73 | + s = struct('id',id,'type',type,'function',fcn); |
| 74 | + txt = jsonencode(s); |
| 75 | + else |
| 76 | + s = jsondecode(this.ResponseText); |
| 77 | + args = json.choices.delta.tool_calls.function.arguments; |
| 78 | + s.function.arguments = [s.function.arguments args]; |
| 79 | + txt = jsonencode(s); |
| 80 | + end |
| 81 | + this.StreamFun(''); |
| 82 | + this.ResponseText = txt; |
62 | 83 | else
|
63 |
| - s = jsondecode(this.ResponseText); |
64 |
| - args = json.choices.delta.tool_calls.function.arguments; |
65 |
| - s.function.arguments = [s.function.arguments args]; |
66 |
| - txt = jsonencode(s); |
| 84 | + txt = json.choices.delta.content; |
| 85 | + this.StreamFun(txt); |
| 86 | + this.ResponseText = [this.ResponseText txt]; |
67 | 87 | end
|
68 |
| - this.StreamFun(''); |
69 |
| - this.ResponseText = txt; |
70 |
| - else |
71 |
| - txt = json.choices.delta.content; |
72 |
| - this.StreamFun(txt); |
73 |
| - this.ResponseText = [this.ResponseText txt]; |
74 | 88 | end
|
| 89 | + else |
| 90 | + txt = json.message.content; |
| 91 | + this.StreamFun(txt); |
| 92 | + this.ResponseText = [this.ResponseText txt]; |
75 | 93 | end
|
76 | 94 | end
|
77 | 95 | end
|
|
0 commit comments