Class: OpenAISwarm::Repl

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-openai-swarm/repl.rb

Class Method Summary collapse

Class Method Details

.pretty_print_messages(messages) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby-openai-swarm/repl.rb', line 40

def pretty_print_messages(messages)
  messages.each do |message|
    next unless message["role"] == "assistant"

    print "\033[94m#{message[:sender]}\033[0m: "

    puts message["content"] if message["content"]

    tool_calls = message.fetch("tool_calls", [])
    puts if tool_calls.length > 1
    tool_calls.each do |tool_call|
      func = tool_call["function"]
      name = func["name"]
      args = JSON.parse(func["arguments"] || "{}").map { |k, v| "#{k}=#{v}" }.join(", ")
      puts "\e[95m#{name}\e[0m(#{args})"
    end
  end
end

.process_and_print_streaming_response(response) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby-openai-swarm/repl.rb', line 4

def process_and_print_streaming_response(response)
  content = []
  last_sender = ""
  response.each do |stream|
    delta = stream['delta']
    if delta
      last_sender = delta['sender'] if delta.key?('sender')

      if delta.key?("content") && !delta["content"].nil?
        if content.empty? && !last_sender.empty?
          puts
          print "\033[94m#{last_sender}:\033[0m "
          last_sender = ""
        end
        print delta["content"]
        content << delta["content"]
      end
    end

    if stream.key?("tool_calls") && !stream["tool_calls"].nil?
      stream["tool_calls"].each do |tool_call|
        f = tool_call["function"]
        name = f["name"]
        next if name.nil?
        print "\033[94m#{last_sender}: \033[95m#{name}\033[0m()"
      end
    end

    if stream.key?("delim") && stream["delim"] == "end" && !content.empty?
      puts
      content = ""
    end
    return stream["response"] if stream.key?("response")
  end
end

.run_demo_loop(starting_agent, context_variables: {}, stream: false, debug: false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby-openai-swarm/repl.rb', line 59

def run_demo_loop(starting_agent, context_variables: {}, stream: false, debug: false)
  client = OpenAISwarm.new
  puts "Starting Swarm CLI 🐝"

  messages = []
  agent = starting_agent

  loop do
    puts
    print "\033[90mUser\033[0m: "
    user_input = gets.chomp
    break if %W[exit exit! exit() quit quit()].include?(user_input.downcase)

    messages << { "role" => "user", "content" => user_input }

    if stream
      chunks = Enumerator.new do |yielder|
        client.run_and_stream(
          agent: agent,
          messages: messages,
          context_variables: context_variables,
          # stream: stream,
          debug: debug
        ) do |chunk|
          yielder << chunk
        end
      end
      response = process_and_print_streaming_response(chunks)
    else
      response = client.run(
        agent: agent,
        messages: messages,
        context_variables: context_variables,
        stream: stream,
        debug: debug
      )
      pretty_print_messages(response.messages)
    end
    messages.concat(response.messages)
    agent = response.agent
  end
end