dify_llm 1.8.2 → 1.9.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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -3
  3. data/lib/generators/ruby_llm/generator_helpers.rb +31 -10
  4. data/lib/generators/ruby_llm/install/templates/create_messages_migration.rb.tt +3 -0
  5. data/lib/generators/ruby_llm/install/templates/create_models_migration.rb.tt +5 -0
  6. data/lib/generators/ruby_llm/install/templates/create_tool_calls_migration.rb.tt +7 -1
  7. data/lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb +1 -1
  8. data/lib/generators/ruby_llm/upgrade_to_v1_9/templates/add_v1_9_message_columns.rb.tt +15 -0
  9. data/lib/generators/ruby_llm/upgrade_to_v1_9/upgrade_to_v1_9_generator.rb +49 -0
  10. data/lib/ruby_llm/active_record/acts_as.rb +22 -24
  11. data/lib/ruby_llm/active_record/chat_methods.rb +41 -13
  12. data/lib/ruby_llm/active_record/message_methods.rb +11 -2
  13. data/lib/ruby_llm/active_record/model_methods.rb +1 -1
  14. data/lib/ruby_llm/aliases.json +61 -32
  15. data/lib/ruby_llm/attachment.rb +44 -13
  16. data/lib/ruby_llm/chat.rb +13 -2
  17. data/lib/ruby_llm/configuration.rb +6 -1
  18. data/lib/ruby_llm/connection.rb +3 -3
  19. data/lib/ruby_llm/content.rb +23 -0
  20. data/lib/ruby_llm/message.rb +11 -6
  21. data/lib/ruby_llm/model/info.rb +4 -0
  22. data/lib/ruby_llm/models.json +9649 -8211
  23. data/lib/ruby_llm/models.rb +14 -22
  24. data/lib/ruby_llm/provider.rb +23 -1
  25. data/lib/ruby_llm/providers/anthropic/chat.rb +22 -3
  26. data/lib/ruby_llm/providers/anthropic/content.rb +44 -0
  27. data/lib/ruby_llm/providers/anthropic/media.rb +3 -2
  28. data/lib/ruby_llm/providers/anthropic/models.rb +15 -0
  29. data/lib/ruby_llm/providers/anthropic/streaming.rb +2 -0
  30. data/lib/ruby_llm/providers/anthropic/tools.rb +20 -18
  31. data/lib/ruby_llm/providers/bedrock/media.rb +2 -1
  32. data/lib/ruby_llm/providers/bedrock/streaming/content_extraction.rb +15 -0
  33. data/lib/ruby_llm/providers/bedrock/streaming/payload_processing.rb +2 -0
  34. data/lib/ruby_llm/providers/dify/chat.rb +16 -5
  35. data/lib/ruby_llm/providers/gemini/chat.rb +352 -69
  36. data/lib/ruby_llm/providers/gemini/media.rb +59 -1
  37. data/lib/ruby_llm/providers/gemini/tools.rb +146 -25
  38. data/lib/ruby_llm/providers/gemini/transcription.rb +116 -0
  39. data/lib/ruby_llm/providers/gemini.rb +2 -1
  40. data/lib/ruby_llm/providers/gpustack/media.rb +1 -0
  41. data/lib/ruby_llm/providers/ollama/media.rb +1 -0
  42. data/lib/ruby_llm/providers/openai/chat.rb +7 -2
  43. data/lib/ruby_llm/providers/openai/media.rb +2 -1
  44. data/lib/ruby_llm/providers/openai/streaming.rb +7 -2
  45. data/lib/ruby_llm/providers/openai/tools.rb +26 -6
  46. data/lib/ruby_llm/providers/openai/transcription.rb +70 -0
  47. data/lib/ruby_llm/providers/openai.rb +1 -0
  48. data/lib/ruby_llm/providers/vertexai/transcription.rb +16 -0
  49. data/lib/ruby_llm/providers/vertexai.rb +11 -11
  50. data/lib/ruby_llm/railtie.rb +24 -22
  51. data/lib/ruby_llm/stream_accumulator.rb +10 -4
  52. data/lib/ruby_llm/tool.rb +126 -0
  53. data/lib/ruby_llm/transcription.rb +35 -0
  54. data/lib/ruby_llm/utils.rb +46 -0
  55. data/lib/ruby_llm/version.rb +1 -1
  56. data/lib/ruby_llm.rb +7 -0
  57. metadata +24 -3
@@ -8,8 +8,10 @@ module RubyLLM
8
8
  def initialize
9
9
  @content = +''
10
10
  @tool_calls = {}
11
- @input_tokens = 0
12
- @output_tokens = 0
11
+ @input_tokens = nil
12
+ @output_tokens = nil
13
+ @cached_tokens = nil
14
+ @cache_creation_tokens = nil
13
15
  @latest_tool_call_id = nil
14
16
  end
15
17
 
@@ -35,8 +37,10 @@ module RubyLLM
35
37
  model_id: model_id,
36
38
  conversation_id: conversation_id,
37
39
  tool_calls: tool_calls_from_stream,
38
- input_tokens: @input_tokens.positive? ? @input_tokens : nil,
39
- output_tokens: @output_tokens.positive? ? @output_tokens : nil,
40
+ input_tokens: @input_tokens,
41
+ output_tokens: @output_tokens,
42
+ cached_tokens: @cached_tokens,
43
+ cache_creation_tokens: @cache_creation_tokens,
40
44
  raw: response
41
45
  )
42
46
  end
@@ -92,6 +96,8 @@ module RubyLLM
92
96
  def count_tokens(chunk)
93
97
  @input_tokens = chunk.input_tokens if chunk.input_tokens
