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

feat: model typedoc #58

Merged
merged 7 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
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/
38 changes: 19 additions & 19 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<string> {
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 @@ -61,22 +61,22 @@ export default class ModelController {
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 +90,32 @@ 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;
async run(modelName: string): Promise<void> {
await true;
}

//TODO(woojae): implement this
//NOTE:(woojae): need admin
async stop(modelName: string) {
return await true;
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