Class: Legion::Extensions::Agentic::Memory::Semantic::Helpers::KnowledgeStore
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Memory::Semantic::Helpers::KnowledgeStore
show all
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb
Constant Summary
Constants included
from Constants
Constants::ACCESS_BOOST, Constants::ACCESS_DECAY, Constants::CONFIDENCE_ALPHA, Constants::CONFIDENCE_DECAY, Constants::CONFIDENCE_FLOOR, Constants::CONFIDENCE_LABELS, Constants::DEFAULT_CONFIDENCE, Constants::MAX_CONCEPTS, Constants::MAX_HISTORY, Constants::MAX_RELATIONS_PER_CONCEPT, Constants::MAX_SPREAD_HOPS, Constants::RELATION_TYPES, Constants::SPREAD_FACTOR, Constants::SPREAD_THRESHOLD
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of KnowledgeStore.
14
15
16
17
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 14
def initialize
@concepts = {}
@retrieval_history = []
end
|
Instance Attribute Details
#concepts ⇒ Object
Returns the value of attribute concepts.
12
13
14
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 12
def concepts
@concepts
end
|
#retrieval_history ⇒ Object
Returns the value of attribute retrieval_history.
12
13
14
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 12
def retrieval_history
@retrieval_history
end
|
Instance Method Details
#check_is_a(concept_name, category_name) ⇒ Object
60
61
62
63
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 60
def check_is_a(concept_name, category_name)
rels = query_relations(name: concept_name, type: :is_a)
rels.any? { |r| r[:target] == category_name }
end
|
#concept_count ⇒ Object
109
110
111
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 109
def concept_count
@concepts.size
end
|
#concepts_in_domain(domain) ⇒ Object
96
97
98
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 96
def concepts_in_domain(domain)
@concepts.values.select { |c| c.domain == domain }
end
|
#decay_all ⇒ Object
104
105
106
107
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 104
def decay_all
@concepts.each_value(&:decay)
@concepts.reject! { |_, c| c.faded? }
end
|
#instances_of(category_name) ⇒ Object
65
66
67
68
69
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 65
def instances_of(category_name)
@concepts.values.select do |c|
c.relations.any? { |r| r[:type] == :is_a && r[:target] == category_name }
end
end
|
#query_relations(name:, type: nil) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 48
def query_relations(name:, type: nil)
concept = @concepts[name]
return [] unless concept
concept.access
if type
concept.relations_of_type(type)
else
concept.relations
end
end
|
#relate(source:, target:, type:, confidence: nil) ⇒ Object
33
34
35
36
37
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 33
def relate(source:, target:, type:, confidence: nil)
store(name: source) unless @concepts.key?(source)
store(name: target) unless @concepts.key?(target)
@concepts[source].add_relation(type: type, target_name: target, confidence: confidence)
end
|
#relation_count ⇒ Object
113
114
115
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 113
def relation_count
@concepts.values.sum { |c| c.relations.size }
end
|
#retrieve(name:) ⇒ Object
39
40
41
42
43
44
45
46
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 39
def retrieve(name:)
concept = @concepts[name]
return nil unless concept
concept.access
record_retrieval(name)
concept
end
|
#search(query) ⇒ Object
100
101
102
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 100
def search(query)
@concepts.values.select { |c| c.name.to_s.include?(query.to_s) }
end
|
#spreading_activation(seed:, hops: MAX_SPREAD_HOPS) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 71
def spreading_activation(seed:, hops: MAX_SPREAD_HOPS)
activated = {}
queue = [[seed, 1.0]]
hops.times do
next_queue = []
queue.each do |name, strength|
next if strength < SPREAD_THRESHOLD
next if activated.key?(name) && activated[name] >= strength
activated[name] = strength
concept = @concepts[name]
next unless concept
concept.related_concepts.each do |related|
next_strength = strength * SPREAD_FACTOR
next_queue << [related, next_strength] if next_strength >= SPREAD_THRESHOLD
end
end
queue = next_queue
end
activated.sort_by { |_, s| -s }.to_h
end
|
#store(name:, domain: :general, confidence: nil, properties: {}) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 19
def store(name:, domain: :general, confidence: nil, properties: {})
if @concepts.key?(name)
existing = @concepts[name]
existing.access
properties.each { |k, v| existing.set_property(k, v) }
return existing
end
ensure_capacity
concept = Concept.new(name: name, domain: domain, confidence: confidence, properties: properties)
@concepts[name] = concept
concept
end
|
#to_h ⇒ Object
117
118
119
120
121
122
123
124
|
# File 'lib/legion/extensions/agentic/memory/semantic/helpers/knowledge_store.rb', line 117
def to_h
{
concepts: concept_count,
relations: relation_count,
domains: @concepts.values.map(&:domain).uniq.size,
history_size: @retrieval_history.size
}
end
|