fix: Handle OpenAI API errors (#9560)

This commit is contained in:
Muhsin Keloth
2024-08-22 10:24:13 +05:30
committed by GitHub
parent 6571baf211
commit 429d281501
8 changed files with 30 additions and 18 deletions

View File

@@ -77,8 +77,12 @@ class Integrations::OpenaiBaseService
response = HTTParty.post(API_URL, headers: headers, body: body)
Rails.logger.info("OpenAI API response: #{response.body}")
return { error: response.parsed_response, error_code: response.code } unless response.success?
choices = JSON.parse(response.body)['choices']
choices.present? ? choices.first['message']['content'] : nil
return { message: choices.first['message']['content'] } if choices.present?
{ message: nil }
end
end