Skip to content

Commit

Permalink
Merge pull request #58 from ainize-team/feat/woojae/typedoc
Browse files Browse the repository at this point in the history
feat: model typedoc
  • Loading branch information
akastercomcom authored Sep 22, 2023
2 parents 8476ab1 + 30aaafa commit 5726a5d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# typedoc
docs/
47 changes: 21 additions & 26 deletions src/controllers/modelController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Path } from "../constants";
import { getRequestDepositOp, getTransferOp } from "../utils/operator";
import { buildSetOperation, buildTxBody } from "../utils/builder";
import Handler from "../handlers/handler";
import { ContainerStatus } from "../types/type";
import { ContainerStatus, creditHistories } from "../types/type";

export default class ModelController {
private static instance: ModelController | undefined;
Expand All @@ -17,19 +17,19 @@ export default class ModelController {
return ModelController.instance;
}

async isRunning(modelName: string) {
async isRunning(modelName: string): Promise<void> {
const isRunning = await this.ain.getValue(Path.app(modelName).status());
if(isRunning !== ContainerStatus.RUNNING) {
throw new Error('Model is not running');
}
}

//TODO(woojae): implement this
async getInformation(modelName: string) {
async getInformation(modelName: string): Promise<any> {
return await 'information of model';
}

async calculateCost(modelName: string, requestData: string) {
async calculateCost(modelName: string, requestData: string): Promise<number> {
const billingConfig = await this.ain.getValue(Path.app(modelName).billingConfig());
const token = requestData.split(' ').length;
let cost = token * billingConfig.costPerToken;
Expand All @@ -41,7 +41,7 @@ export default class ModelController {
return cost;
}

async chargeCredit(modelName: string, amount: number) {
async chargeCredit(modelName: string, amount: number): Promise<string> {
this.isLoggedIn();
this.isRunning(modelName);
const transferKey = Date.now();
Expand All @@ -55,28 +55,26 @@ export default class ModelController {
return this.ain.sendTransaction(txBody);
}


//TODO(woojae): implement this
async withdrawCredit(modelName: string, amount: number) {
return await true;
}

async getCreditBalance(modelName: string) {
async getCreditBalance(modelName: string): Promise<number> {
this.isLoggedIn();
const userAddress = this.ain.getAddress();
const balancePath = Path.app(modelName).balanceOfUser(userAddress);
return await this.ain.getValue(balancePath) | 0;
return await this.ain.getValue(balancePath) as number | 0;
}

async getCreditHistory(modelName: string) {
async getCreditHistory(modelName: string): Promise<creditHistories> {
this.isLoggedIn();
const userAddress = this.ain.getAddress();
const creditHistoryPath = Path.app(modelName).historyOfUser(userAddress);
return await this.ain.getValue(creditHistoryPath);
return await this.ain.getValue(creditHistoryPath) as creditHistories;
}

//TODO(woojae): connect with handler
async use(modelName: string, requestData: string) {
async use(modelName: string, requestData: string) : Promise<string> {
this.isLoggedIn();
this.isRunning(modelName);
const result = await new Promise(async (resolve, reject) => {
Expand All @@ -90,32 +88,29 @@ export default class ModelController {
await this.ain.sendTransaction(txBody);
return requestKey;
});
return result;
return result as string;
}

//TODO(woojae): implement this
//NOTE(woojae): need admin
async run(modelName: string) {
return await true;
//TODO(woojae): implement this.
async run(modelName: string): Promise<void> {
await true;
}

//TODO(woojae): implement this
//NOTE:(woojae): need admin
async stop(modelName: string) {
return await true;
//TODO(woojae): implement this.
async stop(modelName: string): Promise<void> {
await true;
}

//TODO:(woojae): implement this
//NOTE:(woojae): need admin
async changeModelInfo(modelName: string, config: any) {
return await true;
async changeModelInfo(modelName: string, config: any): Promise<void> {
await true;
}

private async getDepositAddress(appName: string) {
private async getDepositAddress(appName: string): Promise<string> {
return (await this.ain.getValue(Path.app(appName).billingConfig())).depositAddress;
}

private isLoggedIn() {
private isLoggedIn(): boolean {
if(!this.ain.getDefaultAccount())
throw new Error('You should login First.');
return true;
Expand Down
53 changes: 47 additions & 6 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ModelController from "./controllers/modelController";
import { creditHistories } from "./types/type";

export default class Model {
modelName: string;
Expand All @@ -8,54 +9,94 @@ export default class Model {
this.modelName = modelName;
this.modelController = ModelController.getInstance();
}
//TODO(woojae): login not Required

/**
* Check if model is running. It throws error when model is not running.
*/
async isRunning() {
return await this.modelController.isRunning(this.modelName);
}

//TODO(woojae): login not Required
/**
* Get model information. not implemented yet.
* @returns {string} Model information.
*/
async getInformation() {
return await this.modelController.getInformation(this.modelName);
}

//TODO(woojae): login not Required
/**
* Calculate estimated cost for given request data.
* @param {string} rerquestData string data for request to model.
* @returns {number} Estimated cost.
*/
async calculateCost (requestData: string) {
return await this.modelController.calculateCost(this.modelName, requestData);
}

/**
* Charge credit to model.
* @param {number} amount Amount of credit to charge.
* @returns {string} Transaction hash.
*/
async chargeCredit(amount: number) {
return await this.modelController.chargeCredit(this.modelName, amount);
}

/**
* Withdraw credit from model.
* @param {number} amount Amount of credit to withdraw.
* @returns {string} Transaction hash.
*/
async withdrawCredit(amount: number) {
return await this.modelController.withdrawCredit(this.modelName, amount);
}

/**
* Get credit balance of model.
* @returns {number} Amount of credit balance.
*/
async getCreditBalance() {
return await this.modelController.getCreditBalance(this.modelName);
}

/**
* Get credit history of model.
* @returns {creditHistories} Histories of credit deposit and usage.
*/
async getCreditHistory() {
return await this.modelController.getCreditHistory(this.modelName);
}

/**
* Use model with given request data.
* @param {string} requestData String data for request to model.
* @returns {string} Response data from model.
*/
async use(requestData: string) {
return await this.modelController.use(this.modelName, requestData);
}

//NOTE(woojae): need admin
/**
* Change status of AI model container to Running. Need to be admin. Not implemented yet.
*/
async run() {
await this.isAdmin();
return await this.modelController.run(this.modelName);
}

//NOTE(woojae): need admin
/**
* Change status of AI model container to Stopped. Need to be admin. Not implemented yet.
*/
async stop() {
await this.isAdmin();
return await this.modelController.stop(this.modelName);
}

//NOTE(woojae): need admin
/**
* Change model configuration. Need to be admin. Not implemented yet.
* @param {any} config Configuration to change. Not implemented yet.
*/
async changeModelInfo(config: any) {
await this.isAdmin();
return await this.modelController.changeModelInfo(this.modelName, config);
Expand Down
11 changes: 11 additions & 0 deletions src/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export enum HISTORY_TYPE {
USAGE = "USAGE",
}

export type creditHistories = {
[timestamp: string]: creditHistory;
}

export type creditHistory = {
type: HISTORY_TYPE;
amount: number;
transferKey?: string;
requestTimestamp?: string;
}

export type opResult = {
code: number;
bandwidth_gas_amount: number;
Expand Down

0 comments on commit 5726a5d

Please sign in to comment.