feat: Allow label suggestions with OpenAI APIs (#7428)
This commit is contained in:
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user