class RubyLLM::Provider
Base class for LLM providers.
Attributes
Public Class Methods
Source
# File lib/ruby_llm/provider.rb, line 183 def assume_models_exist? false end
Source
# File lib/ruby_llm/provider.rb, line 171 def configuration_options [] end
# File lib/ruby_llm/provider.rb, line 167 def configuration_requirements [] end
Source
# File lib/ruby_llm/provider.rb, line 187 def configured?(config) configuration_requirements.all? { |req| config.send(req) } end
# File lib/ruby_llm/provider.rb, line 217 def configured_providers(config) providers.select do |_slug, provider_class| provider_class.configured?(config) end.values end
# File lib/ruby_llm/provider.rb, line 223 def configured_remote_providers(config) providers.select do |_slug, provider_class| provider_class.remote? && provider_class.configured?(config) end.values end
Source
# File lib/ruby_llm/provider.rb, line 200 def for(model) model_info = Models.find(model) resolve model_info.provider end
Source
# File lib/ruby_llm/provider.rb, line 209 def local_providers providers.select { |_slug, provider_class| provider_class.local? } end
Source
# File lib/ruby_llm/provider.rb, line 13 def initialize(config) @config = config ensure_configured! @connection = Connection.new(self, @config) end
# File lib/ruby_llm/provider.rb, line 191 def register(name, provider_class) providers[name.to_sym] = provider_class RubyLLM::Configuration.register_provider_options(provider_class.configuration_options) end
Source
# File lib/ruby_llm/provider.rb, line 213 def remote_providers providers.select { |_slug, provider_class| provider_class.remote? } end
Source
# File lib/ruby_llm/provider.rb, line 196 def resolve(name) providers[name.to_sym] end
Public Instance Methods
Source
# File lib/ruby_llm/provider.rb, line 19 def api_base raise NotImplementedError end
Source
# File lib/ruby_llm/provider.rb, line 113 def assume_models_exist? self.class.assume_models_exist? end
Source
# File lib/ruby_llm/provider.rb, line 35 def capabilities self.class.capabilities end
# File lib/ruby_llm/provider.rb, line 44 def complete(messages, tools:, temperature:, model:, params: {}, headers: {}, schema: nil, thinking: nil, tool_prefs: nil, &) normalized_temperature = maybe_normalize_temperature(temperature, model) payload = Utils.deep_merge( render_payload( messages, tools: tools, tool_prefs: tool_prefs, temperature: normalized_temperature, model: model, stream: block_given?, schema: schema, thinking: thinking ), params ) if block_given? stream_response @connection, payload, headers, & else sync_response @connection, payload, headers end end
rubocop:disable Metrics/ParameterLists
# File lib/ruby_llm/provider.rb, line 39 def configuration_requirements self.class.configuration_requirements end
Source
# File lib/ruby_llm/provider.rb, line 101 def configured? configuration_requirements.all? { |req| @config.send(req) } end
# File lib/ruby_llm/provider.rb, line 75 def embed(text, model:, dimensions:) payload = render_embedding_payload(text, model:, dimensions:) response = @connection.post(embedding_url(model:), payload) parse_embedding_response(response, model:, text:) end
Source
# File lib/ruby_llm/provider.rb, line 137 def format_messages(messages) messages.map do |msg| { role: msg.role.to_s, content: msg.content } end end
# File lib/ruby_llm/provider.rb, line 146 def format_tool_calls(_tool_calls) nil end
Source
# File lib/ruby_llm/provider.rb, line 70 def list_models response = @connection.get models_url parse_list_models_response response, slug, capabilities end
rubocop:enable Metrics/ParameterLists
Source
# File lib/ruby_llm/provider.rb, line 81 def moderate(input, model:) payload = render_moderation_payload(input, model:) response = @connection.post moderation_url, payload parse_moderation_response(response, model:) end
# File lib/ruby_llm/provider.rb, line 87 def paint(prompt, model:, size:, with: nil, mask: nil, params: {}) # rubocop:disable Metrics/ParameterLists validate_paint_inputs!(with:, mask:) payload = render_image_payload(prompt, model:, size:, with:, mask:, params:) response = @connection.post images_url(with:, mask:), payload parse_image_response(response, model:) end
Source
# File lib/ruby_llm/provider.rb, line 117 def parse_error(response) return if response.body.empty? body = try_parse_json(response.body) case body when Hash error = body['error'] return error if error.is_a?(String) body.dig('error', 'message') when Array body.map do |part| error = part['error'] error.is_a?(String) ? error : part.dig('error', 'message') end.join('. ') else body end end
# File lib/ruby_llm/provider.rb, line 150 def parse_tool_calls(_tool_calls) nil end
# File lib/ruby_llm/provider.rb, line 94 def transcribe(audio_file, model:, language:, **options) file_part = build_audio_file_part(audio_file) payload = render_transcription_payload(file_part, model:, language:, **options) response = @connection.post transcription_url, payload parse_transcription_response(response, model:) end