Sam 2c6459429f
DEV: use a proper object for tool definition (#1337)
* DEV: use a proper object for tool definition

This moves away from using a loose hash to define tools, which
is error prone.

Instead given a proper object we will also be able to coerce the
return values to match tool definition correctly

* fix xml tools

* fix anthropic tools

* fix specs... a few more to go

* specs are passing

* FIX: coerce values for XML tool calls

* Update spec/lib/completions/tool_definition_spec.rb

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-15 17:32:39 +10:00

51 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module DiscourseAi
module Completions
module Dialects
# TODO: Define the Tool class to be inherited by all tools.
class OllamaTools
def initialize(tools)
@raw_tools = tools
end
def instructions
"" # Noop. Tools are listed separate.
end
def translated_tools
raw_tools.map do |tool|
{
type: "function",
function: {
name: tool.name,
description: tool.description,
parameters: tool.parameters_json_schema,
},
}
end
end
def from_raw_tool_call(raw_message)
call_details = JSON.parse(raw_message[:content], symbolize_names: true)
call_details[:name] = raw_message[:name]
{
role: "assistant",
content: nil,
tool_calls: [{ type: "function", function: call_details }],
}
end
def from_raw_tool(raw_message)
{ role: "tool", content: raw_message[:content], name: raw_message[:name] }
end
private
attr_reader :raw_tools
end
end
end
end