class FlowEngine::LLM::Adapter
Thread-safe: RubyLLM configuration is protected by a mutex.
{.api_key_var_name} and {.default_model} to integrate with a specific provider.
Abstract adapter for LLM API calls. Subclass and implement the class methods
def self.api_key_var_name
-
(String)- name of the environment variable for this provider's API key
def self.api_key_var_name raise NotImplementedError, "#{name}.api_key_var_name must be implemented" end
def self.default_model
-
(String)- default model identifier for this provider
def self.default_model raise NotImplementedError, "#{name}.default_model must be implemented" end
def self.provider
-
(Symbol)-
def self.provider name.split("::").last.downcase.gsub("adapter", "").to_sym end
def chat(system_prompt:, user_prompt:, model: @model)
-
(String)- the LLM's response content
Parameters:
-
model(String) -- model identifier (defaults to the adapter's model) -
user_prompt(String) -- user's text -
system_prompt(String) -- system instructions for the LLM
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
def configure_ruby_llm!
def configure_ruby_llm! method_name = "#{vendor}_api_key=" key = api_key CONFIGURE_MUTEX.synchronize do RubyLLM.configure { |config| config.send(method_name, key) } end end
def initialize(api_key: nil, model: nil, qualifier: :default)
-
(Errors::NoAPIKeyFoundError)- if no API key is available
Parameters:
-
qualifier(Symbol) -- adapter qualifier (:top, :default, :fastest) -
model(String, nil) -- model identifier; falls back to {.default_model} -
api_key(String, nil) -- API key; falls back to env var from {.api_key_var_name}
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
def inspect
def inspect "#<#{self.class.name} vendor=#{vendor} model=#{model} qualifier=#{qualifier}>" end