intelli_agent 0.2.11 → 0.2.13

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: e94ab4d8cf68071308ab502086f092fc7424622de20420d00bba18451cd10705
4
- data.tar.gz: f74579964f8d1a40a7d3f26f64b079ada32d202f2cf6c08f15e615e24ded8ee7
3
+ metadata.gz: c91be5552b13825b657164e1ac9836e88fbdd98e99930ba7aececb54cb6e273a
4
+ data.tar.gz: f80215a13d785d1d50f835148be4f842a6c058f9f8445b94928feeb4f64b6e01
5
5
  SHA512:
6
- metadata.gz: fbc1c8b705f7050b8f9bf18aa8aef380559e38019bde3911f598b4546ac6cdd2d2596b158502509663058cdfa8d052cd4e3dc992973f813739f86bd9336930bc
7
- data.tar.gz: b82503246fe4365c46fad3528522cd4ee1acf8d7ecb9a72d3c4975acfde835031fc0a6556cb85944fecf3806bcf1de8c75d9b1fa04a03c21d7f8b2398ac989fe
6
+ metadata.gz: 184e9e14319cfa84487eb3919ffb0b27a1f1b273464fd9410992d1c0467bbfbf2f68eabf7e31e3002f26feecd5107d37cae2acbfafdf7a3473ec1a0076a1699a
7
+ data.tar.gz: 8a3facc0c1bc891a457573e20b4581422565dd970cf666e1a848e46e226b5fd7203130e2740859c2c6393ef3826314ff914bfab0811c79af9845ed5192359ad9
@@ -0,0 +1,41 @@
1
+ class IntelliAgent::OpenAI::Messages < Array
2
+ # VALID_ROLES = %i[system user assistant function tool].freeze
3
+
4
+ def initialize messages = nil
5
+ super parse_messages(messages)
6
+ end
7
+
8
+ def add(message) = concat(parse_messages(message))
9
+
10
+ =begin
11
+ The output format should be an array of hashes with the keys `role` and `content`.
12
+
13
+ The input can be:
14
+ - A simple hash `{ user: 'user message' }`, which should be transformed into `{ role: 'user', content: 'user message' }`
15
+ - A hash with multiple keys `{system: 'message', user: 'user message' }`, which should be transformed into `[{ role: 'system', content: 'message' }, { role: 'user', content: 'user message' }]`
16
+ - An array of hashes `[{ system: 'message' }, { user: 'user message' }]`, which should be transformed into `[{ role: 'system', content: 'message' }, { role: 'user', content: 'user message' }]`
17
+
18
+ Important note: the content can be either a string or an array.
19
+ =end
20
+ private
21
+ def parse_messages(messages)
22
+ return [] if messages.nil?
23
+
24
+ messages = [messages] unless messages.is_a?(Array)
25
+
26
+ messages.flat_map do |msg|
27
+ if msg.is_a?(Hash)
28
+ if msg.keys.size == 1
29
+ role, content = msg.first
30
+ { role: role.to_s, content: content }
31
+ elsif msg.key?(:role) && msg.key?(:content)
32
+ { role: msg[:role].to_s, content: msg[:content] }
33
+ else
34
+ msg.map { |role, content| { role: role.to_s, content: content } }
35
+ end
36
+ else
37
+ raise ArgumentError, "Invalid message format: #{msg}"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,40 @@
1
+ class IntelliAgent::OpenAI::Messages < Array
2
+ VALID_ROLES = %i[system user assistant tool].freeze
3
+
4
+ def initialize(messages = [])
5
+ @messages = self.class.parse_messages(messages)
6
+ end
7
+
8
+ def add(message) = @messages.concat(self.class.parse_messages(message))
9
+
10
+ =begin
11
+ O Formato de saída deve ser um array de hashes com as chaves `role` e `content`.
12
+
13
+ A entrada pode ser:
14
+ - um hash simples { user: 'mensagem do usuário' }, deve ser transformado em { role: 'user', content: 'mensagem do usuário' }
15
+ - um hash com várias chaves {system: 'mensagem', user: 'mensagem do usuário' }, deve ser transformado em [{ role: 'system', content: 'mensagem'}, { role: 'user', content: 'mensagem do usuário' }]
16
+ - um array de hashes [{ system: 'mensagem'}, { user: 'mensagem do usuário' }], deve ser transformado em [{ role: 'system', content: 'mensagem'}, { role: 'user', content: 'mensagem do usuário' }]
17
+ Obervação imporante: o content pode ser uma string ou um array
18
+ =end
19
+
20
+ def self.parse_messages(messages)
21
+ return [] if messages.empty?
22
+
23
+ messages = [messages] unless messages.is_a?(Array)
24
+
25
+ messages.flat_map do |msg|
26
+ if msg.is_a?(Hash)
27
+ if msg.keys.size == 1
28
+ role, content = msg.first
29
+ { role: role.to_s, content: content }
30
+ elsif msg.key?(:role) && msg.key?(:content)
31
+ { role: msg[:role].to_s, content: msg[:content] }
32
+ else
33
+ msg.map { |role, content| { role: role.to_s, content: content } }
34
+ end
35
+ else
36
+ raise ArgumentError, "Invalid message format: #{msg}"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -75,7 +75,7 @@ module IntelliAgent::OpenAI
75
75
  model = select_model(model)
76
76
  is_o1_model = model.start_with?('o1')
77
77
 
78
- messages = parse_messages(messages)
78
+ messages = IntelliAgent::OpenAI::Messages.new(messages) unless messages.is_a?(IntelliAgent::OpenAI::Messages)
79
79
 
80
80
  parameters = { model:, messages:, store: }
81
81
  parameters[:metadata] = metadata if metadata
@@ -120,16 +120,4 @@ module IntelliAgent::OpenAI
120
120
  model
121
121
  end
122
122
  end
123
-
124
- def self.parse_messages(messages)
125
- case messages
126
- in [{ role: String | Symbol, content: String | Array }, *]
127
- messages
128
- else
129
- messages.map do |msg|
130
- role, content = msg.first
131
- { role: role.to_s, content: content }
132
- end
133
- end
134
- end
135
123
  end
data/lib/intelli_agent.rb CHANGED
@@ -6,3 +6,4 @@ end
6
6
 
7
7
  require 'intelli_agent/anthropic'
8
8
  require 'intelli_agent/openai'
9
+ require 'intelli_agent/openai/messages'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: intelli_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.11
4
+ version: 0.2.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gedean Dias
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-10 00:00:00.000000000 Z
11
+ date: 2024-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-openai
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '7.1'
19
+ version: '7.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '7.1'
26
+ version: '7.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: anthropic
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -65,6 +65,8 @@ files:
65
65
  - lib/intelli_agent/agent_test.rb_
66
66
  - lib/intelli_agent/anthropic.rb
67
67
  - lib/intelli_agent/openai.rb
68
+ - lib/intelli_agent/openai/messages.rb
69
+ - lib/intelli_agent/openai/messages_helper copy.rb
68
70
  homepage: https://github.com/gedean/intelli_agent
69
71
  licenses:
70
72
  - MIT