Method: Langchain::Tool::Calculator#execute

Defined in:
lib/langchain/tool/calculator.rb

#execute(input:) ⇒ Langchain::Tool::Response

Evaluates a pure math expression or if equation contains non-math characters (e.g.: “12F in Celsius”) then it uses the google search calculator to evaluate the expression

Parameters:

  • input (String)

    math expression

Returns:

  • (Langchain::Tool::Response)

    Answer


30
31
32
33
34
35
36
37
# File 'lib/langchain/tool/calculator.rb', line 30

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

  result = Eqn::Calculator.calc(input)
  tool_response(content: result)
rescue Eqn::ParseError, Eqn::NoVariableValueError
  tool_response(content: "\"#{input}\" is an invalid mathematical expression")
end