diff --git a/lib/integrations/openai_base_service.rb b/lib/integrations/openai_base_service.rb index 056ec998a..f38f4b5a6 100644 --- a/lib/integrations/openai_base_service.rb +++ b/lib/integrations/openai_base_service.rb @@ -42,12 +42,20 @@ class Integrations::OpenaiBaseService return nil unless event_is_cacheable? return nil if cache_key.blank? - Redis::Alfred.get(cache_key) + value_from_cache = Redis::Alfred.get(cache_key) + return nil if value_from_cache.blank? + + JSON.parse(value_from_cache, symbolize_names: true) + rescue JSON::ParserError + nil end def save_to_cache(response) return nil unless event_is_cacheable? + # if the response is a hash, save it as a json + # This allows the fetching to be simplified + response = response.to_json if response.is_a?(Hash) Redis::Alfred.setex(cache_key, response) end diff --git a/lib/redis/redis_keys.rb b/lib/redis/redis_keys.rb index 396d7e50b..f8c467c9a 100644 --- a/lib/redis/redis_keys.rb +++ b/lib/redis/redis_keys.rb @@ -33,7 +33,7 @@ module Redis::RedisKeys LATEST_CHATWOOT_VERSION = 'LATEST_CHATWOOT_VERSION'.freeze # Check if a message create with same source-id is in progress? MESSAGE_SOURCE_KEY = 'MESSAGE_SOURCE_KEY::%s'.freeze - OPENAI_CONVERSATION_KEY = 'OPEN_AI_CONVERSATION_KEY::v1::%s::%d::%d'.freeze + OPENAI_CONVERSATION_KEY = 'OPEN_AI_CONVERSATION_KEY::V1::%s::%d::%d'.freeze ## Sempahores / Locks # We don't want to process messages from the same sender concurrently to prevent creating double conversations