Module: LLM

Defined in:
lib/llm.rb,
lib/llm/agent.rb,
lib/llm/error.rb,
lib/llm/skill.rb,
lib/llm/buffer.rb,
lib/llm/stream.rb,
lib/llm/tracer.rb,
lib/llm/context.rb,
lib/llm/message.rb,
lib/llm/session.rb,
lib/llm/version.rb,
lib/llm/contract.rb,
lib/llm/response.rb,
lib/llm/tracer/null.rb,
lib/llm/eventhandler.rb,
lib/llm/json_adapter.rb,
lib/llm/providers/xai.rb,
lib/llm/providers/zai.rb,
lib/llm/tracer/logger.rb,
lib/llm/providers/google.rb,
lib/llm/providers/ollama.rb,
lib/llm/providers/openai.rb,
lib/llm/tracer/langsmith.rb,
lib/llm/tracer/telemetry.rb,
lib/llm/providers/deepseek.rb,
lib/llm/providers/llamacpp.rb,
lib/llm/providers/anthropic.rb

Defined Under Namespace

Modules: ActiveRecord, Contract, EventStream, Sequel, Utils Classes: Agent, Anthropic, Buffer, Context, Cost, DeepSeek, Error, EventHandler, File, Function, Google, JSONAdapter, LlamaCpp, MCP, Message, Mime, Model, Multipart, Object, Ollama, OpenAI, Prompt, Provider, Registry, Response, Schema, ServerTool, Skill, Stream, Tool, Tracer, 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)
Interrupt =

When a request is interrupted

Class.new(Error)
NoSuchToolError =

When a tool call cannot be mapped to a local tool

Class.new(Error)
NoSuchModelError =

When Registry can’t map a model

Class.new(Error)
NoSuchRegistryError =

When Registry can’t map a registry

Class.new(Error)
Bot =

Backward-compatible alias

Context
Session =
Deprecated.

Use Context instead. Scheduled for removal in v6.0.

Backward-compatible alias for LLM::Context

Context
VERSION =
"4.21.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

  • base_path (String)

    Optional base path prefix for HTTP API routes.

  • persistent (Boolean)

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

Returns:

  • (Anthropic)

    a new instance of Anthropic



100
101
102
103
# File 'lib/llm.rb', line 100

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

.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.



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

def self.clients = @clients

.deepseekLLM::DeepSeek

Parameters:

  • key (String, nil)

    The secret key for authentication

Returns:



132
133
134
135
# File 'lib/llm.rb', line 132

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:



193
194
195
# File 'lib/llm.rb', line 193

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

.googleGoogle

Returns a new instance of Google.

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

  • base_path (String)

    Optional base path prefix for HTTP API routes.

  • persistent (Boolean)

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

Returns:

  • (Google)

    a new instance of Google



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

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

.jsonClass

Returns the JSON adapter used by the library

Returns:

  • (Class)

    Returns a class that responds to ‘dump` and `load`



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

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

.json=(adapter) ⇒ void

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/llm.rb', line 81

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:



124
125
126
127
# File 'lib/llm.rb', line 124

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

.lock(name) ⇒ void

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



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

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

.mcp(llm = nil) ⇒ LLM::MCP

Parameters:

  • llm (LLM::Provider, nil) (defaults to: nil)

    The provider to use for MCP transports that need one

  • stdio (Hash, nil)

Returns:



174
175
176
# File 'lib/llm.rb', line 174

def mcp(llm = nil, **)
  LLM::MCP.new(llm, **)
end

.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



116
117
118
119
# File 'lib/llm.rb', line 116

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



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

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

.registry_for(llm) ⇒ LLM::Object

Parameters:

  • llm (Symbol, LLM::Provider)

    The name of a provider, or an instance of LLM::Provider

Returns:



56
57
58
59
60
61
# File 'lib/llm.rb', line 56

def self.registry_for(llm)
  lock(:registry) do
    name = Symbol === llm ? llm : llm.name
    @registry[name] ||= Registry.for(name)
  end
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



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

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



158
159
160
161
# File 'lib/llm.rb', line 158

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