Class: MoondreamClient::Caption

Inherits:
Object
  • Object
show all
Defined in:
lib/moondream-client/caption.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, client: MoondreamClient.client) ⇒ Caption

Initialize a new Caption result object.

Parameters:

  • attributes (Hash)

    Raw attributes from the /caption endpoint response.

  • client (MoondreamClient::Client) (defaults to: MoondreamClient.client)


15
16
17
18
# File 'lib/moondream-client/caption.rb', line 15

def initialize(attributes, client: MoondreamClient.client)
  @client = client
  reset_attributes(attributes)
end

Instance Attribute Details

#captionString (readonly)

Returns The generated caption text.

Returns:

  • (String)

    The generated caption text.



9
10
11
# File 'lib/moondream-client/caption.rb', line 9

def caption
  @caption
end

#request_idString (readonly)

Returns The server-generated request identifier.

Returns:

  • (String)

    The server-generated request identifier.



6
7
8
# File 'lib/moondream-client/caption.rb', line 6

def request_id
  @request_id
end

Class Method Details

.create!(image_url:, length: "normal", stream: false, client: MoondreamClient.client) ⇒ MoondreamClient::Caption

Create a caption for an image. Corresponds to POST /caption

Parameters:

  • image_url (String)

    A URL or data URL for the image.

  • length (String) (defaults to: "normal")

    “short” or “normal” (default).

  • stream (Boolean) (defaults to: false)

    Whether to stream the response (default: false).

  • client (MoondreamClient::Client) (defaults to: MoondreamClient.client)

Returns:



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/moondream-client/caption.rb', line 30

def create!(image_url:, length: "normal", stream: false, client: MoondreamClient.client)
  image_data_url = MoondreamClient::Image.to_data_url(image_url)
  payload = {
    image_url: image_data_url,
    length: length,
    stream: stream
  }

  attributes = client.post("/caption", payload)
  new(attributes, client: client)
end

.stream!(image_url:, length: "normal", client: MoondreamClient.client) {|chunk| ... } ⇒ MoondreamClient::Caption

Stream caption chunks and return the final Caption instance.

Parameters:

  • image_url (String)
  • length (String) (defaults to: "normal")
  • client (MoondreamClient::Client) (defaults to: MoondreamClient.client)

Yields:

  • (chunk)

    yields each text chunk String as it arrives

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/moondream-client/caption.rb', line 49

def stream!(image_url:, length: "normal", client: MoondreamClient.client, &block)
  image_data_url = MoondreamClient::Image.to_data_url(image_url)
  payload = {
    image_url: image_data_url,
    length: length,
    stream: true
  }

  caption = nil

  client.post_stream("/caption", payload) do |data|
    if (chunk = data["chunk"]) && !chunk.to_s.empty?
      block&.call(chunk)
    end

    caption = data["caption"] if data["caption"]
  end

  # Build the final Caption from aggregated stream content.
  new({ "caption" => caption }, client: client)
end