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

Fixed readme to develop #155

Merged
merged 7 commits into from
Dec 18, 2024
Merged
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
111 changes: 39 additions & 72 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,65 @@
# ainize-js

A Typescript JS for the Ainize, a system for running AI models on the AI Network.
A JavaScript library for the Ainize, a system for running AI models on the AI Network.

## Requirements

node >= 16
node >= 18

## usage

### Install
## Installation

```bash
// from NPM
npm install @ainize-team/ainize-js

yarn install @ainize-team/ainize-js
// from Yarn
yarn add @ainize-team/ainize-js
```

### Import

CHAIN_ID

- 0: AI Network test net
- 1: AI Network main net

Then import the libraries in your code:
```typescript
// ES6
import Ainize from '@ainize-team/ainize-js';

import Ainize from '@ainize-team/ainize-js'
const ainize = new Ainize(<CHAIN_ID>);
// CommonJS
const Ainize = require('@ainize-team/ainize-js').default;
```

### Login

You should login to ainize with AI Network account before deploy the container.
## Usage

```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>);
```
### Create account

You should login to ainize with AI Network account before deploy the container.\
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';
import Ainize from '@ainize-team/ainize-js';
const wallet = Ainize.createAinAccount();
console.log(wallet);
// {
// address: '0x44f2...985B',
// private_key: '14ba...4e67',
// public_key: '5fec...7784'
// }
```

### Login
```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>);
```

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';
import Ainize from '@ainize-team/ainize-js';
const ainize = new Ainize(1);
ainize.loginWithSigner();
```


This feature is supported from AIN Wallet version 2.0.5 or later.

### Using model
Expand All @@ -63,50 +68,7 @@ You can use a model using `ainize.getModel(<MODEL_NAME>)`.
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
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. 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
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
import { Ainize } from '@ainize-team/ainize-js';
import Ainize from '@ainize-team/ainize-js';
const ainPrivateKey = ''; // Insert your private key here

const main = async () => {
Expand All @@ -115,21 +77,25 @@ const main = async () => {
await ainize.login(ainPrivateKey);
const inferenceModel = await ainize.getModel('meta-llama/Llama-3.1-8B-instruct');
const request = {
"prompt": "hi"
"prompt": "Hi! How’s it going?"
};
const cost = await inferenceModel.calculateCost(request.prompt);
console.log(cost);
const response = await inferenceModel.request(request);
console.log(response);
ainize.logout();
}catch(e) {
} catch(e) {
console.log(e);
}
}
main();

```

### Currently supported models
| Model | MODEL_NAME | Insight Link |
| -------- | ------- | ------- |
| LLaMA 3.1 8B | meta-llama/Llama-3.1-8B-instruct | [Link](https://insight.ainetwork.ai/database/values/apps/meta_llama_llama_3_1_8b_instruct/) |

<!--
### Deploy

You can deploy your AI model to ainize. Anyone can use your AI model with AIN token.
Expand Down Expand Up @@ -171,3 +137,4 @@ You can stop or run your model container. Only model deployer can use this.
model.stop();
model.run();
```
-->
Loading