diff --git a/enterprise/lib/enterprise/integrations/openai_processor_service.rb b/enterprise/lib/enterprise/integrations/openai_processor_service.rb index d081a5e92..2b086f8d2 100644 --- a/enterprise/lib/enterprise/integrations/openai_processor_service.rb +++ b/enterprise/lib/enterprise/integrations/openai_processor_service.rb @@ -13,7 +13,7 @@ module Enterprise::Integrations::OpenaiProcessorService # 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.gsub(/^(label|labels):/i, '') + response.present? ? response.gsub(/^(label|labels):/i, '') : '' end private diff --git a/enterprise/spec/integrations/openai/processor_service_spec.rb b/enterprise/spec/integrations/openai/processor_service_spec.rb index 2f4033ef7..966062d37 100644 --- a/enterprise/spec/integrations/openai/processor_service_spec.rb +++ b/enterprise/spec/integrations/openai/processor_service_spec.rb @@ -35,7 +35,7 @@ RSpec.describe Integrations::Openai::ProcessorService do context 'when event name is label_suggestion with labels with >3 messages' do let(:event) { { 'name' => 'label_suggestion', 'data' => { 'conversation_display_id' => conversation.display_id } } } - it 'returns the label suggestions' do + before do create(:message, account: account, conversation: conversation, message_type: :incoming, content: 'hello agent') create(:message, account: account, conversation: conversation, message_type: :outgoing, content: 'hello customer') create(:message, account: account, conversation: conversation, message_type: :incoming, content: 'hello agent 2') @@ -44,7 +44,9 @@ RSpec.describe Integrations::Openai::ProcessorService do create(:label, account: account) create(:label, account: account) + end + it 'returns the label suggestions' do stub_request(:post, 'https://api.openai.com/v1/chat/completions') .with(body: anything, headers: expected_headers) .to_return(status: 200, body: openai_response, headers: {}) @@ -52,6 +54,15 @@ RSpec.describe Integrations::Openai::ProcessorService do result = subject.perform expect(result).to eq('This is a reply from openai.') end + + it 'returns empty string if openai response is blank' do + stub_request(:post, 'https://api.openai.com/v1/chat/completions') + .with(body: anything, headers: expected_headers) + .to_return(status: 200, body: '{}', headers: {}) + + result = subject.perform + expect(result).to eq('') + end end context 'when event name is label_suggestion with no labels' do diff --git a/lib/integrations/openai_base_service.rb b/lib/integrations/openai_base_service.rb index 73cc9b44f..9cfdc71c3 100644 --- a/lib/integrations/openai_base_service.rb +++ b/lib/integrations/openai_base_service.rb @@ -74,7 +74,10 @@ class Integrations::OpenaiBaseService 'Authorization' => "Bearer #{hook.settings['api_key']}" } + Rails.logger.info("OpenAI API request: #{body}") response = HTTParty.post(API_URL, headers: headers, body: body) + Rails.logger.info("OpenAI API response: #{response.body}") + choices = JSON.parse(response.body)['choices'] choices.present? ? choices.first['message']['content'] : nil