chore: Fix OpenAI sentry errors (#7687)
- add check for nil condition - add logs for openAI
This commit is contained in:
@@ -13,7 +13,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.gsub(/^(label|labels):/i, '')
|
response.present? ? response.gsub(/^(label|labels):/i, '') : ''
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ RSpec.describe Integrations::Openai::ProcessorService do
|
|||||||
context 'when event name is label_suggestion with labels with >3 messages' 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 } } }
|
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: :incoming, content: 'hello agent')
|
||||||
create(:message, account: account, conversation: conversation, message_type: :outgoing, content: 'hello customer')
|
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')
|
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)
|
||||||
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')
|
stub_request(:post, 'https://api.openai.com/v1/chat/completions')
|
||||||
.with(body: anything, headers: expected_headers)
|
.with(body: anything, headers: expected_headers)
|
||||||
.to_return(status: 200, body: openai_response, headers: {})
|
.to_return(status: 200, body: openai_response, headers: {})
|
||||||
@@ -52,6 +54,15 @@ RSpec.describe Integrations::Openai::ProcessorService do
|
|||||||
result = subject.perform
|
result = subject.perform
|
||||||
expect(result).to eq('This is a reply from openai.')
|
expect(result).to eq('This is a reply from openai.')
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context 'when event name is label_suggestion with no labels' do
|
context 'when event name is label_suggestion with no labels' do
|
||||||
|
|||||||
@@ -74,7 +74,10 @@ class Integrations::OpenaiBaseService
|
|||||||
'Authorization' => "Bearer #{hook.settings['api_key']}"
|
'Authorization' => "Bearer #{hook.settings['api_key']}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Rails.logger.info("OpenAI API request: #{body}")
|
||||||
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}")
|
||||||
|
|
||||||
choices = JSON.parse(response.body)['choices']
|
choices = JSON.parse(response.body)['choices']
|
||||||
|
|
||||||
choices.present? ? choices.first['message']['content'] : nil
|
choices.present? ? choices.first['message']['content'] : nil
|
||||||
|
|||||||
Reference in New Issue
Block a user