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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/lib/ruby_llm/accounting/usage.rb +1 -0
  4. data/lib/ruby_llm/active_record/chat_methods.rb +28 -1
  5. data/lib/ruby_llm/active_record/model.rb +1 -1
  6. data/lib/ruby_llm/active_record/run_claims.rb +73 -18
  7. data/lib/ruby_llm/agent.rb +2 -2
  8. data/lib/ruby_llm/aliases.json +14 -0
  9. data/lib/ruby_llm/chat/tool_call_state.rb +1 -1
  10. data/lib/ruby_llm/chat/tool_concurrency.rb +58 -18
  11. data/lib/ruby_llm/chat.rb +20 -13
  12. data/lib/ruby_llm/code_execution/input_names.rb +80 -0
  13. data/lib/ruby_llm/code_execution/workspace.rb +4 -37
  14. data/lib/ruby_llm/code_execution.rb +44 -12
  15. data/lib/ruby_llm/configuration.rb +55 -0
  16. data/lib/ruby_llm/file_store/directory.rb +10 -7
  17. data/lib/ruby_llm/mcp/content.rb +2 -0
  18. data/lib/ruby_llm/model/pricing.rb +46 -0
  19. data/lib/ruby_llm/model.rb +55 -4
  20. data/lib/ruby_llm/models/lookup.rb +2 -1
  21. data/lib/ruby_llm/models.json +3281 -1791
  22. data/lib/ruby_llm/models.rb +1 -1
  23. data/lib/ruby_llm/protocol/stream_accumulator.rb +9 -4
  24. data/lib/ruby_llm/protocol/streaming.rb +22 -8
  25. data/lib/ruby_llm/protocols/bedrock/event_stream.rb +3 -7
  26. data/lib/ruby_llm/protocols/converse/streaming.rb +2 -2
  27. data/lib/ruby_llm/protocols/gemini/decisions.rb +106 -0
  28. data/lib/ruby_llm/protocols/gemini/streaming.rb +1 -1
  29. data/lib/ruby_llm/providers/gemini.rb +2 -0
  30. data/lib/ruby_llm/providers/minimax.rb +0 -6
  31. data/lib/ruby_llm/providers/openai/rerank.rb +13 -0
  32. data/lib/ruby_llm/providers/openai.rb +14 -0
  33. data/lib/ruby_llm/question.rb +60 -5
  34. data/lib/ruby_llm/tool.rb +24 -5
  35. data/lib/ruby_llm/transport/connection.rb +8 -5
  36. data/lib/ruby_llm/transport/log_formatter.rb +39 -0
  37. data/lib/ruby_llm/transport/release_request.rb +6 -2
  38. data/lib/ruby_llm/version.rb +1 -1
  39. data/lib/ruby_llm.rb +0 -1
  40. data/skills/rubyllm/references/cost-and-tracing.md +1 -1
  41. data/skills/rubyllm/references/tools-and-agents.md +1 -1
  42. metadata +5 -3
  43. data/lib/ruby_llm/providers/minimax/chat_completions.rb +0 -48
@@ -363,7 +363,7 @@ module RubyLLM
363
363
  if models_dev_model.type == :chat && provider_model.type != :chat
364
364
  data[:modalities] = provider_model.modalities.to_h
365
365
  end
366
- data[:pricing] = Support::Utils.deep_merge(provider_model.pricing.to_h, data[:pricing].to_h)
366
+ data[:pricing] = Support::Utils.deep_merge(provider_model.registry_pricing.to_h, data[:pricing].to_h)
367
367
  data[:metadata] = provider_model.metadata.merge(data[:metadata] || {})
368
368
  data[:capabilities] = merge_capabilities(models_dev_model, provider_model, data[:modalities])
369
369
  normalize_embedding_modalities(data)
@@ -8,7 +8,8 @@ module RubyLLM
8
8
  class StreamAccumulator # :nodoc:
9
9
  attr_reader :content, :model, :tool_calls
10
10
 
11
- def initialize
11
+ def initialize(log: nil)
12
+ @log = log
12
13
  @content = +''
13
14
  @attachments = []
14
15
  @citations = []
@@ -27,7 +28,7 @@ module RubyLLM
27
28
  end
28
29
 
29
30
  def add(chunk)
