openai-toolable 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +25 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +9 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/examples/simple.rb +33 -0
- data/lib/openai/toolable/parameter.rb +14 -0
- data/lib/openai/toolable/tool_handler.rb +25 -0
- data/lib/openai/toolable/version.rb +7 -0
- data/lib/openai/toolable.rb +46 -0
- data/sig/openai/toolable.rbs +6 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '0087364577f6cf8ca019db6c85e945f22268215cfeec1645d2057f65e521f2e6'
|
4
|
+
data.tar.gz: 85d1280a72de872827da87f64269310537a8673b7e85d950732dd60db527dd46
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6d03ff67dd8d3da0a2cfb0110ce25143c975cd4468704e432d7fab474887a5e4da43764ed28f9575a05c2d7174d4ed3f89afaf97eee640d259b4e8bbf57fd24b
|
7
|
+
data.tar.gz: 4f79f0fde24df17c8c76e86e9cba087ba81700f551c7d0f67f8a05c1656303f1550f6666fa864fbf6fff5b1361e4bf3dc9fb41d5f47e083ea8431a0bd937a04a
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
openai-toolable (0.1.0)
|
5
|
+
openai (~> 0.16.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
connection_pool (2.5.3)
|
11
|
+
minitest (5.25.5)
|
12
|
+
openai (0.16.0)
|
13
|
+
connection_pool
|
14
|
+
rake (13.3.0)
|
15
|
+
|
16
|
+
PLATFORMS
|
17
|
+
arm64-darwin-23
|
18
|
+
|
19
|
+
DEPENDENCIES
|
20
|
+
minitest (~> 5.0)
|
21
|
+
openai-toolable!
|
22
|
+
rake (~> 13.0)
|
23
|
+
|
24
|
+
BUNDLED WITH
|
25
|
+
2.4.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Van Curen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# OpenAI Toolable
|
2
|
+
|
3
|
+
`openai-toolable` is a Ruby gem that extends the official `openai` gem to provide a simple and flexible way to use tools with the OpenAI API. It allows you to define tools, including their names, descriptions, and parameters, and then easily integrate them into your chat completion requests.
|
4
|
+
|
5
|
+
This gem is built on top of the `openai-ruby` gem and is designed to be a lightweight and intuitive extension for developers who want to leverage function calling in their Ruby applications.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'openai-toolable'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install openai-toolable
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Here's a simple example of how to use `openai-toolable` to define a tool and use it with the OpenAI API:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require "openai/toolable"
|
29
|
+
|
30
|
+
# Define a tool
|
31
|
+
weather_tool = Openai::Toolable::ToolFactory.build(
|
32
|
+
name: "get_weather",
|
33
|
+
description: "Get the current weather in a given location",
|
34
|
+
parameters: [
|
35
|
+
{ name: "location", type: :string, description: "The city and state, e.g. San Francisco, CA" },
|
36
|
+
{ name: "unit", type: :string, description: "The unit of temperature, e.g. celsius or fahrenheit" }
|
37
|
+
]
|
38
|
+
)
|
39
|
+
|
40
|
+
# Create a tool handler and register the tool
|
41
|
+
tool_handler = Openai::Toolable::ToolHandler.new
|
42
|
+
tool_handler.register(
|
43
|
+
name: "get_weather",
|
44
|
+
lambda: ->(location:, unit:) { puts "Getting the weather in #{location} (#{unit})..." }
|
45
|
+
)
|
46
|
+
|
47
|
+
# Create a client
|
48
|
+
client = OpenAI::Client.new
|
49
|
+
|
50
|
+
# Create a chat completion
|
51
|
+
response = client.chat(
|
52
|
+
parameters: {
|
53
|
+
model: "gpt-3.5-turbo",
|
54
|
+
messages: [{ role: "user", content: "What's the weather like in Boston?" }],
|
55
|
+
tools: [weather_tool.to_json]
|
56
|
+
}
|
57
|
+
)
|
58
|
+
|
59
|
+
# Handle the response
|
60
|
+
tool_handler.handle(response: response)
|
61
|
+
```
|
62
|
+
|
63
|
+
## Development
|
64
|
+
|
65
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
66
|
+
|
67
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/vancuren/openai-toolable.
|
72
|
+
|
73
|
+
## Acknowledgments
|
74
|
+
|
75
|
+
This gem is an extension of the official `openai-ruby` gem. We are grateful to the maintainers of the `openai-ruby` for their excellent work.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "openai/toolable"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/examples/simple.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "openai/toolable"
|
2
|
+
|
3
|
+
# Define a tool
|
4
|
+
weather_tool = Openai::Toolable::ToolFactory.build(
|
5
|
+
name: "get_weather",
|
6
|
+
description: "Get the current weather in a given location",
|
7
|
+
parameters: [
|
8
|
+
{ name: "location", type: :string, description: "The city and state, e.g. San Francisco, CA" },
|
9
|
+
{ name: "unit", type: :string, description: "The unit of temperature, e.g. celsius or fahrenheit" }
|
10
|
+
]
|
11
|
+
)
|
12
|
+
|
13
|
+
# Create a tool handler and register the tool
|
14
|
+
tool_handler = Openai::Toolable::ToolHandler.new
|
15
|
+
tool_handler.register(
|
16
|
+
name: "get_weather",
|
17
|
+
lambda: ->(location:, unit:) { puts "Getting the weather in #{location} (#{unit})..." }
|
18
|
+
)
|
19
|
+
|
20
|
+
# Create a client
|
21
|
+
client = OpenAI::Client.new
|
22
|
+
|
23
|
+
# Create a chat completion
|
24
|
+
response = client.chat(
|
25
|
+
parameters: {
|
26
|
+
model: "gpt-3.5-turbo",
|
27
|
+
messages: [{ role: "user", content: "What's the weather like in Boston?" }],
|
28
|
+
tools: [weather_tool.to_json]
|
29
|
+
}
|
30
|
+
)
|
31
|
+
|
32
|
+
# Handle the response
|
33
|
+
tool_handler.handle(response: response)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Openai
|
2
|
+
module Toolable
|
3
|
+
class Parameter
|
4
|
+
attr_reader :name, :type, :description, :required
|
5
|
+
|
6
|
+
def initialize(name:, type:, description:, required: false)
|
7
|
+
@name = name
|
8
|
+
@type = type
|
9
|
+
@description = description
|
10
|
+
@required = required
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
module Openai
|
4
|
+
module Toolable
|
5
|
+
class ToolHandler
|
6
|
+
def initialize
|
7
|
+
@tools = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def register(name:, lambda:)
|
11
|
+
@tools[name] = lambda
|
12
|
+
end
|
13
|
+
|
14
|
+
def handle(response:)
|
15
|
+
tool_call = response.dig("choices", 0, "message", "tool_calls", 0)
|
16
|
+
return unless tool_call
|
17
|
+
|
18
|
+
function_name = tool_call.dig("function", "name")
|
19
|
+
arguments = JSON.parse(tool_call.dig("function", "arguments"))
|
20
|
+
|
21
|
+
@tools[function_name]&.call(**arguments.transform_keys(&:to_sym))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative "toolable/version"
|
2
|
+
require_relative "toolable/parameter"
|
3
|
+
require_relative "toolable/tool_handler"
|
4
|
+
|
5
|
+
module Openai
|
6
|
+
module Toolable
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
class Tool
|
10
|
+
attr_reader :name, :description, :parameters
|
11
|
+
|
12
|
+
def initialize(name:, description:, parameters: [])
|
13
|
+
@name = name
|
14
|
+
@description = description
|
15
|
+
@parameters = parameters.map do |param|
|
16
|
+
Parameter.new(
|
17
|
+
name: param[:name],
|
18
|
+
type: param[:type],
|
19
|
+
description: param[:description],
|
20
|
+
required: param.fetch(:required, false)
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_json
|
26
|
+
{
|
27
|
+
name: name,
|
28
|
+
description: description,
|
29
|
+
parameters: {
|
30
|
+
type: :object,
|
31
|
+
properties: parameters.each_with_object({}) do |param, hash|
|
32
|
+
hash[param.name] = { type: param.type, description: param.description }
|
33
|
+
end,
|
34
|
+
required: parameters.select(&:required).map(&:name)
|
35
|
+
}
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class ToolFactory
|
41
|
+
def self.build(name:, description:, parameters: [])
|
42
|
+
Tool.new(name: name, description: description, parameters: parameters)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openai-toolable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Russell Van Curen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-08-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: openai
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.16.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.16.0
|
27
|
+
description: This gem provides a simple and flexible way to define and use tools with
|
28
|
+
the OpenAI API.
|
29
|
+
email:
|
30
|
+
- russell@vancuren.net
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- Gemfile
|
36
|
+
- Gemfile.lock
|
37
|
+
- LICENSE.txt
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- bin/console
|
41
|
+
- bin/setup
|
42
|
+
- examples/simple.rb
|
43
|
+
- lib/openai/toolable.rb
|
44
|
+
- lib/openai/toolable/parameter.rb
|
45
|
+
- lib/openai/toolable/tool_handler.rb
|
46
|
+
- lib/openai/toolable/version.rb
|
47
|
+
- sig/openai/toolable.rbs
|
48
|
+
homepage: https://github.com/vancuren/openai-toolable
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata:
|
52
|
+
allowed_push_host: https://rubygems.org
|
53
|
+
homepage_uri: https://github.com/vancuren/openai-toolable
|
54
|
+
source_code_uri: https://github.com/vancuren/openai-toolable
|
55
|
+
changelog_uri: https://github.com/vancuren/openai-toolable/blob/main/CHANGELOG.md
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 3.2.0
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubygems_version: 3.4.1
|
72
|
+
signing_key:
|
73
|
+
specification_version: 4
|
74
|
+
summary: A Ruby gem that extends the OpenAI SDK to support function calling.
|
75
|
+
test_files: []
|