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

Finetune #8

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
394 changes: 344 additions & 50 deletions README.md

Large diffs are not rendered by default.

9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
"version": "0.1.0",
"description": "TS SDK for 0G Compute Network",
"main": "./lib.commonjs/index.js",
"files": [
"types",
"lib.commonjs",
"lib.esm",
"wasm"
],
"files": ["types", "lib.commonjs", "lib.esm", "wasm"],
"exports": {
"require": "./lib.commonjs/index.js",
"import": "./lib.esm/index.mjs"
Expand All @@ -21,7 +16,7 @@
"format": "prettier --write src.ts/**/*.ts src.ts/*.ts example/**/*.ts",
"clean": "rm -rf dist lib.esm lib.commonjs types",
"build": "npm run clean && tsc -b tsconfig.commonjs.json tsconfig.types.json && npx rollup -c rollup.config.mjs",
"gen-contract-type": "typechain --target ethers-v6 --node16-modules --out-dir src.ts/contract/serving '../0g-serving-broker/api/libs/0g-serving-contract/artifacts/contracts/Serving.sol/Serving.json'",
"gen-contract-type": "typechain --target ethers-v6 --node16-modules --out-dir src.ts/contract/serving '../0g-serving-broker/api/libs/0g-serving-contract/artifacts/contracts/fine-tune/FineTuneServing.sol/FineTuneServing.json'",
"gen-doc": "npx typedoc --tsconfig tsconfig.esm.json",
"test": "mocha -r ts-node/register 'src.ts/**/*.test.ts'"
},
Expand Down
11 changes: 6 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 1 addition & 27 deletions src.ts/broker/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { genKeyPair } from '../settle-signer'
import { AddressLike } from 'ethers'
import { encryptData, privateKeyToStr } from '../utils'


/**
* AccountProcessor contains methods for creating, depositing funds, and retrieving 0G Serving Accounts.
*/
Expand Down Expand Up @@ -73,31 +74,4 @@ export class AccountProcessor extends ZGServingUserBrokerBase {
}
}

private async createSettleSignerKey(providerAddress: string): Promise<{
settleSignerPublicKey: [bigint, bigint]
settleSignerEncryptedPrivateKey: string
}> {
try {
// [pri, pub]
const keyPair = await genKeyPair()
const key = `${this.contract.getUserAddress()}_${providerAddress}`

this.metadata.storeSettleSignerPrivateKey(
key,
keyPair.packedPrivkey
)

const settleSignerEncryptedPrivateKey = await encryptData(
this.contract.signer,
privateKeyToStr(keyPair.packedPrivkey)
)

return {
settleSignerEncryptedPrivateKey,
settleSignerPublicKey: keyPair.doublePackedPubkey,
}
} catch (error) {
throw error
}
}
}
200 changes: 153 additions & 47 deletions src.ts/broker/base.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { ServingContract } from '../contract'
import {
InferenceServingContract,
FineTuneServingContract,
InferenceServiceStruct,
FineTuneServing,
FineTuneServiceStruct,
} from '../contract'
import { Cache, CacheValueTypeEnum, Metadata } from '../storage'
import { ChatBot, Extractor } from '../extractor'
import { ServiceStructOutput } from '../contract/serving/Serving'
import { ServingRequestHeaders } from './request'
import { decryptData, getNonce, strToPrivateKey } from '../utils'
import { PackedPrivkey, Request, signData } from '../settle-signer'
import { ChatBot, Extractor, ModelFineTune } from '../extractor'
import { ServingRequestHeaders } from './inference/request'
import { decryptData, encryptData, getNonce, privateKeyToStr, strToPrivateKey } from '../utils'
import { genKeyPair, PackedPrivkey, Request, signData } from '../settle-signer'

