Class: Braintrust::Trace::Attachment
- Inherits:
-
Object
- Object
- Braintrust::Trace::Attachment
- Defined in:
- lib/braintrust/trace/attachment.rb
Overview
Attachment represents binary data (images, audio, PDFs, etc.) that can be logged as part of traces in Braintrust. Attachments are stored securely and can be viewed in the Braintrust UI.
Attachments are particularly useful for multimodal AI applications, such as vision models that process images.
Constant Summary collapse
- IMAGE_PNG =
Common MIME type constants for convenience
"image/png"- IMAGE_JPEG =
"image/jpeg"- IMAGE_JPG =
"image/jpg"- IMAGE_GIF =
"image/gif"- IMAGE_WEBP =
"image/webp"- TEXT_PLAIN =
"text/plain"- APPLICATION_PDF =
"application/pdf"
Class Method Summary collapse
-
.from_bytes(content_type, data) ⇒ Attachment
Creates an attachment from raw bytes.
-
.from_file(content_type, path) ⇒ Attachment
Creates an attachment by reading from a file.
-
.from_url(url) ⇒ Attachment
Creates an attachment by fetching data from a URL.
Instance Method Summary collapse
-
#to_data_url ⇒ String
Converts the attachment to a data URL format.
-
#to_message ⇒ Hash
(also: #to_h)
Converts the attachment to a message format suitable for LLM APIs.
Class Method Details
.from_bytes(content_type, data) ⇒ Attachment
Creates an attachment from raw bytes.
64 65 66 |
# File 'lib/braintrust/trace/attachment.rb', line 64 def self.from_bytes(content_type, data) new(content_type, data) end |
.from_file(content_type, path) ⇒ Attachment
Creates an attachment by reading from a file.
77 78 79 80 |
# File 'lib/braintrust/trace/attachment.rb', line 77 def self.from_file(content_type, path) data = File.binread(path) new(content_type, data) end |
.from_url(url) ⇒ Attachment
Creates an attachment by fetching data from a URL.
The content type is inferred from the Content-Type header in the HTTP response. If the header is not present, it falls back to “application/octet-stream”.
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/braintrust/trace/attachment.rb', line 93 def self.from_url(url) uri = URI.parse(url) request = Net::HTTP::Get.new(uri) response = Braintrust::Internal::Http.with_redirects(uri, request) unless response.is_a?(Net::HTTPSuccess) raise StandardError, "Failed to fetch URL: #{response.code} #{response.}" end content_type = response.content_type || "application/octet-stream" new(content_type, response.body) end |
Instance Method Details
#to_data_url ⇒ String
Converts the attachment to a data URL format.
114 115 116 117 |
# File 'lib/braintrust/trace/attachment.rb', line 114 def to_data_url encoded = Internal::Encoding::Base64.strict_encode64(@data) "data:#{@content_type};base64,#{encoded}" end |
#to_message ⇒ Hash Also known as: to_h
Converts the attachment to a message format suitable for LLM APIs.
127 128 129 130 131 132 |
# File 'lib/braintrust/trace/attachment.rb', line 127 def { "type" => "base64_attachment", "content" => to_data_url } end |