ruby_llm_community 1.2.0 → 1.3.0

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -7
  3. data/lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb +127 -67
  4. data/lib/generators/ruby_llm/chat_ui/templates/controllers/chats_controller.rb.tt +12 -12
  5. data/lib/generators/ruby_llm/chat_ui/templates/controllers/messages_controller.rb.tt +7 -7
  6. data/lib/generators/ruby_llm/chat_ui/templates/controllers/models_controller.rb.tt +4 -4
  7. data/lib/generators/ruby_llm/chat_ui/templates/jobs/chat_response_job.rb.tt +6 -6
  8. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/_chat.html.erb.tt +4 -4
  9. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/_form.html.erb.tt +5 -5
  10. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/index.html.erb.tt +5 -5
  11. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/new.html.erb.tt +4 -4
  12. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/show.html.erb.tt +8 -8
  13. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_content.html.erb.tt +1 -0
  14. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_form.html.erb.tt +5 -5
  15. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_message.html.erb.tt +9 -6
  16. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_tool_calls.html.erb.tt +7 -0
  17. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/create.turbo_stream.erb.tt +5 -5
  18. data/lib/generators/ruby_llm/chat_ui/templates/views/models/_model.html.erb.tt +9 -9
  19. data/lib/generators/ruby_llm/chat_ui/templates/views/models/index.html.erb.tt +4 -6
  20. data/lib/generators/ruby_llm/chat_ui/templates/views/models/show.html.erb.tt +11 -11
  21. data/lib/generators/ruby_llm/generator_helpers.rb +131 -87
  22. data/lib/generators/ruby_llm/install/install_generator.rb +75 -79
  23. data/lib/generators/ruby_llm/install/templates/create_messages_migration.rb.tt +3 -0
  24. data/lib/generators/ruby_llm/install/templates/initializer.rb.tt +1 -1
  25. data/lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb +88 -85
  26. data/lib/generators/ruby_llm/upgrade_to_v1_9/templates/add_v1_9_message_columns.rb.tt +15 -0
  27. data/lib/generators/ruby_llm/upgrade_to_v1_9/upgrade_to_v1_9_generator.rb +49 -0
  28. data/lib/ruby_llm/active_record/acts_as.rb +17 -8
  29. data/lib/ruby_llm/active_record/chat_methods.rb +41 -13
  30. data/lib/ruby_llm/active_record/message_methods.rb +11 -2
  31. data/lib/ruby_llm/active_record/model_methods.rb +1 -1
  32. data/lib/ruby_llm/aliases.json +46 -20
  33. data/lib/ruby_llm/attachment.rb +8 -0
  34. data/lib/ruby_llm/chat.rb +13 -2
  35. data/lib/ruby_llm/configuration.rb +6 -1
  36. data/lib/ruby_llm/connection.rb +4 -4
  37. data/lib/ruby_llm/content.rb +23 -0
  38. data/lib/ruby_llm/message.rb +17 -9
  39. data/lib/ruby_llm/model/info.rb +4 -0
  40. data/lib/ruby_llm/models.json +8846 -7005
  41. data/lib/ruby_llm/models.rb +14 -22
  42. data/lib/ruby_llm/provider.rb +27 -5
  43. data/lib/ruby_llm/providers/anthropic/chat.rb +18 -5
  44. data/lib/ruby_llm/providers/anthropic/content.rb +44 -0
  45. data/lib/ruby_llm/providers/anthropic/media.rb +5 -4
  46. data/lib/ruby_llm/providers/anthropic/models.rb +9 -2
  47. data/lib/ruby_llm/providers/anthropic/tools.rb +20 -18
  48. data/lib/ruby_llm/providers/bedrock/media.rb +2 -1
  49. data/lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb +9 -2
  50. data/lib/ruby_llm/providers/gemini/chat.rb +353 -72
  51. data/lib/ruby_llm/providers/gemini/media.rb +59 -1
  52. data/lib/ruby_llm/providers/gemini/tools.rb +146 -25
  53. data/lib/ruby_llm/providers/gemini/transcription.rb +116 -0
  54. data/lib/ruby_llm/providers/gemini.rb +2 -1
  55. data/lib/ruby_llm/providers/gpustack/media.rb +1 -0
  56. data/lib/ruby_llm/providers/ollama/media.rb +1 -0
  57. data/lib/ruby_llm/providers/openai/capabilities.rb +15 -7
  58. data/lib/ruby_llm/providers/openai/chat.rb +7 -3
  59. data/lib/ruby_llm/providers/openai/media.rb +2 -1
  60. data/lib/ruby_llm/providers/openai/streaming.rb +7 -3
  61. data/lib/ruby_llm/providers/openai/tools.rb +34 -12
  62. data/lib/ruby_llm/providers/openai/transcription.rb +70 -0
  63. data/lib/ruby_llm/providers/openai_base.rb +1 -0
  64. data/lib/ruby_llm/providers/vertexai/transcription.rb +16 -0
  65. data/lib/ruby_llm/providers/vertexai.rb +3 -0
  66. data/lib/ruby_llm/stream_accumulator.rb +8 -12
  67. data/lib/ruby_llm/tool.rb +126 -0
  68. data/lib/ruby_llm/transcription.rb +35 -0
  69. data/lib/ruby_llm/utils.rb +46 -0
  70. data/lib/ruby_llm/version.rb +1 -1
  71. data/lib/ruby_llm_community.rb +6 -1
  72. metadata +27 -3
