Class: Datarobot::AiApi::AI

Inherits:
Object
  • Object
show all
Includes:
Refreshable
Defined in:
lib/datarobot/ai_api/ai.rb

Overview

This is the primary model in the AI API. This is the thing that learns about the data that you give it

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Refreshable

#refresh, #refresh!

Constructor Details

#initialize(options = {}) ⇒ AI

Given a parsed response body from the API, will create a new AI object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/datarobot/ai_api/ai.rb', line 62

def initialize(options = {})
  # Suppresses warnings about uninitialized variables
  @id = nil
  @name = nil
  @dataset_count = nil
  @output_count = nil
  @learning_session_count = nil

  set_from_options(options)
  @outputs = nil
end

Instance Attribute Details

#dataset_countObject (readonly)

Returns the value of attribute dataset_count.



8
9
10
# File 'lib/datarobot/ai_api/ai.rb', line 8

def dataset_count
  @dataset_count
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/datarobot/ai_api/ai.rb', line 8

def id
  @id
end

#learning_session_countObject (readonly)

Returns the value of attribute learning_session_count.



8
9
10
# File 'lib/datarobot/ai_api/ai.rb', line 8

def learning_session_count
  @learning_session_count
end

#output_countObject (readonly)

Returns the value of attribute output_count.



8
9
10
# File 'lib/datarobot/ai_api/ai.rb', line 8

def output_count
  @output_count
end

Class Method Details

.create(name: 'New AI') ⇒ Datarobot::AiApi::AI

Creates an AI

Parameters:

  • name (String) (defaults to: 'New AI')

    The ID of the AI to retrieve

Returns:



44
45
46
47
48
49
# File 'lib/datarobot/ai_api/ai.rb', line 44

def self.create(name: 'New AI')
  Datarobot::AiApi.request_endpoint('/aiapi/ais/', method: 'post', body: { name: name }) do |data|
    ai_data = Datarobot::AiApi.get(data["links"]["result"])
    new(ai_data)
  end
end

.delete(id) ⇒ nil

