Class: Llmemory::LongTerm::FileBased::Memory
- Inherits:
-
Object
- Object
- Llmemory::LongTerm::FileBased::Memory
- Includes:
- MemoryModule
- Defined in:
- lib/llmemory/long_term/file_based/memory.rb
Instance Attribute Summary collapse
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
-
#forget(ids:, reason: nil) ⇒ Object
Removes items/resources by id and records the removal in the audit log.
-
#initialize(user_id:, storage: nil, llm: nil, extractor: nil) ⇒ Memory
constructor
A new instance of Memory.
- #list(user_id: nil, limit: nil) ⇒ Object
- #memorize(conversation_text) ⇒ Object
-
#remember_fact(content:, category: "general", importance: 0.6, provenance: nil) ⇒ Object
Stores a single fact produced outside the extraction flow (e.g. by reflection over episodes), preserving caller-supplied provenance so the insight remains traceable to its source.
- #retrieve(query) ⇒ Object
- #search_candidates(query, user_id: nil, top_k: 20) ⇒ Object
- #stats(user_id: nil) ⇒ Object
-
#write(payload, **_meta) ⇒ Object
— MemoryModule uniform interface —.
Methods included from MemoryModule
Constructor Details
#initialize(user_id:, storage: nil, llm: nil, extractor: nil) ⇒ Memory
Returns a new instance of Memory.
16 17 18 19 20 21 |
# File 'lib/llmemory/long_term/file_based/memory.rb', line 16 def initialize(user_id:, storage: nil, llm: nil, extractor: nil) @user_id = user_id @storage = storage || Storages.build @llm = llm || Llmemory::LLM.client @extractor = extractor || Llmemory::Extractors::FactExtractor.new(llm: @llm) end |
Instance Attribute Details
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
134 135 136 |
# File 'lib/llmemory/long_term/file_based/memory.rb', line 134 def storage @storage end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
134 135 136 |
# File 'lib/llmemory/long_term/file_based/memory.rb', line 134 def user_id @user_id end |
Instance Method Details
#forget(ids:, reason: nil) ⇒ Object
Removes items/resources by id and records the removal in the audit log.
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/llmemory/long_term/file_based/memory.rb', line 123 def forget(ids:, reason: nil) requested = Array(ids).map(&:to_s) existing = (@storage.get_all_items(@user_id) + @storage.get_all_resources(@user_id)) .map { |r| (r[:id] || r["id"]).to_s } removed = requested & existing @storage.archive_items(@user_id, removed) @storage.archive_resources(@user_id, removed) forget_log.record(@user_id, memory_type: "file_based", ids: removed, reason: reason) removed.size end |
#list(user_id: nil, limit: nil) ⇒ Object
114 115 116 |
# File 'lib/llmemory/long_term/file_based/memory.rb', line 114 def list(user_id: nil, limit: nil) @storage.list_items(user_id: user_id || @user_id, limit: limit) end |
#memorize(conversation_text) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/llmemory/long_term/file_based/memory.rb', line 23 def memorize(conversation_text) text = Llmemory.configuration.noise_filter_enabled ? NoiseFilter.filter?(conversation_text) : conversation_text.to_s return true if text.strip.empty? resource_id = save_resource(text) append_to_daily_log(text) if Llmemory.configuration.daily_logs_enabled && @storage.respond_to?(:save_daily_log_entry) items = @extractor.extract_items(text) updates_by_category = {} items.each do |item| content = item.is_a?(Hash) ? (item["content"] || item[:content]) : item.to_s importance = (item["importance"] || item[:importance] || 0.7).to_f cat = @extractor.classify_item(content) updates_by_category[cat] ||= [] updates_by_category[cat] << content.to_s save_item(category: cat, item: item, source_resource_id: resource_id, importance: importance) end updates_by_category.each do |category, new_memories| existing_summary = @storage.load_category(@user_id, category) updated_summary = @extractor.evolve_summary(existing: existing_summary, new_memories: new_memories) @storage.save_category(@user_id, category, updated_summary) end true end |
#remember_fact(content:, category: "general", importance: 0.6, provenance: nil) ⇒ Object
Stores a single fact produced outside the extraction flow (e.g. by reflection over episodes), preserving caller-supplied provenance so the insight remains traceable to its source. Returns the item id.
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/llmemory/long_term/file_based/memory.rb', line 96 def remember_fact(content:, category: "general", importance: 0.6, provenance: nil) return nil if content.to_s.strip.empty? @storage.save_item( @user_id, category: category.to_s, content: content.to_s, source_resource_id: nil, importance: importance, provenance: provenance ) end |
#retrieve(query) ⇒ Object
50 51 52 53 |
# File 'lib/llmemory/long_term/file_based/memory.rb', line 50 def retrieve(query) retrieval = Retrieval.new(user_id: @user_id, storage: @storage, llm: @llm) retrieval.retrieve(query) end |
#search_candidates(query, user_id: nil, top_k: 20) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/llmemory/long_term/file_based/memory.rb', line 55 def search_candidates(query, user_id: nil, top_k: 20) uid = user_id || @user_id items = @storage.search_items(uid, query) resources = @storage.search_resources(uid, query) daily_logs = load_daily_logs_for_retrieval(uid) if Llmemory.configuration.daily_logs_enabled && @storage.respond_to?(:load_daily_logs) category_summaries = load_category_summaries_as_candidates(uid, query) out = [] category_summaries.each do |c| out << c.merge(evergreen: true) end items.first(top_k).each do |i| out << { id: i[:id] || i["id"], text: i[:content] || i["content"], timestamp: i[:created_at] || i["created_at"], score: 1.0, importance: (i[:importance] || i["importance"] || 1.0).to_f, evergreen: i[:evergreen] || i["evergreen"] } end resources.first([top_k - out.size, 0].max).each do |r| out << { id: r[:id] || r["id"], text: r[:text] || r["text"], timestamp: r[:created_at] || r["created_at"], score: 0.9 } end if daily_logs daily_logs.each do |log| out << { text: log[:content], timestamp: log[:date].to_time, score: 0.85 } end end out end |
#stats(user_id: nil) ⇒ Object
118 119 120 |
# File 'lib/llmemory/long_term/file_based/memory.rb', line 118 def stats(user_id: nil) { items: @storage.count_items(user_id: user_id || @user_id) } end |
#write(payload, **_meta) ⇒ Object
— MemoryModule uniform interface —
110 111 112 |
# File 'lib/llmemory/long_term/file_based/memory.rb', line 110 def write(payload, **) memorize(payload) end |