mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-07-25 23:43:26 +00:00
Open AI support function calling, this has a very specific shape that other LLMs have not quite adopted. This simulates a command framework using system prompts on LLMs that are not open AI. Features include: - Smart system prompt to steer the LLM - Parameter validation (we ensure all the params are specified correctly) This is being tested on Anthropic at the moment and intial results are promising.
50 lines
1.3 KiB
Ruby
50 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module ::DiscourseAi
|
|
module Inference
|
|
class Function
|
|
attr_reader :name, :description, :parameters, :type
|
|
|
|
def initialize(name:, description:, type: nil)
|
|
@name = name
|
|
@description = description
|
|
@type = type || "object"
|
|
@parameters = []
|
|
end
|
|
|
|
def add_parameter(name:, type:, description:, enum: nil, required: false)
|
|
@parameters << {
|
|
name: name,
|
|
type: type,
|
|
description: description,
|
|
enum: enum,
|
|
required: required,
|
|
}
|
|
end
|
|
|
|
def to_json(*args)
|
|
as_json.to_json(*args)
|
|
end
|
|
|
|
def as_json
|
|
required_params = []
|
|
|
|
properties = {}
|
|
parameters.each do |parameter|
|
|
definition = { type: parameter[:type], description: parameter[:description] }
|
|
definition[:enum] = parameter[:enum] if parameter[:enum]
|
|
|
|
required_params << parameter[:name] if parameter[:required]
|
|
properties[parameter[:name]] = definition
|
|
end
|
|
|
|
params = { type: @type, properties: properties }
|
|
|
|
params[:required] = required_params if required_params.present?
|
|
|
|
{ name: name, description: description, parameters: params }
|
|
end
|
|
end
|
|
end
|
|
end
|