Class: Llmemory::LongTerm::GraphBased::KnowledgeGraph
- Inherits:
-
Object
- Object
- Llmemory::LongTerm::GraphBased::KnowledgeGraph
- Defined in:
- lib/llmemory/long_term/graph_based/knowledge_graph.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
- #add_edge(subject:, predicate:, object:, properties: {}) ⇒ Object
- #add_node(entity_type:, name:, properties: {}) ⇒ Object
- #archive_edge(edge_id, reason: nil) ⇒ Object
- #find_edges(subject: nil, predicate: nil, object: nil, include_archived: false) ⇒ Object
- #find_node(name: nil, id: nil) ⇒ Object
- #find_node_by_id(id) ⇒ Object
-
#initialize(user_id:, storage: nil) ⇒ KnowledgeGraph
constructor
A new instance of KnowledgeGraph.
- #list_nodes ⇒ Object
- #traverse(start_node:, depth: 2) ⇒ Object
Constructor Details
#initialize(user_id:, storage: nil) ⇒ KnowledgeGraph
Returns a new instance of KnowledgeGraph.
12 13 14 15 |
# File 'lib/llmemory/long_term/graph_based/knowledge_graph.rb', line 12 def initialize(user_id:, storage: nil) @user_id = user_id @storage = storage || Storages::MemoryStorage.new end |
Instance Attribute Details
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
110 111 112 |
# File 'lib/llmemory/long_term/graph_based/knowledge_graph.rb', line 110 def storage @storage end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
110 111 112 |
# File 'lib/llmemory/long_term/graph_based/knowledge_graph.rb', line 110 def user_id @user_id end |
Instance Method Details
#add_edge(subject:, predicate:, object:, properties: {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/llmemory/long_term/graph_based/knowledge_graph.rb', line 44 def add_edge(subject:, predicate:, object:, properties: {}) subject_id = subject.is_a?(Node) ? subject.id : subject.to_s object_id = object.is_a?(Node) ? object.id : object.to_s edge = Edge.new( id: nil, user_id: @user_id, subject_id: subject_id, predicate: predicate.to_s, target_id: object_id, properties: properties, created_at: Time.now, archived_at: nil ) @storage.save_edge(@user_id, edge) end |
#add_node(entity_type:, name:, properties: {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/llmemory/long_term/graph_based/knowledge_graph.rb', line 17 def add_node(entity_type:, name:, properties: {}) existing = @storage.find_node_by_name(@user_id, entity_type, name) return existing.id if existing node = Node.new( id: nil, user_id: @user_id, entity_type: entity_type.to_s, name: name.to_s, properties: properties, created_at: Time.now, updated_at: Time.now ) @storage.save_node(@user_id, node) end |
#archive_edge(edge_id, reason: nil) ⇒ Object
102 103 104 |
# File 'lib/llmemory/long_term/graph_based/knowledge_graph.rb', line 102 def archive_edge(edge_id, reason: nil) @storage.archive_edge(@user_id, edge_id, archived_at: Time.now) end |
#find_edges(subject: nil, predicate: nil, object: nil, include_archived: false) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/llmemory/long_term/graph_based/knowledge_graph.rb', line 60 def find_edges(subject: nil, predicate: nil, object: nil, include_archived: false) subject_id = subject.is_a?(Node) ? subject.id : subject object_id = object.is_a?(Node) ? object.id : object @storage.find_edges( @user_id, subject_id: subject_id, predicate: predicate&.to_s, object_id: object_id, include_archived: include_archived ) end |
#find_node(name: nil, id: nil) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/llmemory/long_term/graph_based/knowledge_graph.rb', line 32 def find_node(name: nil, id: nil) if id @storage.find_node_by_id(@user_id, id) elsif name list_nodes.find { |n| n.name.to_s == name.to_s } end end |
#find_node_by_id(id) ⇒ Object
40 41 42 |
# File 'lib/llmemory/long_term/graph_based/knowledge_graph.rb', line 40 def find_node_by_id(id) @storage.find_node_by_id(@user_id, id) end |
#list_nodes ⇒ Object
106 107 108 |
# File 'lib/llmemory/long_term/graph_based/knowledge_graph.rb', line 106 def list_nodes @storage.list_nodes(@user_id) end |
#traverse(start_node:, depth: 2) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/llmemory/long_term/graph_based/knowledge_graph.rb', line 72 def traverse(start_node:, depth: 2) start_id = start_node.is_a?(Node) ? start_node.id : start_node.to_s visited = {} queue = [[start_id, 0]] result_nodes = [] result_edges = [] while queue.any? node_id, d = queue.shift next if d > depth next if visited[node_id] visited[node_id] = true node = @storage.find_node_by_id(@user_id, node_id) result_nodes << node if node edges = @storage.find_edges(@user_id, subject_id: node_id, include_archived: false) edges.each do |e| result_edges << e queue << [e.target_id, d + 1] unless visited[e.target_id] end edges_out = @storage.find_edges(@user_id, object_id: node_id, include_archived: false) edges_out.each do |e| result_edges << e queue << [e.subject_id, d + 1] unless visited[e.subject_id] end end { nodes: result_nodes.uniq, edges: result_edges.uniq } end |