30
- RubyLLM.logger.debug { chunk.inspect } if RubyLLM.config.log_stream_debug
31
+ log_stream_debug { chunk.inspect }
31
32
  @model = chunk.model if @model.to_s.empty?
32
33
 
33
34
  handle_chunk_content(chunk)
@@ -39,7 +40,7 @@ module RubyLLM
39
40
  @finish_reason = chunk.finish_reason if chunk.finish_reason
40
41
  accumulate_content_filters(chunk.content_filters)
41
42
  count_tokens chunk
42
- RubyLLM.logger.debug { inspect } if RubyLLM.config.log_stream_debug
43
+ log_stream_debug { inspect }
43
44
  end
44
45
 
45
46
  def to_message(response)
@@ -147,7 +148,7 @@ module RubyLLM
147
148
  end
148
149
 
149
150
  def accumulate_tool_calls(new_tool_calls)
150
- RubyLLM.logger.debug { "Accumulating tool calls: #{new_tool_calls}" } if RubyLLM.config.log_stream_debug
151
+ log_stream_debug { "Accumulating tool calls: #{new_tool_calls}" }
151
152
  new_tool_calls.to_h.each do |stream_key, tool_call|
152
153
  if continues_tool_call?(stream_key, tool_call)
153
154
  append_tool_call_fragment(stream_key, tool_call)
@@ -233,6 +234,10 @@ module RubyLLM
233
234
  end
234
235
  @thinking_signature ||= thinking.signature # rubocop:disable Naming/MemoizedInstanceVariableName
235
236
  end
237
+
238
+ def log_stream_debug(&)
239
+ @log&.call(&)
240
+ end
236
241
  end
237
242
  end
238
243
  end
@@ -56,7 +56,7 @@ module RubyLLM
56
56
  module_function
57
57
 
58
58
  def stream_response(payload, additional_headers = {}, &block)
59
- accumulator = StreamAccumulator.new
59
+ accumulator = StreamAccumulator.new(log: method(:log_stream_debug))
60
60
  received = false
61
61
 
62
62
  response = stream_events(stream_url, payload, additional_headers) do |data|
@@ -73,7 +73,7 @@ module RubyLLM
73
73
  end
74
74
 
75
75
  message = accumulator.to_message(response)
76
- RubyLLM.logger.debug { "Stream completed: #{message.content}" }
76
+ log_body_debug { "Stream completed: #{message.content}" }
77
77
  message
78
78
  end
79
79
 
@@ -107,6 +107,20 @@ module RubyLLM
107
107
  Faraday::VERSION.start_with?('1')
108
108
  end
109
109
 
110
+ def log_debug(&)
111
+ Transport::LogFormatter.log_debug(config, &)
112
+ end
113
+
114
+ # For lines that quote what the provider sent, such as the answer or a
115
+ # JSON parse error, which includes the text it could not parse.
116
+ def log_body_debug(&)
117
+ log_debug(&) unless config.log_bodies == false
118
+ end
119
+
120
+ def log_stream_debug(&)
121
+ log_debug(&) if config.log_stream_debug
122
+ end
123
+
110
124
  def post_stream(url, payload, on_data:, progress:, on_reset:, on_failure: nil)
111
125
  @connection.post url, payload, usage: @usage_tracker, stream: true do |request|
112
126
  yield request if block_given?
@@ -143,7 +157,7 @@ module RubyLLM
143
157
  end
144
158
 
145
159
  def process_stream_chunk(chunk, state, env, progress, &)
146
- RubyLLM.logger.debug { "Received chunk: #{chunk}" } if RubyLLM.config.log_stream_debug
160
+ log_stream_debug { "Received chunk: #{chunk}" }
147
161
 
148
162
  if json_body?(chunk, state)
149
163
  handle_json_body(chunk, state, env)
@@ -186,7 +200,7 @@ module RubyLLM
186
200
  parsed = JSON.parse(state.buffer)
187
201
  raise_stream_error(state.buffer, parsed, env) if parsed.is_a?(Hash) && parsed['error']
188
202
  rescue JSON::ParserError
189
- RubyLLM.logger.debug { 'Waiting for the remaining JSON response bytes' }
203
+ log_debug { 'Waiting for the remaining JSON response bytes' }
190
204
  end
191
205
 
192
206
  # Runs once the response has ended, when a body that never parsed as