export abstract class ZGServingUserBrokerBase {
protected contract: ServingContract
protected contract: any
protected metadata: Metadata
protected cache: Cache

constructor(contract: ServingContract, metadata: Metadata, cache: Cache) {
constructor(contract: any, metadata: Metadata, cache: Cache) {
this.contract = contract
this.metadata = metadata
this.cache = cache
Expand All @@ -25,24 +30,93 @@ export abstract class ZGServingUserBrokerBase {
return { settleSignerPrivateKey }
}

protected a0giToNeuron(value: number): bigint {
const valueStr = value.toFixed(18)
const parts = valueStr.split('.')

// Handle integer part
const integerPart = parts[0]
let integerPartAsBigInt = BigInt(integerPart) * BigInt(10 ** 18)

// Handle fractional part if it exists
if (parts.length > 1) {
let fractionalPart = parts[1]
while (fractionalPart.length < 18) {
fractionalPart += '0'
}
if (fractionalPart.length > 18) {
fractionalPart = fractionalPart.slice(0, 18) // Truncate to avoid overflow
}

const fractionalPartAsBigInt = BigInt(fractionalPart)
integerPartAsBigInt += fractionalPartAsBigInt
}

return integerPartAsBigInt
}

protected neuronToA0gi(value: bigint): number {
const divisor = BigInt(10 ** 18)
const integerPart = value / divisor
const remainder = value % divisor
const decimalPart = Number(remainder) / Number(divisor)

return Number(integerPart) + decimalPart
}

protected async createSettleSignerKey(providerAddress: string): Promise<{
settleSignerPublicKey: [bigint, bigint]
settleSignerEncryptedPrivateKey: string
}> {
try {
// [pri, pub]
const keyPair = await genKeyPair()
const key = `${this.contract.getUserAddress()}_${providerAddress}`

await this.metadata.storeSettleSignerPrivateKey(
key,
keyPair.packedPrivkey
)

const settleSignerEncryptedPrivateKey = await encryptData(
this.contract.signer,
privateKeyToStr(keyPair.packedPrivkey)
)

return {
settleSignerEncryptedPrivateKey,
settleSignerPublicKey: keyPair.doublePackedPubkey,
}
} catch (error) {
throw error
}
}

}

export abstract class ZGInferenceServingUserBroker extends ZGServingUserBrokerBase {
constructor(contract: InferenceServingContract, metadata: Metadata, cache: Cache) {
super(contract, metadata, cache)
}

protected async getService(
providerAddress: string,
svcName: string,
useCache = true
): Promise<ServiceStructOutput> {
): Promise<InferenceServiceStruct> {
const key = providerAddress + svcName
const cachedSvc = await this.cache.getItem(key)
if (cachedSvc && useCache) {
return cachedSvc
}

try {
const svc = await this.contract.getService(providerAddress, svcName)
const svc: InferenceServiceStruct = await this.contract.getService(providerAddress, svcName)
await this.cache.setItem(
key,
svc,
1 * 60 * 1000,
CacheValueTypeEnum.Service
CacheValueTypeEnum.InferenceService
)
return svc
} catch (error) {
Expand All @@ -68,7 +142,7 @@ export abstract class ZGServingUserBrokerBase {
}
}

protected createExtractor(svc: ServiceStructOutput): Extractor {
protected createExtractor(svc: InferenceServiceStruct): Extractor {
switch (svc.serviceType) {
case 'chatbot':
return new ChatBot(svc)
Expand All @@ -77,40 +151,6 @@ export abstract class ZGServingUserBrokerBase {
}
}

protected a0giToNeuron(value: number): bigint {
const valueStr = value.toFixed(18)
const parts = valueStr.split('.')

// Handle integer part
const integerPart = parts[0]
let integerPartAsBigInt = BigInt(integerPart) * BigInt(10 ** 18)

// Handle fractional part if it exists
if (parts.length > 1) {
let fractionalPart = parts[1]
while (fractionalPart.length < 18) {
fractionalPart += '0'
}
if (fractionalPart.length > 18) {
fractionalPart = fractionalPart.slice(0, 18) // Truncate to avoid overflow
}

const fractionalPartAsBigInt = BigInt(fractionalPart)
integerPartAsBigInt += fractionalPartAsBigInt
}

return integerPartAsBigInt
}

protected neuronToA0gi(value: bigint): number {
const divisor = BigInt(10 ** 18)
const integerPart = value / divisor
const remainder = value % divisor
const decimalPart = Number(remainder) / Number(divisor)

return Number(integerPart) + decimalPart
}

async getHeader(
providerAddress: string,
svcName: string,
Expand Down Expand Up @@ -170,7 +210,73 @@ export abstract class ZGServingUserBrokerBase {
private async calculateInputFees(extractor: Extractor, content: string) {
const svc = await extractor.getSvcInfo()
const inputCount = await extractor.getInputCount(content)
const inputFee = BigInt(inputCount) * svc.inputPrice
const inputFee = BigInt(inputCount) * svc?.inputPrice
return inputFee
}
}

export abstract class ZGFineTuneServingUserBroker extends ZGServingUserBrokerBase {
constructor(contract: FineTuneServingContract, metadata?: Metadata, cache?: Cache) {
super(contract, metadata, cache)
}

protected async getService(
providerAddress: string,
svcName: string,
useCache = true
): Promise<FineTuneServiceStruct> {
const key = providerAddress + svcName
const cachedSvc = await this.cache.getItem(key)
if (cachedSvc && useCache) {
return cachedSvc
}

try {
const svc: FineTuneServiceStruct = await this.contract.getService(providerAddress, svcName)
await this.cache.setItem(
key,
svc,
1 * 60 * 1000,
CacheValueTypeEnum.FineTuneService
)
return svc
} catch (error) {
throw error
}
}

protected async getExtractor(
providerAddress: string,
svcName: string,
useCache = true
): Promise<Extractor> {
try {
const svc = await this.getService(
providerAddress,
svcName,
useCache
)
const extractor = this.createExtractor(svc)
return extractor
} catch (error) {
throw error
}
}

protected createExtractor(svc: FineTuneServiceStruct): Extractor {
// todo: extract from finetune service?
switch (svc.name) {
case 'finetune':
return new ModelFineTune(svc)
default:
throw new Error('Unknown service type')
}
}

// private async calculateFee(extractor: Extractor, dataset_hash: string) {
// const svc = await extractor.getSvcInfo()
// const inputCount = await extractor.getInputCount(content)
// const inputFee = BigInt(inputCount) * svc.inputPrice
// return inputFee
// }
}
Loading