@@ -8,10 +8,10 @@ module RubyLLM
8
8
  def initialize
9
9
  @content = nil
10
10
  @tool_calls = {}
11
- @input_tokens = 0
12
- @output_tokens = 0
13
- @cached_tokens = 0
14
- @cache_creation_tokens = 0
11
+ @input_tokens = nil
12
+ @output_tokens = nil
13
+ @cached_tokens = nil
14
+ @cache_creation_tokens = nil
15
15
  @latest_tool_call_id = nil
16
16
  @reasoning_id = nil
17
17
  end
@@ -40,10 +40,10 @@ module RubyLLM
40
40
  content: content,
41
41
  model_id: model_id,
42
42
  tool_calls: tool_calls_from_stream,
43
- input_tokens: positive_or_nil(@input_tokens),
44
- output_tokens: positive_or_nil(@output_tokens),
45
- cached_tokens: positive_or_nil(@cached_tokens),
46
- cache_creation_tokens: positive_or_nil(@cache_creation_tokens),
43
+ input_tokens: @input_tokens,
44
+ output_tokens: @output_tokens,
45
+ cached_tokens: @cached_tokens,
46
+ cache_creation_tokens: @cache_creation_tokens,
47
47
  raw: response
48
48
  )
49
49
  end
@@ -58,10 +58,6 @@ module RubyLLM
58
58
  end
59
59
  end
60
60
 
61
- def positive_or_nil(value)
62
- value.positive? ? value : nil
63
- end
64
-
65
61
  def accumulate_content(new_content)
66
62
  return unless new_content
67
63
 
data/lib/ruby_llm/tool.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'ruby_llm/schema'
4
+
3
5
  module RubyLLM
4
6
  # Parameter definition for Tool methods.
5
7
  class Parameter
@@ -29,6 +31,8 @@ module RubyLLM
29
31
  end
30
32
 
31
33
  class << self
34
+ attr_reader :params_schema_definition
35
+
32
36
  def description(text = nil)
33
37
  return @description unless text
34
38
 
@@ -42,6 +46,20 @@ module RubyLLM
42
46
  def parameters
43
47
  @parameters ||= {}
44
48
  end
49
+
50
+ def params(schema = nil, &block)
51
+ @params_schema_definition = SchemaDefinition.new(schema:, block:)
52
+ self
53
+ end
54
+
55
+ def with_params(**params)
56
+ @provider_params = params
57
+ self
58
+ end
59
+
60
+ def provider_params
61
+ @provider_params ||= {}
62
+ end
45
63
  end
46
64
 
47
65
  def name
@@ -63,6 +81,23 @@ module RubyLLM
63
81
  self.class.parameters
64
82
  end
65
83
 
84
+ def provider_params
85
+ self.class.provider_params
86
+ end
87
+
88
+ def params_schema
89
+ return @params_schema if defined?(@params_schema)
90
+
91
+ @params_schema = begin
92
+ definition = self.class.params_schema_definition
93
+ if definition&.present?
94
+ definition.json_schema
95
+ elsif parameters.any?
96
+ SchemaDefinition.from_parameters(parameters)&.json_schema
97
+ end
98
+ end
99
+ end
100
+
66
101
  def call(args)
