Class: FlowEngine::LLM::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/flowengine/llm/adapter.rb

Overview

Abstract adapter for LLM API calls. Subclass and implement the class methods Adapter.api_key_var_name and Adapter.default_model to integrate with a specific provider. Thread-safe: RubyLLM configuration is protected by a mutex.

Constant Summary collapse

CONFIGURE_MUTEX =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, model: nil, qualifier: :default) ⇒ Adapter

Returns a new instance of Adapter.

Parameters:

  • api_key (String, nil) (defaults to: nil) —

    API key; falls back to env var from api_key_var_name

  • model (String, nil) (defaults to: nil) —

    model identifier; falls back to default_model

  • qualifier (Symbol) (defaults to: :default) —

    adapter qualifier (:top, :default, :fastest)

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/flowengine/llm/adapter.rb', line 19

def initialize(api_key: nil, model: nil, qualifier: :default)
  @qualifier = qualifier
  @api_key = api_key || ENV.fetch(self.class.api_key_var_name, nil)
  @model = model || self.class.default_model
  @vendor = self.class.provider

  unless @api_key
    raise ::FlowEngine::Errors::NoAPIKeyFoundError,
          "#{vendor} API key not available ($#{self.class.api_key_var_name} not set)"
  end

  configure_ruby_llm!
  freeze
end

Instance Attribute Details

#api_key ⇒ Object (readonly)

Returns the value of attribute api_key.



13
14
15
# File 'lib/flowengine/llm/adapter.rb', line 13

def api_key
  @api_key
end

#model ⇒ Object (readonly)

Returns the value of attribute model.



13
14
15
# File 'lib/flowengine/llm/adapter.rb', line 13

def model
  @model
end

#qualifier ⇒ Object (readonly)

Returns the value of attribute qualifier.



13
14
15
# File 'lib/flowengine/llm/adapter.rb', line 13

def qualifier
  @qualifier
end

#vendor ⇒ Object (readonly)

Returns the value of attribute vendor.



13
14
15
# File 'lib/flowengine/llm/adapter.rb', line 13

def vendor
  @vendor
end

Class Method Details

.api_key_var_name ⇒ String

Returns name of the environment variable for this provider's API key.

Returns:

  • (String) —

    name of the environment variable for this provider's API key

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/flowengine/llm/adapter.rb', line 55

def self.api_key_var_name
  raise NotImplementedError, "#{name}.api_key_var_name must be implemented"
end

.default_model ⇒ String

Returns default model identifier for this provider.

Returns:

  • (String) —

    default model identifier for this provider

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/flowengine/llm/adapter.rb', line 60

def self.default_model
  raise NotImplementedError, "#{name}.default_model must be implemented"
end

.provider ⇒ Symbol

Derives the provider symbol from the class name. e.g. AnthropicAdapter => :anthropic, OpenAIAdapter => :openai

Returns:

  • (Symbol)


50
51
52
# File 'lib/flowengine/llm/adapter.rb', line 50

def self.provider
  name.split("::").last.downcase.gsub("adapter", "").to_sym
end

Instance Method Details

#chat(system_prompt:, user_prompt:, model: @model) ⇒ String

Sends a system + user prompt pair to the LLM and returns the response text.

Parameters:

  • system_prompt (String) —

    system instructions for the LLM

  • user_prompt (String) —

    user's text

  • model (String) (defaults to: @model) —

    model identifier (defaults to the adapter's model)

Returns:

  • (String) —

    the LLM's response content



40
41
42
43
44
# File 'lib/flowengine/llm/adapter.rb', line 40

def chat(system_prompt:, user_prompt:, model: @model)
  conversation = RubyLLM.chat(model: model)
  response = conversation.with_instructions(system_prompt).ask(user_prompt)
  response.content
end

#inspect ⇒ Object Also known as: to_s



64
65
66
# File 'lib/flowengine/llm/adapter.rb', line 64

def inspect
  "#<#{self.class.name} vendor=#{vendor} model=#{model} qualifier=#{qualifier}>"
end