RedisAI / redisai-js

A high-performance JavaScript client for RedisAI
https://redisai.io
BSD 3-Clause "New" or "Revised" License
13 stars 4 forks source link

Gettin a basic example going in TypeScript #22

Open Distortedlogic opened 3 years ago

Distortedlogic commented 3 years ago

I have been using redisai-py for a while. Now I am starting to integrate use of redisai into my website with redisai-js.

I am using typescript and ts-node in development. Below is the code I added following the example and the error I received.

import redis from "redis";
import redisai from "redisai-js";

(async () => {
    const nativeClient = redis.createClient({ url: "redis://redis:6379" });
    const rai = new redisai.Client(nativeClient );
})();
Error: Cannot find module './backend'
Require stack:
- /app/node_modules/redisai-js/lib/index.js
- /app/src/index.ts
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/app/node_modules/redisai-js/src/index.ts:2:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.require.extensions.<computed> [as .js] (/app/node_modules/ts-node/src/index.ts:851:44)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
[nodemon] app crashed - waiting for file changes before starting...
filipecosta90 commented 3 years ago

Hi there @Distortedlogic please notice you need to import either specific parts or all from redisai-js given there is no default import. Regarding a sample code, hope the following one helps ( included tensorset and tensorget given they dont need any extra model/script ):

import redis = require('redis');
import redisai = require('redisai-js');

console.log('Sample https://github.com/RedisAI/redisai-js/issues/22!');

(async () => {
  const nativeClient: redis.RedisClient = redis.createClient(6379, 'localhost');
  const aiclient = new redisai.Client(nativeClient);
  const tensorA = new redisai.Tensor(redisai.Dtype.float32, [1, 2], [3, 5]);
  const result = await aiclient.tensorset('tensorA', tensorA);

  // AI.TENSORSET result: OK
  console.log(`AI.TENSORSET result: ${result}`);

  const tensorGetReply = await aiclient.tensorget('tensorA');

  // AI.TENSORGET reply: datatype FLOAT shape [1,2] , data [3,5]
  console.log(
    `AI.TENSORGET reply: datatype ${tensorGetReply.dtype} shape [${tensorGetReply.shape}] , data [${tensorGetReply.data}]`
  );
  await aiclient.end();
})();

Sample output ( transpiling first ):

$ npm run compile

> redisai-js-sample-22@0.0.1 compile /tmp/sample-redisai-js-22
> tsc

$ node build/src/index.js
Sample https://github.com/RedisAI/redisai-js/issues/22!
AI.TENSORSET result: OK
AI.TENSORGET reply: datatype FLOAT shape [1,2] , data [3,5]

PS, this client tests are packed with several examples of tensorset,tensorget, modelrun, scriptrun, etc... - they can be of help for more complex examples also. https://github.com/RedisAI/redisai-js/blob/master/tests/test_client.ts

Can you please check if the above sample runs accordingly after transpiling. Will gladly share a sample GH repo if required with all structure if required. Kind regards,

Distortedlogic commented 3 years ago

I tried

import redisai = require('redisai-js');
import {Client, Backend} from 'redisai-js';

I still get the same error

Error: Cannot find module './backend'

here is the command I run to start my app

nodemon --exec ts-node src/index.ts

Must it be transpiled to JS, n not compatible with ts-node? just throwin a thought out there

CICCIOSGAMINO commented 2 years ago

I think you're using the version 4 of redis module? I've experienced the same issue with the version 4 of redis module, so i've solved with the ^3.1.2 as project's dependency.

JeremySazMining commented 2 years ago

thanks for the idea but I just quit tryina use it.. Redis is slacking way too much with its AI stuff plus the company's become taken over with political agendas .. I even went as far as making PRs, talked with a team member to make sure it looked good, and just never got merged