intelli_agent 0.2.2 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/intelli_agent/openai.rb +13 -24
- metadata +2 -3
- data/lib/intelli_agent/openai.bkp.rb +0 -145
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e97b3065bf46da0d6d7759a41fe0b43ca0b6eac98e21aa8df9823efa88e54643
|
4
|
+
data.tar.gz: ff04e8c8df7ff838bc332f99d517085b9e571dcb77e299b922ef3ef7605542e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 754a73693f3d8a13295e63c4fa512bec9cae6fbeceaed5848c3fc9ea95ed008b87307f1d24bb136be91541fe641ad047b8393dcd41f3b3a86b76747fc882a6c0
|
7
|
+
data.tar.gz: 565000945b207b0680f30ab78b4ab9ffa624836e6ec8545f9a3825841e73e3d29c22769549a7f83c2c1e1b6b415449bb102147c48168cdb6dd46ea3bc861970b
|
data/lib/intelli_agent/openai.rb
CHANGED
@@ -4,25 +4,13 @@ module IntelliAgent::OpenAI
|
|
4
4
|
MAX_TOKENS = ENV.fetch('OPENAI_MAX_TOKENS', 16_383).to_i
|
5
5
|
|
6
6
|
module ResponseExtender
|
7
|
-
def
|
8
|
-
dig('choices', 0, 'message', 'content')
|
9
|
-
end
|
10
|
-
|
11
|
-
def message
|
12
|
-
dig('choices', 0, 'message')
|
13
|
-
end
|
7
|
+
def message = dig('choices', 0, 'message')
|
14
8
|
|
15
|
-
def content
|
16
|
-
|
17
|
-
end
|
9
|
+
def content = dig('choices', 0, 'message', 'content')
|
10
|
+
def content? = !content.nil?
|
18
11
|
|
19
|
-
def tool_calls
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
def tool_calls?
|
24
|
-
!tool_calls.nil?
|
25
|
-
end
|
12
|
+
def tool_calls = dig('choices', 0, 'message', 'tool_calls')
|
13
|
+
def tool_calls? = !tool_calls.nil?
|
26
14
|
|
27
15
|
def functions
|
28
16
|
return if tool_calls.nil?
|
@@ -39,14 +27,13 @@ module IntelliAgent::OpenAI
|
|
39
27
|
functions_list
|
40
28
|
end
|
41
29
|
|
42
|
-
def functions?
|
43
|
-
!functions.nil?
|
44
|
-
end
|
30
|
+
def functions? = !functions.nil?
|
45
31
|
end
|
46
32
|
|
47
|
-
def self.
|
33
|
+
def self.embeddings(input, model: 'text-embedding-3-large')
|
48
34
|
response = OpenAI::Client.new.embeddings(parameters: { input:, model: })
|
49
|
-
response.dig('data', 0, 'embedding')
|
35
|
+
def response.embedding = dig('data', 0, 'embedding')
|
36
|
+
response
|
50
37
|
end
|
51
38
|
|
52
39
|
def self.vision(prompt:, image_url:, model: :advanced, response_format: nil, max_tokens: MAX_TOKENS)
|
@@ -59,7 +46,9 @@ module IntelliAgent::OpenAI
|
|
59
46
|
|
60
47
|
response = OpenAI::Client.new.chat(parameters:)
|
61
48
|
|
62
|
-
response.dig('choices', 0, 'message', 'content').strip
|
49
|
+
def response.content = dig('choices', 0, 'message', 'content').strip
|
50
|
+
|
51
|
+
response
|
63
52
|
end
|
64
53
|
|
65
54
|
def self.single_prompt(prompt:, model: :basic, response_format: nil, max_tokens: MAX_TOKENS, tools: nil, function_run_context: self)
|
@@ -124,4 +113,4 @@ module IntelliAgent::OpenAI
|
|
124
113
|
end
|
125
114
|
end
|
126
115
|
end
|
127
|
-
end
|
116
|
+
end
|
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.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gedean Dias
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2027-09-
|
11
|
+
date: 2027-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-openai
|
@@ -63,7 +63,6 @@ files:
|
|
63
63
|
- lib/intelli_agent/agent.rb.disabled
|
64
64
|
- lib/intelli_agent/agent_test.rb_
|
65
65
|
- lib/intelli_agent/anthropic.rb
|
66
|
-
- lib/intelli_agent/openai.bkp.rb
|
67
66
|
- lib/intelli_agent/openai.rb
|
68
67
|
homepage: https://github.com/gedean/intelli_agent
|
69
68
|
licenses:
|
@@ -1,145 +0,0 @@
|
|
1
|
-
module IntelliAgent::OpenAI
|
2
|
-
BASIC_MODEL = ENV.fetch('OPENAI_BASIC_MODEL', 'gpt-4o-mini')
|
3
|
-
ADVANCED_MODEL = ENV.fetch('OPENAI_ADVANCED_MODEL', 'gpt-4o-2024-08-06')
|
4
|
-
MAX_TOKENS = ENV.fetch('OPENAI_MAX_TOKENS', 16_383).to_i
|
5
|
-
|
6
|
-
module ResponseExtender
|
7
|
-
def content
|
8
|
-
dig('choices', 0, 'message', 'content')
|
9
|
-
end
|
10
|
-
|
11
|
-
def message
|
12
|
-
dig('choices', 0, 'message')
|
13
|
-
end
|
14
|
-
|
15
|
-
def content?
|
16
|
-
!content.nil?
|
17
|
-
end
|
18
|
-
|
19
|
-
def tool_calls
|
20
|
-
dig('choices', 0, 'message', 'tool_calls')
|
21
|
-
end
|
22
|
-
|
23
|
-
def tool_calls?
|
24
|
-
!tool_calls.nil?
|
25
|
-
end
|
26
|
-
|
27
|
-
def functions
|
28
|
-
return if tool_calls.nil?
|
29
|
-
|
30
|
-
functions = tool_calls.filter { |tool| tool['type'].eql? 'function' }
|
31
|
-
return if functions.empty?
|
32
|
-
|
33
|
-
functions_list = []
|
34
|
-
functions.map.with_index do |function, function_index|
|
35
|
-
function_def = tool_calls.dig(function_index, 'function')
|
36
|
-
functions_list << { id: function['id'], name: function_def['name'], arguments: Oj.load(function_def['arguments'], symbol_keys: true) }
|
37
|
-
end
|
38
|
-
|
39
|
-
functions_list
|
40
|
-
end
|
41
|
-
|
42
|
-
def functions?
|
43
|
-
!functions.nil?
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def self.embed(input, model: 'text-embedding-3-large')
|
48
|
-
response = OpenAI::Client.new.embeddings(parameters: { input:, model: })
|
49
|
-
response.dig('data', 0, 'embedding')
|
50
|
-
end
|
51
|
-
|
52
|
-
def self.vision(prompt:, image_url:, model: :advanced, response_format: nil, max_tokens: MAX_TOKENS)
|
53
|
-
model = select_model(model)
|
54
|
-
messages = [{ type: :text, text: prompt },
|
55
|
-
{ type: :image_url, image_url: { url: image_url } }]
|
56
|
-
|
57
|
-
parameters = { model: model, messages: [{ role: :user, content: messages }], max_tokens: }
|
58
|
-
parameters[:response_format] = { type: 'json_object' } if response_format.eql?(:json)
|
59
|
-
|
60
|
-
response = OpenAI::Client.new.chat(parameters:)
|
61
|
-
|
62
|
-
response.dig('choices', 0, 'message', 'content').strip
|
63
|
-
end
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
def self.single_prompt(prompt:, model: :basic, response_format: nil, max_tokens: MAX_TOKENS, tools: nil, function_run_context: self)
|
68
|
-
model = select_model(model)
|
69
|
-
|
70
|
-
parameters = { model:, messages: [{ role: 'user', content: prompt }], max_tokens: }
|
71
|
-
parameters[:response_format] = { type: 'json_object' } if response_format.eql?(:json)
|
72
|
-
parameters[:tools] = tools if tools
|
73
|
-
banana(parameters:)
|
74
|
-
end
|
75
|
-
|
76
|
-
def self.single_chat(system:, user:, model: :basic, response_format: nil, max_tokens: MAX_TOKENS, tools: nil, function_run_context: self)
|
77
|
-
model = select_model(model)
|
78
|
-
parameters = { model:,
|
79
|
-
messages: [
|
80
|
-
{ role: 'system', content: system },
|
81
|
-
{ role: 'user', content: user }
|
82
|
-
], max_tokens: }
|
83
|
-
|
84
|
-
parameters[:response_format] = { type: 'json_object' } if response_format.eql?(:json)
|
85
|
-
parameters[:tools] = tools if tools
|
86
|
-
|
87
|
-
banana(parameters:)
|
88
|
-
end
|
89
|
-
|
90
|
-
def self.chat(messages:, model: :basic, response_format: nil, max_tokens: MAX_TOKENS, tools: nil, function_run_context: self)
|
91
|
-
model = select_model(model)
|
92
|
-
|
93
|
-
messages = parse_messages(messages)
|
94
|
-
|
95
|
-
parameters = { model:, messages:, max_tokens: }
|
96
|
-
parameters[:response_format] = { type: 'json_object' } if response_format.eql?(:json)
|
97
|
-
parameters[:tools] = tools if tools
|
98
|
-
|
99
|
-
response = OpenAI::Client.new.chat(parameters:)
|
100
|
-
response.extend(ResponseExtender)
|
101
|
-
|
102
|
-
if response.functions?
|
103
|
-
parameters[:messages] << response.message
|
104
|
-
|
105
|
-
response.functions.each do |function|
|
106
|
-
parameters[:messages] << {
|
107
|
-
tool_call_id: function[:id],
|
108
|
-
role: :tool,
|
109
|
-
name: function[:name],
|
110
|
-
content: parameters[:function_run_context].send(function[:name], **function[:arguments])
|
111
|
-
}
|
112
|
-
end
|
113
|
-
|
114
|
-
response = OpenAI::Client.new.chat(parameters:)
|
115
|
-
response.extend(ResponseExtender)
|
116
|
-
end
|
117
|
-
|
118
|
-
response
|
119
|
-
end
|
120
|
-
|
121
|
-
def self.models = OpenAI::Client.new.models.list
|
122
|
-
|
123
|
-
def self.select_model(model)
|
124
|
-
case model
|
125
|
-
when :basic
|
126
|
-
BASIC_MODEL
|
127
|
-
when :advanced
|
128
|
-
ADVANCED_MODEL
|
129
|
-
else
|
130
|
-
model
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
def self.parse_messages(messages)
|
135
|
-
case messages
|
136
|
-
in [{ role: String, content: String }, *]
|
137
|
-
messages
|
138
|
-
else
|
139
|
-
messages.map do |msg|
|
140
|
-
role, content = msg.first
|
141
|
-
{ role: role.to_s, content: content }
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|