Class: Datarobot::AiApi::AI
- Inherits:
-
Object
- Object
- Datarobot::AiApi::AI
- 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
-
#dataset_count ⇒ Object
readonly
Returns the value of attribute dataset_count.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#learning_session_count ⇒ Object
readonly
Returns the value of attribute learning_session_count.
-
#output_count ⇒ Object
readonly
Returns the value of attribute output_count.
Class Method Summary collapse
-
.create(name: 'New AI') ⇒ Datarobot::AiApi::AI
Creates an AI.
-
.delete(id) ⇒ nil
Deletes an AI.
-
.find(id, &block) ⇒ Datarobot::AiApi::AI
Retrieves an AI given an ID.
Instance Method Summary collapse
-
#add_dataset(dataset_id) ⇒ Bool
Adds a dataset to the AI.
-
#add_learning_session(session_id) ⇒ Bool
Adds a learning session to the AI.
-
#datasets ⇒ Datarobot::AiApi::Page(Datarobot::AiApi::Dataset)
Lists all the datasets associated with the AI as a paginated resource.
-
#find_output(name) ⇒ Datarobot::AiApi::Output
Finds an output by name.
-
#initialize(options = {}) ⇒ AI
constructor
Given a parsed response body from the API, will create a new AI object.
-
#outputs ⇒ Datarobot::AiApi::Page(Datarobot::AiApi::Output)
Lists all the outputs associated with the AI as a paginated resource.
-
#predict(target, data = {}) ⇒ Object
Predicts a target feature given a data hash.
-
#set_from_options(options = {}) ⇒ void
Takes a response body from the API.
-
#train(target, file_path) ⇒ Bool
Trains an AI on the given target with the data in the file at the given path.
Methods included from Refreshable
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( = {}) # Suppresses warnings about uninitialized variables @id = nil @name = nil @dataset_count = nil @output_count = nil @learning_session_count = nil () @outputs = nil end |
Instance Attribute Details
#dataset_count ⇒ Object (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 |
#id ⇒ Object (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_count ⇒ Object (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_count ⇒ Object (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
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
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.
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
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
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 |
#datasets ⇒ Datarobot::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
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 |
#outputs ⇒ Datarobot::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
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
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/datarobot/ai_api/ai.rb', line 79 def ( = {}) # one-liner replacement for `stringify_keys` = .collect{|k,v| [k.to_s, v]}.to_h @output_count = .dig("outputCount") || @output_count @dataset_count = .dig("datasetCount") || @dataset_count @learning_session_count = .dig("learningSessionCount") || @learning_session_count @name = .dig("name") || @name @id = .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
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 |