ruby_llm 2.2.2 → 2.2.3
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/README.md +2 -2
- data/lib/ruby_llm/accounting/usage.rb +1 -0
- data/lib/ruby_llm/active_record/chat_methods.rb +28 -1
- data/lib/ruby_llm/active_record/model.rb +1 -1
- data/lib/ruby_llm/active_record/run_claims.rb +73 -18
- data/lib/ruby_llm/agent.rb +2 -2
- data/lib/ruby_llm/aliases.json +14 -0
- data/lib/ruby_llm/chat/tool_call_state.rb +1 -1
- data/lib/ruby_llm/chat/tool_concurrency.rb +58 -18
- data/lib/ruby_llm/chat.rb +20 -13
- data/lib/ruby_llm/code_execution/input_names.rb +80 -0
- data/lib/ruby_llm/code_execution/workspace.rb +4 -37
- data/lib/ruby_llm/code_execution.rb +44 -12
- data/lib/ruby_llm/configuration.rb +55 -0
- data/lib/ruby_llm/file_store/directory.rb +10 -7
- data/lib/ruby_llm/mcp/content.rb +2 -0
- data/lib/ruby_llm/model/pricing.rb +46 -0
- data/lib/ruby_llm/model.rb +55 -4
- data/lib/ruby_llm/models/lookup.rb +2 -1
- data/lib/ruby_llm/models.json +3281 -1791
- data/lib/ruby_llm/models.rb +1 -1
- data/lib/ruby_llm/protocol/stream_accumulator.rb +9 -4
- data/lib/ruby_llm/protocol/streaming.rb +22 -8
- data/lib/ruby_llm/protocols/bedrock/event_stream.rb +3 -7
- data/lib/ruby_llm/protocols/converse/streaming.rb +2 -2
- data/lib/ruby_llm/protocols/gemini/decisions.rb +106 -0
- data/lib/ruby_llm/protocols/gemini/streaming.rb +1 -1
- data/lib/ruby_llm/providers/gemini.rb +2 -0
- data/lib/ruby_llm/providers/minimax.rb +0 -6
- data/lib/ruby_llm/providers/openai/rerank.rb +13 -0
- data/lib/ruby_llm/providers/openai.rb +14 -0
- data/lib/ruby_llm/question.rb +60 -5
- data/lib/ruby_llm/tool.rb +24 -5
- data/lib/ruby_llm/transport/connection.rb +8 -5
- data/lib/ruby_llm/transport/log_formatter.rb +39 -0
- data/lib/ruby_llm/transport/release_request.rb +6 -2
- data/lib/ruby_llm/version.rb +1 -1
- data/lib/ruby_llm.rb +0 -1
- data/skills/rubyllm/references/cost-and-tracing.md +1 -1
- data/skills/rubyllm/references/tools-and-agents.md +1 -1
- metadata +5 -3
- data/lib/ruby_llm/providers/minimax/chat_completions.rb +0 -48
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d77a8e86ea56c1fb34211cb390e8a43eca2456d2bec1e6287311e74d1104195d
|
|
4
|
+
data.tar.gz: f960c3651f47f3bde9f94f5f3f1bf87353be57dcbbac0d3be2dd26ebd12df89a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 13533d7d0974760ce527435223df6d49d412a46820342e6360fc30948284632d5c74b75db0e339e28bacfcde8157ecf760be8127896d39668e8977a28595a86d
|
|
7
|
+
data.tar.gz: '0926ccd365474d021e049c3a9396ed09a124b4669f2954770af29c18133a195341c668ca7947443a16662d5b61ddd26d843606e9c200049af4abb017b5e44713'
|
data/README.md
CHANGED
|
@@ -179,7 +179,7 @@ Start your Rails server and visit `http://localhost:3000/chats` to try the chat
|
|
|
179
179
|
|
|
180
180
|
## Start with one line. Add files, tools, and agents
|
|
181
181
|
|
|
182
|
-
These examples use **2.2.
|
|
182
|
+
These examples use **2.2.3**. Follow [Getting Started](https://rubyllm.com/getting-started/) to install it and configure the providers you want to try. For 1.x, use the [1.x docs](https://rubyllm.com/v1/).
|
|
183
183
|
|
|
184
184
|
```ruby
|
|
185
185
|
# Just ask
|
|
@@ -309,7 +309,7 @@ response.parsed
|
|
|
309
309
|
Install RubyLLM 2.2:
|
|
310
310
|
|
|
311
311
|
```bash
|
|
312
|
-
bundle add ruby_llm --version 2.2.
|
|
312
|
+
bundle add ruby_llm --version 2.2.3
|
|
313
313
|
```
|
|
314
314
|
|
|
315
315
|
Configure a provider in your script, or in `config/initializers/ruby_llm.rb` in Rails:
|
|
@@ -274,6 +274,7 @@ module RubyLLM
|
|
|
274
274
|
|
|
275
275
|
def result_model(result)
|
|
276
276
|
return @model_info if result.model.nil? || result.model == @model_info&.id
|
|
277
|
+
return @model_info if @model_info&.configured_prices?
|
|
277
278
|
|
|
278
279
|
RubyLLM.models.find(result.model, provider: @provider.slug, config: @config)
|
|
279
280
|
rescue ModelNotFoundError
|
|
@@ -64,7 +64,34 @@ module RubyLLM
|
|
|
64
64
|
# Chains with other scopes. Returns none when the chats table has no
|
|
65
65
|
# claim columns, since then no process records that it runs a chat.
|
|
66
66
|
|
|
67
|
+
##
|
|
68
|
+
# :singleton-method: around_claim_renewal
|
|
69
|
+
# :call-seq: around_claim_renewal(*methods, &block)
|
|
70
|
+
#
|
|
71
|
+
# Registers a callback around each renewal of a running chat's
|
|
72
|
+
# claims. The renewal runs on a background thread, on database
|
|
73
|
+
# connections it checks out for that renewal, without the Current
|
|
74
|
+
# attributes or the connection settings of the request or job that
|
|
75
|
+
# runs the chat. Set what the database needs to see the chat's rows
|
|
76
|
+
# there, such as the tenant for row-level security, and yield:
|
|
77
|
+
#
|
|
78
|
+
# around_claim_renewal :with_tenant
|
|
79
|
+
#
|
|
80
|
+
# def with_tenant
|
|
81
|
+
# transaction do
|
|
82
|
+
# self.class.connection.execute(tenant_setting)
|
|
83
|
+
# yield
|
|
84
|
+
# end
|
|
85
|
+
# end
|
|
86
|
+
#
|
|
87
|
+
# A transaction covers the chat's connection. When tool calls use
|
|
88
|
+
# another connection pool, even one to the same database, open a
|
|
89
|
+
# transaction on its connection in the callback as well and set the
|
|
90
|
+
# context there. An error the callback raises counts as a failed
|
|
91
|
+
# renewal, which the next one retries.
|
|
92
|
+
|
|
67
93
|
included do
|
|
94
|
+
define_model_callbacks :claim_renewal, only: :around
|
|
68
95
|
scope :interrupted, -> { RunClaims.interrupted(self) }
|
|
69
96
|
before_save :resolve_model
|
|
70
97
|
after_commit :remember_model_registry_stored, if: -> { @model_registry_loaded_into }
|
|
@@ -1112,7 +1139,7 @@ module RubyLLM
|
|
|
1112
1139
|
record.knowledge_cutoff = model_info.knowledge_cutoff
|
|
1113
1140
|
record.capabilities = model_info.capabilities || []
|
|
1114
1141
|
record.modalities = model_info.modalities.to_h
|
|
1115
|
-
record.pricing = model_info.
|
|
1142
|
+
record.pricing = model_info.registry_pricing.to_h
|
|
1116
1143
|
record.metadata = model_info.metadata || {}
|
|
1117
1144
|
end
|
|
1118
1145
|
rescue ::ActiveRecord::RecordInvalid, ::ActiveRecord::RecordNotUnique
|
|
@@ -155,7 +155,7 @@ module RubyLLM
|
|
|
155
155
|
knowledge_cutoff: model_info.knowledge_cutoff,
|
|
156
156
|
modalities: model_info.modalities.to_h,
|
|
157
157
|
capabilities: model_info.capabilities,
|
|
158
|
-
pricing: model_info.
|
|
158
|
+
pricing: model_info.registry_pricing.to_h,
|
|
159
159
|
metadata: model_info.metadata,
|
|
160
160
|
unlisted_at: nil
|
|
161
161
|
}
|
|
@@ -96,10 +96,10 @@ module RubyLLM
|
|
|
96
96
|
# expired at once like the chat's: the expired claim is the only
|
|
97
97
|
# record that it started. So does an interrupted call this run didn't
|
|
98
98
|
# get to answer, which stays interrupted for the next one. Every other
|
|
99
|
-
# call is released. A round that
|
|
100
|
-
#
|
|
101
|
-
#
|
|
102
|
-
# releases
|
|
99
|
+
# call is released. A round that was stopped before it +finished+
|
|
100
|
+
# writes its claims without yielding to a fiber scheduler, as
|
|
101
|
+
# #unyielding explains, while one that finished, or failed with an
|
|
102
|
+
# error, releases them as usual.
|
|
103
103
|
def release_persisted_tool_calls(tool_call_ids, stopped_by = nil, finished: false) # :nodoc:
|
|
104
104
|
expired = Time.current.change(usec: 0)
|
|
105
105
|
shutting_down = shutting_down?(stopped_by)
|
|
@@ -108,9 +108,11 @@ module RubyLLM
|
|
|
108
108
|
next unless claim
|
|
109
109
|
|
|
110
110
|
kept = claim.interrupted || (claim.started && shutting_down)
|
|
111
|
-
unyielding(
|
|
112
|
-
RubyLLM::ActiveRecord::ToolCall
|
|
113
|
-
|
|
111
|
+
unyielding(finished, stopped_by) do
|
|
112
|
+
ending_claim(RubyLLM::ActiveRecord::ToolCall) do
|
|
113
|
+
RubyLLM::ActiveRecord::ToolCall.where(id: claim.row, result_id: nil, claimed_until: claim.written)
|
|
114
|
+
.update_all(claimed_until: (expired if kept))
|
|
115
|
+
end
|
|
114
116
|
end
|
|
115
117
|
end
|
|
116
118
|
ensure
|
|
@@ -213,11 +215,11 @@ module RubyLLM
|
|
|
213
215
|
@message.destroy if blank_placeholder?
|
|
214
216
|
resync_messages if @chat
|
|
215
217
|
busy.call
|
|
216
|
-
rescue SignalException, SystemExit => e
|
|
218
|
+
rescue SignalException, SystemExit, StandardError => e
|
|
217
219
|
stopped_by = e
|
|
218
220
|
raise
|
|
219
221
|
ensure
|
|
220
|
-
unyielding(
|
|
222
|
+
unyielding(completed, stopped_by) do
|
|
221
223
|
stop_claim_heartbeat
|
|
222
224
|
ensure
|
|
223
225
|
shutting_down?(stopped_by) ? expire_run_claim : release_run_claim(completed)
|
|
@@ -227,11 +229,46 @@ module RubyLLM
|
|
|
227
229
|
# Under a fiber scheduler, as in Async or Solid Queue's fiber workers,
|
|
228
230
|
# a fiber whose thread is exiting or whose reactor is stopping is
|
|
229
231
|
# cancelled, or never resumed, once it waits on a socket, a file or a
|
|
230
|
-
# thread. So a run that
|
|
231
|
-
#
|
|
232
|
-
#
|
|
233
|
-
|
|
234
|
-
|
|
232
|
+
# thread. So a run that was stopped before it +finished+, by a thread
|
|
233
|
+
# kill, a signal, an exit or the scheduler's own stop, ends its claims
|
|
234
|
+
# without yielding to the scheduler. One that finished, or that
|
|
235
|
+
# +stopped_by+ an error while its thread carries on, yields as usual.
|
|
236
|
+
def unyielding(finished, stopped_by, &)
|
|
237
|
+
failed = stopped_by.is_a?(StandardError) && Thread.current.status != 'aborting'
|
|
238
|
+
finished || failed ? yield : Fiber.blocking(&)
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# A fiber that can't yield to its scheduler holds up every other fiber
|
|
242
|
+
# on its thread while it waits for a connection from the pool of
|
|
243
|
+
# +model+, so none of them can return one, and the wait ends in
|
|
244
|
+
# ActiveRecord::ConnectionTimeoutError. Such a fiber ends a claim only
|
|
245
|
+
# with a connection it holds or one that is free, and otherwise leaves
|
|
246
|
+
# the claim to lapse within its lease, rather than raise over what
|
|
247
|
+
# stopped the run. Returns the block's result, or nil when skipped.
|
|
248
|
+
def ending_claim(model, &)
|
|
249
|
+
return yield unless Fiber.scheduler && Fiber.blocking?
|
|
250
|
+
|
|
251
|
+
ending_claim_without_waiting(model.connection_pool, &)
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def ending_claim_without_waiting(pool)
|
|
255
|
+
return yield if pool.active_connection? || connection_free?(pool)
|
|
256
|
+
|
|
257
|
+
RubyLLM.logger.warn("RubyLLM left a stopped run's claim to lapse within #{CLAIM_LEASE} seconds: " \
|
|
258
|
+
'no database connection was free')
|
|
259
|
+
nil
|
|
260
|
+
rescue ::ActiveRecord::ConnectionTimeoutError => e
|
|
261
|
+
RubyLLM.logger.warn("RubyLLM left a stopped run's claim to lapse within #{CLAIM_LEASE} seconds: " \
|
|
262
|
+
"#{e.class}: #{e.message}")
|
|
263
|
+
nil
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# Whether a checkout from +pool+ would get a connection without
|
|
267
|
+
# waiting: one is idle, or the pool may open another, and no thread
|
|
268
|
+
# is waiting ahead of it. Max connections is nil when unlimited.
|
|
269
|
+
def connection_free?(pool)
|
|
270
|
+
stat = pool.stat
|
|
271
|
+
stat[:waiting].zero? && (stat[:idle].positive? || stat[:size].nil? || stat[:connections] < stat[:size])
|
|
235
272
|
end
|
|
236
273
|
|
|
237
274
|
# Whether the process is shutting down: a thread killed at exit, or
|
|
@@ -281,7 +318,11 @@ module RubyLLM
|
|
|
281
318
|
def expire_run_claim
|
|
282
319
|
token = @run_claim_token
|
|
283
320
|
@synced_usage_count = @synced_run_claim_token = @run_claim_token = nil
|
|
284
|
-
|
|
321
|
+
return unless token
|
|
322
|
+
|
|
323
|
+
ending_claim(self.class) do
|
|
324
|
+
chat_row.where(claim_token: token).update_all(claimed_until: Time.current.change(usec: 0))
|
|
325
|
+
end
|
|
285
326
|
end
|
|
286
327
|
|
|
287
328
|
def release_run_claim(completed)
|
|
@@ -298,7 +339,8 @@ module RubyLLM
|
|
|
298
339
|
cancellable = has_attribute?(:cancelled)
|
|
299
340
|
released = { claim_token: nil, claimed_until: nil }
|
|
300
341
|
released[:cancelled] = false if cancellable
|
|
301
|
-
|
|
342
|
+
freed = ending_claim(self.class) { chat_row.where(claim_token: token).update_all(released) == 1 }
|
|
343
|
+
self[:cancelled] = false if freed && cancellable
|
|
302
344
|
end
|
|
303
345
|
|
|
304
346
|
def renew_run_claim(force: false)
|
|
@@ -360,7 +402,7 @@ module RubyLLM
|
|
|
360
402
|
next unless deadline
|
|
361
403
|
|
|
362
404
|
claimed_until = by_exit([now.change(usec: 0) + CLAIM_LEASE, deadline].min)
|
|
363
|
-
next if deadline <= now || claim.written
|
|
405
|
+
next if deadline <= now || claim.written == [claimed_until]
|
|
364
406
|
|
|
365
407
|
[tool_call_id, claim.row, claim.written.dup, claim.written.push(claimed_until).last]
|
|
366
408
|
end
|
|
@@ -409,10 +451,23 @@ module RubyLLM
|
|
|
409
451
|
|
|
410
452
|
claim_lock
|
|
411
453
|
@claim_heartbeat = ClaimHeartbeat.new([self.class, RubyLLM::ActiveRecord::ToolCall], claim_renewal_interval) do
|
|
412
|
-
|
|
454
|
+
renew_claim_leases_in_callbacks
|
|
413
455
|
end
|
|
414
456
|
end
|
|
415
457
|
|
|
458
|
+
# A callback that raises may have rolled back renewals already
|
|
459
|
+
# counted as written, so the expiries from before stay good matches
|
|
460
|
+
# for the rows, and with more than one the next renewal writes again.
|
|
461
|
+
def renew_claim_leases_in_callbacks
|
|
462
|
+
written = claim_lock.synchronize { tool_call_claims.transform_values { |claim| claim.written.dup } }
|
|
463
|
+
run_callbacks(:claim_renewal) { renew_claim_leases }
|
|
464
|
+
rescue StandardError
|
|
465
|
+
claim_lock.synchronize do
|
|
466
|
+
tool_call_claims.each { |tool_call_id, claim| claim.written |= written.fetch(tool_call_id, []) }
|
|
467
|
+
end
|
|
468
|
+
raise
|
|
469
|
+
end
|
|
470
|
+
|
|
416
471
|
def stop_idle_claim_heartbeat
|
|
417
472
|
stop_claim_heartbeat unless @run_claim_token || claim_lock.synchronize { tool_call_claims.any? }
|
|
418
473
|
end
|
data/lib/ruby_llm/agent.rb
CHANGED
|
@@ -194,8 +194,8 @@ module RubyLLM
|
|
|
194
194
|
|
|
195
195
|
# Sets how chats this agent builds use their tools, applied via
|
|
196
196
|
# Chat#with_tool_options. Accepts +choice:+, +calls:+, +concurrency:+,
|
|
197
|
-
# and +rounds:+. A block defers evaluation until the chat
|
|
198
|
-
# Called with no arguments, returns the configured options.
|
|
197
|
+
# +workers:+, and +rounds:+. A block defers evaluation until the chat
|
|
198
|
+
# is built. Called with no arguments, returns the configured options.
|
|
199
199
|
#
|
|
200
200
|
# tool_options choice: :required, calls: :one
|
|
201
201
|
# tool_options rounds: 25, calls: 50
|
data/lib/ruby_llm/aliases.json
CHANGED
|
@@ -134,6 +134,10 @@
|
|
|
134
134
|
"vertexai": "deepseek-ai/deepseek-v4.1-flash-maas",
|
|
135
135
|
"openrouter": "deepseek/deepseek-v4.1-flash"
|
|
136
136
|
},
|
|
137
|
+
"deepseek-v4.1-flash-1017": {
|
|
138
|
+
"vertexai": "deepseek-ai/deepseek-v4.1-flash-1017-maas",
|
|
139
|
+
"openrouter": "deepseek/deepseek-v4.1-flash-1017"
|
|
140
|
+
},
|
|
137
141
|
"enhanced": {
|
|
138
142
|
"deepgram": "enhanced-general"
|
|
139
143
|
},
|
|
@@ -521,6 +525,16 @@
|
|
|
521
525
|
"openrouter": "openai/gpt-6.1-terra",
|
|
522
526
|
"azure": "gpt-6.1-terra"
|
|
523
527
|
},
|
|
528
|
+
"gpt-6.2-astra": {
|
|
529
|
+
"openai": "gpt-6.2-astra",
|
|
530
|
+
"openrouter": "openai/gpt-6.2-astra",
|
|
531
|
+
"azure": "gpt-6.2-astra"
|
|
532
|
+
},
|
|
533
|
+
"gpt-6.2-sol": {
|
|
534
|
+
"openai": "gpt-6.2-sol",
|
|
535
|
+
"openrouter": "openai/gpt-6.2-sol",
|
|
536
|
+
"azure": "gpt-6.2-sol"
|
|
537
|
+
},
|
|
524
538
|
"gpt-audio": {
|
|
525
539
|
"openai": "gpt-audio",
|
|
526
540
|
"openrouter": "openai/gpt-audio",
|
|
@@ -104,7 +104,7 @@ module RubyLLM
|
|
|
104
104
|
end
|
|
105
105
|
|
|
106
106
|
# Frees the claims on +ids+ once their round ends, whether it ended
|
|
107
|
-
# normally or by +stopped_by+, the signal or
|
|
107
|
+
# normally or by +stopped_by+, the signal, exit or error that stopped it.
|
|
108
108
|
# A round that +finished+ left only calls that paused unanswered.
|
|
109
109
|
def release(_ids, _stopped_by = nil, finished: false); end
|
|
110
110
|
|
|
@@ -10,14 +10,15 @@ module RubyLLM
|
|
|
10
10
|
|
|
11
11
|
# Runs +tool_calls+ in rounds split by the calls +exclusive+ picks
|
|
12
12
|
# out. A round with a result +paused+ picks out stops the rounds
|
|
13
|
-
# after it, which wait for that call to finish.
|
|
14
|
-
|
|
13
|
+
# after it, which wait for that call to finish. +workers+ threads or
|
|
14
|
+
# fibers share a round's calls; without it each call gets its own.
|
|
15
|
+
def run(mode, tool_calls, exclusive: nil, paused: nil, workers: nil, on_result: nil, config: nil, &)
|
|
15
16
|
rounds = rounds(tool_calls, exclusive)
|
|
16
17
|
case mode
|
|
17
18
|
when :threads
|
|
18
|
-
run_with_threads(rounds, paused:, on_result:, config:, &)
|
|
19
|
+
run_with_threads(rounds, paused:, workers:, on_result:, config:, &)
|
|
19
20
|
when :fibers
|
|
20
|
-
run_with_fibers(rounds, paused:, on_result:, config:, &)
|
|
21
|
+
run_with_fibers(rounds, paused:, workers:, on_result:, config:, &)
|
|
21
22
|
end
|
|
22
23
|
end
|
|
23
24
|
|
|
@@ -31,7 +32,7 @@ module RubyLLM
|
|
|
31
32
|
indexed.chunk_while { |(first, _), (second, _)| !exclusive.call(first) && !exclusive.call(second) }.to_a
|
|
32
33
|
end
|
|
33
34
|
|
|
34
|
-
def run_with_threads(rounds, paused:, on_result:, config:, &execute)
|
|
35
|
+
def run_with_threads(rounds, paused:, workers:, on_result:, config:, &execute)
|
|
35
36
|
caller_context = execution_context
|
|
36
37
|
workflow_context = Support::Instrumentation.current_workflow
|
|
37
38
|
instrumentation_context = Support::Instrumentation.capture_context(config:)
|
|
@@ -39,18 +40,18 @@ module RubyLLM
|
|
|
39
40
|
rounds.each do |round|
|
|
40
41
|
queue = Queue.new
|
|
41
42
|
threads = []
|
|
42
|
-
|
|
43
|
+
jobs = jobs(round)
|
|
44
|
+
pool_size(round, workers).times do
|
|
43
45
|
thread = Thread.new do
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
queue << (result || Result.new(index:, tool_call:, error: thread_exited_error(tool_call)))
|
|
46
|
+
work_through(jobs, queue, workers) do |tool_call, index|
|
|
47
|
+
capture_result(index, tool_call, caller_context, execute, workflow_context:, instrumentation_context:)
|
|
48
|
+
end
|
|
48
49
|
end
|
|
49
50
|
thread.report_on_exception = false
|
|
50
51
|
threads << thread
|
|
51
52
|
end
|
|
52
53
|
|
|
53
|
-
collect_results(queue,
|
|
54
|
+
collect_results(queue, round.size, results, on_result:)
|
|
54
55
|
break if paused?(round, results, paused)
|
|
55
56
|
ensure
|
|
56
57
|
threads&.each(&:join)
|
|
@@ -58,7 +59,7 @@ module RubyLLM
|
|
|
58
59
|
results
|
|
59
60
|
end
|
|
60
61
|
|
|
61
|
-
def run_with_fibers(rounds, paused:, on_result:, config:, &execute)
|
|
62
|
+
def run_with_fibers(rounds, paused:, workers:, on_result:, config:, &execute)
|
|
62
63
|
require_async
|
|
63
64
|
caller_context = execution_context
|
|
64
65
|
contexts = { workflow_context: Support::Instrumentation.current_workflow,
|
|
@@ -69,7 +70,7 @@ module RubyLLM
|
|
|
69
70
|
Sync do |task|
|
|
70
71
|
isolated(caller_context) do
|
|
71
72
|
rounds.each do |round|
|
|
72
|
-
run_fiber_round(task, round, results, caller_context, contexts, on_result:, &execute)
|
|
73
|
+
run_fiber_round(task, round, results, caller_context, contexts, workers:, on_result:, &execute)
|
|
73
74
|
break if paused?(round, results, paused)
|
|
74
75
|
end
|
|
75
76
|
end
|
|
@@ -77,15 +78,19 @@ module RubyLLM
|
|
|
77
78
|
results
|
|
78
79
|
end
|
|
79
80
|
|
|
80
|
-
def run_fiber_round(task, round, results, caller_context, contexts, on_result:, &execute)
|
|
81
|
+
def run_fiber_round(task, round, results, caller_context, contexts, workers:, on_result:, &execute)
|
|
81
82
|
queue = Async::Queue.new
|
|
82
83
|
tasks = []
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
tasks <<
|
|
84
|
+
jobs = jobs(round)
|
|
85
|
+
pool_size(round, workers).times do
|
|
86
|
+
tasks << task.async do
|
|
87
|
+
work_through(jobs, queue, workers) do |tool_call, index|
|
|
88
|
+
capture_result(index, tool_call, caller_context, execute, **contexts)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
86
91
|
end
|
|
87
92
|
|
|
88
|
-
collect_results(queue,
|
|
93
|
+
collect_results(queue, round.size, results, on_result:)
|
|
89
94
|
rescue Async::Stop
|
|
90
95
|
tasks&.each(&:stop)
|
|
91
96
|
raise
|
|
@@ -93,6 +98,40 @@ module RubyLLM
|
|
|
93
98
|
tasks&.each(&:wait)
|
|
94
99
|
end
|
|
95
100
|
|
|
101
|
+
def jobs(round)
|
|
102
|
+
Queue.new.tap do |jobs|
|
|
103
|
+
round.each { |job| jobs << job }
|
|
104
|
+
jobs.close
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def pool_size(round, workers)
|
|
109
|
+
[workers || round.size, round.size].min
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Takes one call, or with a pool every call it can get. A pool has
|
|
113
|
+
# nobody left to run the calls behind one that raised or whose thread
|
|
114
|
+
# exited, so they fail with the same error and the round still ends.
|
|
115
|
+
def work_through(jobs, queue, pooled)
|
|
116
|
+
while (job = jobs.pop)
|
|
117
|
+
result = nil
|
|
118
|
+
begin
|
|
119
|
+
result = yield(*job)
|
|
120
|
+
ensure
|
|
121
|
+
result ||= Result.new(index: job.last, tool_call: job.first, error: thread_exited_error(job.first))
|
|
122
|
+
queue << result
|
|
123
|
+
fail_remaining(jobs, queue, result.error) if pooled && result.error
|
|
124
|
+
end
|
|
125
|
+
break unless pooled
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def fail_remaining(jobs, queue, error)
|
|
130
|
+
while (job = jobs.pop)
|
|
131
|
+
queue << Result.new(index: job.last, tool_call: job.first, error:)
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
96
135
|
def require_async
|
|
97
136
|
begin
|
|
98
137
|
require 'async'
|
|
@@ -169,6 +208,7 @@ module RubyLLM
|
|
|
169
208
|
end
|
|
170
209
|
|
|
171
210
|
private_class_method :rounds, :paused?, :run_with_threads, :run_with_fibers, :run_fiber_round, :require_async,
|
|
211
|
+
:jobs, :pool_size, :work_through, :fail_remaining,
|
|
172
212
|
:collect_results, :capture_result, :thread_exited_error, :isolated,
|
|
173
213
|
:execution_context, :rails_executor
|
|
174
214
|
end
|
data/lib/ruby_llm/chat.rb
CHANGED
|
@@ -35,7 +35,7 @@ module RubyLLM
|
|
|
35
35
|
# The provider-neutral options #with_compaction accepts.
|
|
36
36
|
COMPACTION_OPTIONS = %i[at instructions pause_after].freeze
|
|
37
37
|
THINKING_OPTIONS = %i[effort budget display].freeze
|
|
38
|
-
TOOL_OPTIONS = %i[choice calls concurrency rounds].freeze
|
|
38
|
+
TOOL_OPTIONS = %i[choice calls concurrency workers rounds].freeze
|
|
39
39
|
PAUSED = Object.new.freeze
|
|
40
40
|
TOOL_CHOICE_MODES = %i[auto none required].freeze
|
|
41
41
|
AGENT_RUN_KEY = :ruby_llm_agent_run
|
|
@@ -138,11 +138,12 @@ module RubyLLM
|
|
|
138
138
|
|
|
139
139
|
attr_reader :tool_prefs, :fallback_errors, :usage_entries # :nodoc:
|
|
140
140
|
|
|
141
|
-
# Returns the +choice+, +calls+, +concurrency+, and +rounds+
|
|
142
|
-
# #with_tool_options, with +nil+ for anything left at the
|
|
141
|
+
# Returns the +choice+, +calls+, +concurrency+, +workers+, and +rounds+
|
|
142
|
+
# set with #with_tool_options, with +nil+ for anything left at the
|
|
143
|
+
# default.
|
|
143
144
|
def tool_options
|
|
144
145
|
{ choice: tool_prefs[:choice], calls: tool_prefs[:calls] || @tool_call_cap, concurrency: concurrency,
|
|
145
|
-
rounds: @tool_rounds }
|
|
146
|
+
workers: @tool_workers, rounds: @tool_rounds }
|
|
146
147
|
end
|
|
147
148
|
|
|
148
149
|
# Creates a chat with +model:+, or with the configured default model
|
|
@@ -855,6 +856,9 @@ module RubyLLM
|
|
|
855
856
|
# emits one +tool_calls_capped.ruby_llm+ event per response. It is off
|
|
856
857
|
# by default. +concurrency:+ runs tool calls
|
|
857
858
|
# concurrently: +true+ or +:threads+ for threads, +:fibers+ for fibers.
|
|
859
|
+
# +workers:+ limits how many of a response's calls run at once under
|
|
860
|
+
# +concurrency:+. The others wait their turn, and every call still
|
|
861
|
+
# runs. Without it they all start together.
|
|
858
862
|
# +rounds:+ limits how many model responses that ask for tools one
|
|
859
863
|
# #complete may run, however many calls each holds. It is off by
|
|
860
864
|
# default. Once the last round's results are saved, #complete raises
|
|
@@ -862,10 +866,12 @@ module RubyLLM
|
|
|
862
866
|
# #complete continues with a fresh count, as does a pause for a person.
|
|
863
867
|
# #step is not limited. An omitted option is left unchanged; passing
|
|
864
868
|
# +nil+ explicitly resets that option (+concurrency: nil+ returns to the
|
|
865
|
-
# configured default, +rounds: nil+
|
|
869
|
+
# configured default, +rounds: nil+ and +workers: nil+ remove the
|
|
870
|
+
# limit). Returns +self+.
|
|
866
871
|
#
|
|
867
872
|
# chat.with_tools(Weather, Search).with_tool_options(choice: :required)
|
|
868
873
|
# chat.with_tool_options(calls: :one, concurrency: :threads)
|
|
874
|
+
# chat.with_tool_options(concurrency: :threads, workers: 4)
|
|
869
875
|
# chat.with_tool_options(rounds: 25, calls: 50)
|
|
870
876
|
# chat.with_tool_options(choice: nil)
|
|
871
877
|
#
|
|
@@ -875,7 +881,8 @@ module RubyLLM
|
|
|
875
881
|
when :choice then apply_tool_choice(value)
|
|
876
882
|
when :calls then apply_tool_calls(value)
|
|
877
883
|
when :concurrency then apply_tool_concurrency(value)
|
|
878
|
-
when :
|
|
884
|
+
when :workers then @tool_workers = normalize_limit(:workers, value)
|
|
885
|
+
when :rounds then @tool_rounds = normalize_limit(:rounds, value)
|
|
879
886
|
else raise ArgumentError, "Unknown tool option: #{option}. Valid options are: #{TOOL_OPTIONS.join(', ')}"
|
|
880
887
|
end
|
|
881
888
|
end
|
|
@@ -2369,11 +2376,10 @@ module RubyLLM
|
|
|
2369
2376
|
end
|
|
2370
2377
|
end
|
|
2371
2378
|
|
|
2372
|
-
# A run's names shift as files arrive, so descriptions are not memoized.
|
|
2373
2379
|
def attachment_describer
|
|
2374
2380
|
return ->(_attachment) {} unless @code_execution
|
|
2375
2381
|
|
|
2376
|
-
CodeExecution.describer(messages
|
|
2382
|
+
CodeExecution.describer(messages)
|
|
2377
2383
|
end
|
|
2378
2384
|
|
|
2379
2385
|
# A process that dies mid-round leaves a blank assistant placeholder,
|
|
@@ -2602,7 +2608,7 @@ module RubyLLM
|
|
|
2602
2608
|
handle_sequential_tool_calls(runnable)
|
|
2603
2609
|
end
|
|
2604
2610
|
finished = true
|
|
2605
|
-
rescue SignalException, SystemExit => e
|
|
2611
|
+
rescue SignalException, SystemExit, StandardError => e
|
|
2606
2612
|
stopped_by = e
|
|
2607
2613
|
raise
|
|
2608
2614
|
ensure
|
|
@@ -2776,7 +2782,8 @@ module RubyLLM
|
|
|
2776
2782
|
def execute_tools_concurrently(tool_calls, &on_result)
|
|
2777
2783
|
exclusive = ->(tool_call) { exclusive_call?(tool_call) }
|
|
2778
2784
|
paused = ->(result) { result.equal?(PAUSED) }
|
|
2779
|
-
ToolConcurrency.run(concurrency, tool_calls, exclusive:, paused:,
|
|
2785
|
+
ToolConcurrency.run(concurrency, tool_calls, exclusive:, paused:, workers: @tool_workers, on_result:,
|
|
2786
|
+
config: @config) do |tool_call|
|
|
2780
2787
|
execute_tool_with_callbacks(tool_call)
|
|
2781
2788
|
end
|
|
2782
2789
|
end
|
|
@@ -3117,10 +3124,10 @@ module RubyLLM
|
|
|
3117
3124
|
end
|
|
3118
3125
|
end
|
|
3119
3126
|
|
|
3120
|
-
def
|
|
3121
|
-
return
|
|
3127
|
+
def normalize_limit(option, limit)
|
|
3128
|
+
return limit if limit.nil? || (limit.is_a?(Integer) && limit.positive?)
|
|
3122
3129
|
|
|
3123
|
-
raise ArgumentError, "Invalid
|
|
3130
|
+
raise ArgumentError, "Invalid #{option} value: #{limit.inspect}. Valid values are positive integers"
|
|
3124
3131
|
end
|
|
3125
3132
|
|
|
3126
3133
|
def normalize_tool_choice(choice)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyLLM
|
|
4
|
+
class CodeExecution
|
|
5
|
+
# Names the conversation's files under +attachments/+ in the order they
|
|
6
|
+
# arrived. The newest file of a name keeps it, as it would on a disk,
|
|
7
|
+
# and the file that held it moves to a numbered name once and stays
|
|
8
|
+
# there, so a name the model was told never points at another earlier
|
|
9
|
+
# file. Names compare without case, for the filesystems that do, and
|
|
10
|
+
# files with equal content share one.
|
|
11
|
+
class InputNames # :nodoc:
|
|
12
|
+
Move = Struct.new(:files, :from, :to)
|
|
13
|
+
|
|
14
|
+
# Returns each named attachment's name, by identity.
|
|
15
|
+
attr_reader :names
|
|
16
|
+
|
|
17
|
+
def initialize
|
|
18
|
+
@names = {}.compare_by_identity
|
|
19
|
+
@holders = {}
|
|
20
|
+
@folders = Set.new
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Names +attachment+ and returns the moves that made room for it.
|
|
24
|
+
def add(attachment)
|
|
25
|
+
return [] if attachment.provider_file? || @names.key?(attachment)
|
|
26
|
+
|
|
27
|
+
name = Workspace.filename(attachment.filename)
|
|
28
|
+
moves = clear_folders(name)
|
|
29
|
+
holder = @holders[name.downcase]
|
|
30
|
+
if holder && holder.last.first.content.b == attachment.content.b
|
|
31
|
+
hold(holder.first, holder.last << attachment)
|
|
32
|
+
else
|
|
33
|
+
hold(name, [attachment])
|
|
34
|
+
moves << relocate(holder) if holder
|
|
35
|
+
end
|
|
36
|
+
moves
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def hold(name, files)
|
|
42
|
+
@holders[name.downcase] = [name, files]
|
|
43
|
+
files.each { |file| @names[file] = name }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def relocate(holder)
|
|
47
|
+
from, files = holder
|
|
48
|
+
to = numbered_names(original_name(files)).find do |candidate|
|
|
49
|
+
!@holders.key?(candidate.downcase) && !@folders.include?(candidate.downcase)
|
|
50
|
+
end
|
|
51
|
+
hold(to, files)
|
|
52
|
+
Move.new(files, from, to)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# A numbered file gives way to a folder another input needs.
|
|
56
|
+
def clear_folders(name)
|
|
57
|
+
parts = name.downcase.split('/')
|
|
58
|
+
folders = (1...parts.size).map { |size| parts.first(size).join('/') }
|
|
59
|
+
@folders.merge(folders)
|
|
60
|
+
folders.filter_map do |folder|
|
|
61
|
+
holder = @holders[folder]
|
|
62
|
+
next if holder.nil? || holder.first == original_name(holder.last)
|
|
63
|
+
|
|
64
|
+
@holders.delete(folder)
|
|
65
|
+
relocate(holder)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def original_name(files)
|
|
70
|
+
Workspace.filename(files.first.filename)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def numbered_names(name)
|
|
74
|
+
extension = File.extname(name)
|
|
75
|
+
stem = name.delete_suffix(extension)
|
|
76
|
+
(2..).lazy.map { |number| "#{stem}-#{number}#{extension}" }
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|