feat: Allow label suggestions with OpenAI APIs (#7428)

This commit is contained in:
Shivam Mishra
2023-07-10 14:40:54 +05:30
committed by GitHub
parent 5b4f92ccd2
commit a940f4c1d2
7 changed files with 274 additions and 56 deletions

View File

@@ -2,18 +2,62 @@
import ApiClient from '../ApiClient';
/**
* Represents the data object for a OpenAI hook.
* @typedef {Object} ConversationMessageData
* @property {string} [tone] - The tone of the message.
* @property {string} [content] - The content of the message.
* @property {string} [conversation_display_id] - The display ID of the conversation (optional).
*/
/**
* A client for the OpenAI API.
* @extends ApiClient
*/
class OpenAIAPI extends ApiClient {
/**
* Creates a new OpenAIAPI instance.
*/
constructor() {
super('integrations', { accountScoped: true });
/**
* The conversation events supported by the API.
* @type {string[]}
*/
this.conversation_events = [
'summarize',
'reply_suggestion',
'label_suggestion',
];
/**
* The message events supported by the API.
* @type {string[]}
*/
this.message_events = ['rephrase'];
}
/**
* Processes an event using the OpenAI API.
* @param {Object} options - The options for the event.
* @param {string} [options.type='rephrase'] - The type of event to process.
* @param {string} [options.content] - The content of the event.
* @param {string} [options.tone] - The tone of the event.
* @param {string} [options.conversationId] - The ID of the conversation to process the event for.
* @param {string} options.hookId - The ID of the hook to use for processing the event.
* @returns {Promise} A promise that resolves with the result of the event processing.
*/
processEvent({ type = 'rephrase', content, tone, conversationId, hookId }) {
/**
* @type {ConversationMessageData}
*/
let data = {
tone,
content,
};
if (type === 'reply_suggestion' || type === 'summarize') {
if (this.conversation_events.includes(type)) {
data = {
conversation_display_id: conversationId,
};

View File

@@ -0,0 +1,54 @@
module Enterprise::Integrations::OpenaiProcessorService
ALLOWED_EVENT_NAMES = %w[rephrase summarize reply_suggestion label_suggestion].freeze
CACHEABLE_EVENTS = %w[label_suggestion].freeze
def label_suggestion_message
payload = label_suggestion_body
return nil if payload.blank?
response = make_api_call(label_suggestion_body)
# LLMs are not deterministic, so this is bandaid solution
# 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, '')
end
private
def labels_with_messages
labels = hook.account.labels.pluck(:title).join(', ')
character_count = labels.length
conversation = find_conversation
messages = init_messages_body(false)
add_messages_until_token_limit(conversation, messages, false, character_count)
return nil if messages.blank? || labels.blank?
"Messages:\n#{messages}\nLabels:\n#{labels}"
end
def label_suggestion_body
content = labels_with_messages
return nil if content.blank?
{
model: self.class::GPT_MODEL,
messages: [
{
role: 'system',
content: 'Your role is as an assistant to a customer support agent. You will be provided with a transcript of a conversation between a ' \
'customer and the support agent, along with a list of potential labels. ' \
'Your task is to analyze the conversation and select the two labels from the given list that most accurately ' \
'represent the themes or issues discussed. Ensure you preserve the exact casing of the labels as they are provided in the list. ' \
'Do not create new labels; only choose from those provided. Once you have made your selections, please provide your response ' \
'as a comma-separated list of the provided labels. Remember, your response should only contain the labels you\'ve selected, ' \
'in their original casing, and nothing else. '
},
{ role: 'user', content: content }
]
}.to_json
end
end

View File

@@ -0,0 +1,81 @@
require 'rails_helper'
RSpec.describe Integrations::Openai::ProcessorService do
subject { described_class.new(hook: hook, event: event) }
let(:account) { create(:account) }
let(:hook) { create(:integrations_hook, :openai, account: account) }
let(:expected_headers) { { 'Authorization' => "Bearer #{hook.settings['api_key']}" } }
let(:openai_response) do
{
'choices' => [
{
'message' => {
'content' => 'This is a reply from openai.'
}
}
]
}.to_json
end
let!(:conversation) { create(:conversation, account: account) }
let!(:customer_message) { create(:message, account: account, conversation: conversation, message_type: :incoming, content: 'hello agent') }
let!(:agent_message) { create(:message, account: account, conversation: conversation, message_type: :outgoing, content: 'hello customer') }
describe '#perform' do
context 'when event name is label_suggestion with labels' do
let(:event) { { 'name' => 'label_suggestion', 'data' => { 'conversation_display_id' => conversation.display_id } } }
let(:label1) { create(:label, account: account) }
let(:label2) { create(:label, account: account) }
let(:label_suggestion_payload) do
labels = "#{label1.title}, #{label2.title}"
messages =
"Customer #{customer_message.sender.name} : #{customer_message.content}\nAgent #{agent_message.sender.name} : #{agent_message.content}"
"Messages:\n#{messages}\n\nLabels:\n#{labels}"
end
it 'returns the label suggestions' do
request_body = {
'model' => 'gpt-3.5-turbo',
'messages' => [
{
role: 'system',
content: 'Your role is as an assistant to a customer support agent. You will be provided with ' \
'a transcript of a conversation between a customer and the support agent, along with a list of potential labels. ' \
'Your task is to analyze the conversation and select the two labels from the given list that most accurately ' \
'represent the themes or issues discussed. Ensure you preserve the exact casing of the labels as they are provided ' \
'in the list. Do not create new labels; only choose from those provided. Once you have made your selections, ' \
'please provide your response as a comma-separated list of the provided labels. Remember, your response should only contain ' \
'the labels you\'ve selected, in their original casing, and nothing else. '
},
{ role: 'user', content: label_suggestion_payload }
]
}.to_json
stub_request(:post, 'https://api.openai.com/v1/chat/completions')
.with(body: request_body, headers: expected_headers)
.to_return(status: 200, body: openai_response, headers: {})
result = subject.perform
expect(result).to eq('This is a reply from openai.')
end
end
context 'when event name is label_suggestion with no labels' do
let(:event) { { 'name' => 'label_suggestion', 'data' => { 'conversation_display_id' => conversation.display_id } } }
it 'returns nil' do
result = subject.perform
expect(result).to be_nil
end
end
context 'when event name is not one that can be processed' do
let(:event) { { 'name' => 'unknown', 'data' => {} } }
it 'returns nil' do
expect(subject.perform).to be_nil
end
end
end
end

View File

@@ -1,28 +1,18 @@
class Integrations::Openai::ProcessorService
# 3.5 support 4,096 tokens
# 1 token is approx 4 characters
# 4,096 * 4 = 16,384 characters, sticking to 15,000 to be safe
TOKEN_LIMIT = 15_000
API_URL = 'https://api.openai.com/v1/chat/completions'.freeze
GPT_MODEL = 'gpt-3.5-turbo'.freeze
class Integrations::Openai::ProcessorService < Integrations::OpenaiBaseService
def reply_suggestion_message
make_api_call(reply_suggestion_body)
end
ALLOWED_EVENT_NAMES = %w[rephrase summarize reply_suggestion].freeze
def summarize_message
make_api_call(summarize_body)
end
pattr_initialize [:hook!, :event!]
def perform
event_name = event['name']
return nil unless valid_event_name?(event_name)
send("#{event_name}_message")
def rephrase_message
make_api_call(rephrase_body)
end
private
def valid_event_name?(event_name)
ALLOWED_EVENT_NAMES.include?(event_name)
end
def rephrase_body
{
model: GPT_MODEL,
@@ -42,12 +32,8 @@ class Integrations::Openai::ProcessorService
add_messages_until_token_limit(conversation, messages, in_array_format)
end
def find_conversation
hook.account.conversations.find_by(display_id: event['data']['conversation_display_id'])
end
def add_messages_until_token_limit(conversation, messages, in_array_format)
character_count = 0
def add_messages_until_token_limit(conversation, messages, in_array_format, start_from = 0)
character_count = start_from
conversation.messages.chat.reorder('id desc').each do |message|
character_count, message_added = add_message_if_within_limit(character_count, message, messages, in_array_format)
break unless message_added
@@ -112,26 +98,6 @@ class Integrations::Openai::ProcessorService
].concat(conversation_messages(in_array_format: true))
}.to_json
end
def reply_suggestion_message
make_api_call(reply_suggestion_body)
end
def summarize_message
make_api_call(summarize_body)
end
def rephrase_message
make_api_call(rephrase_body)
end
def make_api_call(body)
headers = {
'Content-Type' => 'application/json',
'Authorization' => "Bearer #{hook.settings['api_key']}"
}
response = HTTParty.post(API_URL, headers: headers, body: body)
JSON.parse(response.body)['choices'].first['message']['content']
end
end
Integrations::Openai::ProcessorService.prepend_mod_with('Integrations::OpenaiProcessorService')

View File

@@ -0,0 +1,80 @@
class Integrations::OpenaiBaseService
# 3.5 support 4,096 tokens
# 1 token is approx 4 characters
# 4,096 * 4 = 16,384 characters, sticking to 15,000 to be safe
TOKEN_LIMIT = 15_000
API_URL = 'https://api.openai.com/v1/chat/completions'.freeze
GPT_MODEL = 'gpt-3.5-turbo'.freeze
ALLOWED_EVENT_NAMES = %w[rephrase summarize reply_suggestion].freeze
CACHEABLE_EVENTS = %w[].freeze
pattr_initialize [:hook!, :event!]
def perform
return nil unless valid_event_name?
return value_from_cache if value_from_cache.present?
response = send("#{event_name}_message")
save_to_cache(response) if response.present?
response
end
private
def event_name
event['name']
end
def cache_key
return nil unless event_is_cacheable?
conversation = find_conversation
return nil unless conversation
# since the value from cache depends on the conversation last_activity_at, it will always be fresh
format(::Redis::Alfred::OPENAI_CONVERSATION_KEY, event_name: event_name, conversation_id: conversation.id,
updated_at: conversation.last_activity_at.to_i)
end
def value_from_cache
return nil unless event_is_cacheable?
return nil if cache_key.blank?
Redis::Alfred.get(cache_key)
end
def save_to_cache(response)
return nil unless event_is_cacheable?
Redis::Alfred.setex(cache_key, response)
end
def find_conversation
hook.account.conversations.find_by(display_id: event['data']['conversation_display_id'])
end
def valid_event_name?
# self.class::ALLOWED_EVENT_NAMES is way to access ALLOWED_EVENT_NAMES defined in the class hierarchy of the current object.
# This ensures that if ALLOWED_EVENT_NAMES is updated elsewhere in it's ancestors, we access the latest value.
self.class::ALLOWED_EVENT_NAMES.include?(event_name)
end
def event_is_cacheable?
# self.class::CACHEABLE_EVENTS is way to access CACHEABLE_EVENTS defined in the class hierarchy of the current object.
# This ensures that if CACHEABLE_EVENTS is updated elsewhere in it's ancestors, we access the latest value.
self.class::CACHEABLE_EVENTS.include?(event_name)
end
def make_api_call(body)
headers = {
'Content-Type' => 'application/json',
'Authorization' => "Bearer #{hook.settings['api_key']}"
}
response = HTTParty.post(API_URL, headers: headers, body: body)
JSON.parse(response.body)['choices'].first['message']['content']
end
end

View File

@@ -32,4 +32,5 @@ module Redis::RedisKeys
# Check if a message create with same source-id is in progress?
MESSAGE_SOURCE_KEY = 'MESSAGE_SOURCE_KEY::%<id>s'.freeze
CUSTOM_FILTER_RECORDS_COUNT_KEY = 'CUSTOM_FILTER::%<account_id>d::%<user_id>d::%<filter_id>d'.freeze
OPENAI_CONVERSATION_KEY = 'OPEN_AI_CONVERSATION_KEY::%<event_name>s::%<conversation_id>d::%<updated_at>d'.freeze
end

View File

@@ -97,13 +97,5 @@ RSpec.describe Integrations::Openai::ProcessorService do
expect(result).to eq('This is a reply from openai.')
end
end
context 'when event name is not one that can be processed' do
let(:event) { { 'name' => 'unknown', 'data' => {} } }
it 'returns nil' do
expect(subject.perform).to be_nil
end
end
end
end