Class: LLM::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/agent.rb

Overview

LLM::Agent provides a class-level DSL for defining reusable, preconfigured assistants with defaults for model, tools, schema, and instructions.

Notes:

  • Instructions are injected only on the first request.
  • An agent will automatically execute tool calls (unlike LLM::Session).
  • The idea originally came from RubyLLM and was adapted to llm.rb.

Examples:

class SystemAdmin < LLM::Agent
  model "gpt-4.1-nano"
  instructions "You are a Linux system admin"
  tools Shell
  schema Result
end

llm = LLM.openai(key: ENV["KEY"])
agent = SystemAdmin.new(llm)
agent.talk("Run 'date'")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(llm, params = {}) ⇒ Agent

Returns a new instance of Agent.

Parameters:

  • provider (LLM::Provider)

    A provider

  • params (Hash) (defaults to: {})

    The parameters to maintain throughout the conversation. Any parameter the provider supports can be included and not only those listed here.

Options Hash (params):

  • :model (String)

    Defaults to the provider's default model

  • :tools (Array<LLM::Function>, nil)

    Defaults to nil

  • :schema (#to_json, nil)

    Defaults to nil



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

def initialize(llm, params = {})
  defaults = {model: self.class.model, tools: self.class.tools, schema: self.class.schema}.compact
  @llm = llm
  @ses = LLM::Session.new(llm, defaults.merge(params))
end

Instance Attribute Details

#llmLLM::Provider (readonly)

Returns a provider

Returns:



29
30
31
# File 'lib/llm/agent.rb', line 29

def llm
  @llm
end

Class Method Details

.tools(*tools) ⇒ Array<LLM::Function>

Set or get the default tools

Parameters:

Returns:

  • (Array<LLM::Function>)

    Returns the current tools when no argument is provided



48
49
50
51
# File 'lib/llm/agent.rb', line 48

def self.tools(*tools)
  return @tools || [] if tools.empty?
  @tools = tools.flatten
end

.schema(schema = nil) ⇒ #to_json?

Set or get the default schema

Parameters:

  • schema (#to_json, nil) (defaults to: nil)

    The schema

Returns:

  • (#to_json, nil)

    Returns the current schema when no argument is provided



59
60
61
62
# File 'lib/llm/agent.rb', line 59

def self.schema(schema = nil)
  return @schema if schema.nil?
  @schema = schema
end

.instructions(instructions = nil) ⇒ String?

Set or get the default instructions

Parameters:

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

    The system instructions

Returns:

  • (String, nil)

    Returns the current instructions when no argument is provided



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

def self.instructions(instructions = nil)
  return @instructions if instructions.nil?
  @instructions = instructions
end

.model(model = nil) ⇒ String?

Set or get the default model

Parameters:

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

    The model identifier

Returns:

  • (String, nil)

    Returns the current model when no argument is provided



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

def self.model(model = nil)
  return @model if model.nil?
  @model = model
end

Instance Method Details

#deserialize(**kw) ⇒ LLM::Session Also known as: restore

Parameters:

  • path (String, nil)

    The path to a JSON file

  • string (String, nil)

    A raw JSON string

Returns:



220
221
222
# File 'lib/llm/agent.rb', line 220

def deserialize(**kw)
  @ses.deserialize(**kw)
end

#talk(prompt, params = {}) ⇒ LLM::Response Also known as: chat

Maintain a conversation via the chat completions API. This method immediately sends a request to the LLM and returns the response.

Examples:

llm = LLM.openai(key: ENV["KEY"])
agent = LLM::Agent.new(llm)
response = agent.talk("Hello, what is your name?")
puts response.choices[0].content

Parameters:

  • params (Hash) (defaults to: {})

    The params passed to the provider, including optional :stream, :tools, :schema etc.

  • prompt (String)

    The input prompt to be completed

Options Hash (params):

  • :max_tool_rounds (Integer)

    The maxinum number of tool call iterations (default 10)

Returns:



104
105
106
107
108
109
110
111
112
113
# File 'lib/llm/agent.rb', line 104

def talk(prompt, params = {})
  i, max = 0, Integer(params.delete(:max_tool_rounds) || 10)
  res = @ses.talk(apply_instructions(prompt), params)
  until @ses.functions.empty?
    raise LLM::ToolLoopError, "pending tool calls remain" if i >= max
    res = @ses.talk @ses.functions.map(&:call), params
    i += 1
  end
  res
end

#respond(prompt, params = {}) ⇒ LLM::Response

Note:

Not all LLM providers support this API

Maintain a conversation via the responses API. This method immediately sends a request to the LLM and returns the response.

Examples:

llm = LLM.openai(key: ENV["KEY"])
agent = LLM::Agent.new(llm)
res = agent.respond("What is the capital of France?")
puts res.output_text

Parameters:

  • params (Hash) (defaults to: {})

    The params passed to the provider, including optional :stream, :tools, :schema etc.

  • prompt (String)

    The input prompt to be completed

Options Hash (params):

  • :max_tool_rounds (Integer)

    The maxinum number of tool call iterations (default 10)

Returns:



130
131
132
133
134
135
136
137
138
139
# File 'lib/llm/agent.rb', line 130

def respond(prompt, params = {})
  i, max = 0, Integer(params.delete(:max_tool_rounds) || 10)
  res = @ses.respond(apply_instructions(prompt), params)
  until @ses.functions.empty?
    raise LLM::ToolLoopError, "pending tool calls remain" if i >= max
    res = @ses.respond @ses.functions.map(&:call), params
    i += 1
  end
  res
end

#messagesLLM::Buffer<LLM::Message>



143
144
145
# File 'lib/llm/agent.rb', line 143

def messages
  @ses.messages
end

#functionsArray<LLM::Function>

Returns:



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

def functions
  @ses.functions
end

#usageLLM::Object

Returns:



155
156
157
# File 'lib/llm/agent.rb', line 155

def usage
  @ses.usage
end

#prompt(&b) ⇒ LLM::Prompt Also known as: build_prompt

Parameters:

  • b (Proc)

    A block that composes messages. If it takes one argument, it receives the prompt object. Otherwise it runs in prompt context.

Returns:

See Also:



163
164
165
# File 'lib/llm/agent.rb', line 163

def prompt(&b)
  @ses.prompt(&b)
end

#image_url(url) ⇒ LLM::Object

Returns a tagged object

Parameters:

  • url (String)

    The URL

Returns:



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

def image_url(url)
  @ses.image_url(url)
end

#local_file(path) ⇒ LLM::Object

Returns a tagged object

Parameters:

  • path (String)

    The path

Returns:



182
183
184
# File 'lib/llm/agent.rb', line 182

def local_file(path)
  @ses.local_file(path)
end

#remote_file(res) ⇒ LLM::Object

Returns a tagged object

Parameters:

Returns:



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

def remote_file(res)
  @ses.remote_file(res)
end

#tracerLLM::Tracer

Returns an LLM tracer

Returns:



198
199
200
# File 'lib/llm/agent.rb', line 198

def tracer
  @ses.tracer
end

#modelString

Returns the model an Agent is actively using

Returns:

  • (String)


205
206
207
# File 'lib/llm/agent.rb', line 205

def model
  @ses.model
end

#serialize(**kw) ⇒ void Also known as: save

This method returns an undefined value.



212
213
214
# File 'lib/llm/agent.rb', line 212

def serialize(**kw)
  @ses.serialize(**kw)
end