Module: LLM

Defined in:
lib/llm.rb,
lib/llm/agent.rb,
lib/llm/error.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: Contract Classes: Agent, Anthropic, Buffer, Context, Cost, DeepSeek, Error, File, Function, Google, JSONAdapter, LlamaCpp, MCP, Message, Model, Object, Ollama, OpenAI, Prompt, Provider, Registry, Response, Schema, ServerTool, 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.13.0"

Class Method Summary collapse

Class Method Details

.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

.registry_for(llm) ⇒ LLM::Object

Parameters:

  • llm (Symbol, LLM::Provider)

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

Returns:



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

def self.registry_for(llm)
  lock(:registry) do
    name = Symbol === llm ? llm : llm.name
    @registry[name] ||= Registry.for(name)
  end
end

.jsonClass

Returns the JSON adapter used by the library

Returns:

  • (Class)

    Returns a class that responds to dump and load



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

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



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

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

.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



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

def anthropic(**)
  lock(:require) { require_relative "llm/providers/anthropic" unless defined?(LLM::Anthropic) }
  LLM::Anthropic.new(**)
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

  • persistent (Boolean)

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

Returns:

  • (Google)

    a new instance of Google



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

def google(**)
  lock(:require) { require_relative "llm/providers/google" unless defined?(LLM::Google) }
  LLM::Google.new(**)
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



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

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

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

Parameters:

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

    The secret key for authentication

Returns:



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

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

.deepseekLLM::DeepSeek

Parameters:

  • key (String, nil)

    The secret key for authentication

Returns:



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

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

.openaiOpenAI

Returns a new instance of OpenAI.

Parameters:

  • key (String, nil)

    The secret key for authentication

Returns:

  • (OpenAI)

    a new instance of OpenAI



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

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



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

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



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

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

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



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

def mcp(llm = nil, **)
  LLM::MCP.new(llm, **)
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:



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

def function(key, &b)
  LLM::Function.new(key, &b)
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



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

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

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



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

def self.clients = @clients