From cec4251b00208436ed1b90590117807f3d5d7f0d Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 1 Feb 2024 22:01:07 +1100 Subject: [PATCH] DEV: improve error bedrock error messages (#454) When bedrock rate limits it returns a 200 BUT also returns a JSON document with the error. Previously we had no special case here so we complained about nil New code properly logs the problem --- lib/completions/endpoints/aws_bedrock.rb | 26 ++++++++++++++++-------- lib/completions/endpoints/base.rb | 3 +++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/completions/endpoints/aws_bedrock.rb b/lib/completions/endpoints/aws_bedrock.rb index 96e86d76..92c5e16f 100644 --- a/lib/completions/endpoints/aws_bedrock.rb +++ b/lib/completions/endpoints/aws_bedrock.rb @@ -93,15 +93,23 @@ module DiscourseAi end def decode(chunk) - Aws::EventStream::Decoder - .new - .decode_chunk(chunk) - .first - .payload - .string - .then { JSON.parse(_1) } - .dig("bytes") - .then { Base64.decode64(_1) } + parsed = + Aws::EventStream::Decoder + .new + .decode_chunk(chunk) + .first + .payload + .string + .then { JSON.parse(_1) } + + bytes = parsed.dig("bytes") + + if !bytes + Rails.logger.error("#{self.class.name}: #{parsed.to_s[0..500]}") + nil + else + Base64.decode64(parsed.dig("bytes")) + end rescue JSON::ParserError, Aws::EventStream::Errors::MessageChecksumError, Aws::EventStream::Errors::PreludeChecksumError => e diff --git a/lib/completions/endpoints/base.rb b/lib/completions/endpoints/base.rb index 2b842327..e86a9a8e 100644 --- a/lib/completions/endpoints/base.rb +++ b/lib/completions/endpoints/base.rb @@ -135,6 +135,9 @@ module DiscourseAi end decoded_chunk = decode(chunk) + if decoded_chunk.nil? + raise CompletionFailed, "#{self.class.name}: Failed to decode LLM completion" + end response_raw << decoded_chunk redo_chunk = leftover + decoded_chunk