File tree Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Original file line number Diff line number Diff line change @@ -93,20 +93,20 @@ def self.to_json(string)
93
93
JSON . parse ( string . gsub ( "}\n {" , "},{" ) . prepend ( "[" ) . concat ( "]" ) )
94
94
end
95
95
96
+ # Given a proc, returns a proc that can be used to iterate over a JSON stream of chunks.
97
+ # For each chunk, the proc is called giving it the JSON object. The JSON object could be a
98
+ # data object or an error object as described in the OpenAI API documentation.
99
+ #
100
+ # If the JSON object for a given data or error message is invalid, it is ignored.
101
+ #
102
+ # @param user_proc [Proc] The proc to call for each JSON object in the chunk.
103
+ # @return [Proc] A proc that can be used to iterate over the JSON stream.
96
104
private_class_method def self . to_json_stream ( user_proc :)
97
105
proc do |chunk , _ |
98
- # The regex below matches the following pattern:
99
- # data: {JSON}
100
- # error: {JSON}
101
- # data: {JSON}
102
- # ...
103
- # data: [DONE]
104
-
105
- # Only call the user_proc if the chunk contains a JSON object.
106
106
chunk . scan ( /(?:data|error): (\{ .*\} )/i ) . flatten . each do |data |
107
107
user_proc . call ( JSON . parse ( data ) )
108
108
rescue JSON ::ParserError
109
- # If the JSON object is invalid, then just ignore it .
109
+ # Ignore invalid JSON .
110
110
end
111
111
end
112
112
end
You can’t perform that action at this time.
0 commit comments