Method: LlmMemory::RedisStore#add

Defined in:
lib/llm_memory/stores/redis_store.rb

#add(data: []) ⇒ Object

data = [{ content: “”, content_vector: [], metadata: {} }]



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/llm_memory/stores/redis_store.rb', line 78

def add(data: [])
  result = {}
  @client.pipelined do |pipeline|
    data.each_with_index do |d, i|
      key = @index_name # index_name:create_time:metadata_timestamp:uuid
      timestamp = d.dig(:metadata, :timestamp)
      key += ":#{Time.now.strftime("%Y%m%d%H%M%S")}"
      key += ":#{timestamp}"
      key += ":#{SecureRandom.hex(8)}"

      meta_json = d[:metadata].nil? ? "" : d[:metadata].to_json # serialize
      vector_value = d[:vector].map(&:to_f).pack("f*")
      pipeline.hset(
        key,
        {
          @content_key => d[:content],
          @vector_key => vector_value,
           => meta_json
        }
      )
      result[key] = d[:content]
    end
  end
  result
# data.each_with_index do |d, i|
#   key = "#{@index_name}:#{i}"
#   vector_value = d[:content_vector].map(&:to_f).pack("f*")
#   pp vector_value
#   @client.hset(
#     key,
#     {
#       @content_key => d[:content],
#       @vector_key => vector_value,
#       @metadata_key => ""
#     }
#   )
# end
# rescue Redis::Pipeline::Error => e
#   # Handle the error if there is any issue with the pipeline execution
#   puts "Pipeline Error: #{e.message}"
# rescue Redis::BaseConnectionError => e
#   # Handle connection errors
#   puts "Connection Error: #{e.message}"
rescue => e
  # Handle any other errors
  puts "Unexpected Error: #{e.message}"
end