Compare commits

...
Author SHA1 Message Date
Muhsin KelothandGitHub be2d8e52cb Merge branch 'develop' into feat/add-more-ai-reply-options 2023-07-10 14:46:53 +05:30
Muhsin KelothandGitHub e819f7ee92 Merge branch 'develop' into feat/add-more-ai-reply-options 2023-07-10 14:36:18 +05:30
Muhsin KelothandGitHub 72022c263e Merge branch 'develop' into feat/add-more-ai-reply-options 2023-07-09 21:47:03 +05:30
Muhsin KelothandGitHub 6b2171f6aa Merge branch 'develop' into feat/add-more-ai-reply-options 2023-07-06 12:08:15 +05:30
Muhsin Keloth b698a332e0 feat: add more ai reply options 2023-07-06 12:05:40 +05:30
Shivam Mishra 92aecb1262 chore: remove extra button 2023-07-05 14:26:33 +05:30
Shivam Mishra be028dbedc refactor: remove label messages 2023-07-05 14:13:19 +05:30
Shivam MishraandGitHub acf19bc70d Merge branch 'develop' into feature/cw-2159 2023-07-05 14:10:22 +05:30
Shivam Mishra e8d0f14554 feat: move label suggestions to enterprise 2023-07-05 14:07:47 +05:30
Shivam Mishra 587db31cdd test: nil condition if labels are missing 2023-07-03 16:06:43 +05:30
Shivam Mishra 8f0b4e1b58 feat: add test for label suggestions 2023-07-03 15:58:20 +05:30
Shivam Mishra c5dd246071 refactor: simplify conditions and add guard to cache methods 2023-07-03 15:31:38 +05:30
Shivam Mishra 4cdf28860a Merge branch 'feature/cw-2159' of github.com:chatwoot/chatwoot into feature/cw-2159 2023-07-03 15:05:38 +05:30
Muhsin KelothandGitHub e3aef4a778 Merge branch 'develop' into feature/cw-2159 2023-07-03 11:45:27 +05:30
Shivam Mishra 870e3716d9 refactor: rename suggest_label to label_suggestion 2023-06-30 14:00:39 +05:30
Shivam Mishra e62f74bb80 feat: update key format 2023-06-30 12:31:01 +05:30
Shivam Mishra 9d1ee2d506 refactor: set response in cache only for a day 2023-06-30 12:28:10 +05:30
Shivam Mishra a622041696 feat: save to cache only if response is present 2023-06-30 12:26:53 +05:30
Shivam Mishra 18245dd757 Merge branch 'develop' of github.com:chatwoot/chatwoot into feature/cw-2159 2023-06-30 12:25:43 +05:30
Shivam Mishra 0ddf241d7f feat: update comments 2023-06-29 13:36:13 +05:30
Shivam Mishra 99dc6a0eb3 feat: add guards to ensure message and labels are always present 2023-06-29 13:26:02 +05:30
Shivam Mishra 3be125ea66 feat: update openapi.js 2023-06-29 13:12:26 +05:30
Shivam Mishra 5af2f541a1 feat: allow caching of response 2023-06-29 13:12:15 +05:30
Shivam Mishra 908db4df5e feat: add label suggest prompt 2023-06-29 12:47:57 +05:30
3 changed files with 160 additions and 1 deletions
@@ -1,5 +1,6 @@
module Enterprise::Integrations::OpenaiProcessorService
ALLOWED_EVENT_NAMES = %w[rephrase summarize reply_suggestion label_suggestion].freeze
ALLOWED_EVENT_NAMES = %w[rephrase summarize reply_suggestion fix_spelling_grammar shorten expand make_friendly make_formal simplify
label_suggestion].freeze
CACHEABLE_EVENTS = %w[label_suggestion].freeze
def label_suggestion_message
@@ -0,0 +1,80 @@
class Integrations::OpenaiProcessorService
# 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 fix_spelling_grammar shorten expand make_friendly make_formal simplify].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
@@ -97,5 +97,83 @@ RSpec.describe Integrations::Openai::ProcessorService do
expect(result).to eq('This is a reply from openai.')
end
end
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
context 'when event name is fix_spelling_grammar' do
let(:event) { { 'name' => 'fix_spelling_grammar', 'data' => { 'content' => 'This is a test' } } }
it 'returns the corrected text' do
request_body = {
'model' => 'gpt-3.5-turbo',
'messages' => [
{ 'role' => 'system', 'content' => 'You are a helpful support agent. Please fix the spelling and grammar of the following response. ' \
'Reply in the user\'s language.' },
{ 'role' => 'user', 'content' => event['data']['content'] }
]
}.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
end
end