Module: Swarm::Util

Defined in:
lib/swarm/util.rb

Class Method Summary collapse

Class Method Details

.debug_print(debug, *args) ⇒ Object



6
7
8
9
10
11
# File 'lib/swarm/util.rb', line 6

def self.debug_print(debug, *args)
  return unless debug
  timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
  message = args.join(" ")
  puts "\e[97m[\e[90m#{timestamp}\e[97m]\e[90m #{message}\e[0m"
end

.function_to_json(func) ⇒ Object



34
35
36
37
38
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
# File 'lib/swarm/util.rb', line 34

def self.function_to_json(func)
  type_map = {
    String => "string",
    Integer => "integer",
    Float => "number",
    TrueClass => "boolean",
    FalseClass => "boolean",
    Array => "array",
    Hash => "object",
    NilClass => "null"
  }

  parameters = {}
  required = []

  func.parameters.each do |type, name|
    param_type = type_map[name.class] || "string" # Default to 'string' if type is unknown

    if name.to_s == "context_variables" && type == :keyreq
      param_type = "object"
    end

    parameters[name.to_s] = {"type" => param_type}
    required << name.to_s if type == :req || type == :keyreq
  end

  {
    "name" => func.name.to_s,
    "description" => "",
    "parameters" => {
      "type" => "object",
      "properties" => parameters,
      "required" => required
    }
  }
end

.merge_chunk(final_response, delta) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/swarm/util.rb', line 23

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



13
14
15
16
17
18
19
20
21
# File 'lib/swarm/util.rb', line 13

def self.merge_fields(target, source)
  source.each do |key, value|
    if value.is_a?(String)
      target[key] += value
    elsif value.is_a?(Hash)
      merge_fields(target[key], value)
    end
  end
end