Class: LLM::Function::Return

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

Overview

LLM::Function::Return represents the result of a tool call.

In llm.rb, tool execution is not complete until the requested function is answered with a return object and that return is sent back through the context. This is the object that closes that loop.

The return carries:

  • the tool call ID
  • the tool name
  • the tool's return value

That value is usually a Hash, but it can be any JSON-like structure your tool returns. LLM::Function#call produces one automatically, and LLM::Function#cancel produces one that represents a cancelled tool call.

You can also construct one directly when you need to intercept, scrub, or synthesize a tool return before sending it back to the model.

Examples:

Returning a normal tool result

ret = LLM::Function::Return.new("call_1", "weather", {forecast: "sunny"})
ctx.talk(ret)

Returning a tool result after rewriting its payload

value = ret.value.merge(email: "[REDACTED_EMAIL]")
ctx.talk(LLM::Function::Return.new(ret.id, ret.name, value))

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#id ⇒ Object

Returns the value of attribute id

Returns:

  • (Object) —

    the current value of id



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

def id
  @id
end

#name ⇒ Object

Returns the value of attribute name

Returns:

  • (Object) —

    the current value of name



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

def name
  @name
end

#value ⇒ Object

Returns the value of attribute value

Returns:

  • (Object) —

    the current value of value



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

def value
  @value
end

Instance Method Details

#error? ⇒ Boolean

Returns true when the return value represents an error.

Returns:

  • (Boolean)


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

def error?
  Hash === value and value[:error] == true
end

#to_h ⇒ Hash

Returns a Hash representation of LLM::Function::Return

Returns:

  • (Hash)


118
119
120
# File 'lib/llm/function.rb', line 118

def to_h
  {id:, name:, value:}
end

#to_json ⇒ String

Returns:

  • (String)


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

def to_json(...)
  LLM.json.dump(to_h, ...)
end

#interrupt! ⇒ nil Also known as: cancel!

Returns:

  • (nil)


130
131
132
# File 'lib/llm/function.rb', line 130

def interrupt!
  nil
end