Class: Mana::Backends::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mana/backends/base.rb

Overview

Base class for LLM backends. Provides shared HTTP infrastructure and the factory method for backend resolution.

Direct Known Subclasses

Anthropic, OpenAI

Constant Summary collapse

ANTHROPIC_PATTERN =

Model name pattern for auto-detecting Anthropic backend

/^claude-/i

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Base

Returns a new instance of Base.



15
16
17
# File 'lib/mana/backends/base.rb', line 15

def initialize(config)
  @config = config
end

Class Method Details

.for(config) ⇒ Object

Resolve a backend instance from configuration. Priority: pre-built instance > explicit name > auto-detect from model name.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mana/backends/base.rb', line 28

def self.for(config)
  return config.backend if config.backend.is_a?(Base)

  config.validate!

  case config.backend&.to_s
  when "openai" then OpenAI.new(config)
  when "anthropic" then Anthropic.new(config)
  else
    config.model.match?(ANTHROPIC_PATTERN) ? Anthropic.new(config) : OpenAI.new(config)
  end
end

Instance Method Details

#chat(system:, messages:, tools:, model:, max_tokens: 4096) ⇒ Object

Send a chat request. Subclasses must implement this.

Raises:

  • (NotImplementedError)


20
21
22
# File 'lib/mana/backends/base.rb', line 20

def chat(system:, messages:, tools:, model:, max_tokens: 4096)
  raise NotImplementedError, "#{self.class}#chat not implemented"
end