Sha256: 1750f673abe24073c255fa3af8f19b0d7b99d155a8731fa40be7d4b3d060fd8c

Contents?: true

Size: 964 Bytes

Versions: 2

Compression:

Stored size: 964 Bytes

Contents

# frozen_string_literal: true

module Bristow
  class Function
    attr_reader :name, :description, :parameters

    def initialize(name:, description:, parameters:, &block)
      raise ArgumentError, "block is required" unless block_given?
      @name = name
      @description = description
      @parameters = parameters
      @block = block

      if !parameters.has_key?(:type)
        parameters[:type] = "object"
      end
    end

    def call(**kwargs)
      validate_required_parameters!(kwargs)
      @block.call(**kwargs)
    end

    def to_openai_schema
      {
        name: name,
        description: description,
        parameters: parameters
      }
    end

    private

    def validate_required_parameters!(kwargs)
      required_params = parameters.dig(:required) || []
      missing = required_params.map(&:to_sym) - kwargs.keys.map(&:to_sym)
      raise ArgumentError, "missing keyword: #{missing.first}" if missing.any?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bristow-0.2.1 lib/bristow/function.rb
bristow-0.2.0 lib/bristow/function.rb