Class: Llmemory::LongTerm::GraphBased::Memory
- Inherits:
-
Object
- Object
- Llmemory::LongTerm::GraphBased::Memory
- Includes:
- MemoryModule
- Defined in:
- lib/llmemory/long_term/graph_based/memory.rb
Instance Attribute Summary collapse
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
-
#forget(ids:, reason: nil, mode: :soft) ⇒ Object
Forgets relations by archiving the edges identified by the candidate ids returned from #read/#search_candidates (edge ids), recording the removal in the audit log.
-
#initialize(user_id:, storage: nil, vector_store: nil, llm: nil, extractor: nil, cipher: nil) ⇒ Memory
constructor
A new instance of Memory.
- #list(user_id: nil, limit: nil, offset: nil) ⇒ Object
- #memorize(conversation_text) ⇒ Object
-
#remember_fact(content:, category: nil, importance: nil, provenance: nil) ⇒ Object
Stores a fact produced outside the conversational flow (e.g. a reflection insight) by extracting entities/relations from
contentand adding them to the graph, preserving caller-supplied provenance. - #retrieve(query, top_k: 10) ⇒ Object
- #search_candidates(query, user_id: nil, top_k: 20) ⇒ Object
- #stats(user_id: nil) ⇒ Object
- #storage ⇒ Object
-
#write(payload, **_meta) ⇒ Object
--- MemoryModule uniform interface ---.
Methods included from MemoryModule
Constructor Details
#initialize(user_id:, storage: nil, vector_store: nil, llm: nil, extractor: nil, cipher: nil) ⇒ Memory
Returns a new instance of Memory.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/llmemory/long_term/graph_based/memory.rb', line 17 def initialize(user_id:, storage: nil, vector_store: nil, llm: nil, extractor: nil, cipher: nil) @user_id = user_id @cipher = cipher || Llmemory.build_cipher @graph_storage = storage || Storages.build(cipher: @cipher) @kg = KnowledgeGraph.new(user_id: user_id, storage: @graph_storage) @conflict_resolver = ConflictResolver.new(@kg) @vector_store = vector_store || build_vector_store @llm = llm || Llmemory::LLM.client @extractor = extractor || Llmemory::Extractors::EntityRelationExtractor.new(llm: @llm) end |
Instance Attribute Details
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
60 61 62 |
# File 'lib/llmemory/long_term/graph_based/memory.rb', line 60 def user_id @user_id end |
Instance Method Details
#forget(ids:, reason: nil, mode: :soft) ⇒ Object
Forgets relations by archiving the edges identified by the candidate ids returned from #read/#search_candidates (edge ids), recording the removal in the audit log. Edges are soft-archived (archived_at) so they no longer appear in retrieval; nodes are left in place (a node may still be referenced by other active edges). Returns the number archived.
103 104 105 106 107 108 109 110 111 |
# File 'lib/llmemory/long_term/graph_based/memory.rb', line 103 def forget(ids:, reason: nil, mode: :soft) # `:hard` would physically delete edge rows; not yet wired (the graph # store only exposes soft archive_edge). Both modes route to archive # for now; behavior is the same — kept for API uniformity. archived = Array(ids).map(&:to_s).select { |edge_id| @kg.archive_edge(edge_id) } forget_log.record(@user_id, memory_type: "graph_based", ids: archived, reason: reason) Llmemory::Instrumentation.instrument(:memory_forget, memory_type: "graph_based", user_id: @user_id, count: archived.size, mode: mode) archived.size end |
#list(user_id: nil, limit: nil, offset: nil) ⇒ Object
89 90 91 |
# File 'lib/llmemory/long_term/graph_based/memory.rb', line 89 def list(user_id: nil, limit: nil, offset: nil) @graph_storage.list_nodes(user_id || @user_id, limit: limit, offset: offset) end |
#memorize(conversation_text) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/llmemory/long_term/graph_based/memory.rb', line 28 def memorize(conversation_text) text = Llmemory.configuration.noise_filter_enabled ? NoiseFilter.filter?(conversation_text) : conversation_text.to_s return true if text.strip.empty? entities, relations = extract_graph(text) return true if entities.empty? && relations.empty? provenance = Llmemory::Provenance.from_text_fingerprint(text, method: "entity_relation_extraction") ingest(entities, relations, provenance) true end |
#remember_fact(content:, category: nil, importance: nil, provenance: nil) ⇒ Object
Stores a fact produced outside the conversational flow (e.g. a
reflection insight) by extracting entities/relations from content and
adding them to the graph, preserving caller-supplied provenance. Lets
the Reflector target graph-based semantic memory.
70 71 72 73 74 75 76 77 |
# File 'lib/llmemory/long_term/graph_based/memory.rb', line 70 def remember_fact(content:, category: nil, importance: nil, provenance: nil) return nil if content.to_s.strip.empty? entities, relations = extract_graph(content) return nil if entities.empty? && relations.empty? prov = provenance || Llmemory::Provenance.from_text_fingerprint(content, method: "reflection") ingest(entities, relations, prov) true end |
#retrieve(query, top_k: 10) ⇒ Object
40 41 42 43 |
# File 'lib/llmemory/long_term/graph_based/memory.rb', line 40 def retrieve(query, top_k: 10) results = hybrid_search(query, top_k: top_k) format_as_context(results) end |
#search_candidates(query, user_id: nil, top_k: 20) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/llmemory/long_term/graph_based/memory.rb', line 45 def search_candidates(query, user_id: nil, top_k: 20) uid = user_id || @user_id return [] unless uid == @user_id results = hybrid_search(query, top_k: top_k) results.map do |r| { id: r[:id], text: r[:text], timestamp: r[:created_at] || r[:timestamp], score: r[:score] || 1.0, importance: r[:importance] } end end |
#stats(user_id: nil) ⇒ Object
93 94 95 96 |
# File 'lib/llmemory/long_term/graph_based/memory.rb', line 93 def stats(user_id: nil) uid = user_id || @user_id { nodes: @graph_storage.count_nodes(uid), edges: @graph_storage.count_edges(uid) } end |
#storage ⇒ Object
62 63 64 |
# File 'lib/llmemory/long_term/graph_based/memory.rb', line 62 def storage @graph_storage end |
#write(payload, **_meta) ⇒ Object
--- MemoryModule uniform interface ---
81 82 83 84 85 86 87 |
# File 'lib/llmemory/long_term/graph_based/memory.rb', line 81 def write(payload, **) result = nil Llmemory::Instrumentation.instrument(:memory_write, memory_type: "graph_based", user_id: @user_id) do result = memorize(payload) end result end |