From 756db0f8d7c2d1c4c5d00ed6b2f2b658d9b3d2fd Mon Sep 17 00:00:00 2001 From: Obie Fernandez Date: Fri, 26 May 2023 16:46:30 -0600 Subject: [PATCH] debug and error handling for streams --- lib/openai/http.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/openai/http.rb b/lib/openai/http.rb index dd34efac..2f979b6d 100644 --- a/lib/openai/http.rb +++ b/lib/openai/http.rb @@ -53,11 +53,19 @@ def to_json(string) # @param user_proc [Proc] The inner proc to call for each JSON object in the chunk. # @return [Proc] An outer proc that iterates over a raw stream, converting it to JSON. def to_json_stream(user_proc:) - proc do |chunk, _| - chunk.scan(/(?:data|error): (\{.*\})/i).flatten.each do |data| - user_proc.call(JSON.parse(data)) - rescue JSON::ParserError - # Ignore invalid JSON. + proc do |chunk, overall_received_bytes, env| + Rails.logger.debug("raw chunk: #{chunk}") + + if chunk.start_with?("data: {") + chunk.each_line do |line| + next unless line.start_with?("data: {") + line.delete_prefix!("data: ") + user_proc.call(JSON.parse(line)) + end + elsif chunk.include?('"error": ') + user_proc.call(JSON.parse(chunk)) + else + Rails.logger.warn("unkown raw chunk: #{chunk}") end end end