intelli_agent 0.2.12 → 0.2.14

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: 37f7cac456bcc0441d34d9f0e9901dfd49d04b3fde889ab44498bd1a735dcbd0
4
- data.tar.gz: 0e3cb4e3597410c51db19ee141d273839a1ea8945c99029d422ce85c247874c7
3
+ metadata.gz: '0316292d323a05a6e573a22aedb01706c5f5e2458acf33cbb59393a3821ee3ef'
4
+ data.tar.gz: c77fb697c72ce71ca89ee866f10e7d3c4fbd1f39719f6f95742dd9c130edbb22
5
5
  SHA512:
6
- metadata.gz: 47133255200fa5ee3cac2d91db4293348eadd243c11d0893f65280c7ecc378a39db2196cfd81b52343273e1cdc36e738bfa469b5c670e4bb5840c494e5f1cde1
7
- data.tar.gz: cfc9cbf181b2971f51e44cf056bebd5bd0cd344d7561dfea2824249db4cc7c629100dade0fe0013ba29538d11a520e68ded5d811729585b0e7db2dd7f12fef9c
6
+ metadata.gz: a235150a5f8790a7662c1d72bb2f644c3da98d2d16ce13eb1337873221efd8fa1d871eea38828f6d942329e66b0afabc94b2d617ae4decc2d10609f296764999
7
+ data.tar.gz: 3d002f5c352348ba7e975f229f8f3831d3ee8875b82749b14ff31eb4c58f85b6c178c73e716abe872acc2d646a15a906d7158d477e0045016f74ba0a3c2b0a81
@@ -0,0 +1,43 @@
1
+ class IntelliAgent::OpenAI::Messages < Array
2
+
3
+ def initialize messages = nil
4
+ super parse_messages(messages)
5
+ end
6
+
7
+ def add(message) = concat(parse_messages(message))
8
+
9
+ =begin
10
+ The output format should be an array of hashes with the keys `role` and `content`.
11
+
12
+ The input can be:
13
+ - A simple hash `{ user: 'user message' }`, which should be transformed into `{ role: 'user', content: 'user message' }`
14
+ - A hash with multiple keys `{system: 'message', user: 'user message' }`, which should be transformed into `[{ role: 'system', content: 'message' }, { role: 'user', content: 'user message' }]`
15
+ - An array of hashes `[{ system: 'message' }, { user: 'user message' }]`, which should be transformed into `[{ role: 'system', content: 'message' }, { role: 'user', content: 'user message' }]`
16
+
17
+ Important note: the content can be either a string or an array.
18
+ =end
19
+ private
20
+ def parse_messages(messages)
21
+ return [] if messages.nil?
22
+
23
+ messages = [messages] unless messages.is_a?(Array)
24
+
25
+ # if first element is ok, then do not parse the rest
26
+ return messages if messages.first in { role: String | Symbol, content: String | Array | Hash}
27
+
28
+ messages.flat_map do |msg|
29
+ if msg.is_a?(Hash)
30
+ if msg.keys.size == 1
31
+ role, content = msg.first
32
+ { role: role.to_s, content: content }
33
+ elsif msg.key?(:role) && msg.key?(:content)
34
+ { role: msg[:role].to_s, content: msg[:content] }
35
+ else
36
+ msg.map { |role, content| { role: role.to_s, content: content } }
37
+ end
38
+ else
39
+ raise ArgumentError, "Invalid message format: #{msg}"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -75,12 +75,11 @@ 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
82
82
 
83
-
84
83
  # o1 family models doesn't support max_tokens params. Instead, use max_completion_tokens
85
84
  parameters[:max_completion_tokens] = max_tokens if is_o1_model
86
85
  parameters[:max_tokens] = max_tokens unless is_o1_model
@@ -88,7 +87,11 @@ module IntelliAgent::OpenAI
88
87
  parameters[:response_format] = { type: 'json_object' } if response_format.eql?(:json)
89
88
  parameters[:tools] = tools if tools
90
89
 
91
- response = OpenAI::Client.new.chat(parameters:)
90
+ begin
91
+ response = OpenAI::Client.new.chat(parameters:)
92
+ rescue => e
93
+ raise "Error in OpenAI chat: #{e.message}\nParameters: #{parameters.inspect}"
94
+ end
92
95
 
93
96
  response[:chat_params] = parameters
94
97
  response.extend(ResponseExtender)
@@ -120,16 +123,4 @@ module IntelliAgent::OpenAI
120
123
  model
121
124
  end
122
125
  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
126
  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.12
4
+ version: 0.2.14
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
@@ -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/message_collection.rb
69
+ - lib/intelli_agent/openai/messages.rb
68
70
  homepage: https://github.com/gedean/intelli_agent
69
71
  licenses:
70
72
  - MIT