ruby-openai-swarm 0.3.0 → 0.3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/ruby-openai-swarm/agent.rb +1 -1
- data/lib/ruby-openai-swarm/agents/change_tracker.rb +24 -0
- data/lib/ruby-openai-swarm/agents/strategy_options.rb +11 -0
- data/lib/ruby-openai-swarm/core.rb +4 -4
- data/lib/ruby-openai-swarm/util.rb +8 -0
- data/lib/ruby-openai-swarm/version.rb +1 -1
- data/lib/ruby-openai-swarm.rb +2 -2
- metadata +3 -3
- data/lib/ruby-openai-swarm/agent_change_tracker.rb +0 -22
- data/lib/ruby-openai-swarm/agent_strategy_options.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98aa27742c5ae980d9c1c8b9457beb2e61a4ff3a1548790d4205cd0f68cef039
|
4
|
+
data.tar.gz: 37d7a5280f47c2a454a2aba4a5f64587787f1e829fc07b6c27accc90f63f91f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d671ef9fa72e895bfd3b3e62561fccfc3894a6f19605dd43e68da14b0c9f958d22559da8d9eecd981277a7db035ed39c17535f261cf40849f16a95ac6884516
|
7
|
+
data.tar.gz: 96c6229510613dbb8b858200916bb9ab22583b7918d604adae0b1bc85bb5b89cfae28a6c833b8c16b2acc1758d1d89643c61f240c896ff6d1d3153536801b46d
|
data/Gemfile.lock
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
module OpenAISwarm
|
2
|
+
module Agents
|
3
|
+
class ChangeTracker
|
4
|
+
attr_reader :current_agent, :previous_agent
|
5
|
+
|
6
|
+
def initialize(agent)
|
7
|
+
update(agent)
|
8
|
+
end
|
9
|
+
|
10
|
+
def update(agent)
|
11
|
+
@previous_agent = @current_agent
|
12
|
+
@current_agent = agent
|
13
|
+
end
|
14
|
+
|
15
|
+
def agent_changed?
|
16
|
+
previous_agent&.name != current_agent&.name
|
17
|
+
end
|
18
|
+
|
19
|
+
def switch_agent_reset_message?
|
20
|
+
agent_changed? && current_agent.strategy.switch_agent_reset_message
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -148,7 +148,7 @@ module OpenAISwarm
|
|
148
148
|
end
|
149
149
|
|
150
150
|
def run(agent:, messages:, context_variables: {}, model_override: nil, stream: false, debug: false, max_turns: Float::INFINITY, execute_tools: true)
|
151
|
-
agent_tracker = OpenAISwarm::
|
151
|
+
agent_tracker = OpenAISwarm::Agents::ChangeTracker.new(agent)
|
152
152
|
if stream
|
153
153
|
return run_and_stream(
|
154
154
|
agent: agent,
|
@@ -168,7 +168,7 @@ module OpenAISwarm
|
|
168
168
|
|
169
169
|
while history.length - init_len < max_turns && active_agent
|
170
170
|
agent_tracker.update(active_agent)
|
171
|
-
history =
|
171
|
+
history = OpenAISwarm::Util.latest_role_user_message(history) if agent_tracker.switch_agent_reset_message?
|
172
172
|
|
173
173
|
completion = get_chat_completion(
|
174
174
|
agent_tracker,
|
@@ -212,7 +212,7 @@ module OpenAISwarm
|
|
212
212
|
|
213
213
|
# TODO(Grayson): a lot of copied code here that will be refactored
|
214
214
|
def run_and_stream(agent:, messages:, context_variables: {}, model_override: nil, debug: false, max_turns: Float::INFINITY, execute_tools: true)
|
215
|
-
agent_tracker = OpenAISwarm::
|
215
|
+
agent_tracker = OpenAISwarm::Agents::ChangeTracker.new(agent)
|
216
216
|
active_agent = agent
|
217
217
|
context_variables = context_variables.dup
|
218
218
|
history = messages.dup
|
@@ -220,7 +220,7 @@ module OpenAISwarm
|
|
220
220
|
|
221
221
|
while history.length - init_len < max_turns && active_agent
|
222
222
|
agent_tracker.update(active_agent)
|
223
|
-
history =
|
223
|
+
history = OpenAISwarm::Util.latest_role_user_message(history) if agent_tracker.switch_agent_reset_message?
|
224
224
|
|
225
225
|
message = OpenAISwarm::Util.message_template(agent.name)
|
226
226
|
completion = get_chat_completion(
|
@@ -1,5 +1,13 @@
|
|
1
1
|
module OpenAISwarm
|
2
2
|
module Util
|
3
|
+
class << self
|
4
|
+
def latest_role_user_message(history)
|
5
|
+
return history if history.empty?
|
6
|
+
filtered_messages = symbolize_keys_to_string(history.dup)
|
7
|
+
last_user_message = filtered_messages.reverse.find { |msg| msg['role'] == 'user' }
|
8
|
+
last_user_message ? [last_user_message] : history
|
9
|
+
end
|
10
|
+
end
|
3
11
|
def self.debug_print(debug, *args)
|
4
12
|
return unless debug
|
5
13
|
timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
data/lib/ruby-openai-swarm.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'ruby-openai-swarm/version'
|
2
2
|
require 'ruby-openai-swarm/core_ext'
|
3
3
|
require 'ruby-openai-swarm/agent'
|
4
|
-
require 'ruby-openai-swarm/
|
5
|
-
require 'ruby-openai-swarm/
|
4
|
+
require 'ruby-openai-swarm/agents/change_tracker'
|
5
|
+
require 'ruby-openai-swarm/agents/strategy_options'
|
6
6
|
require 'ruby-openai-swarm/response'
|
7
7
|
require 'ruby-openai-swarm/result'
|
8
8
|
require 'ruby-openai-swarm/util'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-openai-swarm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.0
|
4
|
+
version: 0.3.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Grayson
|
@@ -106,8 +106,8 @@ files:
|
|
106
106
|
- examples/weather_agent/run.rb
|
107
107
|
- lib/ruby-openai-swarm.rb
|
108
108
|
- lib/ruby-openai-swarm/agent.rb
|
109
|
-
- lib/ruby-openai-swarm/
|
110
|
-
- lib/ruby-openai-swarm/
|
109
|
+
- lib/ruby-openai-swarm/agents/change_tracker.rb
|
110
|
+
- lib/ruby-openai-swarm/agents/strategy_options.rb
|
111
111
|
- lib/ruby-openai-swarm/configuration.rb
|
112
112
|
- lib/ruby-openai-swarm/core.rb
|
113
113
|
- lib/ruby-openai-swarm/core_ext.rb
|
@@ -1,22 +0,0 @@
|
|
1
|
-
module OpenAISwarm
|
2
|
-
class AgentChangeTracker
|
3
|
-
attr_reader :current_agent, :previous_agent
|
4
|
-
|
5
|
-
def initialize(agent)
|
6
|
-
update(agent)
|
7
|
-
end
|
8
|
-
|
9
|
-
def update(agent)
|
10
|
-
@previous_agent = @current_agent
|
11
|
-
@current_agent = agent
|
12
|
-
end
|
13
|
-
|
14
|
-
def agent_changed?
|
15
|
-
previous_agent&.name != current_agent&.name
|
16
|
-
end
|
17
|
-
|
18
|
-
def switch_agent_reset_message?
|
19
|
-
agent_changed? && current_agent.strategy.switch_agent_reset_message
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|