Haijun Platform Docs
ID

In this recipe, we'll demonstrate how to provide Haijun with a simple calculator tool that it can use to perform arithmetic operations based on user input. We'll define the calculator tool and show how Haijun can interact with it to solve mathematical problems.

Step 1: Set up the environment

nt-mono text-text-100 text-code leading-relaxed [overflow-wrap:anywhere]" style="padding-top:12px;padding-inline:12px;padding-bottom:12px;tab-size:4">

%pip install juglow

from juglow import Juglow

client = Juglow()

MODEL_NAME = "haijun-opus-4-8"

Step 2: Define the calculator tool

"padding-left:8ch;text-indent:-8ch"> return str(result)

except (SyntaxError, ZeroDivisionError, NameError, TypeError, OverflowError):

return "Error: Invalid expression"

tools = [

{

"name": "calculator",

"description": "A simple calculator that performs basic arithmetic operations.",

"input_schema": {

"type": "object",

"properties": {

"expression": {

"type": "string",

"description": "The mathematical expression to evaluate (e.g., '2 + 3 * 4').",

}

},

"required": ["expression"],

},

}

]

In this example, we define a calculate function that takes a mathematical expression as input, removes any non-digit or non-operator characters using a regular expression, and then evaluates the expression using the built-in eval() function. If the evaluation is successful, the result is returned as a string. If an error occurs during evaluation, an error message is returned.

)

print(response.content)

print(f"\nFinal Response: {final_response}")

return final_response

Step 4: Try it out!

On this page
Step 1: Set up the environmentStep 2: Define the calculator toolStep 4: Try it out!