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



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



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



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


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

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

.geminiGemini



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



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



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



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



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

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

.ollama(key: nil) ⇒ 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



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



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



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