Class: RubyLLM::Provider
- Inherits:
-
Object
- Object
- RubyLLM::Provider
- Includes:
- Streaming
- Defined in:
- lib/ruby_llm/provider.rb
Overview
Base class for LLM providers.
Direct Known Subclasses
RubyLLM::Providers::Anthropic, RubyLLM::Providers::Bedrock, RubyLLM::Providers::Gemini, RubyLLM::Providers::OpenAI
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Class Method Summary collapse
- .capabilities ⇒ Object
- .configuration_requirements ⇒ Object
- .configured?(config) ⇒ Boolean
- .configured_providers(config) ⇒ Object
- .configured_remote_providers(config) ⇒ Object
- .for(model) ⇒ Object
- .local? ⇒ Boolean
- .local_providers ⇒ Object
- .name ⇒ Object
- .providers ⇒ Object
- .register(name, provider_class) ⇒ Object
- .remote? ⇒ Boolean
- .remote_providers ⇒ Object
- .resolve(name) ⇒ Object
- .slug ⇒ Object
Instance Method Summary collapse
- #api_base ⇒ Object
- #capabilities ⇒ Object
-
#complete(messages, tools:, temperature:, model:, params: {}, headers: {}, schema: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists.
- #configuration_requirements ⇒ Object
- #configured? ⇒ Boolean
- #embed(text, model:, dimensions:) ⇒ Object
- #format_messages(messages) ⇒ Object
- #format_tool_calls(_tool_calls) ⇒ Object
- #headers ⇒ Object
-
#initialize(config) ⇒ Provider
constructor
A new instance of Provider.
- #list_models ⇒ Object
- #local? ⇒ Boolean
- #moderate(input, model:) ⇒ Object
- #name ⇒ Object
- #paint(prompt, model:, size:) ⇒ Object
- #parse_error(response) ⇒ Object
- #parse_tool_calls(_tool_calls) ⇒ Object
- #remote? ⇒ Boolean
- #slug ⇒ Object
- #transcribe(audio_file, model:, language:, **options) ⇒ Object
Methods included from Streaming
handle_stream, stream_response
Constructor Details
#initialize(config) ⇒ Provider
Returns a new instance of Provider.
10 11 12 13 14 |
# File 'lib/ruby_llm/provider.rb', line 10 def initialize(config) @config = config ensure_configured! @connection = Connection.new(self, @config) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/ruby_llm/provider.rb', line 8 def config @config end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/ruby_llm/provider.rb', line 8 def connection @connection end |
Class Method Details
.capabilities ⇒ Object
146 147 148 |
# File 'lib/ruby_llm/provider.rb', line 146 def capabilities raise NotImplementedError end |
.configuration_requirements ⇒ Object
150 151 152 |
# File 'lib/ruby_llm/provider.rb', line 150 def configuration_requirements [] end |
.configured?(config) ⇒ Boolean
162 163 164 |
# File 'lib/ruby_llm/provider.rb', line 162 def configured?(config) configuration_requirements.all? { |req| config.send(req) } end |
.configured_providers(config) ⇒ Object
191 192 193 194 195 |
# File 'lib/ruby_llm/provider.rb', line 191 def configured_providers(config) providers.select do |_slug, provider_class| provider_class.configured?(config) end.values end |
.configured_remote_providers(config) ⇒ Object
197 198 199 200 201 |
# File 'lib/ruby_llm/provider.rb', line 197 def configured_remote_providers(config) providers.select do |_slug, provider_class| provider_class.remote? && provider_class.configured?(config) end.values end |
.for(model) ⇒ Object
174 175 176 177 |
# File 'lib/ruby_llm/provider.rb', line 174 def for(model) model_info = Models.find(model) resolve model_info.provider end |
.local? ⇒ Boolean
154 155 156 |
# File 'lib/ruby_llm/provider.rb', line 154 def local? false end |
.local_providers ⇒ Object
183 184 185 |
# File 'lib/ruby_llm/provider.rb', line 183 def local_providers providers.select { |_slug, provider_class| provider_class.local? } end |
.name ⇒ Object
138 139 140 |
# File 'lib/ruby_llm/provider.rb', line 138 def name to_s.split('::').last end |
.providers ⇒ Object
179 180 181 |
# File 'lib/ruby_llm/provider.rb', line 179 def providers @providers ||= {} end |
.register(name, provider_class) ⇒ Object
166 167 168 |
# File 'lib/ruby_llm/provider.rb', line 166 def register(name, provider_class) providers[name.to_sym] = provider_class end |
.remote? ⇒ Boolean
158 159 160 |
# File 'lib/ruby_llm/provider.rb', line 158 def remote? !local? end |
.remote_providers ⇒ Object
187 188 189 |
# File 'lib/ruby_llm/provider.rb', line 187 def remote_providers providers.select { |_slug, provider_class| provider_class.remote? } end |
.resolve(name) ⇒ Object
170 171 172 |
# File 'lib/ruby_llm/provider.rb', line 170 def resolve(name) providers[name.to_sym] end |
.slug ⇒ Object
142 143 144 |
# File 'lib/ruby_llm/provider.rb', line 142 def slug name.downcase end |
Instance Method Details
#api_base ⇒ Object
16 17 18 |
# File 'lib/ruby_llm/provider.rb', line 16 def api_base raise NotImplementedError end |
#capabilities ⇒ Object
32 33 34 |
# File 'lib/ruby_llm/provider.rb', line 32 def capabilities self.class.capabilities end |
#complete(messages, tools:, temperature:, model:, params: {}, headers: {}, schema: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ruby_llm/provider.rb', line 40 def complete(, tools:, temperature:, model:, params: {}, headers: {}, schema: nil, &) # rubocop:disable Metrics/ParameterLists normalized_temperature = maybe_normalize_temperature(temperature, model) payload = Utils.deep_merge( render_payload( , tools: tools, temperature: normalized_temperature, model: model, stream: block_given?, schema: schema ), params ) if block_given? stream_response @connection, payload, headers, & else sync_response @connection, payload, headers end end |
#configuration_requirements ⇒ Object
36 37 38 |
# File 'lib/ruby_llm/provider.rb', line 36 def configuration_requirements self.class.configuration_requirements end |
#configured? ⇒ Boolean
92 93 94 |
# File 'lib/ruby_llm/provider.rb', line 92 def configured? configuration_requirements.all? { |req| @config.send(req) } end |
#embed(text, model:, dimensions:) ⇒ Object
67 68 69 70 71 |
# File 'lib/ruby_llm/provider.rb', line 67 def (text, model:, dimensions:) payload = (text, model:, dimensions:) response = @connection.post((model:), payload) (response, model:, text:) end |
#format_messages(messages) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/ruby_llm/provider.rb', line 120 def () .map do |msg| { role: msg.role.to_s, content: msg.content } end end |
#format_tool_calls(_tool_calls) ⇒ Object
129 130 131 |
# File 'lib/ruby_llm/provider.rb', line 129 def format_tool_calls(_tool_calls) nil end |
#headers ⇒ Object
20 21 22 |
# File 'lib/ruby_llm/provider.rb', line 20 def headers {} end |
#list_models ⇒ Object
62 63 64 65 |
# File 'lib/ruby_llm/provider.rb', line 62 def list_models response = @connection.get models_url parse_list_models_response response, slug, capabilities end |
#local? ⇒ Boolean
96 97 98 |
# File 'lib/ruby_llm/provider.rb', line 96 def local? self.class.local? end |
#moderate(input, model:) ⇒ Object
73 74 75 76 77 |
# File 'lib/ruby_llm/provider.rb', line 73 def moderate(input, model:) payload = render_moderation_payload(input, model:) response = @connection.post moderation_url, payload parse_moderation_response(response, model:) end |
#name ⇒ Object
28 29 30 |
# File 'lib/ruby_llm/provider.rb', line 28 def name self.class.name end |
#paint(prompt, model:, size:) ⇒ Object
79 80 81 82 83 |
# File 'lib/ruby_llm/provider.rb', line 79 def paint(prompt, model:, size:) payload = render_image_payload(prompt, model:, size:) response = @connection.post images_url, payload parse_image_response(response, model:) end |
#parse_error(response) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/ruby_llm/provider.rb', line 104 def parse_error(response) return if response.body.empty? body = try_parse_json(response.body) case body when Hash body.dig('error', 'message') when Array body.map do |part| part.dig('error', 'message') end.join('. ') else body end end |
#parse_tool_calls(_tool_calls) ⇒ Object
133 134 135 |
# File 'lib/ruby_llm/provider.rb', line 133 def parse_tool_calls(_tool_calls) nil end |
#remote? ⇒ Boolean
100 101 102 |
# File 'lib/ruby_llm/provider.rb', line 100 def remote? self.class.remote? end |
#slug ⇒ Object
24 25 26 |
# File 'lib/ruby_llm/provider.rb', line 24 def slug self.class.slug end |
#transcribe(audio_file, model:, language:, **options) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/ruby_llm/provider.rb', line 85 def transcribe(audio_file, model:, language:, **) file_part = build_audio_file_part(audio_file) payload = render_transcription_payload(file_part, model:, language:, **) response = @connection.post transcription_url, payload parse_transcription_response(response, model:) end |