Class: Langchain::Tool::RubyCodeInterpreter

Inherits:
Object
  • Object
show all
Extended by:
Langchain::ToolDefinition
Includes:
DependencyHelper
Defined in:
lib/langchain/tool/ruby_code_interpreter.rb

Overview

A tool that execute Ruby code in a sandboxed environment.

Gem requirements:

gem "safe_ruby", "~> 1.0.4"

Usage:

interpreter = Langchain::Tool::RubyCodeInterpreter.new

Instance Method Summary collapse

Methods included from Langchain::ToolDefinition

define_function, function_schemas, tool_name

Methods included from DependencyHelper

#depends_on

Constructor Details

#initialize(timeout: 30) ⇒ RubyCodeInterpreter

Returns a new instance of RubyCodeInterpreter.



21
22
23
24
25
# File 'lib/langchain/tool/ruby_code_interpreter.rb', line 21

def initialize(timeout: 30)
  depends_on "safe_ruby"

  @timeout = timeout
end

Instance Method Details

#execute(input:) ⇒ String

Executes Ruby code in a sandboxes environment.

Parameters:

  • input (String)

    ruby code expression

Returns:

  • (String)

    Answer



31
32
33
34
35
# File 'lib/langchain/tool/ruby_code_interpreter.rb', line 31

def execute(input:)
  Langchain.logger.debug("#{self.class} - Executing \"#{input}\"")

  safe_eval(input)
end

#safe_eval(code) ⇒ Object



37
38
39
# File 'lib/langchain/tool/ruby_code_interpreter.rb', line 37

def safe_eval(code)
  SafeRuby.eval(code, timeout: @timeout)
end