From 8233ac7f9fb6f81dff764d90eb787fe3db0bb6ae Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Tue, 17 Dec 2024 15:56:25 +0900 Subject: [PATCH 1/4] fix: lib --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index dc9b10c..e13d1a9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,7 +15,7 @@ "sourceMap": true, "strict": true, "strictNullChecks": true, - "target": "es2016", + "target": "ES2021", }, "include": [ "src/**/*" From b7745fa3616fc38c76791628b55c3266d7b538d0 Mon Sep 17 00:00:00 2001 From: Yoojin Ko Date: Tue, 17 Dec 2024 15:56:32 +0900 Subject: [PATCH 2/4] feat: add name parser --- src/ainize.ts | 19 +++++++++++-------- src/utils/appName.ts | 3 +++ 2 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 src/utils/appName.ts diff --git a/src/ainize.ts b/src/ainize.ts index 0fc0c5e..0dd631e 100644 --- a/src/ainize.ts +++ b/src/ainize.ts @@ -10,6 +10,7 @@ import Internal from "./internal"; import { Account } from "@ainblockchain/ain-util"; import { AinWalletSigner } from "@ainblockchain/ain-js/lib/signer/ain-wallet-signer"; import { ConnectionCallback, DisconnectionCallback } from "@ainblockchain/ain-js/lib/types"; +import { nameParser } from "./utils/appName"; export default class Ainize { private cache: NodeCache; @@ -86,7 +87,8 @@ export default class Ainize { // TODO(yoojin, woojae): Deploy container, advanced. async deploy({modelName, billingConfig, modelUrl}: deployConfig): Promise { // TODO(yoojin, woojae): Add container deploy logic. - const result = await new Promise(async (resolve, reject) => { + const pasredName = nameParser(modelName); + await new Promise(async (resolve, reject) => { const deployer = await this.ain.getAddress(); if (!billingConfig) { billingConfig = { @@ -96,15 +98,15 @@ export default class Ainize { } // NOTE(yoojin): For test. We make fixed url on model. if (!modelUrl) { - modelUrl = `https://${modelName}.ainetwork.xyz`; + modelUrl = `https://${pasredName}.ainetwork.xyz`; } modelUrl = modelUrl.replace(/\/$/, ''); - const modelPath = Path.app(modelName).status(); + const modelPath = Path.app(pasredName).status(); await this.handler.subscribe(modelPath, resolve); - await this.appController.createApp({ appName: modelName, modelUrl, billingConfig }); + await this.appController.createApp({ appName: pasredName, modelUrl, billingConfig }); }); - console.log(`${modelName} deploy success!`); - return this.getModel(modelName); + console.log(`${pasredName} deploy success!`); + return this.getModel(pasredName); } /** @@ -113,12 +115,13 @@ export default class Ainize { * @returns {Model} Deployed model object. */ async getModel(modelName: string): Promise { - const modelPath = Path.app(modelName).root(); + const parsedName = nameParser(modelName); + const modelPath = Path.app(parsedName).root(); const modelData = await this.ain.getValue(modelPath, { is_shallow: true }); if(!modelData) { throw new Error("Model not found"); } - return new Model(modelName); + return new Model(parsedName); } test() { diff --git a/src/utils/appName.ts b/src/utils/appName.ts new file mode 100644 index 0000000..69e5c2e --- /dev/null +++ b/src/utils/appName.ts @@ -0,0 +1,3 @@ +export const nameParser = (name: string) => { + return name.replaceAll(/[./-]/g, "_").toLowerCase(); +} \ No newline at end of file From 94dcfaeabb82f9d1ef0846a721b364328169bf36 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Tue, 17 Dec 2024 16:39:43 +0900 Subject: [PATCH 3/4] refactor: readme --- README.md | 95 +++++++++++++++++++++++++++++++++++++++++---- examples/charge.ts | 2 +- examples/deploy.ts | 2 +- examples/request.ts | 2 +- 4 files changed, 91 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f5fb5a6..55f3852 100644 --- a/README.md +++ b/README.md @@ -34,12 +34,24 @@ const ainize = new Ainize(); You should login to ainize with AI Network account before deploy the container. ```typescript +import { Ainize } from '@ainize-team/ainize-js'; +const ainize = new Ainize(1);// 0 for testnet, 1 for mainnet. You can earn testnet AIN at https://faucet.ainetwork.ai/. ainize.login(); ``` +If you don't have an AI Network account, you can create one with the following script. + +```typescript +import { Ainize } from '@ainize-team/ainize-js'; +const wallet = Ainize.createAinAccount(); +console.log(wallet); +``` + You can also login using [AIN Wallet](https://chromewebstore.google.com/detail/ain-wallet/hbdheoebpgogdkagfojahleegjfkhkpl) on the web. ```typescript +import { Ainize } from '@ainize-team/ainize-js'; +const ainize = new Ainize(1); ainize.loginWithSigner(); ``` @@ -48,23 +60,74 @@ This feature is supported from AIN Wallet version 2.0.5 or later. ### Using model You can use a model using `ainize.getModel()`. -For example, you can use the `ainize_free_inference` model, which runs Meta's [Llama 3.1 8B instruct](https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct) model. +For example, you can use the `meta-llama/Llama-3.1-8B-instruct` model, which runs Meta's [Llama-3.1-8B-instruct](https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct) model. ```typescript -const model = await ainize.getModel(); +import { Ainize } from '@ainize-team/ainize-js'; +const ainPrivateKey = ''; // Insert your private key here +const main = async () => { + try { + const ainize = new Ainize(1); + await ainize.login(ainPrivateKey); + const model = await ainize.getModel('meta-llama/Llama-3.1-8B-instruct'); + console.log(model.modelName); + ainize.logout(); + }catch(e) { + console.log(e); + } +} +main(); + ``` -You should deposit AIN to AI model for credit before using model. +You should deposit AIN to AI model for credit before using model. If you are using a free model, you can skip this step. If you don't have AIN, you can swap at uniswap or you can buy from CEX. ```typescript -await model.chargeCredit(); -const balance = await model.getCreditBalance(); +import { Ainize } from '@ainize-team/ainize-js'; +const ainPrivateKey = ''; // Insert your private key here +const main = async () => { + try { + const ainize = new Ainize(1); + await ainize.login(ainPrivateKey); + console.log('Your ain: ',await ainize.getAinBalance()); + const model = await ainize.getModel('meta-llama/Llama-3.1-8B-instruct'); + console.log("before charge: ",await model.getCreditBalance()); + await model.chargeCredit(10); + console.log("after charge: ",await model.getCreditBalance()); + ainize.logout(); + }catch(e) { + console.log(e); + } +} +main(); + ``` If you have enough credit, you can use the model. ```typescript -const result = await model.request(); +import { Ainize } from '@ainize-team/ainize-js'; +const ainPrivateKey = ''; // Insert your private key here + +const main = async () => { + try { + const ainize = new Ainize(1); + await ainize.login(ainPrivateKey); + const inferenceModel = await ainize.getModel('meta-llama/Llama-3.1-8B-instruct'); + const request = { + "prompt": "hi" + }; + const cost = await inferenceModel.calculateCost(request.prompt); + console.log(cost); + const response = await inferenceModel.request(request); + console.log(response); + ainize.logout(); + }catch(e) { + console.log(e); + } +} +main(); + ``` ### Deploy @@ -81,7 +144,25 @@ CONFIGURATION(JSON) - maxCost: Maximum cost. (optional) ```typescript -const model = await ainize.deploy(); +import { Ainize } from '@ainize-team/ainize-js'; +const ainPrivateKey = ''; // Insert your private key here + +const main = async () => { + try { + const ainize = new Ainize(1); + await ainize.login(ainPrivateKey); + const deployConfig = { + modelName: 'YOUR_MODEL_NAME',// e.g. meta-llama/Llama-3.1-8B-instruct + modelUrl: 'YOUR_MODEL_INFERENCE_URL' // e.g. https://ainize-free-inference.ainetwork.xyz + } + const model = await ainize.deploy(deployConfig); + console.log(model.modelName); + ainize.logout(); + }catch(e) { + console.log(e); + } +} +main(); ``` You can stop or run your model container. Only model deployer can use this. diff --git a/examples/charge.ts b/examples/charge.ts index 8b183e7..7bff876 100644 --- a/examples/charge.ts +++ b/examples/charge.ts @@ -2,7 +2,7 @@ import { Ainize } from '@ainize-team/ainize-js'; const ainPrivateKey = ''; // Insert your private key here const main = async () => { try { - const ainize = new Ainize(0); // 0 for testnet, 1 for mainnet. You can earn testnet AIN at https://faucet.ainetwork.ai/. + const ainize = new Ainize(1); // 0 for testnet, 1 for mainnet. You can earn testnet AIN at https://faucet.ainetwork.ai/. await ainize.login(ainPrivateKey); console.log('balance: ',await ainize.getAinBalance()); const model = await ainize.getModel('ainize_free_inference'); diff --git a/examples/deploy.ts b/examples/deploy.ts index 425fb4b..7e97092 100644 --- a/examples/deploy.ts +++ b/examples/deploy.ts @@ -3,7 +3,7 @@ const ainPrivateKey = ''; // Insert your private key here const main = async () => { try { - const ainize = new Ainize(0); // 0 for testnet, 1 for mainnet. You can earn testnet AIN at https://faucet.ainetwork.ai/. + const ainize = new Ainize(1); // 0 for testnet, 1 for mainnet. You can earn testnet AIN at https://faucet.ainetwork.ai/. await ainize.login(ainPrivateKey); console.log('balance: ',await ainize.getAinBalance()); const deployConfig = { diff --git a/examples/request.ts b/examples/request.ts index 7151cb9..f5cc401 100644 --- a/examples/request.ts +++ b/examples/request.ts @@ -3,7 +3,7 @@ const ainPrivateKey = ''; // Insert your private key here const main = async () => { try { - const ainize = new Ainize(0); // 0 for testnet, 1 for mainnet. You can earn testnet AIN at https://faucet.ainetwork.ai/. + const ainize = new Ainize(1); // 0 for testnet, 1 for mainnet. You can earn testnet AIN at https://faucet.ainetwork.ai/. await ainize.login(ainPrivateKey); console.log('balance: ',await ainize.getAinBalance()); const inferenceModel = await ainize.getModel('ainize_free_inference'); From da56620497d572e43e49d6f96884b30a5b382642 Mon Sep 17 00:00:00 2001 From: yoojinko Date: Tue, 17 Dec 2024 08:23:51 +0000 Subject: [PATCH 4/4] Upgrade version to 1.3.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 258608e..875a922 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ainize-team/ainize-js", - "version": "1.3.2", + "version": "1.3.3", "main": "dist/ainize.js", "types": "dist/ainize.d.ts", "scripts": {