@@ -207,7 +221,7 @@ module RubyLLM
207
221
  error_data = JSON.parse(buffer)
208
222
  raise_stream_error(buffer, error_data, env)
209
223
  rescue JSON::ParserError
210
- RubyLLM.logger.debug { "Accumulating error chunk: #{chunk}" }
224
+ log_stream_debug { "Accumulating error chunk: #{chunk}" }
211
225
  end
212
226
 
213
227
  # Marking progress just before a chunk reaches the user lets the retry
@@ -237,7 +251,7 @@ module RubyLLM
237
251
 
238
252
  raise_stream_error(data, parsed, env)
239
253
  rescue JSON::ParserError => e
240
- RubyLLM.logger.debug { "Failed to parse data chunk: #{e.message}" }
254
+ log_body_debug { "Failed to parse data chunk: #{e.message}" }
241
255
  end
242
256
 
243
257
  def handle_error_event(data, env)
@@ -249,7 +263,7 @@ module RubyLLM
249
263
  message = error_data.is_a?(Hash) ? error_data['message'] : error_data.to_s
250
264
  [500, message || 'Unknown streaming error']
251
265
  rescue JSON::ParserError => e
252
- RubyLLM.logger.debug { "Failed to parse streaming error: #{e.message}" }
266
+ log_body_debug { "Failed to parse streaming error: #{e.message}" }
253
267
  [500, "Failed to parse error: #{data}"]
254
268
  end
255
269
 
@@ -264,7 +278,7 @@ module RubyLLM
264
278
  parsed_data = JSON.parse(data)
265
279
  raise_stream_error(data, parsed_data, env)
266
280
  rescue JSON::ParserError => e
267
- RubyLLM.logger.debug { "#{error_message}: #{e.message}" }
281
+ log_body_debug { "#{error_message}: #{e.message}" }
268
282
  end
269
283
 
270
284
  def build_stream_error_response(parsed_data, env, status)
@@ -59,7 +59,7 @@ module RubyLLM
59
59
  error_response = env.merge(body: JSON.parse(buffer))
60
60
  Transport::ErrorMiddleware.parse_error(provider: self, response: error_response)
61
61
  rescue JSON::ParserError
62
- RubyLLM.logger.debug { "Accumulating Bedrock stream error chunk: #{chunk}" }
62
+ log_stream_debug { "Accumulating Bedrock stream error chunk: #{chunk}" }
63
63
  end
64
64
 
65
65
  def failed_stream_buffer(env)
@@ -112,11 +112,7 @@ module RubyLLM
112
112
 
113
113
  while message
114
114
  event = decode_event(message)
115
- if event && RubyLLM.config.log_stream_debug
116
- RubyLLM.logger.debug do
117
- "Bedrock stream event keys: #{event.keys}"
118
- end
119
- end
115
+ log_stream_debug { "Bedrock stream event keys: #{event.keys}" } if event
120
116
  events << event if event
121
117
  break if eof
122
118
 
@@ -178,7 +174,7 @@ module RubyLLM
178
174
  outer
179
175
  end
180
176
  rescue JSON::ParserError => e
181
- RubyLLM.logger.debug { "Failed to decode Bedrock stream event payload: #{e.message}" }
177
+ log_body_debug { "Failed to decode Bedrock stream event payload: #{e.message}" }
182
178
  nil
183
179
  end
184
180
 
@@ -19,7 +19,7 @@ module RubyLLM
19
19
  @thinking_stream = ThinkingStream.new
20
20
  @stream_text_lengths = Hash.new(0)
21
21
  @stream_citations = {}
22
- accumulator = RubyLLM::Protocol::StreamAccumulator.new
22
+ accumulator = RubyLLM::Protocol::StreamAccumulator.new(log: method(:log_stream_debug))
23
23
 
24
24
  response = stream_events(stream_url, JSON.generate(payload), additional_headers) do |event|
25
25
  chunk = build_chunk(event)
@@ -28,7 +28,7 @@ module RubyLLM
28
28
  end
29
29
 
30
30
  message = accumulator.to_message(response)
31
- RubyLLM.logger.debug { "Stream completed: #{message.content}" }
31
+ log_body_debug { "Stream completed: #{message.content}" }
32
32
  message
33
33
  end
