Skip to content

Commit 113481c

Browse files
committed
fix handling activestorage attachments
1 parent 46bc870 commit 113481c

11 files changed

+314
-25
lines changed

lib/ruby_llm/active_record/acts_as_legacy.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ def prepare_for_active_storage(attachments)
302302
case attachment
303303
when ActionDispatch::Http::UploadedFile, ActiveStorage::Blob
304304
attachment
305-
when ActiveStorage::Attached::One, ActiveStorage::Attached::Many
305+
when ActiveStorage::Attached::One
306+
attachment.blob
307+
when ActiveStorage::Attached::Many
306308
attachment.blobs
307309
when Hash
308310
attachment.values.map { |v| prepare_for_active_storage(v) }

lib/ruby_llm/active_record/chat_methods.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,9 @@ def prepare_for_active_storage(attachments)
315315
case attachment
316316
when ActionDispatch::Http::UploadedFile, ActiveStorage::Blob
317317
attachment
318-
when ActiveStorage::Attached::One, ActiveStorage::Attached::Many
318+
when ActiveStorage::Attached::One
319+
attachment.blob
320+
when ActiveStorage::Attached::Many
319321
attachment.blobs
320322
when Hash
321323
attachment.values.map { |v| prepare_for_active_storage(v) }

spec/dummy/app/models/document.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
class Document < ApplicationRecord
4+
has_one_attached :file
5+
has_many_attached :files
6+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
class CreateDocuments < ActiveRecord::Migration[7.0]
4+
def change
5+
create_table :documents do |t|
6+
t.string :title
7+
t.timestamps
8+
end
9+
end
10+
end

spec/dummy/db/schema.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@
7474
t.index ['tool_call_id'], name: 'index_document_messages_on_tool_call_id'
7575
end
7676

77+
create_table 'documents', force: :cascade do |t|
78+
t.string 'title'
79+
t.datetime 'created_at', null: false
80+
t.datetime 'updated_at', null: false
81+
end
82+
7783
create_table 'messages', force: :cascade do |t|
7884
t.integer 'chat_id'
7985
t.string 'role'

spec/fixtures/vcr_cassettes/activerecord_actsas_attachment_handling_handles_activestorage_attached_one_in_ask_method.yml

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/activerecord_actsas_attachment_handling_handles_multiple_attachments_in_ask_method.yml

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/ruby_llm/active_record/acts_as_attachment_spec.rb

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,47 @@ def uploaded_file(path, type)
4747
expect(llm_message.content.attachments.first.mime_type).to eq('image/png')
4848
end
4949

50-
it 'handles multiple attachments' do
50+
it 'handles ActiveStorage::Attached::One in ask method' do
5151
chat = Chat.create!(model: model)
5252

53-
image_upload = uploaded_file(image_path, 'image/png')
54-
pdf_upload = uploaded_file(pdf_path, 'application/pdf')
55-
56-
response = chat.ask('Analyze these', with: [image_upload, pdf_upload])
57-
58-
user_message = chat.messages.find_by(role: 'user')
59-
expect(user_message.attachments.count).to eq(2)
60-
expect(response.content).to be_present
61-
end
62-
63-
it 'handles attachments in ask method' do
64-
chat = Chat.create!(model: model)
65-
66-
image_upload = uploaded_file(image_path, 'image/png')
53+
document = Document.create!(title: 'Test Document')
54+
document.file.attach(
55+
io: File.open(image_path),
56+
filename: 'ruby.png',
57+
content_type: 'image/png'
58+
)
6759

68-
response = chat.ask('What do you see?', with: image_upload)
60+
response = chat.ask('What do you see?', with: document.file)
6961

7062
user_message = chat.messages.find_by(role: 'user')
7163
expect(user_message.attachments.count).to eq(1)
64+
expect(user_message.attachments.first.filename.to_s).to eq('ruby.png')
7265
expect(response.content).to be_present
7366
end
67+
68+
# it 'handles ActiveStorage::Attached::Many in ask method' do
69+
# chat = Chat.create!(model: model)
70+
71+
# document = Document.create!(title: 'Test Document')
72+
# document.files.attach(
73+
# io: File.open(image_path),
74+
# filename: 'ruby.png',
75+
# content_type: 'image/png'
76+
# )
77+
# document.files.attach(
78+
# io: File.open(pdf_path),
79+
# filename: 'sample.pdf',
80+
# content_type: 'application/pdf'
81+
# )
82+
83+
# response = chat.ask('Analyze these', with: document.files)
84+
85+
# user_message = chat.messages.find_by(role: 'user')
86+
# expect(user_message.attachments.count).to eq(2)
87+
# filenames = user_message.attachments.map { |a| a.filename.to_s }.sort
88+
# expect(filenames).to eq(['ruby.png', 'sample.pdf'])
89+
# expect(response.content).to be_present
90+
# end
7491
end
7592

7693
describe 'attachment types' do

0 commit comments

Comments
 (0)