Class: Legion::CLI::Chat::Tools::EntityExtract

Inherits:
Tools::Base
  • Object
show all
Defined in:
lib/legion/cli/chat/tools/entity_extract.rb

Class Method Summary collapse

Methods inherited from Tools::Base

deferred, deferred?, description, error_response, extension, handle_exception, input_schema, log, mcp_category, mcp_tier, runner, sticky, tags, text_response, tool_name, trigger_words

Class Method Details

.call(text:, entity_types: nil, min_confidence: 0.7) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/legion/cli/chat/tools/entity_extract.rb', line 21

def self.call(text:, entity_types: nil, min_confidence: 0.7)
  return 'Apollo entity extractor not available.' unless extractor_available?

  types = parse_types(entity_types)
  result = run_extraction(text, types, min_confidence.to_f)
  format_result(result)
end

.extractor_available?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/legion/cli/chat/tools/entity_extract.rb', line 29

def self.extractor_available?
  defined?(Legion::Extensions::Apollo::Runners::EntityExtractor)
end

.format_result(result) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/cli/chat/tools/entity_extract.rb', line 48

def self.format_result(result)
  return format('Entity extraction failed: %<err>s', err: result[:error] || 'unknown error') unless result[:success]

  entities = result[:entities]
  return 'No entities found in the provided text.' if entities.empty?

  lines = [format("Extracted %<n>d entities:\n", n: entities.size)]

  grouped = entities.group_by { |e| e[:type] }
  grouped.each do |type, items|
    lines << format('  [%<type>s]', type: type)
    items.sort_by { |e| -(e[:confidence] || 0) }.each do |entity|
      lines << format('    %<name>s (confidence: %<conf>.0f%%)',
                      name: entity[:name], conf: (entity[:confidence] || 0) * 100)
    end
  end

  lines.join("\n")
end

.parse_types(types_str) ⇒ Object



33
34
35
36
37
# File 'lib/legion/cli/chat/tools/entity_extract.rb', line 33

def self.parse_types(types_str)
  return nil if types_str.nil? || types_str.strip.empty?

  types_str.split(',').map(&:strip)
end

.run_extraction(text, types, min_confidence) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/legion/cli/chat/tools/entity_extract.rb', line 39

def self.run_extraction(text, types, min_confidence)
  extractor = Object.new.extend(Legion::Extensions::Apollo::Runners::EntityExtractor)
  extractor.extract_entities(
    text:           text,
    entity_types:   types,
    min_confidence: min_confidence
  )
end