Module: Legion::Extensions::Apollo::Helpers::GraphQuery
- Defined in:
- lib/legion/extensions/apollo/helpers/graph_query.rb
Constant Summary collapse
- SPREAD_FACTOR =
0.6- DEFAULT_DEPTH =
2- MIN_ACTIVATION =
0.1
Class Method Summary collapse
- .build_semantic_search_sql(limit: default_query_limit, min_confidence: default_query_min_confidence, statuses: nil, tags: nil, domain: nil) ⇒ Object
- .build_traversal_sql(depth: default_depth, relation_types: nil, min_activation: self.min_activation) ⇒ Object
- .default_depth ⇒ Object
- .default_query_limit ⇒ Object
- .default_query_min_confidence ⇒ Object
- .min_activation ⇒ Object
- .spread_factor ⇒ Object
Class Method Details
.build_semantic_search_sql(limit: default_query_limit, min_confidence: default_query_min_confidence, statuses: nil, tags: nil, domain: nil) ⇒ 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 |
# File 'lib/legion/extensions/apollo/helpers/graph_query.rb', line 55 def build_semantic_search_sql(limit: default_query_limit, min_confidence: default_query_min_confidence, statuses: nil, tags: nil, domain: nil, **) conditions = ["e.confidence >= #{min_confidence}"] if statuses&.any? status_list = statuses.map { |s| "'#{s}'" }.join(', ') conditions << "e.status IN (#{status_list})" end if &.any? tag_list = .map { |t| "'#{t}'" }.join(', ') conditions << "e.tags && ARRAY[#{tag_list}]::text[]" end conditions << "e.knowledge_domain = '#{domain}'" if domain where_clause = conditions.join(' AND ') <<~SQL SELECT e.id, e.content, e.content_type, e.confidence, e.tags, e.source_agent, e.access_count, e.created_at, e.knowledge_domain, (e.embedding <=> :embedding) AS distance FROM apollo_entries e WHERE #{where_clause} AND e.embedding IS NOT NULL ORDER BY e.embedding <=> :embedding LIMIT #{limit} SQL end |
.build_traversal_sql(depth: default_depth, relation_types: nil, min_activation: self.min_activation) ⇒ Object
21 22 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 49 50 51 52 53 |
# File 'lib/legion/extensions/apollo/helpers/graph_query.rb', line 21 def build_traversal_sql(depth: default_depth, relation_types: nil, min_activation: self.min_activation, **) sf = spread_factor type_filter = if relation_types&.any? types = relation_types.map { |t| "'#{t}'" }.join(', ') "AND r.relation_type IN (#{types})" else '' end <<~SQL WITH RECURSIVE graph AS ( SELECT e.id, e.content, e.content_type, e.confidence, e.tags, e.source_agent, 0 AS depth, 1.0::float AS activation FROM apollo_entries e WHERE e.id = :entry_id UNION ALL SELECT e.id, e.content, e.content_type, e.confidence, e.tags, e.source_agent, g.depth + 1, (g.activation * #{sf} * r.weight)::float FROM graph g JOIN apollo_relations r ON r.from_entry_id = g.id #{type_filter} JOIN apollo_entries e ON e.id = r.to_entry_id WHERE g.depth < #{depth} AND g.activation * #{sf} * r.weight > #{min_activation} ) SELECT DISTINCT ON (id) id, content, content_type, confidence, tags, source_agent, depth, activation FROM graph ORDER BY id, activation DESC SQL end |
.default_depth ⇒ Object
15 |
# File 'lib/legion/extensions/apollo/helpers/graph_query.rb', line 15 def default_depth = Confidence.apollo_setting(:graph, :default_depth, default: DEFAULT_DEPTH) |
.default_query_limit ⇒ Object
18 |
# File 'lib/legion/extensions/apollo/helpers/graph_query.rb', line 18 def default_query_limit = Confidence.apollo_setting(:query, :default_limit, default: 10) |
.default_query_min_confidence ⇒ Object
19 |
# File 'lib/legion/extensions/apollo/helpers/graph_query.rb', line 19 def default_query_min_confidence = Confidence.apollo_setting(:query, :default_min_confidence, default: 0.3) |
.min_activation ⇒ Object
16 |
# File 'lib/legion/extensions/apollo/helpers/graph_query.rb', line 16 def min_activation = Confidence.apollo_setting(:graph, :min_activation, default: MIN_ACTIVATION) |
.spread_factor ⇒ Object
14 |
# File 'lib/legion/extensions/apollo/helpers/graph_query.rb', line 14 def spread_factor = Confidence.apollo_setting(:graph, :spread_factor, default: SPREAD_FACTOR) |