ruby-openai-swarm 0.2.8 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +1 -1
- data/lib/ruby-openai-swarm/agent.rb +7 -1
- data/lib/ruby-openai-swarm/core.rb +3 -1
- data/lib/ruby-openai-swarm/core_ext.rb +16 -0
- data/lib/ruby-openai-swarm/util.rb +44 -0
- data/lib/ruby-openai-swarm/version.rb +1 -1
- data/lib/ruby-openai-swarm.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df40e898023940bd9fa16596e947caf353dc8ce3edeee3d442cb871a8e5bbba0
|
4
|
+
data.tar.gz: c180214e63813c09bea2c986a3c7c1729f458f66a807a2b578d4f71b8b39374b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e16aa9fa53293212ccdb96dba442da4e48db77dd217b749b9bfd80a4711324ac3adaf6bc11b28e27ea348d0a055921d7e1f98fa70dbecb0c4d17bcd7514473b
|
7
|
+
data.tar.gz: 8450574be9da37c9c5fcd67e25ba564795f5b475fe78df337a69561ca2f8b5f09ea100b01d3547b1a6dcb510c56614db6f2a3148693588d6b6e432386cd6231c
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
@@ -3,11 +3,15 @@ module OpenAISwarm
|
|
3
3
|
attr_accessor :name, :model, :instructions,
|
4
4
|
:functions, :tool_choice,
|
5
5
|
:parallel_tool_calls,
|
6
|
+
:noisy_tool_calls,
|
6
7
|
:resource
|
7
8
|
# These attributes can be read and written externally. They include:
|
8
9
|
# - name: The name of the agent.
|
9
10
|
# - model: The model used, e.g., "gpt-4".
|
10
11
|
# - resource: Additional custom parameters or data that the agent might need.
|
12
|
+
# - noisy_tool_calls: is an array that contains the names of tool calls that should be excluded because they are considered "noise".
|
13
|
+
# These tool calls generate irrelevant or unnecessary messages that the agent should not send to OpenAI.
|
14
|
+
# When filtering messages, any message that includes these tool calls will be removed from the list, preventing them from being sent to OpenAI.
|
11
15
|
|
12
16
|
def initialize(
|
13
17
|
name: "Agent",
|
@@ -16,7 +20,8 @@ module OpenAISwarm
|
|
16
20
|
functions: [],
|
17
21
|
tool_choice: nil,
|
18
22
|
parallel_tool_calls: true,
|
19
|
-
resource: nil
|
23
|
+
resource: nil,
|
24
|
+
noisy_tool_calls: []
|
20
25
|
)
|
21
26
|
@name = name
|
22
27
|
@model = model
|
@@ -25,6 +30,7 @@ module OpenAISwarm
|
|
25
30
|
@tool_choice = tool_choice
|
26
31
|
@parallel_tool_calls = parallel_tool_calls
|
27
32
|
@resource = resource
|
33
|
+
@noisy_tool_calls = noisy_tool_calls
|
28
34
|
end
|
29
35
|
end
|
30
36
|
end
|
@@ -29,9 +29,11 @@ module OpenAISwarm
|
|
29
29
|
params[:required]&.delete(CTX_VARS_NAME.to_sym)
|
30
30
|
end
|
31
31
|
|
32
|
+
cleaned_messages = OpenAISwarm::Util.clean_message_tools(messages, agent.noisy_tool_calls)
|
33
|
+
|
32
34
|
create_params = {
|
33
35
|
model: model_override || agent.model,
|
34
|
-
messages:
|
36
|
+
messages: cleaned_messages,
|
35
37
|
tools: tools.empty? ? nil : tools,
|
36
38
|
}
|
37
39
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Backport of Array.wrap for Ruby versions prior to 3.0
|
2
|
+
# This provides a consistent way to wrap objects as arrays across different Ruby versions
|
3
|
+
# link: https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/array/wrap.rb
|
4
|
+
unless Array.respond_to?(:wrap)
|
5
|
+
class Array
|
6
|
+
def self.wrap(object)
|
7
|
+
if object.nil?
|
8
|
+
[]
|
9
|
+
elsif object.respond_to?(:to_ary)
|
10
|
+
object.to_ary || [object]
|
11
|
+
else
|
12
|
+
[object]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -7,6 +7,50 @@ module OpenAISwarm
|
|
7
7
|
puts "\e[97m[\e[90m#{timestamp}\e[97m]\e[90m #{message}\e[0m"
|
8
8
|
end
|
9
9
|
|
10
|
+
def self.symbolize_keys_to_string(obj)
|
11
|
+
case obj
|
12
|
+
when Hash
|
13
|
+
obj.transform_keys(&:to_s).transform_values { |v| symbolize_keys_to_string(v) }
|
14
|
+
when Array
|
15
|
+
obj.map { |v| symbolize_keys_to_string(v) }
|
16
|
+
else
|
17
|
+
obj
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.clean_message_tools(messages, tool_names)
|
22
|
+
return messages if tool_names.empty?
|
23
|
+
filtered_messages = symbolize_keys_to_string(messages.dup)
|
24
|
+
# Collect tool call IDs to be removed
|
25
|
+
tool_call_ids_to_remove = filtered_messages
|
26
|
+
.select { |msg| msg['tool_calls'] }
|
27
|
+
.flat_map { |msg| msg['tool_calls'] }
|
28
|
+
.select { |tool_call| tool_names.include?(tool_call['function']['name']) }
|
29
|
+
.map { |tool_call| tool_call['id'] }
|
30
|
+
|
31
|
+
# Remove specific messages
|
32
|
+
filtered_messages.reject! do |msg|
|
33
|
+
# Remove tool call messages for specified tool names
|
34
|
+
(msg['role'] == 'assistant' &&
|
35
|
+
msg['tool_calls']&.all? { |tool_call| tool_names.include?(tool_call['function']['name']) }) ||
|
36
|
+
# Remove tool response messages for specified tool calls
|
37
|
+
(msg['role'] == 'tool' && tool_call_ids_to_remove.include?(msg['tool_call_id']))
|
38
|
+
end
|
39
|
+
|
40
|
+
# If assistant message's tool_calls becomes empty, modify that message
|
41
|
+
filtered_messages.map! do |msg|
|
42
|
+
if msg['role'] == 'assistant' && msg['tool_calls']
|
43
|
+
msg['tool_calls'].reject! { |tool_call| tool_names.include?(tool_call['function']['name']) }
|
44
|
+
msg['tool_calls'] = nil if msg['tool_calls'].empty?
|
45
|
+
msg
|
46
|
+
else
|
47
|
+
msg
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
filtered_messages
|
52
|
+
end
|
53
|
+
|
10
54
|
def self.message_template(agent_name)
|
11
55
|
{
|
12
56
|
"content" => "",
|
data/lib/ruby-openai-swarm.rb
CHANGED
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.2.
|
4
|
+
version: 0.2.9
|
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-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-openai
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- lib/ruby-openai-swarm/agent.rb
|
109
109
|
- lib/ruby-openai-swarm/configuration.rb
|
110
110
|
- lib/ruby-openai-swarm/core.rb
|
111
|
+
- lib/ruby-openai-swarm/core_ext.rb
|
111
112
|
- lib/ruby-openai-swarm/function_descriptor.rb
|
112
113
|
- lib/ruby-openai-swarm/logger.rb
|
113
114
|
- lib/ruby-openai-swarm/repl.rb
|