Class: LLM::Provider Abstract

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/llm/provider.rb

Overview

This class is abstract.

The Provider class represents an abstract class for LLM (Language Model) providers.

Direct Known Subclasses

Anthropic, Gemini, Ollama, OpenAI

Constant Summary collapse

@@clients =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, host:, port: 443, timeout: 60, ssl: true, persistent: false) ⇒ Provider

Returns a new instance of Provider.

Parameters:

  • key (String, nil)

    The secret key for authentication

  • host (String)

    The host address of the LLM provider

  • port (Integer) (defaults to: 443)

    The port number

  • timeout (Integer) (defaults to: 60)

    The number of seconds to wait for a response

  • ssl (Boolean) (defaults to: true)

    Whether to use SSL for the connection

  • persistent (Boolean) (defaults to: false)

    Whether to use a persistent connection. Requires the net-http-persistent gem.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/llm/provider.rb', line 33

def initialize(key:, host:, port: 443, timeout: 60, ssl: true, persistent: false)
  @key = key
  @host = host
  @port = port
  @timeout = timeout
  @ssl = ssl
  @client = persistent ? persistent_client : transient_client
  @tracer = LLM::Tracer::Null.new(self)
  @base_uri = URI("#{ssl ? "https" : "http"}://#{host}:#{port}/")
  @headers = {"User-Agent" => "llm.rb v#{LLM::VERSION}"}
end

Class Method Details

.clientsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
# File 'lib/llm/provider.rb', line 17

def self.clients = @@clients

Instance Method Details

#tracer=(tracer) ⇒ void

This method returns an undefined value.

Set the tracer

Examples:

llm = LLM.openai(key: ENV["KEY"])
llm.tracer = LLM::Tracer::Logger.new(llm, path: "/path/to/log.txt")
# ...

Parameters:



279
280
281
282
283
284
285
# File 'lib/llm/provider.rb', line 279

def tracer=(tracer)
  @tracer = if tracer.nil?
    LLM::Tracer::Null.new(self)
  else
    tracer
  end
end

#inspectString

Note:

The secret key is redacted in inspect for security reasons

Returns an inspection of the provider object

Returns:

  • (String)


49
50
51
# File 'lib/llm/provider.rb', line 49

def inspect
  "#<#{self.class.name}:0x#{object_id.to_s(16)} @key=[REDACTED] @client=#{@client.inspect} @tracer=#{@tracer.inspect}>"
end

#embed(input, model: nil, **params) ⇒ LLM::Response

Provides an embedding

Parameters:

  • input (String, Array<String>)

    The input to embed

  • model (String) (defaults to: nil)

    The embedding model to use

  • params (Hash)

    Other embedding parameters

Returns:

Raises:

  • (NotImplementedError)

    When the method is not implemented by a subclass



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

def embed(input, model: nil, **params)
  raise NotImplementedError
end

#complete(prompt, params = {}) ⇒ LLM::Response

Provides an interface to the chat completions API

Examples:

llm = LLM.openai(key: ENV["KEY"])
messages = [{role: "system", content: "Your task is to answer all of my questions"}]
res = llm.complete("5 + 2 ?", messages:)
print "[#{res.messages[0].role}]", res.messages[0].content, "\n"

Parameters:

  • prompt (String)

    The input prompt to be completed

  • params (Hash) (defaults to: {})

    The parameters to maintain throughout the conversation. Any parameter the provider supports can be included and not only those listed here.

