ruby-openai-swarm 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb75077c1cd68a61448103423fec5401c54963c3341b9c7a11aef564584375d7
4
- data.tar.gz: 6950967689f250591671095dd296a0e266d3e218b41e618811a3a0c8c8c48ee2
3
+ metadata.gz: df40e898023940bd9fa16596e947caf353dc8ce3edeee3d442cb871a8e5bbba0
4
+ data.tar.gz: c180214e63813c09bea2c986a3c7c1729f458f66a807a2b578d4f71b8b39374b
5
5
  SHA512:
6
- metadata.gz: 0caeb42cc055869a3b9bd830e3d52cb63add1a07af767cd1e42abe8b5fb7d98565327af96a1bd36ff72616d775f3be72066eeb9298da37762afe1461ff0cd345
7
- data.tar.gz: 0d515f09b04a1229e08ee1957e5958805a512f0fd9a7bb79ddf43c0962d868b9415f27714802468be1450d6d55130df7295f4ee3ed791c6c0230e09d2fa149ac
6
+ metadata.gz: 6e16aa9fa53293212ccdb96dba442da4e48db77dd217b749b9bfd80a4711324ac3adaf6bc11b28e27ea348d0a055921d7e1f98fa70dbecb0c4d17bcd7514473b
7
+ data.tar.gz: 8450574be9da37c9c5fcd67e25ba564795f5b475fe78df337a69561ca2f8b5f09ea100b01d3547b1a6dcb510c56614db6f2a3148693588d6b6e432386cd6231c
data/.gitignore CHANGED
@@ -30,3 +30,4 @@ build-iPhoneSimulator/
30
30
 
31
31
  # rspec failure tracking
32
32
  .rspec_status
33
+ log/openai_swarm.log
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-openai-swarm (0.2.8)
4
+ ruby-openai-swarm (0.2.9)
5
5
  ruby-openai (~> 7.3)
6
6
 
7
7
  GEM
@@ -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: 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" => "",
@@ -1,3 +1,3 @@
1
1
  module OpenAISwarm
2
- VERSION = "0.2.8"
2
+ VERSION = "0.2.9"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'ruby-openai-swarm/version'
2
+ require 'ruby-openai-swarm/core_ext'
2
3
  require 'ruby-openai-swarm/agent'
3
4
  require 'ruby-openai-swarm/response'
4
5
  require 'ruby-openai-swarm/result'
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.8
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-12 00:00:00.000000000 Z
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