Class: MoondreamClient::Caption
- Inherits:
-
Object
- Object
- MoondreamClient::Caption
- Defined in:
- lib/moondream-client/caption.rb
Instance Attribute Summary collapse
-
#caption ⇒ String
readonly
The generated caption text.
-
#request_id ⇒ String
readonly
The server-generated request identifier.
Class Method Summary collapse
-
.create!(image_url:, length: "normal", stream: false, client: MoondreamClient.client) ⇒ MoondreamClient::Caption
Create a caption for an image.
-
.stream!(image_url:, length: "normal", client: MoondreamClient.client) {|chunk| ... } ⇒ MoondreamClient::Caption
Stream caption chunks and return the final Caption instance.
Instance Method Summary collapse
-
#initialize(attributes, client: MoondreamClient.client) ⇒ Caption
constructor
Initialize a new Caption result object.
Constructor Details
#initialize(attributes, client: MoondreamClient.client) ⇒ Caption
Initialize a new Caption result object.
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
#caption ⇒ String (readonly)
Returns The generated caption text.
9 10 11 |
# File 'lib/moondream-client/caption.rb', line 9 def caption @caption end |
#request_id ⇒ String (readonly)
Returns 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
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.
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 |