-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.rb
More file actions
50 lines (42 loc) · 1.58 KB
/
Copy pathcalculator.rb
File metadata and controls
50 lines (42 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true
# A tiny end-to-end demo: a calculator agent with two tools.
#
# OPENAI_API_KEY=sk-... ruby examples/calculator.rb
# # or, on a host with no local Ruby:
# script/rb ruby examples/calculator.rb
#
# The agent decides when to call `add` / `multiply`, Truffle runs them, feeds the
# results back, and the model produces the final answer. Every step is printed
# through the event API so you can watch the loop work.
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "truffle"
add = Truffle::Tool.define("add", "Add two integers") do
param :a, :integer, "first addend", required: true
param :b, :integer, "second addend", required: true
run { |a:, b:| a + b }
end
multiply = Truffle::Tool.define("multiply", "Multiply two integers") do
param :a, :integer, "first factor", required: true
param :b, :integer, "second factor", required: true
run { |a:, b:| a * b }
end
agent = Truffle.agent(
provider: :openai,
model: ENV.fetch("TRUFFLE_MODEL", "gpt-5.4-mini"),
system_prompt: "You are a precise calculator. Use the tools for every " \
"arithmetic step. Show the final result clearly.",
tools: [add, multiply]
)
agent.on(:tool_call) do |e|
puts " -> calling #{e[:call].name}(#{e[:call].arguments.map do |k, v|
"#{k}=#{v}"
end.join(", ")})"
end
agent.on(:tool_result) { |e| puts " <- #{e[:call].name} returned #{e[:result]}" }
question = ARGV.join(" ")
question = "What is (12 + 8) multiplied by 7?" if question.empty?
puts "Q: #{question}"
puts "-" * 60
answer = agent.run(question)
puts "-" * 60
puts "A: #{answer}"