feat: allow caching of response

This commit is contained in:
Shivam Mishra
2023-06-29 13:12:15 +05:30
parent 908db4df5e
commit 5af2f541a1
2 changed files with 27 additions and 1 deletions
+26 -1
View File
@@ -4,6 +4,7 @@ class Integrations::OpenaiProcessorService
GPT_MODEL = 'gpt-3.5-turbo'.freeze
ALLOWED_EVENT_NAMES = %w[rephrase summarize reply_suggestion suggest_label].freeze
CACHEABLE_EVENTS = %w[suggest_label].freeze
pattr_initialize [:hook!, :event!]
@@ -11,11 +12,35 @@ class Integrations::OpenaiProcessorService
event_name = event['name']
return nil unless valid_event_name?(event_name)
send("#{event_name}_message")
return value_from_cache if CACHEABLE_EVENTS.include?(event_name) && value_from_cache.present?
response = send("#{event_name}_message")
save_to_cache(response) if CACHEABLE_EVENTS.include?(event_name)
response
end
private
def cache_key
conversation = find_conversation
return nil unless conversation
format(::Redis::Alfred::OPENAI_CONVERSATION_KEY, event_name: event['name'], conversation_id: conversation.id,
updated_at: conversation.updated_at.to_i)
end
def value_from_cache
# since the value from cache depends on the conversation last_updated_at, it will always be fresh
return nil if cache_key.blank?
Redis::Alfred.get(cache_key)
end
def save_to_cache(response)
Redis::Alfred.set(cache_key, response)
end
def find_conversation
hook.account.conversations.find_by(display_id: event['data']['conversation_display_id'])
end
+1
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