Class: OpenAI::Helpers::Chat::ToolRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/helpers/chat/tool_runner.rb,
sig/openai/helpers/chat/tool_runner.rbs

Overview

Executes a bounded, non-streaming Chat Completions function-tool loop.

Defined Under Namespace

Classes: Error, MaxRoundsError

Instance Method Summary collapse

Constructor Details

#initialize(completions:, params:, tool_handlers:, max_rounds:) ⇒ ToolRunner

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ToolRunner.

Parameters:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/openai/helpers/chat/tool_runner.rb', line 17

def initialize(completions:, params:, tool_handlers:, max_rounds:)
  unless max_rounds.is_a?(Integer) && max_rounds.positive?
    raise ArgumentError, "max_rounds must be a positive integer"
  end

  @completions = completions
  @max_rounds = max_rounds
  @params, options = OpenAI::Chat::CompletionCreateParams.dump_request(params)
  @request_options = OpenAI::Internal::RequestOptionsScope.new(options)
  @handlers = tool_handlers.to_h.dup
  validate_options(options)

  # Validate a separate copy: schema conversion must not replace the models
  # used by create to parse the final completion's structured output.
  definitions, = OpenAI::Chat::CompletionCreateParams.dump_request(params)
  completions.get_structured_output_models(definitions)
  names = definitions.fetch(:tools).map do |tool|
    unless tool[:type] == :function || tool[:type] == "function"
      raise ArgumentError, "run_tools supports function tools only"
    end

    name = tool.dig(:function, :name)
    unless name.is_a?(String) && !name.empty? && @handlers[name].respond_to?(:call)
      raise ArgumentError, "Each function tool must have a named callable in tool_handlers"
    end

    name
  end

  unless names.uniq.length == names.length && @handlers.keys.sort == names.sort
    raise ArgumentError, "Tool definitions and tool_handlers must have matching, unique string names"
  end

  @allowed_names = allowed_tool_names(names)
  @seen = {}
  @params.fetch(:messages).each do |message|
    Array(message[:tool_calls]).each { |call| @seen[call[:id]] = true }
  end
end

Instance Method Details

#run ⇒ OpenAI::Chat::ChatCompletion

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (OpenAI::Chat::ChatCompletion)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/openai/helpers/chat/tool_runner.rb', line 58

def run
  @max_rounds.times do |round|
    completion = create_completion(round)
    assistant, prepared = prepare_completion(completion, round)
    return completion unless assistant

    @params[:messages] << assistant
    prepared.each do |call, handler, arguments|
      output = handler.call(arguments)
      content = output.is_a?(String) ? output.dup : JSON.generate(output)
      @params[:messages] << {role: :tool, tool_call_id: call[:id], content: content}
    end

    # A forced tool choice applies to the first round; allow a final answer.
    # Preserve the caller's allowed-tools restriction, when supplied.
    choice = @params[:tool_choice]
    @params[:tool_choice] = if choice.is_a?(Hash) && choice[:type].to_s == "allowed_tools"
      choice.merge(allowed_tools: choice.fetch(:allowed_tools).merge(mode: :auto))
    else
      :auto
    end
  end
end