Module: Ask::Content

Defined in:
lib/ask/content.rb

Overview

Content types for multi-modal messages.

These are frozen value objects representing different kinds of content that can appear in an Message. A message's content_blocks may contain multiple content objects of different types, allowing text, images, audio, video, and files to be interleaved naturally.

All content types are frozen value objects with structural equality. They carry the minimum fields needed for provider-agnostic use; each provider serializes them to its own wire format.

Examples:

Text

Ask::Content::Text.new("What's in this image?")

Image from URL

Ask::Content::Image.new(url: "https://example.com/photo.jpg",
                        mime_type: "image/jpeg")

Multi-modal message

msg = Ask::Message.new(role: :user, content: [
  Ask::Content::Text.new("What's in this image?"),
  Ask::Content::Image.new(url: "https://example.com/photo.jpg",
                          mime_type: "image/jpeg")
])

Defined Under Namespace

Modules: Block Classes: Audio, File, Image, Media, Text, Video

Class Method Summary collapse

Class Method Details

.from_h(hash) ⇒ Block

Rebuild a content block from a to_h hash (persistence round-trips).

Parameters:

Returns:



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/ask/content.rb', line 223

def self.from_h(hash)
  hash = hash.transform_keys(&:to_s)
  case hash["type"]
  when "text"
    Text.new(hash["text"].to_s)
  when "image"
    Image.new(**media_args(hash))
  when "audio"
    Audio.new(**media_args(hash))
  when "video"
    Video.new(**media_args(hash))
  when "file"
    File.new(
      data: hash["data"], mime_type: hash["mime_type"], filename: hash["filename"],
      url: hash["url"], file_id: hash["file_id"]
    )
  else
    raise ArgumentError, "Unknown content block type: #{hash["type"].inspect}"
  end
end