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:
parent
fd6fcfdb61
commit
cec4251b00
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue