Module: LLM

Defined in:
lib/llm.rb,
lib/llm/bot.rb,
lib/llm/agent.rb,
lib/llm/error.rb,
lib/llm/buffer.rb,
lib/llm/client.rb,
lib/llm/message.rb,
lib/llm/version.rb,
lib/llm/contract.rb,
lib/llm/response.rb,
lib/llm/eventhandler.rb,
lib/llm/json_adapter.rb,
lib/llm/providers/xai.rb,
lib/llm/providers/zai.rb,
lib/llm/providers/gemini.rb,
lib/llm/providers/ollama.rb,
lib/llm/providers/openai.rb,
lib/llm/providers/deepseek.rb,
lib/llm/providers/llamacpp.rb,
lib/llm/providers/anthropic.rb

Defined Under Namespace

Modules: Client, Contract Classes: Agent, Anthropic, Bot, Buffer, Builder, DeepSeek, Error, File, Function, Gemini, JSONAdapter, LlamaCpp, Message, Object, Ollama, OpenAI, Provider, Response, Schema, ServerTool, Tool, Usage, XAI, ZAI

Constant Summary collapse

UnauthorizedError =

HTTPUnauthorized

Class.new(Error)
RateLimitError =

HTTPTooManyRequests

Class.new(Error)
ServerError =

HTTPServerError

Class.new(Error)
FormatError =

When an given an input object that is not understood

Class.new(Error)
PromptError =

When given a prompt object that is not understood

Class.new(FormatError)
InvalidRequestError =

When given an invalid request

Class.new(Error)
ContextWindowError =

When the context window is exceeded

Class.new(InvalidRequestError)
ToolLoopError =

When stuck in a tool call loop

Class.new(Error)
VERSION =
"4.1.0"

Class Method Summary collapse

Class Method Details

.anthropicAnthropic

Returns a new instance of Anthropic.

Parameters:

  • key (String, nil)

    The secret key for authentication

  • host (String)

    The host address of the LLM provider

  • port (Integer)

    The port number

  • timeout (Integer)

    The number of seconds to wait for a response

  • ssl (Boolean)

    Whether to use SSL for the connection

  • persistent (Boolean)

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

Returns:

  • (Anthropic)

    a new instance of Anthropic



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

def anthropic(**)
  lock(:require) { require_relative "llm/providers/anthropic" unless defined?(LLM::Anthropic) }
  LLM::Anthropic.new(**)
end

.deepseekLLM::DeepSeek

Parameters:

  • key (String, nil)

    The secret key for authentication

Returns:



102
103
104
105
# File 'lib/llm.rb', line 102

def deepseek(**)
  lock(:require) { require_relative "llm/providers/deepseek" unless defined?(LLM::DeepSeek) }
  LLM::DeepSeek.new(**)
end

.File(obj) ⇒ LLM::File

Parameters:

  • obj (String, File, LLM::Response)

    The path to the file, or an existing file reference

Returns:



82
83
84
85
86
87
88
89
90
91
# File 'lib/llm/file.rb', line 82

def LLM.File(obj)
  case obj
  when File
    obj.close unless obj.closed?
    LLM.File(obj.path)
  when LLM::File, LLM::Response then obj
  when String then LLM::File.new(obj)
  else raise TypeError, "don't know how to handle #{obj.class} objects"
  end
end

.function(key, &b) ⇒ LLM::Function

Define a function

Examples:

LLM.function(:system) do |fn|
  fn.description "Run system command"
  fn.params do |schema|
    schema.object(command: schema.string.required)
  end
  fn.define do |command:|
    system(command)
  end
end

Parameters:

  • key (Symbol)

    The function name / key

  • b (Proc)

    The block to define the function

Returns:



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

def function(key, &b)
  LLM::Function.new(key, &b)
end

.geminiGemini

Returns a new instance of Gemini.

Parameters:

  • key (String, nil)

    The secret key for authentication

  • host (String)

    The host address of the LLM provider

  • port (Integer)

    The port number

  • timeout (Integer)

    The number of seconds to wait for a response

  • ssl (Boolean)

    Whether to use SSL for the connection

  • persistent (Boolean)

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

Returns:

  • (Gemini)

    a new instance of Gemini



78
79
80
81
# File 'lib/llm.rb', line 78

def gemini(**)
  lock(:require) { require_relative "llm/providers/gemini" unless defined?(LLM::Gemini) }
  LLM::Gemini.new(**)
end

.jsonClass

Returns the JSON adapter used by the library

Returns:

  • (Class)

    Returns a class that responds to +dump+ and +load+



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

def json
  @json ||= JSONAdapter::JSON
end

.json=(adapter)

Note:

This should be set once from the main thread when your program starts. Defaults to LLM::JSONAdapter::JSON.

This method returns an undefined value.

Sets the JSON adapter used by the library

Parameters:

  • adapter (Class, String, Symbol)

    A JSON adapter class or its name



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/llm.rb', line 51

def json=(adapter)
  @json = case adapter.to_s
  when "JSON", "json" then JSONAdapter::JSON
  when "Oj", "oj" then JSONAdapter::Oj
  when "Yajl", "yajl" then JSONAdapter::Yajl
  else
    is_class = Class === adapter
    is_subclass = is_class && adapter.ancestors.include?(LLM::JSONAdapter)
    if is_subclass
      adapter
    else
      raise TypeError, "Adapter must be a subclass of LLM::JSONAdapter"
    end
  end
end

.llamacpp(key: nil) ⇒ LLM::LlamaCpp

Parameters:

  • key (String, nil) (defaults to: nil)

    The secret key for authentication

Returns:



94
95
96
97
# File 'lib/llm.rb', line 94

def llamacpp(key: nil, **)
  lock(:require) { require_relative "llm/providers/llamacpp" unless defined?(LLM::LlamaCpp) }
  LLM::LlamaCpp.new(key:, **)
end

.lock(name)

This method returns an undefined value.

Provides a thread-safe lock

Parameters:

  • name (Symbol)

    The name of the lock

  • & (Proc)

    The block to execute within the lock



157
# File 'lib/llm.rb', line 157

def lock(name, &) = @monitors[name].synchronize(&)

.ollama(key: nil) ⇒ Ollama

Returns a new instance of Ollama.

Parameters:

  • key (String, nil) (defaults to: nil)

    The secret key for authentication

Returns:

  • (Ollama)

    a new instance of Ollama



86
87
88
89
# File 'lib/llm.rb', line 86

def ollama(key: nil, **)
  lock(:require) { require_relative "llm/providers/ollama" unless defined?(LLM::Ollama) }
  LLM::Ollama.new(key:, **)
end

.openaiOpenAI

Returns a new instance of OpenAI.

Parameters:

  • key (String, nil)

    The secret key for authentication

Returns:

  • (OpenAI)

    a new instance of OpenAI



110
111
112
113
# File 'lib/llm.rb', line 110

def openai(**)
  lock(:require) { require_relative "llm/providers/openai" unless defined?(LLM::OpenAI) }
  LLM::OpenAI.new(**)
end

.xaiXAI

Returns a new instance of XAI.

Parameters:

  • key (String, nil)

    The secret key for authentication

  • host (String)

    A regional host or the default ("api.x.ai")

Returns:

  • (XAI)

    a new instance of XAI



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

def xai(**)
  lock(:require) { require_relative "llm/providers/xai" unless defined?(LLM::XAI) }
  LLM::XAI.new(**)
end

.zaiZAI

Returns a new instance of ZAI.

Parameters:

  • key (String, nil)

    The secret key for authentication

  • host (String)

    A regional host or the default ("api.z.ai")

Returns:

  • (ZAI)

    a new instance of ZAI



128
129
130
131
# File 'lib/llm.rb', line 128

def zai(**)
  lock(:require) { require_relative "llm/providers/zai" unless defined?(LLM::ZAI) }
  LLM::ZAI.new(**)
end