From 94dcfaeabb82f9d1ef0846a721b364328169bf36 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Tue, 17 Dec 2024 16:39:43 +0900 Subject: [PATCH] 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');