Class: LLM::Provider

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

Overview

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

Direct Known Subclasses

Anthropic, Gemini, Ollama, OpenAI

Instance Method Summary collapse

Methods included from HTTPClient

#request

Constructor Details

#initialize(secret, host:, port: 443, ssl: true) ⇒ Provider

Returns a new instance of Provider.

Parameters:

  • secret (String)

    The secret key for authentication

  • host (String)

    The host address of the LLM provider

  • port (Integer) (defaults to: 443)

    The port number



17
18
19
20
21
22
# File 'lib/llm/provider.rb', line 17

def initialize(secret, host:, port: 443, ssl: true)
  @secret = secret
  @http = Net::HTTP.new(host, port).tap do |http|
    http.use_ssl = ssl
  end
end

Instance Method Details

#chat(prompt, role = :user, **params) ⇒ LLM::LazyConversation

Starts a new lazy conversation

Parameters:

  • prompt (String)

    The input prompt to be completed

  • role (Symbol) (defaults to: :user)

    The role of the prompt (e.g. :user, :system)

Returns:

Raises:

  • (NotImplementedError)

    When the method is not implemented by a subclass



61
62
63
# File 'lib/llm/provider.rb', line 61

def chat(prompt, role = :user, **params)
  LazyConversation.new(self).chat(prompt, role, **params)
end

#chat!(prompt, role = :user, **params) ⇒ LLM::Conversation

Starts a new conversation

Parameters:

  • prompt (String)

    The input prompt to be completed

  • role (Symbol) (defaults to: :user)

    The role of the prompt (e.g. :user, :system)

Returns:

Raises:

  • (NotImplementedError)

    When the method is not implemented by a subclass



71
72
73
# File 'lib/llm/provider.rb', line 71

def chat!(prompt, role = :user, **params)
  Conversation.new(self).chat(prompt, role, **params)
end

#complete(prompt, role = :user, **params) ⇒ LLM::Response::Completion

Completes a given prompt using the LLM

Parameters:

  • prompt (String)

    The input prompt to be completed

  • role (Symbol) (defaults to: :user)

    The role of the prompt (e.g. :user, :system)

Returns:

Raises:

  • (NotImplementedError)

    When the method is not implemented by a subclass



51
52
53
# File 'lib/llm/provider.rb', line 51

def complete(prompt, role = :user, **params)
  raise NotImplementedError
end

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

Parameters:

  • input (String)

    The input to embed

Returns:

Raises:

  • (NotImplementedError)

    When the method is not implemented by a subclass



38
39
40
# File 'lib/llm/provider.rb', line 38

def embed(input, **params)
  raise NotImplementedError
end

#inspectString

Note:

The secret key is redacted in inspect for security reasons

Returns an inspection of the provider object

Returns:

  • (String)


28
29
30
# File 'lib/llm/provider.rb', line 28

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