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

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



72
73
74
# File 'lib/llm/function.rb', line 72

def id
  @id
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



72
73
74
# File 'lib/llm/function.rb', line 72

def name
  @name
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



72
73
74
# File 'lib/llm/function.rb', line 72

def value
  @value
end

Instance Method Details

#error?Boolean

Returns true when the return value represents an error.

Returns:

  • (Boolean)


76
77
78
# File 'lib/llm/function.rb', line 76

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

#interrupt!nil Also known as: cancel!

Returns:

  • (nil)


95
96
97
# File 'lib/llm/function.rb', line 95

def interrupt!
  nil
end

#to_hHash

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

Returns:

  • (Hash)


83
84
85
# File 'lib/llm/function.rb', line 83

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

#to_jsonString

Returns:

  • (String)


89
90
91
# File 'lib/llm/function.rb', line 89

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