Skip to content

Commit

Permalink
fix: Handle OpenAI API errors (chatwoot#9560)
Browse files Browse the repository at this point in the history
  • Loading branch information
muhsin-k authored Aug 22, 2024
1 parent 6571baf commit 429d281
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ def update
end

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

def destroy
Expand Down
6 changes: 5 additions & 1 deletion app/javascript/dashboard/mixins/aiMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ export default {
} = result;
return generatedMessage;
} 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 '';
}
},
Expand Down
2 changes: 1 addition & 1 deletion app/models/integrations/hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def process_event(event)
when 'openai'
Integrations::Openai::ProcessorService.new(hook: self, event: event).perform if app_id == 'openai'
else
'No processor found'
{ error: 'No processor found' }
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def label_suggestion_message
# To what you ask? Sometimes, the response includes
# "Labels:" in it's response in some format. This is a hacky way to remove it
# TODO: Fix with with a better prompt
response.present? ? response.gsub(/^(label|labels):/i, '') : ''
response[:message] ? response[:message].gsub(/^(label|labels):/i, '') : ''
end

private
Expand Down
6 changes: 5 additions & 1 deletion lib/integrations/openai_base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ def make_api_call(body)
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@
params: params,
headers: agent.create_new_auth_token,
as: :json

expect(response).to have_http_status(:success)
expect(response.parsed_body['message']).to eq('No processor found')
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq 'No processor found'
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions spec/lib/integrations/openai/processor_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
.to_return(status: 200, body: openai_response, headers: {})

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

Expand All @@ -76,7 +76,7 @@
.to_return(status: 200, body: openai_response, headers: {})

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

Expand All @@ -101,7 +101,7 @@
.to_return(status: 200, body: openai_response, headers: {})

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

Expand Down Expand Up @@ -140,7 +140,7 @@
.to_return(status: 200, body: openai_response, headers: {})

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

Expand All @@ -162,7 +162,7 @@
.to_return(status: 200, body: openai_response, headers: {})

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

Expand All @@ -184,7 +184,7 @@
.to_return(status: 200, body: openai_response, headers: {})

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

Expand All @@ -206,7 +206,7 @@
.to_return(status: 200, body: openai_response, headers: {})

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

Expand All @@ -228,7 +228,7 @@
.to_return(status: 200, body: openai_response, headers: {})

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

Expand All @@ -250,7 +250,7 @@
.to_return(status: 200, body: openai_response, headers: {})

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
Expand Down
2 changes: 1 addition & 1 deletion spec/models/integrations/hook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

it 'returns no processor found for hooks with out processor defined' do
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

it 'returns results from procesor for openai hook' do
Expand Down

0 comments on commit 429d281

Please sign in to comment.