94
98
  @output_tokens = chunk.output_tokens if chunk.output_tokens
99
+ @cached_tokens = chunk.cached_tokens if chunk.cached_tokens
100
+ @cache_creation_tokens = chunk.cache_creation_tokens if chunk.cache_creation_tokens
95
101
  end
96
102
  end
97
103
  end
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.8.2'
4
+ VERSION = '1.9.1'
5
5
  end
data/lib/ruby_llm.rb CHANGED
@@ -3,10 +3,12 @@
3
3
  require 'base64'
4
4
  require 'event_stream_parser'
5
5
  require 'faraday'
6
+ require 'faraday/multipart'
6
7
  require 'faraday/retry'
7
8
  require 'faraday/multipart'
8
9
  require 'json'
9
10
  require 'logger'
11
+ require 'marcel'
10
12
  require 'securerandom'
11
13
  require 'zeitwerk'
12
14
 
@@ -28,6 +30,7 @@ loader.inflector.inflect(
28
30
  )
29
31
  loader.ignore("#{__dir__}/tasks")
30
32
  loader.ignore("#{__dir__}/generators")
33
+ loader.ignore("#{__dir__}/ruby_llm/railtie.rb")
31
34
  loader.ignore("#{__dir__}/dify_llm.rb")
32
35
  loader.setup
33
36
 
@@ -58,6 +61,10 @@ module RubyLLM
58
61
  Image.paint(...)
59
62
  end
60
63
 
64
+ def transcribe(...)
65
+ Transcription.transcribe(...)
66
+ end
67
+
61
68
  def models
62
69
  Models.instance
63
70
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dify_llm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.2
4
+ version: 1.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carmine Paolino
@@ -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
@@ -169,6 +183,8 @@ files:
169
183
  - lib/generators/ruby_llm/install/templates/tool_call_model.rb.tt
170
184
  - lib/generators/ruby_llm/upgrade_to_v1_7/templates/migration.rb.tt
171
185
  - lib/generators/ruby_llm/upgrade_to_v1_7/upgrade_to_v1_7_generator.rb
186
+ - lib/generators/ruby_llm/upgrade_to_v1_9/templates/add_v1_9_message_columns.rb.tt
187
+ - lib/generators/ruby_llm/upgrade_to_v1_9/upgrade_to_v1_9_generator.rb
172
188
  - lib/ruby_llm.rb
173
189
  - lib/ruby_llm/active_record/acts_as.rb
174
190
  - lib/ruby_llm/active_record/acts_as_legacy.rb
@@ -203,6 +219,7 @@ files:
203
219
  - lib/ruby_llm/providers/anthropic.rb
204
220
  - lib/ruby_llm/providers/anthropic/capabilities.rb
205
221
  - lib/ruby_llm/providers/anthropic/chat.rb
222
+ - lib/ruby_llm/providers/anthropic/content.rb
206
223
  - lib/ruby_llm/providers/anthropic/embeddings.rb
207
224
  - lib/ruby_llm/providers/anthropic/media.rb
208
225
  - lib/ruby_llm/providers/anthropic/models.rb
@@ -237,6 +254,7 @@ files:
237
254
  - lib/ruby_llm/providers/gemini/models.rb
238
255
  - lib/ruby_llm/providers/gemini/streaming.rb
239
256
  - lib/ruby_llm/providers/gemini/tools.rb
257
+ - lib/ruby_llm/providers/gemini/transcription.rb
240
258
  - lib/ruby_llm/providers/gpustack.rb
241
259
  - lib/ruby_llm/providers/gpustack/chat.rb
242
260
  - lib/ruby_llm/providers/gpustack/media.rb
@@ -260,6 +278,7 @@ files:
260
278
  - lib/ruby_llm/providers/openai/moderation.rb
261
279
  - lib/ruby_llm/providers/openai/streaming.rb
262
280
  - lib/ruby_llm/providers/openai/tools.rb
281
+ - lib/ruby_llm/providers/openai/transcription.rb
263
282
  - lib/ruby_llm/providers/openrouter.rb
264
283
  - lib/ruby_llm/providers/openrouter/models.rb
265
284
  - lib/ruby_llm/providers/perplexity.rb
@@ -271,11 +290,13 @@ files:
271
290
  - lib/ruby_llm/providers/vertexai/embeddings.rb
272
291
  - lib/ruby_llm/providers/vertexai/models.rb
273
292
  - lib/ruby_llm/providers/vertexai/streaming.rb
293
+ - lib/ruby_llm/providers/vertexai/transcription.rb
274
294
  - lib/ruby_llm/railtie.rb
275
295
  - lib/ruby_llm/stream_accumulator.rb
276
296
  - lib/ruby_llm/streaming.rb
277
297
  - lib/ruby_llm/tool.rb
278
298
  - lib/ruby_llm/tool_call.rb
299
+ - lib/ruby_llm/transcription.rb
279
300
  - lib/ruby_llm/utils.rb
280
301
  - lib/ruby_llm/version.rb
281
302
  - lib/tasks/models.rake
@@ -294,8 +315,8 @@ metadata:
294
315
  funding_uri: https://github.com/sponsors/crmne
295
316
  rubygems_mfa_required: 'true'
296
317
  post_install_message: |
297
- Upgrading from RubyLLM <= 1.6.x? Check the upgrade guide for new features and migration instructions
298
- --> https://rubyllm.com/upgrading-to-1-7/
318
+ Upgrading from RubyLLM <= 1.8.x? Check the upgrade guide for new features and migration instructions
319
+ --> https://rubyllm.com/upgrading/
299
320
  rdoc_options: []
300
321
  require_paths:
301
322
  - lib