Options Hash (params):

  • :role (Symbol)

    Defaults to the provider's default role

  • :model (String)

    Defaults to the provider's default model

  • :schema (#to_json, nil)

    Defaults to nil

  • :tools (Array<LLM::Function>, nil)

    Defaults to nil

Returns:

Raises:

  • (NotImplementedError)

    When the method is not implemented by a subclass



88
89
90
# File 'lib/llm/provider.rb', line 88

def complete(prompt, params = {})
  raise NotImplementedError
end

#chat(prompt, params = {}) ⇒ LLM::Session

Starts a new chat powered by the chat completions API

Parameters:

  • prompt (String)

    The input prompt to be completed

  • params (Hash) (defaults to: {})

    The parameters to maintain throughout the conversation. Any parameter the provider supports can be included and not only those listed here.

Returns:



97
98
99
100
# File 'lib/llm/provider.rb', line 97

def chat(prompt, params = {})
  role = params.delete(:role)
  LLM::Session.new(self, params).talk(prompt, role:)
end

#respond(prompt, params = {}) ⇒ LLM::Session

Starts a new chat powered by the responses API

Parameters:

  • prompt (String)

    The input prompt to be completed

  • params (Hash) (defaults to: {})

    The parameters to maintain throughout the conversation. Any parameter the provider supports can be included and not only those listed here.

Returns:

Raises:

  • (NotImplementedError)

    When the method is not implemented by a subclass



108
109
110
111
# File 'lib/llm/provider.rb', line 108

def respond(prompt, params = {})
  role = params.delete(:role)
  LLM::Session.new(self, params).respond(prompt, role:)
end

#responsesLLM::OpenAI::Responses

Note:

Compared to the chat completions API, the responses API can require less bandwidth on each turn, maintain state server-side, and produce faster responses.

Returns:

Raises:

  • (NotImplementedError)


120
121
122
# File 'lib/llm/provider.rb', line 120

def responses
  raise NotImplementedError
end

#imagesLLM::OpenAI::Images, LLM::Gemini::Images

Returns an interface to the images API

Returns:

Raises:

  • (NotImplementedError)


127
128
129
# File 'lib/llm/provider.rb', line 127

def images
  raise NotImplementedError
end

#audioLLM::OpenAI::Audio

Returns an interface to the audio API

Returns:

Raises:

  • (NotImplementedError)


134
135
136
# File 'lib/llm/provider.rb', line 134

def audio
  raise NotImplementedError
end

#filesLLM::OpenAI::Files

Returns an interface to the files API

Returns:

Raises:

  • (NotImplementedError)


141
142
143
# File 'lib/llm/provider.rb', line 141

def files
  raise NotImplementedError
end

#modelsLLM::OpenAI::Models

Returns an interface to the models API

Returns:

Raises:

  • (NotImplementedError)


148
149
150
# File 'lib/llm/provider.rb', line 148

def models
  raise NotImplementedError
end

#moderationsLLM::OpenAI::Moderations

Returns an interface to the moderations API

Returns:

Raises:

  • (NotImplementedError)


155
156
157
# File 'lib/llm/provider.rb', line 155

def moderations
  raise NotImplementedError
end

#vector_storesLLM::OpenAI::VectorStore

Returns an interface to the vector stores API

Returns:

  • (LLM::OpenAI::VectorStore)

    Returns an interface to the vector stores API

Raises:

  • (NotImplementedError)


162
163
164
# File 'lib/llm/provider.rb', line 162

def vector_stores
  raise NotImplementedError
end

#assistant_roleString

Returns the role of the assistant in the conversation. Usually "assistant" or "model"

Returns:

  • (String)

    Returns the role of the assistant in the conversation. Usually "assistant" or "model"

Raises:

  • (NotImplementedError)


170
171
172
# File 'lib/llm/provider.rb', line 170

def assistant_role
  raise NotImplementedError
end

#default_modelString

Returns the default model for chat completions

Returns:

  • (String)

    Returns the default model for chat completions

Raises:

  • (NotImplementedError)


177
178
179
# File 'lib/llm/provider.rb', line 177

def default_model
  raise NotImplementedError
end

#schemaLLM::Schema

Returns an object that can generate a JSON schema

Returns:



184
185
186
# File 'lib/llm/provider.rb', line 184

def schema
  @schema ||= LLM::Schema.new
end

#with(headers:) ⇒ LLM::Provider

Add one or more headers to all requests

Examples:

llm = LLM.openai(key: ENV["KEY"])
llm.with(headers: {"OpenAI-Organization" => ENV["ORG"]})
llm.with(headers: {"OpenAI-Project" => ENV["PROJECT"]})

Parameters:

  • headers (Hash<String,String>)

    One or more headers

Returns:



198
199
200
# File 'lib/llm/provider.rb', line 198

def with(headers:)
  tap { @headers.merge!(headers) }
end

#server_toolsString => LLM::ServerTool

Note:

This method might be outdated, and the LLM::Provider#server_tool method can be used if a tool is not found here.

Returns all known tools provided by a provider.

Returns:



208
209
210
# File 'lib/llm/provider.rb', line 208

def server_tools
  {}
end

#server_tool(name, options = {}) ⇒ LLM::ServerTool

Note:

OpenAI, Anthropic, and Gemini provide platform-tools for things like web search, and more.

Returns a tool provided by a provider.

Examples:

llm   = LLM.openai(key: ENV["KEY"])
tools = [llm.server_tool(:web_search)]
res   = llm.responses.create("Summarize today's news", tools:)
print res.output_text, "\n"

Parameters:

  • name (String, Symbol)

    The name of the tool

  • options (Hash) (defaults to: {})

    Configuration options for the tool

Returns:



225
226
227
# File 'lib/llm/provider.rb', line 225

def server_tool(name, options = {})
  LLM::ServerTool.new(name, options, self)
end

#web_search(query:) ⇒ LLM::Response

Provides a web search capability

Parameters:

  • query (String)

    The search query

Returns:

Raises:

  • (NotImplementedError)

    When the method is not implemented by a subclass



235
236
237
# File 'lib/llm/provider.rb', line 235

def web_search(query:)
  raise NotImplementedError
end

#user_roleSymbol

Returns:

  • (Symbol)


241
242
243
# File 'lib/llm/provider.rb', line 241

def user_role
  :user
end

#system_roleSymbol

Returns:

  • (Symbol)


247
248
249
# File 'lib/llm/provider.rb', line 247

def system_role
  :system
end

#developer_roleSymbol

Returns:

  • (Symbol)


253
254
255
# File 'lib/llm/provider.rb', line 253

def developer_role
  :developer
end

#tool_roleSymbol

Returns:

  • (Symbol)


259
260
261
# File 'lib/llm/provider.rb', line 259

def tool_role
  :tool
end

#tracerLLM::Tracer

Returns an LLM tracer

Returns:



266
267
268
# File 'lib/llm/provider.rb', line 266

def tracer
  @tracer
end