mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-01 11:37:58 +00:00
fix: Handle OpenAI API errors (#9560)
This commit is contained in:
@@ -11,7 +11,12 @@ class Api::V1::Accounts::Integrations::HooksController < Api::V1::Accounts::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def process_event
|
def process_event
|
||||||
render json: { message: @hook.process_event(params[:event]) }
|
response = @hook.process_event(params[:event])
|
||||||
|
if response[:error]
|
||||||
|
render json: { error: response[:error] }, status: :unprocessable_entity
|
||||||
|
else
|
||||||
|
render json: { message: response[:message] }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
|||||||
@@ -100,7 +100,11 @@ export default {
|
|||||||
} = result;
|
} = result;
|
||||||
return generatedMessage;
|
return generatedMessage;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
useAlert(this.$t('INTEGRATION_SETTINGS.OPEN_AI.GENERATE_ERROR'));
|
const errorData = error.response.data.error;
|
||||||
|
const errorMessage =
|
||||||
|
errorData?.error?.message ||
|
||||||
|
this.$t('INTEGRATION_SETTINGS.OPEN_AI.GENERATE_ERROR');
|
||||||
|
useAlert(errorMessage);
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ class Integrations::Hook < ApplicationRecord
|
|||||||
when 'openai'
|
when 'openai'
|
||||||
Integrations::Openai::ProcessorService.new(hook: self, event: event).perform if app_id == 'openai'
|
Integrations::Openai::ProcessorService.new(hook: self, event: event).perform if app_id == 'openai'
|
||||||
else
|
else
|
||||||
'No processor found'
|
{ error: 'No processor found' }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ module Enterprise::Integrations::OpenaiProcessorService
|
|||||||
# To what you ask? Sometimes, the response includes
|
# To what you ask? Sometimes, the response includes
|
||||||
# "Labels:" in it's response in some format. This is a hacky way to remove it
|
# "Labels:" in it's response in some format. This is a hacky way to remove it
|
||||||
# TODO: Fix with with a better prompt
|
# TODO: Fix with with a better prompt
|
||||||
response.present? ? response.gsub(/^(label|labels):/i, '') : ''
|
response[:message] ? response[:message].gsub(/^(label|labels):/i, '') : ''
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -77,8 +77,12 @@ class Integrations::OpenaiBaseService
|
|||||||
response = HTTParty.post(API_URL, headers: headers, body: body)
|
response = HTTParty.post(API_URL, headers: headers, body: body)
|
||||||
Rails.logger.info("OpenAI API response: #{response.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 = 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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -97,9 +97,8 @@ RSpec.describe 'Integration Hooks API', type: :request do
|
|||||||
params: params,
|
params: params,
|
||||||
headers: agent.create_new_auth_token,
|
headers: agent.create_new_auth_token,
|
||||||
as: :json
|
as: :json
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
expect(response).to have_http_status(:success)
|
expect(response.parsed_body['error']).to eq 'No processor found'
|
||||||
expect(response.parsed_body['message']).to eq('No processor found')
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ RSpec.describe Integrations::Openai::ProcessorService do
|
|||||||
.to_return(status: 200, body: openai_response, headers: {})
|
.to_return(status: 200, body: openai_response, headers: {})
|
||||||
|
|
||||||
result = subject.perform
|
result = subject.perform
|
||||||
expect(result).to eq('This is a reply from openai.')
|
expect(result).to eq({ :message => 'This is a reply from openai.' })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ RSpec.describe Integrations::Openai::ProcessorService do
|
|||||||
.to_return(status: 200, body: openai_response, headers: {})
|
.to_return(status: 200, body: openai_response, headers: {})
|
||||||
|
|
||||||
result = subject.perform
|
result = subject.perform
|
||||||
expect(result).to eq('This is a reply from openai.')
|
expect(result).to eq({ :message => 'This is a reply from openai.' })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -101,7 +101,7 @@ RSpec.describe Integrations::Openai::ProcessorService do
|
|||||||
.to_return(status: 200, body: openai_response, headers: {})
|
.to_return(status: 200, body: openai_response, headers: {})
|
||||||
|
|
||||||
result = subject.perform
|
result = subject.perform
|
||||||
expect(result).to eq('This is a reply from openai.')
|
expect(result).to eq({ :message => 'This is a reply from openai.' })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -140,7 +140,7 @@ RSpec.describe Integrations::Openai::ProcessorService do
|
|||||||
.to_return(status: 200, body: openai_response, headers: {})
|
.to_return(status: 200, body: openai_response, headers: {})
|
||||||
|
|
||||||
result = subject.perform
|
result = subject.perform
|
||||||
expect(result).to eq('This is a reply from openai.')
|
expect(result).to eq({ :message => 'This is a reply from openai.' })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -162,7 +162,7 @@ RSpec.describe Integrations::Openai::ProcessorService do
|
|||||||
.to_return(status: 200, body: openai_response, headers: {})
|
.to_return(status: 200, body: openai_response, headers: {})
|
||||||
|
|
||||||
result = subject.perform
|
result = subject.perform
|
||||||
expect(result).to eq('This is a reply from openai.')
|
expect(result).to eq({ :message => 'This is a reply from openai.' })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -184,7 +184,7 @@ RSpec.describe Integrations::Openai::ProcessorService do
|
|||||||
.to_return(status: 200, body: openai_response, headers: {})
|
.to_return(status: 200, body: openai_response, headers: {})
|
||||||
|
|
||||||
result = subject.perform
|
result = subject.perform
|
||||||
expect(result).to eq('This is a reply from openai.')
|
expect(result).to eq({ :message => 'This is a reply from openai.' })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -206,7 +206,7 @@ RSpec.describe Integrations::Openai::ProcessorService do
|
|||||||
.to_return(status: 200, body: openai_response, headers: {})
|
.to_return(status: 200, body: openai_response, headers: {})
|
||||||
|
|
||||||
result = subject.perform
|
result = subject.perform
|
||||||
expect(result).to eq('This is a reply from openai.')
|
expect(result).to eq({ :message => 'This is a reply from openai.' })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -228,7 +228,7 @@ RSpec.describe Integrations::Openai::ProcessorService do
|
|||||||
.to_return(status: 200, body: openai_response, headers: {})
|
.to_return(status: 200, body: openai_response, headers: {})
|
||||||
|
|
||||||
result = subject.perform
|
result = subject.perform
|
||||||
expect(result).to eq('This is a reply from openai.')
|
expect(result).to eq({ :message => 'This is a reply from openai.' })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -250,7 +250,7 @@ RSpec.describe Integrations::Openai::ProcessorService do
|
|||||||
.to_return(status: 200, body: openai_response, headers: {})
|
.to_return(status: 200, body: openai_response, headers: {})
|
||||||
|
|
||||||
result = subject.perform
|
result = subject.perform
|
||||||
expect(result).to eq('This is a reply from openai.')
|
expect(result).to eq({ :message => 'This is a reply from openai.' })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ RSpec.describe Integrations::Hook do
|
|||||||
|
|
||||||
it 'returns no processor found for hooks with out processor defined' do
|
it 'returns no processor found for hooks with out processor defined' do
|
||||||
hook = create(:integrations_hook, account: account)
|
hook = create(:integrations_hook, account: account)
|
||||||
expect(hook.process_event(params)).to eq('No processor found')
|
expect(hook.process_event(params)).to eq({ :error => 'No processor found' })
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns results from procesor for openai hook' do
|
it 'returns results from procesor for openai hook' do
|
||||||
|
|||||||
Reference in New Issue
Block a user