Class: FlowEngine::LLM::Adapter
- Inherits:
-
Object
- Object
- FlowEngine::LLM::Adapter
- 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.
Direct Known Subclasses
FlowEngine::LLM::Adapters::AnthropicAdapter, FlowEngine::LLM::Adapters::GeminiAdapter, FlowEngine::LLM::Adapters::OpenAIAdapter
Constant Summary collapse
- CONFIGURE_MUTEX =
Mutex.new
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#qualifier ⇒ Object
readonly
Returns the value of attribute qualifier.
-
#vendor ⇒ Object
readonly
Returns the value of attribute vendor.
Class Method Summary collapse
-
.api_key_var_name ⇒ String
Name of the environment variable for this provider's API key.
-
.default_model ⇒ String
Default model identifier for this provider.
-
.provider ⇒ Symbol
Derives the provider symbol from the class name.
Instance Method Summary collapse
-
#chat(system_prompt:, user_prompt:, model: @model) ⇒ String
Sends a system + user prompt pair to the LLM and returns the response text.
-
#initialize(api_key: nil, model: nil, qualifier: :default) ⇒ Adapter
constructor
A new instance of Adapter.
- #inspect ⇒ Object (also: #to_s)
Constructor Details
#initialize(api_key: nil, model: nil, qualifier: :default) ⇒ Adapter
Returns a new instance of Adapter.
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.
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.
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
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.
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 |