Class: Aigen::Google::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/aigen/google/content.rb

Overview

Content represents text, image, or multimodal content for Gemini API requests. Provides builder methods for creating content and serialization to API format.

Examples:

Text content

content = Aigen::Google::Content.text("Hello, world!")
content.to_h # => {parts: [{text: "Hello, world!"}]}

Image content (Base64-encoded)

require "base64"
image_data = Base64.strict_encode64(File.read("image.jpg"))
content = Aigen::Google::Content.image(data: image_data, mime_type: "image/jpeg")
content.to_h # => {parts: [{inline_data: {mime_type: "image/jpeg", data: "..."}}]}

Multimodal content (text + image)

text_part = {text: "What is in this image?"}
image_part = {inline_data: {mime_type: "image/jpeg", data: base64_data}}
content = Aigen::Google::Content.new([text_part, image_part])

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts) ⇒ Content

Initializes a Content instance with an array of parts.

Examples:

parts = [
  {text: "Describe this image:"},
  {inline_data: {mime_type: "image/jpeg", data: base64_data}}
]
content = Content.new(parts)

Parameters:

  • array of content parts

    • Text part: “string”

    • Image part: {mime_type: “…”, data: “…”}



66
67
68
# File 'lib/aigen/google/content.rb', line 66

def initialize(parts)
  @parts = parts
end

Class Method Details

.image(data:, mime_type:) ⇒ Content

Creates an image content instance with Base64-encoded data.

Examples:

require "base64"
data = Base64.strict_encode64(File.read("photo.jpg"))
content = Content.image(data: data, mime_type: "image/jpeg")

Parameters:

  • Base64-encoded image data (use Base64.strict_encode64)

  • the MIME type (e.g., “image/jpeg”, “image/png”)

Returns:

  • a Content instance with inline_data part



45
46
47
48
49
50
51
52
# File 'lib/aigen/google/content.rb', line 45

def self.image(data:, mime_type:)
  new([{
    inline_data: {
      mime_type: mime_type,
      data: data
    }
  }])
end

.text(text) ⇒ Content

Creates a text content instance.

Examples:

content = Content.text("Hello!")
content.to_h # => {parts: [{text: "Hello!"}]}

Parameters:

  • the text content

Returns:

  • a Content instance with text part



31
32
33
# File 'lib/aigen/google/content.rb', line 31

def self.text(text)
  new([{text: text}])
end

Instance Method Details

#to_hHash

Serializes the content to Gemini API format.

Examples:

content = Content.text("Hello")
content.to_h # => {parts: [{text: "Hello"}]}

Returns:

  • the content hash with parts array



77
78
79
# File 'lib/aigen/google/content.rb', line 77

def to_h
  {parts: @parts}
end