redisai-js
Version:
A high-performance Typescript client for RedisAI
106 lines (105 loc) • 3.77 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", {value: true});
var Tensor_1 = require("./Tensor");
var util = __importStar(require("util"));
var dtype_1 = require("./dtype");
var Client = /** @class */ (function () {
function Client(client) {
this._client = client;
this._sendCommand = util.promisify(this._client.send_command).bind(this._client);
}
Object.defineProperty(Client.prototype, "client", {
get: function () {
return this._client;
},
enumerable: true,
configurable: true
});
Client.prototype.end = function (flush) {
this._client.end(flush);
};
Client.prototype.tensorset = function (keName, t) {
var args = [keName, t.dtype];
t.shape.forEach(function (value) {
return args.push(value.toString());
});
if (t.data != null) {
args.push('VALUES');
t.data.forEach(function (value) {
return args.push(value.toString());
});
}
return this._sendCommand('ai.tensorset', args);
};
Client.prototype.tensorget = function (keName) {
var args = [keName, 'META', 'VALUES'];
return this._sendCommand('ai.tensorget', args)
.then(function (reply) {
var dt = null;
var shape = null;
var values = null;
for (var i = 0; i < reply.length; i += 2) {
var key = reply[i];
var obj = reply[i + 1];
switch (key.toString()) {
case 'dtype':
// @ts-ignore
dt = dtype_1.DTypeMap[obj.toString()];
break;
case 'shape':
shape = obj;
break;
case 'values':
values = obj.map(Number);
break;
}
}
if (dt == null || shape == null || values == null) {
throw Error('tensorget reply did not had the full elements to build the tensor');
}
return new Tensor_1.Tensor(dt, shape, values);
})
.catch(function (error) {
throw error;
});
};
Client.prototype.modelset = function (keName, m) {
var args = [keName, m.backend, m.device];
if (m.inputs.length > 0) {
args.push('INPUTS');
m.inputs.forEach(function (value) {
return args.push(value);
});
}
if (m.outputs.length > 0) {
args.push('OUTPUTS');
m.outputs.forEach(function (value) {
return args.push(value);
});
}
args.push('BLOB');
args.push(m.blob.toString());
return this._sendCommand('ai.modelset', args);
};
Client.prototype.modelrun = function (modelName, inputs, outputs) {
var args = [modelName, 'INPUTS'];
inputs.forEach(function (value) {
return args.push(value);
});
args.push('OUTPUTS');
outputs.forEach(function (value) {
return args.push(value);
});
return this._sendCommand('ai.modelrun', args);
};
return Client;
}());
exports.Client = Client;
//# sourceMappingURL=Client.js.map