ox-ai-workers 0.2.5 → 0.3.1
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/CHANGELOG.md +11 -0
- data/README.md +15 -6
- data/exe/oxaiworkers +1 -1
- data/exe/start +3 -3
- data/lib/ox-ai-workers.rb +28 -34
- data/lib/oxaiworkers/assistant/sysop.rb +5 -1
- data/lib/oxaiworkers/compatibility.rb +2 -0
- data/lib/oxaiworkers/delayed_request.rb +95 -93
- data/lib/oxaiworkers/iterator.rb +154 -159
- data/lib/oxaiworkers/load_i18n.rb +4 -0
- data/lib/oxaiworkers/module_request.rb +62 -62
- data/lib/oxaiworkers/present_compat.rb +31 -27
- data/lib/oxaiworkers/request.rb +15 -13
- data/lib/oxaiworkers/state_batch.rb +50 -46
- data/lib/oxaiworkers/state_helper.rb +8 -4
- data/lib/oxaiworkers/state_tools.rb +46 -43
- data/lib/oxaiworkers/tool/eval.rb +20 -18
- data/lib/oxaiworkers/version.rb +1 -1
- data/locales/en.assistant.yml +5 -0
- data/locales/{en.system.yml → en.iterator.yml} +0 -11
- data/locales/en.tool.yml +10 -0
- data/locales/ru.assistant.yml +5 -0
- data/locales/{ru.system.yml → ru.iterator.yml} +0 -11
- data/locales/ru.tool.yml +10 -0
- data/template/my_assistant.rb +6 -1
- data/template/start +10 -4
- metadata +8 -3
data/lib/oxaiworkers/iterator.rb
CHANGED
@@ -1,187 +1,182 @@
|
|
1
|
-
|
2
|
-
extend OxAiWorkers::ToolDefinition
|
3
|
-
attr_accessor :worker, :role, :messages, :context, :result, :tools, :queue, :monologue, :tasks, :milestones
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
module OxAiWorkers
|
4
|
+
class Iterator < OxAiWorkers::StateTools
|
5
|
+
extend OxAiWorkers::ToolDefinition
|
6
|
+
attr_accessor :worker, :role, :messages, :context, :result, :tools, :queue, :monologue, :tasks, :milestones
|
7
|
+
attr_accessor :on_inner_monologue, :on_outer_voice, :on_action_request, :on_pack_history
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
define_function :innerMonologue, description: I18n.t('oxaiworkers.iterator.inner_monologue.description') do
|
10
|
+
property :speach, type: 'string', description: I18n.t('oxaiworkers.iterator.inner_monologue.speach'),
|
11
|
+
required: true
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
define_function :outerVoice, description: I18n.t('oxaiworkers.iterator.outer_voice.description') do
|
15
|
+
property :text, type: 'string', description: I18n.t('oxaiworkers.iterator.outer_voice.text'), required: true
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
define_function :actionRequest, description: I18n.t('oxaiworkers.iterator.action_request.description') do
|
19
|
+
property :action, type: 'string', description: I18n.t('oxaiworkers.iterator.action_request.action'),
|
20
|
+
required: true
|
21
|
+
end
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
@tools = [self] + tools
|
25
|
-
@role = role
|
26
|
-
@context = nil
|
27
|
-
@monologue = I18n.t("oxaiworkers.iterator.monologue")
|
28
|
-
cleanup()
|
23
|
+
define_function :packHistory, description: I18n.t('oxaiworkers.iterator.pack_history.description') do
|
24
|
+
property :text, type: 'string', description: I18n.t('oxaiworkers.iterator.pack_history.text'), required: true
|
25
|
+
end
|
29
26
|
|
30
|
-
|
31
|
-
|
27
|
+
def initialize(worker:, role: nil, tools: [], on_inner_monologue: nil, on_outer_voice: nil, on_action_request: nil,
|
28
|
+
on_pack_history: nil)
|
29
|
+
puts "call: #{__method__}"
|
30
|
+
@worker = worker
|
31
|
+
@tools = [self] + tools
|
32
|
+
@role = role
|
33
|
+
@context = nil
|
34
|
+
@monologue = I18n.t('oxaiworkers.iterator.monologue')
|
32
35
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
@milestones = []
|
38
|
-
@messages = []
|
39
|
-
completeIteration()
|
40
|
-
end
|
36
|
+
@on_inner_monologue = on_inner_monologue
|
37
|
+
@on_outer_voice = on_outer_voice
|
38
|
+
@on_action_request = on_action_request
|
39
|
+
@on_pack_history = on_pack_history
|
41
40
|
|
42
|
-
|
43
|
-
puts Rainbow("monologue: #{speach}").yellow
|
44
|
-
@queue.pop
|
45
|
-
@queue << {role: :system, content: "#{__method__}: #{speach}"}
|
46
|
-
return nil
|
47
|
-
end
|
41
|
+
cleanup
|
48
42
|
|
49
|
-
|
50
|
-
|
51
|
-
@queue.pop
|
52
|
-
@queue << {role: :system, content: "#{__method__}: #{text}"}
|
53
|
-
return nil
|
54
|
-
end
|
43
|
+
super()
|
44
|
+
end
|
55
45
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
46
|
+
def cleanup
|
47
|
+
@result = nil
|
48
|
+
@queue = []
|
49
|
+
@tasks = []
|
50
|
+
@milestones = []
|
51
|
+
@messages = []
|
52
|
+
completeIteration
|
53
|
+
end
|
64
54
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
complete! if can_complete?
|
72
|
-
return nil
|
73
|
-
end
|
55
|
+
def innerMonologue(speach:)
|
56
|
+
@queue.pop
|
57
|
+
@queue << { role: :system, content: "#{__method__}: #{speach}" }
|
58
|
+
@on_inner_monologue&.call(text: speach)
|
59
|
+
nil
|
60
|
+
end
|
74
61
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
62
|
+
def outerVoice(text:)
|
63
|
+
@queue.pop
|
64
|
+
@queue << { role: :system, content: "#{__method__}: #{text}" }
|
65
|
+
@on_outer_voice&.call(text: text)
|
66
|
+
nil
|
67
|
+
end
|
80
68
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
@worker.tools = @tools.map { |tool| tool.class.function_schemas.to_openai_format }.flatten if @tools.any?
|
90
|
-
end
|
69
|
+
def actionRequest(action:)
|
70
|
+
@result = action
|
71
|
+
@queue.pop
|
72
|
+
@messages << { role: :system, content: "#{__method__}: #{action}" }
|
73
|
+
complete! if can_complete?
|
74
|
+
@on_action_request&.call(text: action)
|
75
|
+
nil
|
76
|
+
end
|
91
77
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
78
|
+
def packHistory(text:)
|
79
|
+
@milestones << "#{__method__}: #{text}"
|
80
|
+
@messages = []
|
81
|
+
@worker.finish
|
82
|
+
rebuildWorker
|
83
|
+
complete! if can_complete?
|
84
|
+
@on_pack_history&.call(text: text)
|
85
|
+
nil
|
86
|
+
end
|
99
87
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
88
|
+
def init
|
89
|
+
puts "call: #{__method__} state: #{state_name}"
|
90
|
+
rebuildWorker
|
91
|
+
request!
|
92
|
+
end
|
105
93
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
94
|
+
def rebuildWorker
|
95
|
+
@worker.messages = []
|
96
|
+
@worker.append(role: :system, content: @role) if !@role.nil? && @role.present?
|
97
|
+
@worker.append(role: :system, content: @monologue.join("\n"))
|
98
|
+
@worker.append(messages: @context) if !@context.nil? and @context.any?
|
99
|
+
@tasks.each { |task| @worker.append(role: :user, content: task) }
|
100
|
+
@milestones.each { |milestone| @worker.append(role: :system, content: milestone) }
|
101
|
+
@worker.append(messages: @messages)
|
102
|
+
@worker.tools = @tools.map { |tool| tool.class.function_schemas.to_openai_format }.flatten if @tools.any?
|
103
|
+
end
|
104
|
+
|
105
|
+
def nextIteration
|
106
|
+
puts "call: #{__method__} state: #{state_name}"
|
107
|
+
@worker.append(messages: @queue)
|
108
|
+
@messages += @queue
|
109
|
+
@queue = []
|
110
|
+
request!
|
111
|
+
end
|
112
|
+
|
113
|
+
def externalRequest
|
114
|
+
puts "call: #{__method__} state: #{state_name}"
|
115
|
+
@worker.request!
|
116
|
+
ticker
|
117
|
+
end
|
118
|
+
|
119
|
+
def ticker
|
120
|
+
puts "call: #{__method__} state: #{state_name}"
|
121
|
+
sleep(60) until @worker.completed?
|
122
|
+
analyze!
|
110
123
|
end
|
111
|
-
analyze!
|
112
|
-
end
|
113
124
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
125
|
+
def processResult(_transition)
|
126
|
+
puts "call: #{__method__} state: #{state_name}"
|
127
|
+
@result = @worker.result || @worker.errors
|
128
|
+
if @worker.tool_calls.present?
|
129
|
+
@queue << { role: :system, content: @worker.tool_calls_raw.to_s }
|
130
|
+
@worker.tool_calls.each do |external_call|
|
131
|
+
tool = @tools.select do |t|
|
132
|
+
t.class.tool_name == external_call[:class] && t.respond_to?(external_call[:name])
|
133
|
+
end.first
|
134
|
+
unless tool.nil?
|
135
|
+
out = tool.send(external_call[:name], **external_call[:args])
|
136
|
+
@queue << { role: :system, content: out.to_s } if out.present?
|
137
|
+
end
|
124
138
|
end
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
elsif @worker.result.present?
|
139
|
+
@worker.finish
|
140
|
+
iterate! if can_iterate?
|
141
|
+
|
142
|
+
# tool = @tools.select{|t| t.class.tool_name == @worker.external_call[:class] && t.respond_to?(@worker.external_call[:name]) }.first
|
143
|
+
# out = tool.send(@worker.external_call[:name], **@worker.external_call[:args])
|
144
|
+
# if can_iterate?
|
145
|
+
# @queue << {role: :system, content: out.to_s} if out.present?
|
146
|
+
# iterate!
|
147
|
+
# end
|
148
|
+
elsif @worker.result.present?
|
136
149
|
actionRequest action: @worker.result
|
150
|
+
end
|
137
151
|
end
|
138
|
-
end
|
139
152
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
def addTask task, auto_execute: true
|
146
|
-
@tasks << task
|
147
|
-
@messages << {role: :user, content: task}
|
148
|
-
execute() if auto_execute
|
149
|
-
end
|
153
|
+
def completeIteration
|
154
|
+
@queue = []
|
155
|
+
@worker.finish
|
156
|
+
end
|
150
157
|
|
151
|
-
|
152
|
-
|
153
|
-
|
158
|
+
def addTask(task, auto_execute: true)
|
159
|
+
@tasks << task
|
160
|
+
@messages << { role: :user, content: task }
|
161
|
+
execute if auto_execute
|
162
|
+
end
|
154
163
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
end
|
164
|
+
def appendContext(text, role: :system)
|
165
|
+
@context << { role: role, content: text }
|
166
|
+
end
|
159
167
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
168
|
+
def execute
|
169
|
+
puts "call: #{__method__} state: #{state_name}"
|
170
|
+
prepare! if valid?
|
171
|
+
end
|
164
172
|
|
165
|
-
|
166
|
-
|
167
|
-
|
173
|
+
def cancel
|
174
|
+
puts "call: #{__method__} state: #{state_name}"
|
175
|
+
@worker.cancel if @worker.respond_to?(:cancel)
|
176
|
+
end
|
168
177
|
|
178
|
+
def valid?
|
179
|
+
@messages.any?
|
180
|
+
end
|
181
|
+
end
|
169
182
|
end
|
170
|
-
|
171
|
-
# r = OxAiWorkers::Iterator.new(worker: OxAiWorkers::Request.new)
|
172
|
-
# r.addTask("сколько будет 2+2?")
|
173
|
-
# r.execute
|
174
|
-
# r.result
|
175
|
-
|
176
|
-
|
177
|
-
# @worker.append(role: "user", content: "сколько будет 2+2?")
|
178
|
-
# @worker.request!
|
179
|
-
# @worker.completed?
|
180
|
-
# @worker.result
|
181
|
-
# @worker.finish
|
182
|
-
#
|
183
|
-
# r = OxAiWorkers::Iterator.new(worker: OxAiWorkers::Request.new)
|
184
|
-
# r.role = "ты программный агент внутри моего компьютера"
|
185
|
-
# r.tools = [OxAiWorkers::Tool::Eval.new]
|
186
|
-
# r.addTask("покажи мне файлы на диске, используй код на ruby")
|
187
|
-
# r.execute
|
@@ -1,82 +1,82 @@
|
|
1
|
-
|
2
|
-
attr_accessor :result, :client, :messages, :model, :max_tokens, :custom_id, :temperature, :tools, :errors
|
3
|
-
attr_accessor :tool_calls_raw, :tool_calls
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@model = model || OxAiWorkers.configuration.model
|
10
|
-
@temperature = temperature || OxAiWorkers.configuration.temperature
|
11
|
-
@client = nil
|
12
|
-
|
13
|
-
OxAiWorkers.configuration.access_token ||= ENV["OPENAI"]
|
14
|
-
if OxAiWorkers.configuration.access_token.nil?
|
15
|
-
error_text = "OpenAi access token missing!"
|
16
|
-
raise OxAiWorkers::ConfigurationError, error_text
|
17
|
-
end
|
3
|
+
module OxAiWorkers
|
4
|
+
class ModuleRequest
|
5
|
+
attr_accessor :result, :client, :messages, :model, :max_tokens, :custom_id, :temperature, :tools, :errors,
|
6
|
+
:tool_calls_raw, :tool_calls
|
18
7
|
|
19
|
-
|
20
|
-
|
8
|
+
def initializeRequests(model: nil, max_tokens: nil, temperature: nil)
|
9
|
+
puts "call: ModuleRequest::#{__method__}"
|
10
|
+
@max_tokens = max_tokens || OxAiWorkers.configuration.max_tokens
|
11
|
+
@custom_id = SecureRandom.uuid
|
12
|
+
@model = model || OxAiWorkers.configuration.model
|
13
|
+
@temperature = temperature || OxAiWorkers.configuration.temperature
|
14
|
+
@client = nil
|
21
15
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
)
|
28
|
-
@result = nil
|
29
|
-
@errors = nil
|
30
|
-
@messages = []
|
31
|
-
@tool_calls = nil
|
32
|
-
@tool_calls_raw = nil
|
33
|
-
end
|
16
|
+
OxAiWorkers.configuration.access_token ||= ENV['OPENAI']
|
17
|
+
if OxAiWorkers.configuration.access_token.nil?
|
18
|
+
error_text = 'OpenAi access token missing!'
|
19
|
+
raise OxAiWorkers::ConfigurationError, error_text
|
20
|
+
end
|
34
21
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
22
|
+
cleanup
|
23
|
+
end
|
24
|
+
|
25
|
+
def cleanup
|
26
|
+
puts "call: ModuleRequest::#{__method__}"
|
27
|
+
@client ||= OpenAI::Client.new(
|
28
|
+
access_token: OxAiWorkers.configuration.access_token,
|
29
|
+
log_errors: true # Highly recommended in development, so you can see what errors OpenAI is returning. Not recommended in production because it could leak private data to your logs.
|
30
|
+
)
|
31
|
+
@result = nil
|
32
|
+
@errors = nil
|
33
|
+
@messages = []
|
34
|
+
@tool_calls = nil
|
35
|
+
@tool_calls_raw = nil
|
36
|
+
end
|
39
37
|
|
40
|
-
|
41
|
-
|
38
|
+
def append(role: nil, content: nil, messages: nil)
|
39
|
+
@messages << { role: role, content: content } if role.present? and content.present?
|
40
|
+
@messages += messages if messages.present?
|
41
|
+
end
|
42
|
+
|
43
|
+
def params
|
44
|
+
parameters = {
|
42
45
|
model: @model,
|
43
46
|
messages: @messages,
|
44
47
|
temperature: @temperature,
|
45
48
|
max_tokens: @max_tokens
|
46
49
|
}
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
+
if @tools.present?
|
51
|
+
parameters[:tools] = @tools
|
52
|
+
parameters[:tool_choice] = 'required'
|
53
|
+
end
|
54
|
+
parameters
|
50
55
|
end
|
51
|
-
parameters
|
52
|
-
end
|
53
56
|
|
54
|
-
|
55
|
-
begin
|
57
|
+
def not_found_is_ok
|
56
58
|
yield
|
57
59
|
rescue Faraday::ResourceNotFound => e
|
58
60
|
nil
|
59
61
|
end
|
60
|
-
end
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
63
|
+
def parseChoices(response)
|
64
|
+
# puts response.inspect
|
65
|
+
@tool_calls = []
|
66
|
+
@result = response.dig('choices', 0, 'message', 'content')
|
67
|
+
@tool_calls_raw = response.dig('choices', 0, 'message', 'tool_calls')
|
67
68
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
@tool_calls
|
69
|
+
@tool_calls_raw.each do |tool|
|
70
|
+
function = tool['function']
|
71
|
+
args = JSON.parse(YAML.load(function['arguments']).to_json, { symbolize_names: true })
|
72
|
+
@tool_calls << {
|
73
|
+
class: function['name'].split('__').first,
|
74
|
+
name: function['name'].split('__').last,
|
75
|
+
args: args
|
76
|
+
}
|
77
|
+
end
|
79
78
|
|
80
|
-
|
79
|
+
@tool_calls
|
80
|
+
end
|
81
81
|
end
|
82
|
-
end
|
82
|
+
end
|
@@ -1,29 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module OxAiWorkers
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
module PresentCompat
|
5
|
+
def present?
|
6
|
+
!nil? and !empty?
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module CamelizeCompat
|
11
|
+
def camelize(first_letter = :upper)
|
12
|
+
string = dup
|
13
|
+
string = if first_letter == :upper
|
14
|
+
string.sub(/^[a-z\d]*/) { |match| match.capitalize }
|
15
|
+
else
|
16
|
+
string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
|
17
|
+
end
|
18
|
+
string.gsub(%r{(?:_|(/))([a-z\d]*)}) do
|
19
|
+
"#{::Regexp.last_match(1)}#{::Regexp.last_match(2).capitalize}"
|
20
|
+
end.gsub('/', '::')
|
21
|
+
end
|
7
22
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
def underscore
|
20
|
-
word = self.dup
|
21
|
-
word.gsub!(/::/, '/')
|
22
|
-
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
23
|
-
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
24
|
-
word.tr!("-", "_")
|
25
|
-
word.downcase!
|
26
|
-
word
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
23
|
+
def underscore
|
24
|
+
word = dup
|
25
|
+
word.gsub!(/::/, '/')
|
26
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
27
|
+
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
28
|
+
word.tr!('-', '_')
|
29
|
+
word.downcase!
|
30
|
+
word
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/oxaiworkers/request.rb
CHANGED
@@ -1,23 +1,25 @@
|
|
1
|
-
|
2
|
-
alias_method :initialize, :initializeRequests
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
module OxAiWorkers
|
4
|
+
class Request < OxAiWorkers::ModuleRequest
|
5
|
+
alias initialize initializeRequests
|
6
|
+
|
7
|
+
def finish
|
8
|
+
@custom_id = SecureRandom.uuid
|
9
|
+
cleanup
|
10
|
+
end
|
8
11
|
|
9
|
-
|
10
|
-
begin
|
12
|
+
def request!
|
11
13
|
response = @client.chat(parameters: params)
|
12
14
|
parseChoices(response)
|
13
|
-
|
14
|
-
#puts response.inspect
|
15
|
+
# @result = response.dig("choices", 0, "message", "content")
|
16
|
+
# puts response.inspect
|
15
17
|
rescue OpenAI::Error => e
|
16
18
|
puts e.inspect
|
17
19
|
end
|
18
|
-
end
|
19
20
|
|
20
|
-
|
21
|
-
|
21
|
+
def completed?
|
22
|
+
@result.present? or @errors.present? or @tool_calls.present?
|
23
|
+
end
|
22
24
|
end
|
23
25
|
end
|