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
This commit is contained in:
Sam 2024-02-01 22:01:07 +11:00 committed by GitHub
parent fd6fcfdb61
commit cec4251b00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 9 deletions

View File

@ -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

View File

@ -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