34
34
 
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ module Protocols
5
+ class Gemini
6
+ class Decisions < Protocol # :nodoc:
7
+ include SystemOne::Judgments
8
+ include SystemOne::Responses
9
+
10
+ def judge(...)
11
+ super
12
+ rescue BadRequestError => e
13
+ raise unless %w[safety prohibited_content blocklist image_safety content_blocked].include?(e.error_code)
14
+
15
+ raise ContentFilterError.new(response: e.response, provider: e.provider, error_code: e.error_code,
16
+ provider_message: e.provider_message, redact: @config.redact_error_messages)
17
+ end
18
+
19
+ def judgment_url
20
+ 'decisions'
21
+ end
22
+
23
+ def render_judgment_payload(input, questions:, model:, with: [], provider_options: {})
24
+ reserved = provider_options.keys.map(&:to_s) & %w[model input questions]
25
+ unless reserved.empty?
26
+ raise ArgumentError, "Use the judgment arguments instead of provider_options for #{reserved.join(', ')}"
27
+ end
28
+
29
+ {
30
+ model: "models/#{model.delete_prefix('models/')}",
31
+ input: render_input(input, with),
32
+ questions: questions.transform_values { |question| render_question(question) }
33
+ }.merge(provider_options)
34
+ end
35
+
36
+ def parse_judgment_response(response, questions:)
37
+ body = response.body
38
+ tokens = Tokens.new(input: body.dig('usage', 'total_input_tokens'), output: 0)
39
+ @usage_tracker&.observe_tokens(tokens)
40
+
41
+ answers = questions.values.to_h do |question|
42
+ [question.name, parse_answer(body.fetch('answers').fetch(question.name.to_s), question)]
43
+ end
44
+ RubyLLM::Judgment.new(answers:, model: @model.id, raw: response, model_info: @model, tokens:)
45
+ rescue KeyError, NoMethodError, TypeError => e
46
+ raise Error.new('Gemini Decisions returned an invalid judgment', provider_message: e.message, response:,
47
+ redact: @config&.redact_error_messages)
48
+ end
49
+
50
+ private
51
+
52
+ def parse_score(answer, question)
53
+ indexes = question.criteria.each_index.to_h { |index| [index.to_s, index] }
54
+ RubyLLM::Score.new(
55
+ score: answer.fetch('score'), levels: question.criteria,
56
+ probabilities: parse_probabilities(answer.fetch('probabilities'), indexes),
57
+ confidence: answer.fetch('confidence')
58
+ )
59
+ end
60
+
61
+ def render_question(question)
62
+ payload = super
63
+ payload[:instructions] = describe(question.instructions)
64
+ criteria = payload[:criteria]
65
+ criteria = criteria.transform_keys { |key| "#{key}_description" } if question.type == :probability && criteria
66
+ payload[:criteria] = case criteria
67
+ when Hash
68
+ criteria.transform_keys(&:to_s).transform_values { |value| describe(value).to_s }
69
+ when Array then criteria.map { |value| describe(value).to_s }
70
+ end
71
+ payload.compact
72
+ end
73
+
74
+ def render_input(input, attachments)
75
+ text = describe(input)
76
+ return text if attachments.empty?
77
+
78
+ parts = attachments.map { |attachment| render_attachment(attachment) }
79
+ parts.unshift({ type: 'text', text: }) unless text.nil? || text.empty?
80
+ parts
81
+ end
82
+
83
+ def render_attachment(attachment)
84
+ return { type: 'text', text: attachment.content } if attachment.text? && !attachment.provider_file?
85
+
86
+ type = Interactions::Content.interaction_attachment_type(attachment)
87
+ raise UnsupportedAttachmentError, attachment.mime_type unless type
88
+
89
+ part = { type:, mime_type: attachment.mime_type }
90
+ part[:resolution] = render_resolution(attachment) if attachment.resolution && type != 'audio'
91
+ part.merge(Interactions::Content.interaction_attachment_source(attachment))
92
+ end
93
+
94
+ def render_resolution(attachment)
95
+ level = attachment.resolution == :original ? :ultra_high : attachment.resolution
96
+ level = :high if level == :ultra_high && !attachment.image?
97
+ level.to_s
98
+ end
99
+
100
+ def describe(value)
101
+ value.nil? || value.is_a?(String) ? value : JSON.generate(value)
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -112,7 +112,7 @@ module RubyLLM
112
112
 
