Interface to the fuzzy.ai API for machine intelligence.
Copyright 2014-2018 Fuzzy.ai [email protected]
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
var FuzzyAIClient = require('fuzzy.ai');
var apiKey = 'API key from fuzzy.ai';
var client = new FuzzyAIClient({key: apiKey});
var agentID = 'ID from fuzzy.ai';
var inputs = {temperature: 87};
client.evaluate(agentID, inputs, function(err, outputs) {
if (err) {
console.error(err);
} else {
console.log("Fan speed is " + outputs.fanSpeed);
}
});
This is the main class; it's what's returned from the require().
-
FuzzyAIClient(options) Takes an
optionsargument. This is an object with the following properties:-
key: API key to use. You have to get anapiKeyfrom https://fuzzy.ai/ . Keep this secret, by the way. -
rootis the root of the API server. It has the correct default 'https://api.fuzzy.ai' but if you're doing some testing with a mock, it can be useful. -
queueLengthis the length of the request queue; requests are parallelized and a maximum of this number of requests will be done concurrently. This has a reasonable default of 32. -
maxWaitis the maximum time in seconds to wait for a response before returning an error. This has a reasonable default of 10 seconds. -
timeoutis how long persistent connections will stick around. The default is 0, meaning no persistent connections. If you are going to do a lot of requests, set this to something reasonable, like 1000 or 5000. Infinity means never disconnect (although the server will, eventually). Note that if you use persistent connections, you'll want to usestop()at the end of your program so the connections are cleaned up.
-
This is the main method you need to use:
-
evaluate(agentID, inputs, [meta], callback) Does a single inference.
agentIDis on the main page for the agent on http://fuzzy.ai/ . Theinputsis an object, mapping input names to numeric values.metais a string or boolean value; if provided and truthy,outputwill include ametaproperty with meta information, or a property with the same name as the string value, if you need to avoid using "meta" as a property.callbackis a function with the signaturefunction(err, outputs), whereoutputsis an object mapping output names to numeric values. It may also have ametaor other specified property depending on the meta flag.
To train for better results, use the feedback method with your success metric.
- feedback(evaluationID, metrics, callback) Trains the agent based on the
evaluationIDis returned in the output for theevaluate()method (see above). Thesuccessis an object, mapping success metric names to numeric values. Defining the success metric is up to you, but closer to zero = better.callbackis a function with the signaturefunction(err, metrics), wheremetricsis an object mapping output names to numeric values, which is just what you passed in plus some extra housekeeping data.
These might be useful but you normally don't need to mess with them.
-
getAgents(callback)
userIDis the user ID, not the API key.callbackis a function with the signaturefunction(err, agents), whereagentsis an array of objects withidandnameproperties, one for each agent the user has. -
newAgent(agent, callback)
userIDis the user ID.agentis an agent object with at least propertiesinputs,outputs,rules.callbackis a function with the signaturefunction(err, agent)which returns the fully-realized agent with all its properties like timestamps and IDs. -
getAgent(agentID, callback) Gets a single agent by ID.
callbackis a function with the signaturefunction(err, agent). -
putAgent(agentID, agent, callback) Updates an agent.
callbackhas the signaturefunction(err, agent)which will return the updated version. -
deleteAgent(agentID, callback) Deletes an agent. This method will permanently delete an agent and all related data; use with caution.
callbackhas the signaturefunction(err)which returns an error if there was a problem. -
evaluation(evaluationID, callback) Gets the audit data for the evaluation.
evaluationIDis returned in the output for theevaluate()method (see above).callbackis a function with the signaturefunction(err, audit), whereauditis an object mapping audit data names to data. Important values here:input: the input values that were passed incrisp: the output values that were the results of the evaluationrules: The index of the rules that fired
Most of the rest is fuzzy logic stuff that you probably don't care about.
-
getAgentVersion(versionID, callback) For older versions of an agent, you can get the properties of the older version.
-
apiVersion(callback) Get the version data from the server. This is a good "smoke test" method to see if the server is responding at all.
callbackhas the signaturefunction(err, versionData), whereversionDatais an object with the following properties:name: name of the API server. Usuallyapi.version: (Semver)[http://semver.org/] version for the API server software.controllerVersion: Semver version for the fuzzy controller software.
-
stop(callback) Clean up any persistent connections. You only need to call this if you provided a non-zero
timeoutto the constructor.