Appearance
Basic Chat CLI
This tutorial will show you how to write a simple chatbot application with an embedded Spark wallet that allows users to interact with a LLM and pay for requests.
Installation
Let's start by installing the Spark SDK and Inference Grid SDK.
bash
npm install @buildonspark/spark-sdk@0.1.16 inference-grid-sdk
Setup
We'll build a simple CLI that that uses the Inference Grid to call an LLM and then pays the invoice using a Spark wallet.
typescript
import readline from "readline";
import { SparkWallet } from "@buildonspark/spark-sdk";
import { InferenceGrid, Role } from "inference-grid-sdk";
readline.createInterface({
input: process.stdin,
output: process.stdout
}).question('Enter a message: ', async (message) => {
await handle(message);
});
Handler
In the handle function, we'll initialize our clients and make the calls!
typescript
async function handle(message: string) {
/// "0000..." is a special key which is rate-limited but publicly available. To avoid rate limits, you
// can register your own private key with the inference grid.
const client = new InferenceGrid({
privateKey: "0000000000000000000000000000000000000000000000000000000000000000",
});
// This is an example Spark wallet. Replace the mnemonic with your own! Do not add funds to this
// wallet.
const { wallet } = await SparkWallet.initialize({
mnemonicOrSeed: "record risk crouch submit abuse strategy great category alley lend upgrade fancy",
options: {
network: "MAINNET"
}
});
// Submit an inference request!
const { invoice } = await client.chat({
maxTokens: 1000,
temperature: 0.5,
model: {
modelIds: ['openai/gpt-4o'],
flags: [],
},
messages: [
{
role: Role.SYSTEM,
content: "Respond to everything in the style of a pirate.",
},
{
role: Role.USER,
content: message,
}
]
}, (partialMessage) => {
// Stream the response!
process.stdout.write(partialMessage);
});
// If there is an invoice, pay it with Spark!
if (invoice) {
console.log("Paying invoice...");
let paymentResult = await wallet.payLightningInvoice({
invoice: invoice,
maxFeeSats: 1000,
});
console.log(paymentResult);
} else {
console.log("No invoice returned");
}
// This disconnects to websockets which keep the wallet up-to-date.
await wallet.cleanupConnections();
process.exit(0);
}