Skip to content

Commit dbb19b9

Browse files
committed
fix specs... a few more to go
1 parent ab8cfc7 commit dbb19b9

File tree

8 files changed

+55
-94
lines changed

8 files changed

+55
-94
lines changed

lib/completions/dialects/claude_tools.rb

+1-25
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,8 @@ def initialize(tools)
99
end
1010

1111
def translated_tools
12-
# Transform the raw tools into the required Anthropic Claude API format
1312
raw_tools.map do |t|
14-
properties = {}
15-
required = []
16-
17-
if t.parameters
18-
properties = {}
19-
20-
t.parameters.each do |param|
21-
mapped = { type: param.type, description: param.description }
22-
mapped[:items] = { type: param.item_type } if param.item_type
23-
mapped[:enum] = param.enum if param.enum
24-
properties[param.name] = mapped
25-
end
26-
required = t.parameters.select { |param| param.required }.map { |param| param.name }
27-
end
28-
29-
{
30-
name: t.name,
31-
description: t.description,
32-
input_schema: {
33-
type: "object",
34-
properties: properties,
35-
required: required,
36-
},
37-
}
13+
{ name: t.name, description: t.description, input_schema: t.parameters_json_schema }
3814
end
3915
end
4016

lib/completions/dialects/gemini.rb

+2-16
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,8 @@ def tools
4747

4848
translated_tools =
4949
prompt.tools.map do |t|
50-
tool = t.slice(:name, :description)
51-
52-
if t[:parameters]
53-
tool[:parameters] = t[:parameters].reduce(
54-
{ type: "object", required: [], properties: {} },
55-
) do |memo, p|
56-
name = p[:name]
57-
memo[:required] << name if p[:required]
58-
59-
memo[:properties][name] = p.except(:name, :required, :item_type)
60-
61-
memo[:properties][name][:items] = { type: p[:item_type] } if p[:item_type]
62-
memo
63-
end
64-
end
65-
50+
tool = { name: t.name, description: t.description }
51+
tool[:parameters] = t.parameters_json_schema if t.parameters
6652
tool
6753
end
6854

lib/completions/dialects/ollama_tools.rb

+9-17
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,15 @@ def instructions
1414
end
1515

1616
def translated_tools
17-
raw_tools.map do |t|
18-
tool = t.dup
19-
20-
tool[:parameters] = t[:parameters]
21-
.to_a
22-
.reduce({ type: "object", properties: {}, required: [] }) do |memo, p|
23-
name = p[:name]
24-
memo[:required] << name if p[:required]
25-
26-
except = %i[name required item_type]
27-
except << :enum if p[:enum].blank?
28-
29-
memo[:properties][name] = p.except(*except)
30-
memo
31-
end
32-
33-
{ type: "function", function: tool }
17+
raw_tools.map do |tool|
18+
{
19+
type: "function",
20+
function: {
21+
name: tool.name,
22+
description: tool.description,
23+
parameters: tool.parameters_json_schema,
24+
},
25+
}
3426
end
3527
end
3628

lib/completions/dialects/open_ai_tools.rb

+6-20
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,14 @@ def initialize(tools)
1010

1111
def translated_tools
1212
raw_tools.map do |tool|
13-
properties = {}
14-
required = []
15-
16-
result = {
17-
name: tool.name,
18-
description: tool.description,
19-
parameters: {
20-
type: "object",
21-
properties: properties,
22-
required: required,
13+
{
14+
type: "function",
15+
function: {
16+
name: tool.name,
17+
description: tool.description,
18+
parameters: tool.parameters_json_schema,
2319
},
2420
}
25-
26-
tool.parameters.each do |param|
27-
name = param.name
28-
required << name if param.required
29-
properties[name] = { type: param.type, description: param.description }
30-
properties[name][:items] = { type: param.item_type } if param.item_type
31-
properties[name][:enum] = param.enum if param.enum
32-
end
33-
34-
{ type: "function", function: result }
3521
end
3622
end
3723

lib/completions/tool_definition.rb

+17
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,23 @@ def to_h
9595
end
9696
end
9797

98+
def parameters_json_schema
99+
properties = {}
100+
required = []
101+
102+
result = { type: "object", properties: properties, required: required }
103+
104+
parameters.each do |param|
105+
name = param.name
106+
required << name if param.required
107+
properties[name] = { type: param.type, description: param.description }
108+
properties[name][:items] = { type: param.item_type } if param.item_type
109+
properties[name][:enum] = param.enum if param.enum
110+
end
111+
112+
result
113+
end
114+
98115
attr_reader :name, :description, :parameters
99116

100117
def self.from_hash(hash)

spec/lib/completions/dialects/chat_gpt_spec.rb

+11-7
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,21 @@
9999
it "returns a list of available tools" do
100100
open_ai_tool_f = {
101101
function: {
102-
description: context.tools.first[:description],
103-
name: context.tools.first[:name],
102+
description: context.tools.first.description,
103+
name: context.tools.first.name,
104104
parameters: {
105105
properties:
106-
context.tools.first[:parameters].reduce({}) do |memo, p|
107-
memo[p[:name]] = { description: p[:description], type: p[:type] }
106+
context
107+
.tools
108+
.first
109+
.parameters
110+
.reduce({}) do |memo, p|
111+
memo[p.name] = { description: p.description, type: p.type }
108112

109-
memo[p[:name]][:enum] = p[:enum] if p[:enum]
113+
memo[p.name][:enum] = p.enum if p.enum
110114

111-
memo
112-
end,
115+
memo
116+
end,
113117
required: %w[location unit],
114118
type: "object",
115119
},

spec/lib/completions/dialects/dialect_context.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ def tools
115115
},
116116
],
117117
},
118-
]
118+
].map { |tool| DiscourseAi::Completions::ToolDefinition.from_hash(tool) }
119119
end
120120
end

spec/lib/completions/tool_definition_spec.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@
9090
end
9191
end
9292

93-
context "with items specification" do
94-
it "only allows items for array type parameters" do
93+
context "with item_type specification" do
94+
it "only allows item_type for array type parameters" do
9595
expect {
9696
described_class.new(
9797
name: "colors",
9898
description: "List of colors",
9999
type: :array,
100-
items: :string,
100+
item_type: :string,
101101
)
102102
}.not_to raise_error
103103

@@ -106,9 +106,9 @@
106106
name: "color",
107107
description: "A single color",
108108
type: :string,
109-
items: :string,
109+
item_type: :string,
110110
)
111-
}.to raise_error(ArgumentError, /items can only be specified for array type/)
111+
}.to raise_error(ArgumentError, /item_type can only be specified for array type/)
112112
end
113113
end
114114
end
@@ -179,7 +179,7 @@
179179
name: "numbers",
180180
description: "List of numeric values",
181181
type: :array,
182-
items: :integer,
182+
item_type: :integer,
183183
)
184184

185185
described_class.new(
@@ -311,7 +311,7 @@
311311
name: "colors",
312312
description: "List of colors",
313313
type: :array,
314-
items: :string,
314+
item_type: :string,
315315
required: true,
316316
)
317317

@@ -331,7 +331,7 @@
331331
param_hash = hash[:parameters].first
332332
expect(param_hash[:name]).to eq("colors")
333333
expect(param_hash[:type]).to eq(:array)
334-
expect(param_hash[:items]).to eq(:string)
334+
expect(param_hash[:item_type]).to eq(:string)
335335
expect(param_hash[:required]).to eq(true)
336336
end
337337
end

0 commit comments

Comments
 (0)