Skip to content

Commit

Permalink
Merge pull request #3 from shohey1226/add_timestamp
Browse files Browse the repository at this point in the history
added timestamp into key
  • Loading branch information
shohey1226 committed Jul 27, 2023
2 parents 037b87c + 80f4c09 commit 960acfc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/llm_memory/loaders/file_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ def load(directory_path)

file_name = File.basename(file_path)
file_content = File.read(file_path)
ctime = File.ctime(file_path)

files_array << {
content: file_content,
metadata: {
file_name: file_name
file_name: file_name,
timestamp: ctime.strftime("%Y%m%d%H%M%S") # YYMMDDHHmmss
}
}
end
Expand Down
7 changes: 6 additions & 1 deletion lib/llm_memory/stores/redis_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ def add(data: [])
result = {}
@client.pipelined do |pipeline|
data.each_with_index do |d, i|
key = "#{@index_name}:#{SecureRandom.uuid.delete("-")}"
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(
Expand Down
8 changes: 5 additions & 3 deletions spec/llm_memory/hippocampus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@
describe "memorize", :vcr do
it "should add docs to redis", :vcr do
hippocampus = LlmMemory::Hippocampus.new
timestamp = "20201231235959"
docs = [
{content: "Hello, I'm Shohei.", metadata: {info: "name"}},
{content: "I'm working as a sotware developer", metadata: {info: "profession"}},
{content: "I like music", metadata: {info: "hobby"}}
{content: "Hello, I'm Shohei.", metadata: {info: "name", timestamp: timestamp}},
{content: "I'm working as a sotware developer", metadata: {info: "profession", timestamp: timestamp}},
{content: "I like music", metadata: {info: "hobby", timestamp: timestamp}}
]
res = hippocampus.memorize(docs)
expect(res.keys.map { |k| k.split(":")[2] }.uniq.first).to eq timestamp
expect(res.values.first).to eq("Hello, I'm Shohei.")
end
end
Expand Down
12 changes: 11 additions & 1 deletion spec/llm_memory/wernicke_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

file_content1 = "This is file1 content."
file_content2 = "This is file2 content."
now = Time.now
timestamp = now.strftime("%Y%m%d%H%M%S")

# Stub Find.find and File.directory? methods
allow(Find).to receive(:find).with(directory_path).and_yield(file_path1).and_yield(file_path2)
Expand All @@ -19,8 +21,16 @@
allow(File).to receive(:read).with(file_path1).and_return(file_content1)
allow(File).to receive(:read).with(file_path2).and_return(file_content2)

allow(File).to receive(:ctime).with(file_path1).and_return(now)
allow(File).to receive(:ctime).with(file_path2).and_return(now)

docs = LlmMemory::Wernicke.load(:file, directory_path)
expect(docs).to eq([{content: "This is file1 content.", metadata: {file_name: "file1.txt"}}, {content: "This is file2 content.", metadata: {file_name: "file2.txt"}}])
expect(docs).to eq(
[
{content: "This is file1 content.", metadata: {file_name: "file1.txt", timestamp: timestamp}},
{content: "This is file2 content.", metadata: {file_name: "file2.txt", timestamp: timestamp}}
]
)
end
end
end

0 comments on commit 960acfc

Please sign in to comment.