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

@@ -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