Skip to content

Commit 7d77b47

Browse files
Update method documentation
1 parent 455e61a commit 7d77b47

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

lib/openai/client.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,20 @@ def self.to_json(string)
9393
JSON.parse(string.gsub("}\n{", "},{").prepend("[").concat("]"))
9494
end
9595

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.
96104
private_class_method def self.to_json_stream(user_proc:)
97105
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.
106106
chunk.scan(/(?:data|error): (\{.*\})/i).flatten.each do |data|
107107
user_proc.call(JSON.parse(data))
108108
rescue JSON::ParserError
109-
# If the JSON object is invalid, then just ignore it.
109+
# Ignore invalid JSON.
110110
end
111111
end
112112
end

0 commit comments

Comments
 (0)