module Raix::FunctionDispatch

def chat_completion(**chat_completion_args)

def chat_completion(**chat_completion_args)
  self.chat_completion_args = chat_completion_args
  super
end

def function(name, description = nil, **parameters, &block)

Parameters:
  • block (Proc) -- The block of code to execute when the function is called.
  • parameters (Hash) -- The parameters that the function accepts.
  • description (String) -- An optional description of the function.
  • name (Symbol) -- The name of the function.
def function(name, description = nil, **parameters, &block)
  @functions ||= []
  @functions << begin
    { name:, parameters: { type: "object", properties: {} } }.tap do |definition|
      definition[:description] = description if description.present?
      parameters.map do |key, value|
        definition[:parameters][:properties][key] = value
      end
    end
  end
  define_method(name) do |arguments, cache|
    id = SecureRandom.uuid[0, 23]
    content = if cache.present?
                cache.fetch([name, arguments]) do
                  instance_exec(arguments, &block)
                end
              else
                instance_exec(arguments, &block)
              end
    # add in one operation to prevent race condition and potential wrong
    # interleaving of tool calls in multi-threaded environments
    transcript << [
      {
        role: "assistant",
        content: nil,
        tool_calls: [
          {
            id:,
            type: "function",
            function: {
              name:,
              arguments: arguments.to_json
            }
          }
        ]
      },
      {
        role: "tool",
        tool_call_id: id,
        name:,
        content: content.to_s
      }
    ]
    chat_completion(**chat_completion_args) if loop
  end
end

def stop_looping!

that do not require a final text response to an end user.
Useful for manually halting processing in workflow components
Stops the looping of chat completion after function calls.
def stop_looping!
  self.loop = false
end

def tools

def tools
  return [] unless self.class.functions
  self.class.functions.map { |function| { type: "function", function: } }
end