Module: OpenAISwarm::Util
- Included in:
- Core
- Defined in:
- lib/ruby-openai-swarm/util.rb
Class Method Summary collapse
- .clean_message_tools(messages, tool_names) ⇒ Object
- .debug_print(debug, *args) ⇒ Object
- .function_to_json(func_instance) ⇒ Object
- .latest_role_user_message(history) ⇒ Object
- .merge_chunk(final_response, delta) ⇒ Object
- .merge_fields(target, source) ⇒ Object
- .message_template(agent_name) ⇒ Object
- .request_tools_excluded(tools, tool_names, prevent_agent_reentry = false) ⇒ Object
- .symbolize_keys_to_string(obj) ⇒ Object
Class Method Details
.clean_message_tools(messages, tool_names) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ruby-openai-swarm/util.rb', line 39 def self.(, tool_names) return if tool_names.empty? = symbolize_keys_to_string(.dup) # Collect tool call IDs to be removed tool_call_ids_to_remove = .select { |msg| msg['tool_calls'] } .flat_map { |msg| msg['tool_calls'] } .select { |tool_call| tool_names.include?(tool_call['function']['name']) } .map { |tool_call| tool_call['id'] } # Remove specific messages .reject! do |msg| # Remove tool call messages for specified tool names (msg['role'] == 'assistant' && msg['tool_calls']&.all? { |tool_call| tool_names.include?(tool_call['function']['name']) }) || # Remove tool response messages for specified tool calls (msg['role'] == 'tool' && tool_call_ids_to_remove.include?(msg['tool_call_id'])) end # If assistant message's tool_calls becomes empty, modify that message .map! do |msg| if msg['role'] == 'assistant' && msg['tool_calls'] msg['tool_calls'].reject! { |tool_call| tool_names.include?(tool_call['function']['name']) } msg['tool_calls'] = nil if msg['tool_calls'].empty? msg else msg end end end |
.debug_print(debug, *args) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/ruby-openai-swarm/util.rb', line 21 def self.debug_print(debug, *args) return unless debug = Time.now.strftime("%Y-%m-%d %H:%M:%S") = args.map(&:to_s).join(' ') puts "\e[97m[\e[90m#{timestamp}\e[97m]\e[90m #{message}\e[0m" end |
.function_to_json(func_instance) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/ruby-openai-swarm/util.rb', line 114 def self.function_to_json(func_instance) is_target_method = func_instance.respond_to?(:target_method) || func_instance.is_a?(OpenAISwarm::FunctionDescriptor) func = is_target_method ? func_instance.target_method : func_instance custom_parameters = is_target_method ? func_instance.parameters : nil function_name = func.name function_parameters = func.parameters type_map = { String => "string", Integer => "integer", Float => "number", TrueClass => "boolean", FalseClass => "boolean", Array => "array", Hash => "object", NilClass => "null" } parameters = {} function_parameters.each do |type, param_name| param_type = type_map[param_name.class] || "string" if param_name.to_s == 'context_variables' && type == :opt #type == :keyreq param_type = 'object' end parameters[param_name] = { type: param_type } end required = function_parameters .select { |type, _| [:req, :keyreq].include?(type) } .map { |_, name| name.to_s } description = func_instance.respond_to?(:description) ? func_instance&.description : nil json_parameters = { type: "object", properties: parameters, required: required } { type: "function", function: { name: function_name, description: description || '', parameters: custom_parameters || json_parameters } } end |
.latest_role_user_message(history) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/ruby-openai-swarm/util.rb', line 4 def (history) return history if history.empty? = symbolize_keys_to_string(history.dup) = .reverse.find { |msg| msg['role'] == 'user' } ? [] : history end |
.merge_chunk(final_response, delta) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ruby-openai-swarm/util.rb', line 103 def self.merge_chunk(final_response, delta) delta.delete("role") merge_fields(final_response, delta) tool_calls = delta["tool_calls"] if tool_calls && !tool_calls.empty? index = tool_calls[0].delete("index") merge_fields(final_response["tool_calls"][index], tool_calls[0]) end end |
.merge_fields(target, source) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ruby-openai-swarm/util.rb', line 88 def self.merge_fields(target, source) semantic_keyword = %W[type] source.each do |key, value| if value.is_a?(String) if semantic_keyword.include?(key) target[key] = value else target[key] += value end elsif value.is_a?(Hash) && value != nil merge_fields(target[key], value) end end end |
.message_template(agent_name) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ruby-openai-swarm/util.rb', line 72 def self.(agent_name) { "content" => "", "sender" => agent_name, "role" => "assistant", "function_call" => nil, "tool_calls" => Hash.new do |hash, key| hash[key] = { "function" => { "arguments" => "", "name" => "" }, "id" => "", "type" => "" } end } end |
.request_tools_excluded(tools, tool_names, prevent_agent_reentry = false) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/ruby-openai-swarm/util.rb', line 11 def request_tools_excluded(tools, tool_names, prevent_agent_reentry = false) return nil if tools.empty? return tools if tool_names.empty? || !prevent_agent_reentry symbolize_keys_to_string(tools).reject do |tool| tool_names.include?("#{tool['function']['name']}") end end |
.symbolize_keys_to_string(obj) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ruby-openai-swarm/util.rb', line 28 def self.symbolize_keys_to_string(obj) case obj when Hash obj.transform_keys(&:to_s).transform_values { |v| symbolize_keys_to_string(v) } when Array obj.map { |v| symbolize_keys_to_string(v) } else obj end end |