67
102
  RubyLLM.logger.debug "Tool #{name} called with: #{args.inspect}"
68
103
  result = execute(**args.transform_keys(&:to_sym))
@@ -79,5 +114,96 @@ module RubyLLM
79
114
  def halt(message)
80
115
  Halt.new(message)
81
116
  end
117
+
118
+ # Wraps schema handling for tool parameters, supporting JSON Schema hashes,
119
+ # RubyLLM::Schema instances/classes, and DSL blocks.
120
+ class SchemaDefinition
121
+ def self.from_parameters(parameters)
122
+ return nil if parameters.nil? || parameters.empty?
123
+
124
+ properties = parameters.to_h do |name, param|
125
+ schema = {
126
+ type: map_type(param.type),
127
+ description: param.description
128
+ }.compact
129
+
130
+ schema[:items] = default_items_schema if schema[:type] == 'array'
131
+
132
+ [name.to_s, schema]
133
+ end
134
+
135
+ required = parameters.select { |_, param| param.required }.keys.map(&:to_s)
136
+
137
+ json_schema = {
138
+ type: 'object',
139
+ properties: properties,
140
+ required: required,
141
+ additionalProperties: false,
142
+ strict: true
143
+ }
144
+
145
+ new(schema: json_schema)
146
+ end
147
+
148
+ def self.map_type(type)
149
+ case type.to_s
150
+ when 'integer', 'int' then 'integer'
151
+ when 'number', 'float', 'double' then 'number'
152
+ when 'boolean' then 'boolean'
153
+ when 'array' then 'array'
154
+ when 'object' then 'object'
155
+ else
156
+ 'string'
157
+ end
158
+ end
159
+
160
+ def self.default_items_schema
161
+ { type: 'string' }
162
+ end
163
+
164
+ def initialize(schema: nil, block: nil)
165
+ @schema = schema
166
+ @block = block
167
+ end
168
+
169
+ def present?
170
+ @schema || @block
171
+ end
172
+
173
+ def json_schema
174
+ @json_schema ||= RubyLLM::Utils.deep_stringify_keys(resolve_schema)
175
+ end
176
+
177
+ private
178
+
179
+ def resolve_schema
180
+ return resolve_direct_schema(@schema) if @schema
181
+ return build_from_block(&@block) if @block
182
+
183
+ nil
184
+ end
185
+
186
+ def resolve_direct_schema(schema)
187
+ return extract_schema(schema.to_json_schema) if schema.respond_to?(:to_json_schema)
188
+ return RubyLLM::Utils.deep_dup(schema) if schema.is_a?(Hash)
189
+ if schema.is_a?(Class) && schema.instance_methods.include?(:to_json_schema)
190
+ return extract_schema(schema.new.to_json_schema)
191
+ end
192
+
193
+ nil
194
+ end
195
+
196
+ def build_from_block(&)
197
+ schema_class = RubyLLM::Schema.create(&)
198
+ extract_schema(schema_class.new.to_json_schema)
199
+ end
200
+
201
+ def extract_schema(schema_hash)
202
+ return nil unless schema_hash.is_a?(Hash)
203
+
204
+ schema = schema_hash[:schema] || schema_hash['schema'] || schema_hash
205
+ RubyLLM::Utils.deep_dup(schema)
206
+ end
207
+ end
82
208
  end
83
209
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ # Represents a transcription of audio content.
5
+ class Transcription
6
+ attr_reader :text, :model, :language, :duration, :segments, :input_tokens, :output_tokens
7
+
8
+ def initialize(text:, model:, **attributes)
9
+ @text = text
10
+ @model = model
11
+ @language = attributes[:language]
12
+ @duration = attributes[:duration]
13
+ @segments = attributes[:segments]
14
+ @input_tokens = attributes[:input_tokens]
15
+ @output_tokens = attributes[:output_tokens]
16
+ end
17
+
18
+ def self.transcribe(audio_file, **kwargs)
19
+ model = kwargs.delete(:model)
20
+ language = kwargs.delete(:language)
21
+ provider = kwargs.delete(:provider)
22
+ assume_model_exists = kwargs.delete(:assume_model_exists) { false }
23
+ context = kwargs.delete(:context)
24
+ options = kwargs
25
+
26
+ config = context&.config || RubyLLM.config
27
+ model ||= config.default_transcription_model
28
+ model, provider_instance = Models.resolve(model, provider: provider, assume_exists: assume_model_exists,
29
+ config: config)
30
+ model_id = model.id
31
+
32
+ provider_instance.transcribe(audio_file, model: model_id, language:, **options)
33
+ end
34
+ end
35
+ end
@@ -41,5 +41,51 @@ module RubyLLM
41
41
  end
