ruby-openai-swarm 0.2.9 → 0.3.0
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/examples/airline/main.rb +1 -1
- data/examples/basic/function_calling.rb +17 -1
- data/lib/ruby-openai-swarm/agent.rb +7 -1
- data/lib/ruby-openai-swarm/agent_change_tracker.rb +22 -0
- data/lib/ruby-openai-swarm/agent_strategy_options.rb +9 -0
- data/lib/ruby-openai-swarm/core.rb +14 -3
- data/lib/ruby-openai-swarm/version.rb +1 -1
- data/lib/ruby-openai-swarm.rb +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 161226570a77888c84129d825b862ab7868f1d9f54b233eee7ab13950427678e
|
4
|
+
data.tar.gz: e976c2a6bb9dae417b53fdc8f3f8216d3346a295b7e18838d89d24faff5612ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65cb09238a9e1ee5fb57d2cd9e73b56d0259bafc3dc19290e449a5db75fefce0d0cf92e65a946f525c5e6571db70e813edaa11a26c436d2a3c28c3baa88bff9f
|
7
|
+
data.tar.gz: d90997ebfbbf64a4d9a815510db0653e07f73f2c7b53e5d5837bcfc760589ecabeca2aba87a799b92f67c1c7c82baa3cd64c44445390e6896bae16d8a5aa67e9
|
data/Gemfile.lock
CHANGED
data/examples/airline/main.rb
CHANGED
@@ -65,4 +65,4 @@ params:
|
|
65
65
|
GUIDE_EXAMPLES
|
66
66
|
puts guide_examples
|
67
67
|
|
68
|
-
OpenAISwarm::Repl.run_demo_loop(triage_agent, context_variables: context_variables, debug: env_debug)
|
68
|
+
OpenAISwarm::Repl.run_demo_loop(triage_agent, context_variables: context_variables, stream: true, debug: env_debug)
|
@@ -46,6 +46,7 @@ Details:
|
|
46
46
|
Example: “What’s the weather in NYC?”
|
47
47
|
Action: Calls get_weather with location “New York City”.
|
48
48
|
Response: Only provides weather details.
|
49
|
+
|
49
50
|
2. Multiple Function Calls
|
50
51
|
Example: “Tell me the weather in New York and the latest news headlines.”
|
51
52
|
Action: Calls get_weather for weather and get_news for news.
|
@@ -58,4 +59,19 @@ GUIDE_EXAMPLES
|
|
58
59
|
|
59
60
|
puts guide_examples
|
60
61
|
|
61
|
-
OpenAISwarm::Repl.run_demo_loop(agent, stream: true, debug: env_debug)
|
62
|
+
# OpenAISwarm::Repl.run_demo_loop(agent, stream: true, debug: env_debug)
|
63
|
+
|
64
|
+
OpenAISwarm.new.run_and_stream(
|
65
|
+
agent: agent,
|
66
|
+
debug: true,
|
67
|
+
messages: [{"role" => "user", "content" => "Tell me the weather in New York and the latest news headlines."}]
|
68
|
+
) do |chunk|
|
69
|
+
if chunk.key?("content") && !chunk["content"].nil?
|
70
|
+
puts ">>>#{chunk}"
|
71
|
+
end
|
72
|
+
|
73
|
+
if chunk.key?('response')
|
74
|
+
binding.pry
|
75
|
+
# log_llm_request(chunk['response'])
|
76
|
+
end
|
77
|
+
end
|
@@ -3,7 +3,9 @@ module OpenAISwarm
|
|
3
3
|
attr_accessor :name, :model, :instructions,
|
4
4
|
:functions, :tool_choice,
|
5
5
|
:parallel_tool_calls,
|
6
|
+
:strategy,
|
6
7
|
:noisy_tool_calls,
|
8
|
+
:temperature,
|
7
9
|
:resource
|
8
10
|
# These attributes can be read and written externally. They include:
|
9
11
|
# - name: The name of the agent.
|
@@ -19,18 +21,22 @@ module OpenAISwarm
|
|
19
21
|
instructions: "You are a helpful agent.",
|
20
22
|
functions: [],
|
21
23
|
tool_choice: nil,
|
24
|
+
temperature: nil,
|
22
25
|
parallel_tool_calls: true,
|
23
26
|
resource: nil,
|
24
|
-
noisy_tool_calls: []
|
27
|
+
noisy_tool_calls: [],
|
28
|
+
strategy: {}
|
25
29
|
)
|
26
30
|
@name = name
|
27
31
|
@model = model
|
28
32
|
@instructions = instructions
|
29
33
|
@functions = functions
|
30
34
|
@tool_choice = tool_choice
|
35
|
+
@temperature = temperature
|
31
36
|
@parallel_tool_calls = parallel_tool_calls
|
32
37
|
@resource = resource
|
33
38
|
@noisy_tool_calls = noisy_tool_calls
|
39
|
+
@strategy = AgentStrategyOptions.new(strategy)
|
34
40
|
end
|
35
41
|
end
|
36
42
|
end
|
@@ -0,0 +1,22 @@
|
|
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
|
@@ -14,7 +14,8 @@ module OpenAISwarm
|
|
14
14
|
@logger = OpenAISwarm::Logger.instance.logger
|
15
15
|
end
|
16
16
|
|
17
|
-
def get_chat_completion(
|
17
|
+
def get_chat_completion(agent_tracker, history, context_variables, model_override, stream, debug)
|
18
|
+
agent = agent_tracker.current_agent
|
18
19
|
context_variables = context_variables.dup
|
19
20
|
instructions = agent.instructions.respond_to?(:call) ? agent.instructions.call(context_variables) : agent.instructions
|
20
21
|
messages = [{ role: 'system', content: instructions }] + history
|
@@ -41,6 +42,7 @@ module OpenAISwarm
|
|
41
42
|
# create_params[:functions] = tools unless tools.empty?
|
42
43
|
# create_params[:function_call] = agent.tool_choice if agent.tool_choice
|
43
44
|
|
45
|
+
create_params[:temperature] = agent.temperature if agent.temperature
|
44
46
|
create_params[:tool_choice] = agent.tool_choice if agent.tool_choice
|
45
47
|
create_params[:parallel_tool_calls] = agent.parallel_tool_calls if tools.any?
|
46
48
|
|
@@ -146,6 +148,7 @@ module OpenAISwarm
|
|
146
148
|
end
|
147
149
|
|
148
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::AgentChangeTracker.new(agent)
|
149
152
|
if stream
|
150
153
|
return run_and_stream(
|
151
154
|
agent: agent,
|
@@ -164,8 +167,11 @@ module OpenAISwarm
|
|
164
167
|
init_len = messages.length
|
165
168
|
|
166
169
|
while history.length - init_len < max_turns && active_agent
|
170
|
+
agent_tracker.update(active_agent)
|
171
|
+
history = [history.first] if agent_tracker.switch_agent_reset_message?
|
172
|
+
|
167
173
|
completion = get_chat_completion(
|
168
|
-
|
174
|
+
agent_tracker,
|
169
175
|
history,
|
170
176
|
context_variables,
|
171
177
|
model_override,
|
@@ -204,16 +210,21 @@ module OpenAISwarm
|
|
204
210
|
)
|
205
211
|
end
|
206
212
|
|
213
|
+
# TODO(Grayson): a lot of copied code here that will be refactored
|
207
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::AgentChangeTracker.new(agent)
|
208
216
|
active_agent = agent
|
209
217
|
context_variables = context_variables.dup
|
210
218
|
history = messages.dup
|
211
219
|
init_len = messages.length
|
212
220
|
|
213
221
|
while history.length - init_len < max_turns && active_agent
|
222
|
+
agent_tracker.update(active_agent)
|
223
|
+
history = [history.first] if agent_tracker.switch_agent_reset_message?
|
224
|
+
|
214
225
|
message = OpenAISwarm::Util.message_template(agent.name)
|
215
226
|
completion = get_chat_completion(
|
216
|
-
|
227
|
+
agent_tracker,
|
217
228
|
history,
|
218
229
|
context_variables,
|
219
230
|
model_override,
|
data/lib/ruby-openai-swarm.rb
CHANGED
@@ -1,6 +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/agent_change_tracker'
|
5
|
+
require 'ruby-openai-swarm/agent_strategy_options'
|
4
6
|
require 'ruby-openai-swarm/response'
|
5
7
|
require 'ruby-openai-swarm/result'
|
6
8
|
require 'ruby-openai-swarm/util'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-openai-swarm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Grayson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-12-
|
11
|
+
date: 2024-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-openai
|
@@ -106,6 +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/agent_change_tracker.rb
|
110
|
+
- lib/ruby-openai-swarm/agent_strategy_options.rb
|
109
111
|
- lib/ruby-openai-swarm/configuration.rb
|
110
112
|
- lib/ruby-openai-swarm/core.rb
|
111
113
|
- lib/ruby-openai-swarm/core_ext.rb
|