A high performance node.js RedisAI client
You can use this client to query RedisAI using plain Javascript, or in a type-safe manner using Typescript, since redisai-js comes with its own type definitions built-in.
Installation
Installation is done using the
npm install
command:
npm install --save redisai-js
Overview
Vanilla JS:
Example of AI.TENSORSET and AI.TENSORGET
var redis = ;var redisai = ; async { const nativeClient = redis; const aiclient = nativeClient; const tensorA = redisaiDtypefloat32 1 2 3 5; const result = await aiclient; // AI.TENSORSET result: OK console const tensorGetReply = await aiclient; // AI.TENSORGET reply: datatype FLOAT shape [1,2] , data [3,5] console; await aiclient;};
Example of AI.MODELSET and AI.MODELRUN
var redis = ;var redisai = ;var fs = ; async { const nativeClient = redis; const aiclient = nativeClient; const tensorA = redisaiDtypefloat32 1 2 2 3; const tensorB = redisaiDtypefloat32 1 2 3 5; const result_tA = await aiclient; const result_tB = await aiclient; const model_blob = fs; // AI.TENSORSET tA result: OK console // AI.TENSORSET tB result: OK console const mymodel = redisaiBackendTF "CPU" "a" "b" "c" model_blob; const result_modelSet = await aiclient; // AI.MODELSET result: OK console const result_modelRun = await aiclient; console const tensorC = await aiclient; // AI.TENSORGET tC reply: datatype FLOAT shape [1,2] , data [6,15] console; await aiclient;};
Example of AI.SCRIPTSET and AI.SCRIPTRUN
async { const nativeClient = redis; const aiclient = nativeClient; const tensorA = redisaiDtypefloat32 1 2 2 3; const tensorB = redisaiDtypefloat32 1 2 3 5; const script_str = 'def bar(a, b):\n return a + b\n'; const result_tA = await aiclient; const result_tB = await aiclient; // AI.TENSORSET tA result: OK console // AI.TENSORSET tB result: OK console const myscript = "CPU" script_str; const result_scriptSet = await aiclient; // AI.SCRIPTSET result: OK console const result_scriptRun = await aiclient; console const tensorD = await aiclient; // AI.TENSORGET tD reply: datatype FLOAT shape [1,2] , data [5,8] console; await aiclient;};
Example of AI.DAGRUN enqueuing multiple SCRIPTRUN and MODELRUN commands
A common pattern is enqueuing multiple SCRIPTRUN and MODELRUN commands within a DAG. The following example uses ResNet-50,to classify images into 1000 object categories.
Given that our input tensor contains each color represented as a 8-bit integer and that neural networks usually work with floating-point tensors as their input we need to cast a tensor to floating-point and normalize the values of the pixels - for that we will use pre_process_4ch
function.
To optimize the classification process we can use a post process script to return only the category position with the maximum classification - for that we will use post_process
script.
Using the DAG capabilities we've removed the necessity of storing the intermediate tensors in the keyspace. You can even run the entire process without storing the output tensor, as follows:
var redis = ;var redisai = ;var fs = ; async { const nativeClient = redis; const aiclient = nativeClient; const scriptFileStr = fs; const jsonLabels = fs; const labels = JSON; const dataProcessingScript = 'CPU' scriptFileStr; const resultScriptSet = await aiclient; // AI.SCRIPTSET result: OK console const modelBlob = fs; const imagenetModel = BackendTF 'CPU' 'images' 'output' modelBlob; const resultModelSet = await aiclient; // AI.MODELSET result: OK console const inputImage = await Jimp; const imageWidth = 224; const imageHeight = 224; const image = inputImage; const tensor = Dtypeuint8 imageWidth imageHeight 4 Buffer; /// // Prepare the DAG enqueuing multiple SCRIPTRUN and MODELRUN commands const dag = ; dag; dag; dag; dag; dag; // Send the AI.DAGRUN command to RedisAI server const resultDagRun = await aiclient; // The 5th element of the reply will be the `classification` tensor const classTensor = resultDagRun4; // Print the category in the position with the max classification const idx = classTensordata0; // 281 [ 'n02123045', 'tabby' ] console; await aiclient;};
Further examples
The RedisAI examples repo shows more advanced examples made using redisai-js under js_client folder.
Supported RedisAI Commands
Command | Recommended API and JSDoc |
---|---|
AI.TENSORSET | tensorset |
AI.TENSORGET | tensorget |
AI.MODELSET | modelset |
AI.MODELGET | modelget |
AI.MODELDEL | modeldet |
AI.MODELRUN | modelrun |
AI._MODELSCAN | N/A |
AI.SCRIPTSET | scriptset |
AI.SCRIPTGET | scriptget |
AI.SCRIPTDEL | scriptdel |
AI.SCRIPTRUN | scriptrun |
AI._SCRIPTSCAN | N/A |
AI.DAGRUN | dagrun |
AI.DAGRUN_RO | dagrun_ro |
AI.INFO | info and infoResetStat (for resetting stats) |
AI.CONFIG * | configLoadBackend and configBackendsPath |
Running tests
A simple test suite is provided, and can be run with:
$ npm test
The tests expect a Redis server with the RedisAI module loaded to be available at localhost:6379
License
redisai-js is distributed under the BSD3 license - see LICENSE