Basic checks
What's broken?
I'm using Rails with ActiveStorage for file attachments with Google Cloud Storage. I want the file to go under specific folder in my bucket so I upload them first with
ActiveStorage::Blob.create_and_upload!(
...,
key: "my/file/path.ext"
)
If I pass the blob object using chat.ask, chat.create_user_message, ..., it'll be re-uploaded instead of reusing my original uploaded file.
How to reproduce
- Setup RubyLLM with Rails integration + ActiveStorage for file attachments
- Upload a file using
ActiveStorage::Blob
blob = ActiveStorage::Blob.create_and_upload!(
...,
key: "my/file/path.ext"
)
- Use it with a chat record backed by ActiveRecord
# I might have multiple files here so I'm using a list
chat.ask("Describe the content of the attached file", with: [blob])
Expected behavior
RubyLLM reuse the uploaded file
What actually happened
A new upload is created in my bucket and my old one is "orphaned".
This looks like it's due to build_content wrapping things under RubyLLM::Content.new so ActiveStorage::Blob is wrapped under RubyLLM::Attachment. prepare_for_active_storage doesn't know it's an ActiveStorage::Blob anymore then in the end this line would just create a new blob and re-upload.
The fix for just my use case is probably to update prepare_for_active_storage to handle valid ActiveStorage attachable hash differently then I can just pass in a hash using the with parameter, e.g.
when Hash
if attachment.key?(:io) && attachment.key?(:filename)
attachment
else
attachment.values.map { |v| prepare_for_active_storage(v) }
end
Otherwise I think we should make RubyLLM::Content preserve ActiveStorage-native objects.
Please let me know if you are interested in any direction. I'd love to try and create a PR for it.
Environment
- Ruby version: 3.4.8
- RubyLLM version: 1.13.2
- Provider: OpenAI
- OS: Mac OSX
Basic checks
What's broken?
I'm using Rails with ActiveStorage for file attachments with Google Cloud Storage. I want the file to go under specific folder in my bucket so I upload them first with
If I pass the blob object using
chat.ask,chat.create_user_message, ..., it'll be re-uploaded instead of reusing my original uploaded file.How to reproduce
ActiveStorage::BlobExpected behavior
RubyLLM reuse the uploaded file
What actually happened
A new upload is created in my bucket and my old one is "orphaned".
This looks like it's due to
build_contentwrapping things underRubyLLM::Content.newsoActiveStorage::Blobis wrapped underRubyLLM::Attachment.prepare_for_active_storagedoesn't know it's anActiveStorage::Blobanymore then in the end this line would just create a new blob and re-upload.The fix for just my use case is probably to update
prepare_for_active_storageto handle valid ActiveStorage attachable hash differently then I can just pass in a hash using thewithparameter, e.g.Otherwise I think we should make
RubyLLM::Contentpreserve ActiveStorage-native objects.Please let me know if you are interested in any direction. I'd love to try and create a PR for it.
Environment