-
Notifications
You must be signed in to change notification settings - Fork 3
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: sort out inference code after refactor for finetune #9
Draft
sidosidonie
wants to merge
7
commits into
0glabs:main
Choose a base branch
from
sidosidonie:inf-sido
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2021be3
Feat: sort out inference code after refactor for finetune
d8400f7
Fix calculate fee
cc56e62
Fix: update inference transfer fee
fe63f42
Update request.ts
Ravenyjh bef2da1
Fix: fix inference broker update cache
4497071
Fix: fix retrive
59e7d31
Fix: remove inference.ts and getService
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import {ethers} from 'ethers' | ||
import {createZGComputeNetworkBroker} from './src.ts' | ||
import OpenAI from 'openai' | ||
|
||
async function main() { | ||
const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545') | ||
|
||
// Step 1: Create a wallet with a private key | ||
const privateKey = | ||
'59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d' | ||
const wallet = new ethers.Wallet(privateKey, provider) | ||
|
||
// Step 2: Initialize the broker | ||
try { | ||
const broker = await createZGComputeNetworkBroker( | ||
wallet, | ||
'', | ||
'0x0165878A594ca255338adfa4d48449f69242Eb8F', | ||
'' | ||
) | ||
|
||
// List available services | ||
console.log('Listing available services...') | ||
const services = await broker.inference.listService() | ||
services.forEach((service: any) => { | ||
console.log( | ||
`Service: ${service.name}, Provider: ${service.provider}, Type: ${service.serviceType}, Model: ${service.model}, URL: ${service.url}, verifiability: ${service.verifiability}` | ||
) | ||
}) | ||
|
||
// Select a service | ||
const service = await broker.inference.getService("test") | ||
|
||
const providerAddress = service.provider; | ||
|
||
// create ledger | ||
await broker.ledger.addLedger(0.1) | ||
console.log('Creating ledger successfully.') | ||
|
||
// deposit fund | ||
const depositAmount = 0.01 | ||
await broker.ledger.depositFund(depositAmount) | ||
console.log('Depositing funds...') | ||
|
||
// get ledger | ||
const ledger = await broker.ledger.getLedger() | ||
console.log('Get ledger with fund ', ledger.totalBalance) | ||
|
||
// Step 5: Use the Provider's Services | ||
console.log('Processing a request...') | ||
const serviceName = service.name | ||
const content = 'hello world' | ||
|
||
await broker.inference.settleFee( | ||
providerAddress, | ||
serviceName, | ||
0.0000000008 | ||
) | ||
|
||
// Step 5.1: Get the request metadata | ||
const {endpoint, model} = | ||
await broker.inference.getServiceMetadata( | ||
providerAddress, | ||
serviceName | ||
) | ||
|
||
// Get the request headers | ||
const headers = await broker.inference.getRequestHeaders( | ||
providerAddress, | ||
serviceName, | ||
content | ||
) | ||
|
||
// Send a request to the service | ||
const openai = new OpenAI({ | ||
baseURL: endpoint, | ||
apiKey: '', | ||
}) | ||
const completion = await openai.chat.completions.create( | ||
{ | ||
messages: [{role: 'system', content}], | ||
model: model, | ||
// @ts-expect-error guided_json is not yet public | ||
guided_json: jsonSchema, | ||
}, | ||
{ | ||
headers: { | ||
...headers, | ||
}, | ||
} | ||
) | ||
|
||
const receivedContent = completion.choices[0].message.content | ||
const chatID = completion.id | ||
if (!receivedContent) { | ||
throw new Error('No content received.') | ||
} | ||
console.log('Response:', receivedContent) | ||
|
||
// Step 7: Process the response | ||
console.log('Processing a response...') | ||
const isValid = await broker.inference.processResponse( | ||
providerAddress, | ||
serviceName, | ||
receivedContent, | ||
chatID | ||
) | ||
console.log(`Response validity: ${isValid ? 'Valid' : 'Invalid'}`) | ||
|
||
// close service | ||
await broker.inference.closeService(providerAddress) | ||
console.log("Closing the service...") | ||
} catch (error) { | ||
console.error('Error during execution:', error) | ||
} | ||
} | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After local testing, integrate the content into src.ts/example/inference.md. There's no need to add another file.