113
113
  [error['code'], error['message']]
114
114
  rescue JSON::ParserError => e
115
- RubyLLM.logger.debug { "Failed to parse streaming error: #{e.message}" }
115
+ log_body_debug { "Failed to parse streaming error: #{e.message}" }
116
116
  [500, "Failed to parse error: #{data}"]
117
117
  end
118
118
  end
@@ -8,10 +8,12 @@ module RubyLLM
8
8
 
9
9
  protocol :gemini, Protocols::Gemini, batches: Protocols::Gemini::Batches
10
10
  protocol :interactions, Protocols::Interactions
11
+ protocol :decisions, Protocols::Gemini::Decisions
11
12
  protocol :live_transcription, Protocols::Gemini::LiveTranscription
12
13
  protocol :files, Protocols::Gemini::Files
13
14
 
14
15
  def protocol_for(model, operation: nil, **)
16
+ return protocols[:decisions] if operation == :judge
15
17
  return protocols[:interactions] if operation == :animate
16
18
  return protocols[:interactions] if operation == :transcribe && model.id == 'gemini-3.5-transcribe'
17
19
  return protocols[:live_transcription] if operation == :transcribe && model.id == 'gemini-3.5-transcribe-live'
@@ -7,15 +7,10 @@ module RubyLLM
7
7
  protocol :anthropic, MiniMax::Anthropic
8
8
  protocol :media, Protocols::MiniMax
9
9
  protocol :files, Protocols::MiniMax::Files
10
- protocol :chat_completions, MiniMax::ChatCompletions
11
10
  protocol :responses, MiniMax::Responses
12
11
 
13
12
  MEDIA_OPERATIONS = %i[paint animate speak transcribe].freeze
14
13
 
15
- # The one MiniMax model that applies a response_format, on Chat
16
- # Completion v2. The others ignore every structured output field.
17
- STRUCTURED_OUTPUT_MODEL = 'MiniMax-Text-01'
18
-
19
14
  # The HTTP status each MiniMax status code stands for on endpoints that
20
15
  # answer 200 and report the failure in base_resp.
