Eternal AI
  • The AI layer for the new internet
  • eternals
    • What are Eternals?
    • Specification
    • Proof-of-Compute
  • The new internet, AI-powered
    • Bitcoin, AI-powered
      • Eternals on Bitcoin
      • BitAI Virtual Machine
      • Run a BitAI node
    • Ethereum, AI-powered
    • Solana, AI-powered
  • smart contracts, ai-powered
    • How to use onchain LLM
    • Onchain AI composability - AI Powered Wallet
    • Onchain AI Composability - AI Powered Gaming With Chess
  • neurons
    • What are Neurons?
    • Neuron Device
    • Virtual Neurons
      • Solo Neuron
      • Neuron as a Service
      • Pooled Neuron
  • AI CHAINS
    • What are AI chains?
    • Bittensor and existing concepts
    • Base layer: Bitcoin vs Bittensor
    • AI chains: Bitcoin L2s vs Subnets
    • Apps: Smart contracts vs APIs
  • EAI
    • Utilities
    • Tokenomics
  • fully onchain ai models
    • Architecture
    • Deploy your first fully onchain AI
      • Set up your development environment
      • Create a self-custody wallet
      • Train an AI model in Keras
      • Transform the Keras model to Eternal
      • Send, receive, and trade Eternals
    • Progress
    • Misc
      • Transforming an AI Model into an Eternal
      • Standardized data formats
      • Specification
        • Layers
        • Models
  • Decentralized Inference API
    • API
      • API Key
      • Completions
      • Chat completion
      • Create a dagent
      • Get deposit address
      • Get dagent info
      • Agent Completion
    • Onchain Models
    • Tutorials
      • Build unstoppable Eliza agents
      • Build unstoppable Rig agents
      • Build unstoppable ZerePy agents
      • Decentralized ChatGPT
      • Don't Trust, Verify
      • Adjust your dagent personality
      • Launch on Twitter
      • Chain of thought
      • Build a dagent as a service with EternalAI API
    • Open Source
      • Architecture
      • Installation
Powered by GitBook
On this page
  • Step 0
  • Step 1
  • Step 2
  • Step 3
  1. smart contracts, ai-powered

How to use onchain LLM

PreviousSolana, AI-poweredNextOnchain AI composability - AI Powered Wallet

Last updated 4 months ago

In this code example, we’ll demonstrate how to interact with a LLM model deployed on multiple blockchains, in a few easy steps using TypeScript.

Step 0

Obtain chain (id) and model (id) from . (you will need these two information to run the following code in Step 3 below).

Step 1

Call the infer function of the HybridModel contract with an inference prompt.

const inf = getModelInfo(chosenNetwork, chosenModel);
const modelInstance = (await getContractInstance(
    inf.modelInfo.modelAddress,
    "IHybridModel"
)) as unknown as IHybridModel;

const request = buildRequest(inf.modelInfo.modelName, userPrompt || 'Hello, how are you?');
const txRequest = await modelInstance
    .connect(sender)
    ["infer(bytes)"](ethers.toUtf8Bytes(JSON.stringify(request)));
const receipt = await txRequest.wait();
console.log("Tx status: ", receipt?.status == 1 ? "Success" : "Failed");

Step 2

Periodically check with the PromptScheduler contract to retrieve the response returned by the LLM model’s miners using the inferId.

inferId = getInferId(receipt, inf.promptSchedulerAddress)[0];
while (true) {
    await sleep(30000);
    try {
        inferResult = await tryToGetInferenceResult(
            chosenNetwork,
            inf.promptSchedulerAddress,
            inferId,
            sender
        );
        break;
     } catch (e: any) {     
        console.log(e.message);
        continue;
     }
}
console.log("Inference result: ", inferResult);

Step 3

You can run the code with the following command:

npx hardhat compile && RPC_URL=https://base-mainnet.infura.io/v3/eb492201628143a094aa7afaeb9f32d2  PRIVATE_KEY=<YOUR_KEY>  CHOSEN_MODEL="unsloth/Llama-3.3-70B-Instruct-bnb-4bit"  USER_PROMPT="Hello, how are you?"  npm run sendUniverseAgentRequest:base_mainnet

Note: replace <YOUR_KEY> with your actual wallet private key and the wallet should have some ETH on Base network for paying network fee.

Examples of the prompt request and the miner’s response transaction hashes can be found here:

  • Prompt tx: https://basescan.org/tx/0x641c26eff85f9486dace2d4ac0558b3c8da576b9e3e79773bb94a817fb8db45c

  • Response tx: https://basescan.org/tx/0x133b94f00d31908c4b88b8d1b2602a1241bac7a7ee744c04a248ea3e6f47fc75

Complete example code can be found at:

Onchain Models page
https://github.com/eternalai-org/eternal-ai/blob/master/examples/UniverseDagents/scripts/sendUniverseAgentRequest.ts