Deletes an AI. Returns ‘nil` if the action was successful. Will raise an error if the action was unsuccessful

Parameters:

  • id (String)

    The ID of the AI to delete

Returns:

  • (nil)


57
58
59
# File 'lib/datarobot/ai_api/ai.rb', line 57

def self.delete(id)
  Datarobot::AiApi.request_endpoint("/aiapi/ais/#{id}", method: "delete")
end

.find(id, &block) ⇒ Datarobot::AiApi::AI

Retrieves an AI given an ID.

Parameters:

  • id (String)

    The ID of the AI to retrieve

Returns:

Raises:



28
29
30
31
32
33
34
35
36
37
# File 'lib/datarobot/ai_api/ai.rb', line 28

def self.find(id, &block)
  raise Datarobot::AiApi::NotFoundError, "Cannot find AI with id: nil" if id.nil?
  Datarobot::AiApi.request_endpoint("/aiapi/ais/#{id}") do |data|
    if block_given?
      yield data
    else
      self.new(data)
    end
  end
end

Instance Method Details

#add_dataset(dataset_id) ⇒ Bool

Adds a dataset to the AI. Raises an error on failure. Returns true on success

Parameters:

  • dataset_id (String)

    The ID of the dataset to add

Returns:

  • (Bool)

    true



140
141
142
143
# File 'lib/datarobot/ai_api/ai.rb', line 140

def add_dataset(dataset_id)
  Datarobot::AiApi.request_endpoint("/aiapi/ais/#{@id}/datasets", method: 'post', body: { datasetId: dataset_id })
  true
end

#add_learning_session(session_id) ⇒ Bool

Adds a learning session to the AI. Raises an error on failure. Returns true on success

Parameters:

  • session_id (String)

    The ID of the learning session to add

Returns:

  • (Bool)

    true



150
151
152
153
# File 'lib/datarobot/ai_api/ai.rb', line 150

def add_learning_session(session_id)
  Datarobot::AiApi.request_endpoint("/aiapi/ais/#{@id}/learningSessions", method: 'post', body: { learningSessionId: session_id })
  true
end

#datasetsDatarobot::AiApi::Page(Datarobot::AiApi::Dataset)

Lists all the datasets associated with the AI as a paginated resource



103
104
105
106
107
108
# File 'lib/datarobot/ai_api/ai.rb', line 103

def datasets
  Datarobot::AiApi.request_endpoint("/aiapi/datasets?aiId=#{@id}") do |data|
    data["aiId"] = @id
    Datarobot::AiApi::Page.new(Datarobot::AiApi::Dataset, data)
  end
end

#find_output(name) ⇒ Datarobot::AiApi::Output

Finds an output by name

that you want to find

Parameters:

  • name (String)

    The name of the output associated with this AI

Returns:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/datarobot/ai_api/ai.rb', line 115

def find_output(name)
  # TODO: Update this when AI API supports CGI escaped URIs
  #
  # This code was lifted from source of erb method:
  # https://apidock.com/ruby/v2_6_3/ERB/Util/url_encode
  #
  # URI.escape is deprecated because it does not encode url control
  # characters, thus making it a potential security issue and CGI.escape
  # substitutes whitespace with a + which is correct, but unsupported by
  # the AI API
  encoded_name = name.gsub(/[^a-zA-Z0-9_\-.~]/) { |m|
    sprintf("%%%02X", m.unpack1("C"))
  }

  Datarobot::AiApi.request_endpoint("/aiapi/ais/#{@id}/outputs/#{encoded_name}") do |data|
    data["aiId"] = @id
    Datarobot::AiApi::Output.new(data)
  end
end

#outputsDatarobot::AiApi::Page(Datarobot::AiApi::Output)

Lists all the outputs associated with the AI as a paginated resource



93
94
95
96
97
98
# File 'lib/datarobot/ai_api/ai.rb', line 93

def outputs
  Datarobot::AiApi.request_endpoint("/aiapi/ais/#{@id}/outputs") do |data|
    data["aiId"] = @id
    Datarobot::AiApi::Page.new(Datarobot::AiApi::Output, data)
  end
end

#predict(target, data = {}) ⇒ Object

Predicts a target feature given a data hash

Parameters:

  • target

    The target feature to predict

  • data (defaults to: {})

    The remaining features used to predict target feature

Raises:



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/datarobot/ai_api/ai.rb', line 173

def predict(target, data={})
  output = outputs.find { |o| o.target == target }

  raise NotFoundError, "No output with target #{target.inspect} found AI with ID #{@id.inspect}" if output.nil?

  deployment_id = output.source["deploymentId"]
  key = output.source["datarobot-key"]
  Datarobot::AiApi.request_endpoint(
    "/predApi/v1.0/deployments/#{deployment_id}/predictions/",
    method: 'post',
    body: [ data ],
    headers: {"datarobot-key" => key}
  ) do |data|
    Datarobot::AiApi::Page.new(Datarobot::AiApi::Prediction, data)
  end
end

#set_from_options(options = {}) ⇒ void

This method returns an undefined value.

Takes a response body from the API. Will set all AI attributes from the response body

Parameters:

  • options (Hash) (defaults to: {})

    A parsed response body



79
80
81
82
83
84
85
86
87
88
# File 'lib/datarobot/ai_api/ai.rb', line 79

def set_from_options(options = {})
  # one-liner replacement for `stringify_keys`
  options = options.collect{|k,v| [k.to_s, v]}.to_h

  @output_count = options.dig("outputCount") || @output_count
  @dataset_count = options.dig("datasetCount") || @dataset_count
  @learning_session_count = options.dig("learningSessionCount") || @learning_session_count
  @name = options.dig("name") || @name
  @id = options.dig("id") || @id
end

#train(target, file_path) ⇒ Bool

Trains an AI on the given target with the data in the file at the given path. Returns true if successful. Raises an error otherwise

Parameters:

  • target (String)

    The target to train the AI on

Returns:

  • (Bool)

    true



160
161
162
163
164
165
166
167
# File 'lib/datarobot/ai_api/ai.rb', line 160

def train(target, file_path)
  dataset = Datarobot::AiApi::Dataset.create_from_file(file_path)
  add_dataset(dataset.id)
  learning_session = Datarobot::AiApi::LearningSession.create(dataset_id: dataset.id, target: target)
  add_learning_session(learning_session.id)
  Datarobot::AiApi::Output.create(ai_id: @id, learning_session_id: learning_session.id, output_name: "#{@name} output")
  true
end