Class: Moondream::Client
- Inherits:
-
Object
- Object
- Moondream::Client
- Defined in:
- lib/moondream/client.rb
Constant Summary collapse
- BASE_URL =
"https://api.moondream.ai/v1"
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Instance Method Summary collapse
-
#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.
-
#detect(image_url, object) ⇒ String
The response from the API.
-
#initialize(api_key:) ⇒ Moondream::Client
constructor
The client instance.
-
#point(image_url, object) ⇒ String
The response from the API.
-
#query(image_url, prompt) ⇒ String
The response from the API.
Constructor Details
#initialize(api_key:) ⇒ Moondream::Client
Returns The client instance.
20 21 22 |
# File 'lib/moondream/client.rb', line 20 def initialize(api_key:) @api_key = api_key end |
Instance Attribute Details
#api_key ⇒ Object (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
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.
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.
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.
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 |