feat: Reply suggestion and summary generation using OpenAI (#7029)

Add `reply_suggestions` and `summary_generation` options for OpenAI integration

Fixes: https://linear.app/chatwoot/issue/CW-1595/frontend-for-generating-conversation-summary

Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com>
Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
Muhsin Keloth
2023-05-10 17:50:37 +05:30
committed by GitHub
parent 55e5fa2205
commit 86384bf876
10 changed files with 169 additions and 68 deletions

View File

@@ -35,20 +35,48 @@ class Integrations::Openai::ProcessorService
end
def conversation_messages(in_array_format: false)
conversation = hook.account.conversations.find_by(display_id: event['data']['conversation_display_id'])
messages = in_array_format ? [] : ''
conversation = find_conversation
messages = init_messages_body(in_array_format)
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
conversation.messages.chat.reorder('id desc').each do |message|
character_count += message.content.length
break if character_count > TOKEN_LIMIT
formatted_message = format_message(message, in_array_format)
messages.prepend(formatted_message)
character_count, message_added = add_message_if_within_limit(character_count, message, messages, in_array_format)
break unless message_added
end
messages
end
def add_message_if_within_limit(character_count, message, messages, in_array_format)
if valid_message?(message, character_count)
add_message_to_list(message, messages, in_array_format)
character_count += message.content.length
[character_count, true]
else
[character_count, false]
end
end
def valid_message?(message, character_count)
message.content.present? && character_count + message.content.length <= TOKEN_LIMIT
end
def add_message_to_list(message, messages, in_array_format)
formatted_message = format_message(message, in_array_format)
messages.prepend(formatted_message)
end
def init_messages_body(in_array_format)
in_array_format ? [] : ''
end
def format_message(message, in_array_format)
in_array_format ? format_message_in_array(message) : format_message_in_string(message)
end