Class: Moondream::Client

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

Constant Summary collapse

BASE_URL =
"https://api.moondream.ai/v1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:) ⇒ Moondream::Client

Returns The client instance.

Parameters:

  • api_key (String)

    The API key to use for the client



20
21
22
# File 'lib/moondream/client.rb', line 20

def initialize(api_key:)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



14
15
16
# File 'lib/moondream/client.rb', line 14

def api_key
  @api_key
end

Instance Method Details

#caption(image_url: nil, length: "normal", stream: false) ⇒ String

In this method, we’re using the native Net::HTTP so that the stream support can be supported. The usage of this function if stream=true is: client = Moondream::Client.new(api_key: “your_api_key”) client.caption(“”, “normal”, true) do |response|

response.read_body do |chunk|
  puts chunk # or do something else with the chunk
end

end

Parameters:

  • image_url (String) (defaults to: nil)

    The URL of the image to caption

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

    The length of the caption, “normal” or “short”

  • stream (Boolean) (defaults to: false)

    Whether to stream the response, default is false

Returns:

  • (String)

    The response from the API



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/moondream/client.rb', line 76

def caption(image_url: nil, length: "normal", stream: false)
  if image_url.nil?
    raise ArgumentError, "image_url is required"
  end

  uri = URI("#{BASE_URL}/caption")

  result = nil
  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
    request = Net::HTTP::Post.new(uri)
    request["Authorization"] = "Bearer #{api_key}"
    request["Content-Type"] = "application/json"
    request.body = { image_url: image_url, length: length, stream: stream }.to_json

    http.request(request) do |response|
      yield response if stream && block_given?
      result = response.body
    end
  end
  result
end

#detect(image_url, object) ⇒ String

Returns The response from the API.

Parameters:

  • image_url (String)

    The URL of the image to detect objects in

  • object (String)

    The object to detect in the image

Returns:

  • (String)

    The response from the API



40
41
42
43
44
45
46
47
48
# File 'lib/moondream/client.rb', line 40

def detect(image_url, object)
  response = HTTParty.post(
    "#{BASE_URL}/detect",
    body: { image_url: image_url, object: object },
    headers: { "Authorization" => "Bearer #{api_key}" }
  )

  response.body
end

#point(image_url, object) ⇒ String

Returns The response from the API.

Parameters:

  • image_url (String)

    The URL of the image to point to

  • object (String)

    The object to point to in the image

Returns:

  • (String)

    The response from the API



53
54
55
56
57
58
59
60
61
# File 'lib/moondream/client.rb', line 53

def point(image_url, object)
  response = HTTParty.post(
    "#{BASE_URL}/point",
    body: { image_url: image_url, object: object },
    headers: { "Authorization" => "Bearer #{api_key}" }
  )

  response.body
end

#query(image_url, prompt) ⇒ String

Returns The response from the API.

Parameters:

  • image_url (String)

    The URL of the image to query

  • prompt (String)

    The prompt to query the image with

Returns:

  • (String)

    The response from the API



27
28
29
30
31
32
33
34
35
# File 'lib/moondream/client.rb', line 27

def query(image_url, prompt)
  response = HTTParty.post(
    "#{BASE_URL}/query",
    body: { image_url: image_url, prompt: prompt },
    headers: { "Authorization" => "Bearer #{api_key}" }
  )

  response.body
end