-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathchat_with_tools
More file actions
executable file
·61 lines (50 loc) · 1.71 KB
/
chat_with_tools
File metadata and controls
executable file
·61 lines (50 loc) · 1.71 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
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env ruby
# frozen_string_literal: true
require "omniai/google"
client = OmniAI::Google::Client.new
# @example
# weather = WeatherTool.new
# weather.execute(lat: 43.7, lng: -79.42, unit: "Celsius") # => "20° Celsius at lat=43.7 lng=-79.4"
class WeatherTool < OmniAI::Tool
description "Lookup the weather for a lat / lng."
parameter :lat, :number, description: "The latitude of the location."
parameter :lng, :number, description: "The longitude of the location."
parameter :unit, :string, enum: %w[Celsius Fahrenheit], description: "The unit of measurement."
required %i[lat lng]
# @param lat [Float]
# @param lng [Float]
# @param unit [String] "Celsius" or "Fahrenheit"
#
# @return [String] e.g. "20° Celsius at lat=43.7 lng=-79.4"
def execute(lat:, lng:, unit: "Celsius")
puts "[weather] lat=#{lat} lng=#{lng} unit=#{unit}"
"#{rand(20..50)}° #{unit} at lat=#{lat} lng=#{lng}"
end
end
# @example
# geocode = GeocodeTool.new
# geocode.execute(location: "Toronto, Canada") #
class GeocodeTool < OmniAI::Tool
description "Lookup the latitude and longitude of a location."
parameter :location, :string, description: "The location to geocode."
required %i[location]
# @param location [String] "Toronto, Canada"
#
# @return [Hash] { lat: Float, lng: Float, location: String }
def execute(location:)
puts "[geocode] location=#{location}"
{
lat: rand(-90.0..+90.0),
lng: rand(-180.0..+180.0),
location:,
}
end
end
tools = [
WeatherTool.new,
GeocodeTool.new,
]
client.chat(stream: $stdout, tools:) do |prompt|
prompt.system "You are an expert in weather."
prompt.user 'What is the weather in "London" in Celsius and "Madrid" in Fahrenheit?'
end