Class: HTM::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/htm/configuration.rb

Overview

HTM Configuration

Applications using HTM should configure LLM access by providing two methods:

  1. embedding_generator - Converts text to vector embeddings

  2. tag_extractor - Extracts hierarchical tags from text

  3. logger - Logger instance for HTM operations

Examples:

Configure with custom methods

HTM.configure do |config|
  config.embedding_generator = ->(text) {
    MyApp::LLMService.embed(text)  # Returns Array<Float>
  }
  config.tag_extractor = ->(text, ontology) {
    MyApp::LLMService.extract_tags(text, ontology)  # Returns Array<String>
  }
  config.logger = Rails.logger  # Use Rails logger
end

Use defaults with custom timeouts

HTM.configure do |config|
  config.embedding_timeout = 60      # 1 minute for faster models
  config.tag_timeout = 300           # 5 minutes for larger models
  config.connection_timeout = 10     # 10 seconds connection timeout
  config.reset_to_defaults  # Apply default implementations with new timeouts
end

Use defaults

HTM.configure  # Uses default implementations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/htm/configuration.rb', line 45

def initialize
  # Default configuration
  @embedding_provider = :ollama
  @embedding_model = 'nomic-embed-text'
  @embedding_dimensions = 768

  @tag_provider = :ollama
  @tag_model = 'llama3'

  @ollama_url = ENV['OLLAMA_URL'] || 'http://localhost:11434'

  # Timeout settings (in seconds) - apply to all LLM providers
  @embedding_timeout = 120      # 2 minutes for embedding generation
  @tag_timeout = 180            # 3 minutes for tag generation (LLM inference)
  @connection_timeout = 30      # 30 seconds for initial connection

  # Default logger (STDOUT with INFO level)
  @logger = default_logger

  # Auto-detect job backend based on environment
  @job_backend = detect_job_backend

  # Set default implementations
  reset_to_defaults
end

Instance Attribute Details

#connection_timeoutObject

Returns the value of attribute connection_timeout.



41
42
43
# File 'lib/htm/configuration.rb', line 41

def connection_timeout
  @connection_timeout
end

#embedding_dimensionsObject

Returns the value of attribute embedding_dimensions.



38
39
40
# File 'lib/htm/configuration.rb', line 38

def embedding_dimensions
  @embedding_dimensions
end

#embedding_generatorObject

Returns the value of attribute embedding_generator.



37
38
39
# File 'lib/htm/configuration.rb', line 37

def embedding_generator
  @embedding_generator
end

#embedding_modelObject

Returns the value of attribute embedding_model.



38
39
40
# File 'lib/htm/configuration.rb', line 38

def embedding_model
  @embedding_model
end

#embedding_providerObject

Returns the value of attribute embedding_provider.



38
39
40
# File 'lib/htm/configuration.rb', line 38

def embedding_provider
  @embedding_provider
end

#embedding_timeoutObject

Returns the value of attribute embedding_timeout.



41
42
43
# File 'lib/htm/configuration.rb', line 41

def embedding_timeout
  @embedding_timeout
end

#job_backendObject

Returns the value of attribute job_backend.



43
44
45
# File 'lib/htm/configuration.rb', line 43

def job_backend
  @job_backend
end

#loggerObject

Returns the value of attribute logger.



42
43
44
# File 'lib/htm/configuration.rb', line 42

def logger
  @logger
end

#ollama_urlObject

Returns the value of attribute ollama_url.



40
41
42
# File 'lib/htm/configuration.rb', line 40

def ollama_url
  @ollama_url
end

#tag_extractorObject

Returns the value of attribute tag_extractor.



37
38
39
# File 'lib/htm/configuration.rb', line 37

def tag_extractor
  @tag_extractor
end

#tag_modelObject

Returns the value of attribute tag_model.



39
40
41
# File 'lib/htm/configuration.rb', line 39

def tag_model
  @tag_model
end

#tag_providerObject

Returns the value of attribute tag_provider.



39
40
41
# File 'lib/htm/configuration.rb', line 39

def tag_provider
  @tag_provider
end

#tag_timeoutObject

Returns the value of attribute tag_timeout.



41
42
43
# File 'lib/htm/configuration.rb', line 41

def tag_timeout
  @tag_timeout
end

#token_counterObject

Returns the value of attribute token_counter.



37
38
39
# File 'lib/htm/configuration.rb', line 37

def token_counter
  @token_counter
end

Instance Method Details

#reset_to_defaultsObject

Reset to default RubyLLM-based implementations



72
73
74
75
76
# File 'lib/htm/configuration.rb', line 72

def reset_to_defaults
  @embedding_generator = default_embedding_generator
  @tag_extractor = default_tag_extractor
  @token_counter = default_token_counter
end

#validate!Object

Validate configuration



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/htm/configuration.rb', line 79

def validate!
  unless @embedding_generator.respond_to?(:call)
    raise HTM::ValidationError, "embedding_generator must be callable (proc, lambda, or object responding to :call)"
  end

  unless @tag_extractor.respond_to?(:call)
    raise HTM::ValidationError, "tag_extractor must be callable (proc, lambda, or object responding to :call)"
  end

  unless @token_counter.respond_to?(:call)
    raise HTM::ValidationError, "token_counter must be callable (proc, lambda, or object responding to :call)"
  end

  unless @logger.respond_to?(:info) && @logger.respond_to?(:warn) && @logger.respond_to?(:error)
    raise HTM::ValidationError, "logger must respond to :info, :warn, and :error"
  end

  unless [:active_job, :sidekiq, :inline, :thread].include?(@job_backend)
    raise HTM::ValidationError, "job_backend must be one of: :active_job, :sidekiq, :inline, :thread (got #{@job_backend.inspect})"
  end
end