42
42
  end
43
43
  end
44
+
45
+ def deep_dup(value)
46
+ case value
47
+ when Hash
48
+ value.each_with_object({}) do |(key, val), duped|
49
+ duped[deep_dup(key)] = deep_dup(val)
50
+ end
51
+ when Array
52
+ value.map { |item| deep_dup(item) }
53
+ else
54
+ begin
55
+ value.dup
56
+ rescue TypeError
57
+ value
58
+ end
59
+ end
60
+ end
61
+
62
+ def deep_stringify_keys(value)
63
+ case value
64
+ when Hash
65
+ value.each_with_object({}) do |(key, val), result|
66
+ result[key.to_s] = deep_stringify_keys(val)
67
+ end
68
+ when Array
69
+ value.map { |item| deep_stringify_keys(item) }
70
+ when Symbol
71
+ value.to_s
72
+ else
73
+ value
74
+ end
75
+ end
76
+
77
+ def deep_symbolize_keys(value)
78
+ case value
79
+ when Hash
80
+ value.each_with_object({}) do |(key, val), result|
81
+ symbolized_key = key.respond_to?(:to_sym) ? key.to_sym : key
82
+ result[symbolized_key] = deep_symbolize_keys(val)
83
+ end
84
+ when Array
85
+ value.map { |item| deep_symbolize_keys(item) }
86
+ else
87
+ value
88
+ end
89
+ end
44
90
  end
45
91
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyLLM
4
- VERSION = '1.2.0'
4
+ VERSION = '1.3.0'
5
5
  end
@@ -3,11 +3,12 @@
3
3
  require 'base64'
4
4
  require 'event_stream_parser'
5
5
  require 'faraday'
6
- require 'faraday/retry'
7
6
  require 'faraday/multipart'
7
+ require 'faraday/retry'
8
8
 
9
9
  require 'json'
10
10
  require 'logger'
11
+ require 'marcel'
11
12
  require 'securerandom'
12
13
  require 'zeitwerk'
13
14
 
@@ -66,6 +67,10 @@ module RubyLLM
66
67
  Image.paint(...)
67
68
  end
68
69
 
70
+ def transcribe(...)
71
+ Transcription.transcribe(...)
72
+ end
73
+
69
74
  def models
70
75
  Models.instance
71
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_llm_community
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Shippy
@@ -107,6 +107,20 @@ dependencies:
107
107
  - - "~>"
108
108
  - !ruby/object:Gem::Version
109
109
  version: '1.0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: ruby_llm-schema
112
+ requirement: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.2.1
117
+ type: :runtime
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 0.2.1
110
124
  - !ruby/object:Gem::Dependency
111
125
  name: zeitwerk
112
126
  requirement: !ruby/object:Gem::Requirement
@@ -146,8 +160,10 @@ files:
146
160
  - lib/generators/ruby_llm/chat_ui/templates/views/chats/index.html.erb.tt
147
161
  - lib/generators/ruby_llm/chat_ui/templates/views/chats/new.html.erb.tt
148
162
  - lib/generators/ruby_llm/chat_ui/templates/views/chats/show.html.erb.tt
163
+ - lib/generators/ruby_llm/chat_ui/templates/views/messages/_content.html.erb.tt
149
164
  - lib/generators/ruby_llm/chat_ui/templates/views/messages/_form.html.erb.tt
150
165
  - lib/generators/ruby_llm/chat_ui/templates/views/messages/_message.html.erb.tt
166
+ - lib/generators/ruby_llm/chat_ui/templates/views/messages/_tool_calls.html.erb.tt
151
167
  - lib/generators/ruby_llm/chat_ui/templates/views/messages/create.turbo_stream.erb.tt
152
168
  - lib/generators/ruby_llm/chat_ui/templates/views/models/_model.html.erb.tt
153
169
  - lib/generators/ruby_llm/chat_ui/templates/views/models/index.html.erb.tt