21
16
  FAILURE_STATUSES = {
@@ -28,7 +23,6 @@ module RubyLLM
28
23
  end
29
24
 
30
25
  def protocol_for(model, schema: nil, thinking: nil, tool_prefs: nil, provider_tools: [], messages: [], **)
31
- return protocols[:chat_completions] if schema && model.id == STRUCTURED_OUTPUT_MODEL
32
26
  return protocols[:responses] if web_search_route?(schema, thinking, tool_prefs, provider_tools, messages)
33
27
 
34
28
  super
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ module Providers
5
+ class OpenAI
6
+ # The Jina-style rerank endpoint that OpenAI-compatible servers such as
7
+ # llama.cpp and vLLM serve at /v1/rerank. OpenAI's own API has none.
8
+ class Rerank < Protocols::ChatCompletions # :nodoc:
9
+ include Protocols::ChatCompletions::Rerank
10
+ end
11
+ end
12
+ end
13
+ end
@@ -10,6 +10,7 @@ module RubyLLM
10
10
  batches: Protocols::ChatCompletions::EmbeddingBatches
11
11
  protocol :files, Protocols::OpenAI::Files
12
12
  protocol :decisions, Protocols::OpenAI::Decisions
13
+ protocol :rerank, OpenAI::Rerank
13
14
 
14
15
  RATE_LIMIT_RESET_HEADERS = %w[x-ratelimit-reset-requests x-ratelimit-reset-tokens].freeze
15
16
  RESET_DURATION_UNITS = { 'h' => 3600, 'm' => 60, 's' => 1, 'ms' => 0.001 }.freeze
@@ -24,9 +25,17 @@ module RubyLLM
24
25
 
25
26
  def resolve_protocol(name, model, operation: nil, **options)
26
27
  name ||= :decisions if operation == :judge
28
+ name ||= :rerank if operation == :rerank
27
29
  super
28
30
  end
29
31
 
32
+ def rerank(...) # :nodoc:
33
+ return super unless openai_hosted?
34
+
35
+ raise Error, "OpenAI doesn't support reranking. To rerank on an OpenAI-compatible server with a " \
36
+ '/rerank endpoint, such as llama.cpp or vLLM, set openai_api_base to its address'
37
+ end
38
+
30
39
  # Audio, realtime, and dedicated search models only exist on Chat Completions.
31
40
  def protocol_for(model, **)
32
41
  return fetch_protocol(:chat_completions) if Capabilities::SEARCH_MODELS.include?(model.id)
@@ -98,6 +107,11 @@ module RubyLLM
98
107
 
99
108
  private
100
109
 
110
+ def openai_hosted?
111
+ host = URI(api_base).host.to_s.downcase.delete_suffix('.')
112
+ host == 'openai.com' || host.end_with?('.openai.com')
113
+ end
114
+
101
115
  def parse_reset_duration(value)
102
116
  duration = value.to_s
103
117
  parts = duration.scan(/(\d+(?:\.\d+)?)(ms|h|m|s)/)
@@ -83,7 +83,7 @@ module RubyLLM
83
83
  else
84
84
  params['requestedSchema'] = schema || answer_schema(choices)
85
85
  end
86
- new("ask_#{index}", params)
86
+ new("ask_#{index}", JSON.parse(JSON.generate(params)))
87
87
  end
88
88
 
89
89
  # Returns the approval +chat+ waits on before it runs +tool_call+.
@@ -117,9 +117,29 @@ module RubyLLM
117
117
  def answer_schema(choices)
118
118
  raise ArgumentError, 'choices: needs at least one choice.' if choices && Array(choices).empty?
119
119
 
120
- answer = { 'type' => 'string', 'enum' => choices&.map(&:to_s) }.compact
120
+ answer = { 'type' => 'string', **choice_schema(choices) }
121
121
  { 'type' => 'object', 'properties' => { 'answer' => answer }, 'required' => ['answer'] }
122
122
  end
123
+
124
+ def choice_schema(choices)
125
+ case choices
126
+ when nil then {}
127
+ when Hash then { 'oneOf' => distinct(choices).map { |value, title| choice(value, title) } }
128
+ else { 'enum' => choices.map(&:to_s) }
129
+ end
130
+ end
131
+
132
+ def distinct(choices)
133
+ values = choices.keys.map(&:to_s)
134
+ repeated = values.find { |value| values.count(value) > 1 }
135
+ raise ArgumentError, "choices: has more than one choice #{repeated.inspect}." if repeated
136
+
137
+ choices
138
+ end
139
+
140
+ def choice(value, title)
141
+ { 'const' => value.to_s, 'title' => (title || value).to_s }
142
+ end
123
143
  end
124
144
 
125
145
  def initialize(key, params, id: SecureRandom.hex(8), response: nil, tool_call: nil, chat: nil, # :nodoc:
@@ -252,6 +272,16 @@ module RubyLLM
252
272
  by_name ? content.transform_keys(&:to_sym) : content['answer']
253
273
  end
254
274
 
275
+ # Returns whether +question+ asks what this one asks, so that an answer
276
+ # to one answers the other: the same message and the same kind of
277
+ # answer, with the same choices or fields, however the keys that
278
+ # describe them are ordered or stored. A page to visit is the same
279
+ # page under another address, since a sign-in link often changes on
280
+ # every run.
281
+ def asks?(question) # :nodoc:
282
+ comparable == question.comparable
283
+ end
284
+
255
285
  # Records +response+ as the question's own, and returns +self+.
256
286
  def respond(response) # :nodoc:
257
287
  @response = response
@@ -272,6 +302,15 @@ module RubyLLM
272
302
  { 'id' => @nonce, 'key' => key, 'params' => @params, 'response' => response }.compact
273
303
  end
274
304
 
305
+ protected
306
+
307
+ # The question's params as JSON stores them, without a page's address.
308
+ def comparable
309
+ params = JSON.parse(JSON.generate(@params.to_h))
310
+ params.delete('url') if params['mode'] == 'url'
311
+ params
312
+ end
313
+
275
314
  private
276
315
 
277
316
  def response_without(value, values)
@@ -319,12 +358,28 @@ module RubyLLM
319
358
 
320
359
  def typed(field, value)
321
360
  value = parsed(field, value) if value.is_a?(String)
361
+ value = typed_items(field, value) if value.is_a?(Array)
322
362
  value = value.to_s if value.is_a?(Symbol) && field.type == 'string'
323
- return value if CLASSES.fetch(field.type, [Object]).any? { |kind| value.is_a?(kind) }
363
+ return value if of_type?(field, value)
324
364
 
325
365
  raise ArgumentError, "#{label(field)} takes #{TYPES.fetch(field.type)}."
326
366
  end
327
367
 
368
+ def of_type?(field, value)
369
+ return false unless CLASSES.fetch(field.type, [Object]).any? { |kind| value.is_a?(kind) }
370
+
371
+ field.type != 'number' || value.finite?
372
+ end
373
+
374
+ def typed_items(field, values)
375
+ type = @params.dig('requestedSchema', 'properties', field.name.to_s, 'items', 'type')
376
+ return values if type == 'array'
377
+
378
+ item = field.dup
379
+ item.type = type
380
+ values.map { |value| typed(item, value) }
381
+ end
382
+
328
383
  def parsed(field, text)
329
384
  case field.type
330
385
  when 'integer' then Integer(text, 10)
@@ -386,8 +441,8 @@ module RubyLLM
386
441
  end
387
442
 
388
443
  def titled_choices(property)
389
- values = property['enum'] || property.dig('items', 'enum')
390
- titled = values ? named(values, Array(property['enumNames'])) : options(property)
444
+ listed = property['enum'] ? property : property['items'].to_h
445
+ titled = listed['enum'] ? named(listed['enum'], Array(listed['enumNames'])) : options(property)
391
446
  titled unless titled.empty?
392
447
  end
393
448
 
data/lib/ruby_llm/tool.rb CHANGED
@@ -539,15 +539,26 @@ module RubyLLM
539
539
  #
540
540
  # Code before +ask+ runs again too, so make it safe to repeat: ask
541
541
  # first, then act. A call may ask several questions, one after
542
- # another, and each run asks them in the same order.
542
+ # another, and each run asks them in the same order. An answer
543
+ # reaches only the question it was given to: when a run asks
544
+ # something else in its place, such as after skipping a question or
545
+ # with other choices, the call pauses on the new question, and the
546
+ # questions after it are asked again.
543
547
  #
544
548
  # Without options, +ask+ takes free text and returns the String. With
545
- # +choices:+ it returns the one the person picked. With a block, it
549
+ # +choices:+ it returns the one the person picked, as a String: a
550
+ # choice given as +17+ comes back as <tt>"17"</tt>. With a block, it
546
551
  # asks for the fields the block declares, in the DSL of
547
552
  # Tool.parameters, and returns their values by name. With +url:+, it
548
553
  # asks the person to visit a page, such as to connect an account, and
549
554
  # returns +true+ once they say they did.
550
555
  #
556
+ # A Hash of +choices:+ gives each value the title to show for it, and
557
+ # +ask+ returns the value. A value whose title is +nil+ is shown as
558
+ # itself.
559
+ #
560
+ # ask("Which card?", choices: { "17" => "River bank", "42" => "Money bank" }) # => "42"
561
+ #
551
562
  # passenger = ask("Who is flying?") do
552
563
  # string :name, description: "As on the passport"
553
564
  # integer :age, required: false
@@ -569,11 +580,12 @@ module RubyLLM
569
580
  index = state['asked']
570
581
  state['asked'] += 1
571
582
  requests = Array(state['input']&.[]('requests'))
572
- return Question.from_h(requests[index]).asked_answer(fields) if requests[index]&.[]('response')
573
-
574
583
  schema = SchemaDefinition.new(block: fields).json_schema if fields
575
- asked = requests.first(index).map { |data| Question.from_h(data) }
576
584
  question = Question.asked(index, message, choices:, url:, schema:)
585
+ stored = answered_as(requests[index], question)
586
+ return stored.asked_answer(fields) if stored
587
+
588
+ asked = requests.first(index).map { |data| Question.from_h(data) }
577
589
  raise InputRequiredError.new(name, { 'requests' => [*asked, question] })
578
590
  end
579
591
 
@@ -710,6 +722,13 @@ module RubyLLM
710
722
  Fiber[RESUMING] = nil
711
723
  end
712
724
 
725
+ # The question +data+ stored, when it was answered and asks what
726
+ # +question+ asks.
727
+ def answered_as(data, question) # :nodoc:
728
+ stored = Question.from_h(data) if data
729
+ stored if stored&.answered? && stored.asks?(question)
730
+ end
731
+
713
732
  def execute_accepts_tool_call? # :nodoc:
714
733
  method(:execute).parameters.any? do |kind, name|
715
734
  name == TOOL_CALL_KEYWORD && KEYWORD_PARAMETER_KINDS.include?(kind)
@@ -48,11 +48,14 @@ module RubyLLM
48
48
  f.proxy = config.http_proxy if config.http_proxy
49
49
  f.ssl.update(ssl_snapshot(config.ssl)) if config.ssl
50
50
  f.response :logger,
51
- RubyLLM.logger,
51
+ config.logger || RubyLLM.logger,
52
52
  bodies: false,
53
53
  errors: true,
54
54
  headers: false,
55
- log_level: :debug
55
+ log_level: :debug,
56
+ formatter: LogFormatter do |formatter|
57
+ formatter.config = config
58
+ end
56
59
  f.response :raise_error
57
60
  yield f if block_given?
58
61
  end
@@ -186,7 +189,7 @@ module RubyLLM
186
189
 
187
190
  private
188
191
 
189
- def instrument_request(method, url)
192
+ def instrument_request(method, url, &block)
190
193
  payload = {
191
194
  provider: @provider.slug,
192
195
  method: method,
@@ -194,7 +197,7 @@ module RubyLLM
194
197
  }
195
198
 
196
199
  RubyLLM.instrument('request.ruby_llm', payload, config: @config) do |event|
197
- response = ReleaseRequest.release(yield)
200
+ response = ReleaseRequest.release(LogFormatter.with_config(@config, &block))
198
201
  event[:status] = response.status if response.respond_to?(:status)
199
202
  response
200
203
  rescue RubyLLM::Error => e
@@ -216,7 +219,7 @@ module RubyLLM
216
219
  proxy: @config.http_proxy,
217
220
  ssl: (self.class.ssl_snapshot(@config.ssl) if @config.ssl),
218
221
  logger: logger,
219
- log_bodies: logger.debug?,
222
+ log_bodies: @config.log_bodies.nil? ? logger.debug? : @config.log_bodies,
220
223
  log_regexp_timeout: @config.log_regexp_timeout,
221
224
  max_retries: @config.max_retries,
222
225
  retry_interval: @config.retry_interval,
@@ -7,7 +7,46 @@ module RubyLLM
7
7
  module Transport
8
8
  # A request that succeeded and was billed must not fail because a debug
9
9
  # log filter ran out of time on a large body.
10
+ #
11
+ # The cached connection's formatter is shared by every context, so the
12
+ # configuration whose credentials it filters is set in fiber storage for
13
+ # the duration of each request. A Connection.basic formatter serves one
14
+ # configuration and is given it directly.
10
15
  class LogFormatter < Faraday::Logging::Formatter # :nodoc:
16
+ CONFIG_KEY = :ruby_llm_log_config
17
+ FILTERED = '[FILTERED]'
18
+
19
+ attr_writer :config
20
+
21
+ def self.with_config(config)
22
+ previous = Fiber[CONFIG_KEY]
23
+ Fiber[CONFIG_KEY] = config
24
+ yield
25
+ ensure
26
+ Fiber[CONFIG_KEY] = previous
27
+ end
28
+
29
+ # Writes a debug line to +config+'s logger with its credentials
30
+ # replaced. The block runs only when the logger is at debug level.
31
+ def self.log_debug(config, &message)
32
+ (config.logger || RubyLLM.logger).debug { redact(message.call, config.secret_values) }
33
+ end
34
+
35
+ # Longer secrets go first, so a key that contains another key is
36
+ # replaced whole instead of leaving its remainder in the line.
37
+ def self.redact(text, secrets)
38
+ secrets.uniq.sort_by { |secret| -secret.length }
39
+ .reduce(text.to_s) { |output, secret| output.gsub(secret, FILTERED) }
40
+ end
41
+
42
+ # Faraday 2.12 and earlier write exception messages without
43
+ # apply_filters, so credentials are replaced on the way out instead.
44
+ # Connection.setup_logging logs every line at debug.
45
+ def debug(progname = nil)
46
+ config = @config || Fiber[CONFIG_KEY]
47
+ @logger.debug(progname) { config ? self.class.redact(yield, config.secret_values) : yield }
48
+ end
49
+
11
50
  private
12
51
 
13
52
  def apply_filters(output)