Class: RLM::Runtime::Bridge

Inherits:
Object
  • Object
show all
Defined in:
lib/rlm/runtime/bridge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, trace:, runtime: nil, tools: [], signatures: {}, depth: 0) ⇒ Bridge

Returns a new instance of Bridge.



12
13
14
15
16
17
18
19
20
# File 'lib/rlm/runtime/bridge.rb', line 12

def initialize(context:, trace:, runtime: nil, tools: [], signatures: {}, depth: 0)
  @runtime = runtime
  @context = context
  @trace = trace
  @tools = Array(tools)
  @signatures = signatures
  @depth = depth
  @submitted_output = nil
end

Instance Attribute Details

#submitted_outputObject (readonly)

Returns the value of attribute submitted_output.



10
11
12
# File 'lib/rlm/runtime/bridge.rb', line 10

def 
  @submitted_output
end

Instance Method Details

#list_filesObject



68
69
70
# File 'lib/rlm/runtime/bridge.rb', line 68

def list_files
  context.manifest[:files]
end

#log(message) ⇒ Object

Raises:



72
73
74
75
76
77
# File 'lib/rlm/runtime/bridge.rb', line 72

def log(message)
  raise ValidationError, "log message must be a String" unless message.is_a?(String)

  trace.record(:runtime_logged, message: message)
  nil
end

#predict(signature_name, input_hash) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rlm/runtime/bridge.rb', line 22

def predict(signature_name, input_hash)
  input = ensure_json_value!(input_hash, "predict input")
  signature = find_signature(signature_name)
  raise ValidationError, "Unknown signature: #{signature_name}" if signature.nil?

  validate_signature_input!(signature, input)
  unless runtime.respond_to?(:predict_subcall)
    raise ValidationError, "runtime does not support recursive predict calls"
  end

  runtime.predict_subcall(signature, input, depth: depth + 1)
end

#read_file(handle) ⇒ Object

Raises:



57
58
59
60
61
62
63
64
65
66
# File 'lib/rlm/runtime/bridge.rb', line 57

def read_file(handle)
  raise ValidationError, "file handle must be a String" unless handle.is_a?(String)

  file = context.file_for(handle)
  raise ValidationError, "Unknown file handle: #{handle}" if file.nil?

  content = file.read
  trace.record(:file_read, handle: handle, filename: file.filename, size_bytes: file.size_bytes)
  content
end

#submit(output_hash) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/rlm/runtime/bridge.rb', line 49

def submit(output_hash)
  output = ensure_json_value!(output_hash, "submitted output")
  @submitted_output = output
  runtime.(output) if runtime.respond_to?(:record_submitted_output)
  trace.record(:output_submitted, output: output)
  output
end

#tool(tool_name, input_hash) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rlm/runtime/bridge.rb', line 35

def tool(tool_name, input_hash)
  runtime.record_tool_attempt! if runtime.respond_to?(:record_tool_attempt!)
  input = ensure_json_value!(input_hash, "tool input")
  tool = find_tool(tool_name)
  raise ToolError, "Unknown tool: #{tool_name}" if tool.nil?
  raise ToolError, "Tool is not read-only: #{tool_name}" unless tool_class(tool).category == :read_only

  instance = tool_instance(tool)
  output = instance.call(**symbolize_keys(input))
  ensure_json_value!(output, "tool output")
  trace.record(:tool_called, tool: tool_class(tool).registry_name, input: input)
  output
end