@@ -166,6 +182,8 @@ files:
166
182
  - lib/generators/ruby_llm/install/templates/tool_call_model.rb.tt
167
183
  - lib/generators/ruby_llm/upgrade_to_v1_7/templates/migration.rb.tt
168
184
  - lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb
185
+ - lib/generators/ruby_llm/upgrade_to_v1_9/templates/add_v1_9_message_columns.rb.tt
186
+ - lib/generators/ruby_llm/upgrade_to_v1_9/upgrade_to_v1_9_generator.rb
169
187
  - lib/ruby_llm/active_record/acts_as.rb
170
188
  - lib/ruby_llm/active_record/acts_as_legacy.rb
171
189
  - lib/ruby_llm/active_record/chat_methods.rb
@@ -201,6 +219,7 @@ files:
201
219
  - lib/ruby_llm/providers/anthropic.rb
202
220
  - lib/ruby_llm/providers/anthropic/capabilities.rb
203
221
  - lib/ruby_llm/providers/anthropic/chat.rb
222
+ - lib/ruby_llm/providers/anthropic/content.rb
204
223
  - lib/ruby_llm/providers/anthropic/embeddings.rb
205
224
  - lib/ruby_llm/providers/anthropic/media.rb
206
225
  - lib/ruby_llm/providers/anthropic/models.rb
@@ -230,6 +249,7 @@ files:
230
249
  - lib/ruby_llm/providers/gemini/models.rb
231
250
  - lib/ruby_llm/providers/gemini/streaming.rb
232
251
  - lib/ruby_llm/providers/gemini/tools.rb
252
+ - lib/ruby_llm/providers/gemini/transcription.rb
233
253
  - lib/ruby_llm/providers/gpustack.rb
234
254
  - lib/ruby_llm/providers/gpustack/chat.rb
235
255
  - lib/ruby_llm/providers/gpustack/media.rb
@@ -255,6 +275,7 @@ files:
255
275
  - lib/ruby_llm/providers/openai/response_media.rb
256
276
  - lib/ruby_llm/providers/openai/streaming.rb
257
277
  - lib/ruby_llm/providers/openai/tools.rb
278
+ - lib/ruby_llm/providers/openai/transcription.rb
258
279
  - lib/ruby_llm/providers/openai_base.rb
259
280
  - lib/ruby_llm/providers/openrouter.rb
260
281
  - lib/ruby_llm/providers/openrouter/models.rb
@@ -272,6 +293,7 @@ files:
272
293
  - lib/ruby_llm/providers/vertexai/embeddings.rb
273
294
  - lib/ruby_llm/providers/vertexai/models.rb
274
295
  - lib/ruby_llm/providers/vertexai/streaming.rb
296
+ - lib/ruby_llm/providers/vertexai/transcription.rb
275
297
  - lib/ruby_llm/providers/xai.rb
276
298
  - lib/ruby_llm/providers/xai/capabilities.rb
277
299
  - lib/ruby_llm/providers/xai/chat.rb
@@ -281,6 +303,7 @@ files:
281
303
  - lib/ruby_llm/streaming.rb
282
304
  - lib/ruby_llm/tool.rb
283
305
  - lib/ruby_llm/tool_call.rb
306
+ - lib/ruby_llm/transcription.rb
284
307
  - lib/ruby_llm/utils.rb
285
308
  - lib/ruby_llm/version.rb
286
309
  - lib/ruby_llm_community.rb
@@ -298,10 +321,11 @@ metadata:
298
321
  changelog_uri: https://github.com/tpaulshippy/ruby_llm_community/commits/main
299
322
  documentation_uri: https://rubyllm.com
300
323
  bug_tracker_uri: https://github.com/tpaulshippy/ruby_llm_community/issues
324
+ funding_uri: https://github.com/sponsors/crmne
301
325
  rubygems_mfa_required: 'true'
302
326
  post_install_message: |
303
- Upgrading from RubyLLM <= 1.6.x? Check the upgrade guide for new features and migration instructions
304
- --> https://rubyllm.com/upgrading-to-1-7/
327
+ Upgrading from RubyLLM <= 1.8.x? Check the upgrade guide for new features and migration instructions
328
+ --> https://rubyllm.com/upgrading/
305
329
  rdoc_options: []
306
330
  require_paths:
307
331
  - lib