Class: Whitebox::Client

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: nil, timeout: 30) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
# File 'lib/whitebox/client.rb', line 9

def initialize(api_key:, base_url: nil, timeout: 30)
  @api_key = api_key
  @base_url = base_url || BASE_URL
  @timeout = timeout
end

Instance Method Details

#decide(input:, options:, prompt: nil, runs: 7, threshold: 0.75, sync: true, mode: "standard", models: nil) ⇒ Object

Single decision



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/whitebox/client.rb', line 16

def decide(input:, options:, prompt: nil, runs: 7, threshold: 0.75, sync: true, mode: "standard", models: nil)
  body = {
    input: input,
    options: options,
    prompt: prompt,
    runs: runs,
    threshold: threshold,
    sync: sync,
    mode: mode,
  }
  body[:models] = models if models

  data = request(:post, "/decide", body)
  Decision.from_hash(data)
end

#decide_bulk(items:, prompt: nil, options: nil, runs: 7, threshold: 0.75, webhook_url: nil) ⇒ Object

Bulk decisions



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/whitebox/client.rb', line 38

def decide_bulk(items:, prompt: nil, options: nil, runs: 7, threshold: 0.75, webhook_url: nil)
  body = {
    items: items,
    prompt: prompt,
    options: options,
    runs: runs,
    threshold: threshold,
    webhook_url: webhook_url,
  }.compact

  data = request(:post, "/decide/bulk", body)
  Batch.from_hash(data)
end

#decide_fast(input:, options:, prompt: nil, threshold: 0.75) ⇒ Object

Fast mode: 3 runs, 2 models, sync



33
34
35
# File 'lib/whitebox/client.rb', line 33

def decide_fast(input:, options:, prompt: nil, threshold: 0.75)
  decide(input: input, options: options, prompt: prompt, threshold: threshold, mode: "fast", sync: true)
end

#get_batch(id) ⇒ Object

Get batch status



65
66
67
68
# File 'lib/whitebox/client.rb', line 65

def get_batch(id)
  data = request(:get, "/batches/#{id}")
  Batch.from_hash(data)
end

#get_batch_results(id) ⇒ Object

Get batch results



71
72
73
# File 'lib/whitebox/client.rb', line 71

def get_batch_results(id)
  request(:get, "/batches/#{id}/results")
end

#get_decision(id) ⇒ Object

Get a single decision



53
54
55
56
# File 'lib/whitebox/client.rb', line 53

def get_decision(id)
  data = request(:get, "/decisions/#{id}")
  Decision.from_hash(data)
end

#list_decisions(page: 1, per_page: 20) ⇒ Object

List decisions



59
60
61
62
# File 'lib/whitebox/client.rb', line 59

def list_decisions(page: 1, per_page: 20)
  data = request(:get, "/decisions?page=#{page}&per_page=#{per_page}")
  (data["decisions"] || []).map { |d| Decision.from_hash(d) }
end

#list_reviewsObject

List pending reviews



76
77
78
79
# File 'lib/whitebox/client.rb', line 76

def list_reviews
  data = request(:get, "/reviews")
  (data.is_a?(Array) ? data : []).map { |r| Review.from_hash(r) }
end

#modelsObject

List supported models



88
89
90
# File 'lib/whitebox/client.rb', line 88

def models
  request(:get, "/models")
end

#resolve_review(id, answer:) ⇒ Object

Resolve a review



82
83
84
85
# File 'lib/whitebox/client.rb', line 82

def resolve_review(id, answer:)
  data = request(:patch, "/reviews/#{id}", { answer: answer })
  Review.from_hash(data)
end