Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade version to 1.3.3 #143

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 88 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,24 @@ const ainize = new Ainize(<CHAIN_ID>);
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(<YOUR_PRIVATE_KEY>);
```

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();
```

Expand All @@ -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(<MODEL_NAME>)`.
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(<MODEL_NAME>);
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(<AMOUNT>);
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(<REQUEST_DATA>);
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
Expand All @@ -81,7 +144,25 @@ CONFIGURATION(JSON)
- maxCost: Maximum cost. (optional)

```typescript
const model = await ainize.deploy(<CONFIGURATION>);
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.
Expand Down
2 changes: 1 addition & 1 deletion examples/charge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion examples/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion examples/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
19 changes: 11 additions & 8 deletions src/ainize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,7 +87,8 @@ export default class Ainize {
// TODO(yoojin, woojae): Deploy container, advanced.
async deploy({modelName, billingConfig, modelUrl}: deployConfig): Promise<Model> {
// 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 = {
Expand All @@ -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);
}

/**
Expand All @@ -113,12 +115,13 @@ export default class Ainize {
* @returns {Model} Deployed model object.
*/
async getModel(modelName: string): Promise<Model> {
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() {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/appName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const nameParser = (name: string) => {
return name.replaceAll(/[./-]/g, "_").toLowerCase();
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"sourceMap": true,
"strict": true,
"strictNullChecks": true,
"target": "es2016",
"target": "ES2021",
},
"include": [
"src/**/*"
Expand Down
Loading