Interact with NanoGPT's API for pay-per-prompt interaction with AI models
MIT License
Node.js library to Interact with NanoGPT's API. The NanoGPT service enables pay-per-prompt interaction with chat and image generation models. You will need a prefilled NanoGPT wallet and API key to use this library effectively.
npm install --save nanogptjs
const nanogptjs = require('nanogptjs')
const { YOUR_NANO_GPT_API_KEY } = require('./secrets.js')
;(async function () {
const nanogpt = nanogptjs({
apiKey: YOUR_NANO_GPT_API_KEY,
defaultModel: 'chatgpt-4o-latest'
})
let { reply } = await nanogpt.chat('Tell me something about Nano digital currency?')
console.log(reply) // Nano is a feeless and instant cryptocurrency.
})()
Importing nanogptjs
const nanogptjs = require('nanogptjs')
Create a nanogptjs
instance with your NanoGPT API key (required)
const nanogpt = nanogptjs({
apiKey: YOUR_NANO_GPT_API_KEY
})
Alternatively supply a defaultModel
to be used in further function calls
const nanogpt = nanogptjs({
apiKey: YOUR_NANO_GPT_API_KEY,
defaultModel: 'chatgpt-4o-latest'
})
Sends a chat prompt. Requires defaultModel
field be set when creating your nanogptjs
instance.
let reply = await nanogpt.chat('Tell me something about Nano digital currency?')
Returns a Promise that resolves to an Object with the following format:
{
reply: "Nano is a feeless and instant cryptocurrency.", // the reply to your chat prompt
metadata: {
nanoCost: 0.001089, // the Nano cost of this interaction
inputTokens: 24, // the number of tokens your input created
outputTokens: 21 // the number of tokens your output created
}
}
Sends a chat prompt. Gives you more control by allowing you to specify model and chat context (i.e. history).
let reply = await nanogpt.chat({
prompt: 'What is my favorite color?', // chat prompt
model: 'chatgpt-4o-latest', // model to use
context: [
nanogpt.contextUser('Blue is my favorite color'),
nanogpt.contextAI('Very interesting! I will remember that.'),
] // context for your chat prompt
})
defaultModel
field is set when creating your nanogptjs
instance.contextUser
and contextAI
to specify who is speaking.Returns a Promise that resolves to an Object with the following format:
{
reply: "Based on prior informations, I think blue is your favorite color!", // the reply to your chat prompt
metadata: {
nanoCost: 0.001089, // the Nano cost of this interaction
inputTokens: 24, // the number of tokens your input created
outputTokens: 21 // the number of tokens your output created
}
}
Generates a single image.
let image = await nanogpt.image({
prompt: 'a beautiful landscape, bright sunshine, green fields',
model: 'dreamshaper_8_93211.safetensors',
width: 1024,
height: 1024
})
Returns a Promise that resolves to an Object with the following format:
{
base64: " /9j/4AAQSkZJRgABAQAAAQABAAD...", // base64 encoded version of your generated image
metadata: {
created: 1723814389930, // timestamp of this image generation
nanoCost: 0.0021, // the Nano cost of this interaction
remainingBalance: 0.10210999999999999 // your remaining NanoGPT balance
}
}
Generates a specific number of images.
let imageBatch = await nanogpt.imageBatch({
batchSize: 2,
prompt: 'a beautiful landscape with a large cat',
model: 'dreamshaper_8_93211.safetensors',
width: 1024,
height: 1024
})
Returns a Promise that resolves to an Object with the following format:
{
imageBatch: [ // array of generated images
base64: " /9j/4AAQSkZJRgABAQAAAQAQAQA...", // base64 encoded version of your generated image
base64: " /9j/4AAQSkZJRgABAQAAAQAABAB...", // base64 encoded version of your generated image
]
metadata: {
created: 1723815073228,
nanoCost: 0.00501,
remainingBalance: 0.13908
}
}
Retrieves information about your NanoGPT account.
let account = await nanogpt.account()
Returns a Promise that resolves to an Object with the following format:
{
nanoDepositAddress: 'nano_3d46mow3fim7bfd15xckfgao9ehabmp56y7gkn9q3nky1i3buehtroxi8q67',
nanoReturnAddress: 'nano_15qzsidyztg1i7at4zdeapzzh5hfgy1rip8e5uz1an3tujuunujgesgzf187',
balance: 3.098908,
receivable: 0,
earned: '0.000000'
}
Retrieves your current NanoGPT account balance.
let balance = await nanogpt.balance()
Returns a Number
4.0678
Retrieves information about the models available on NanoGPT.
let models = await nanogpt.models()
Returns a Promise that resolves to an Object with the following format:
{
"models": {
"text": {
"gpt-4o": {
"name": "GPT 4o",
"model": "gpt-4o",
"description": "OpenAI's most advanced model. It matches GPT-4 Turbo performance on text in English and code, with significant improvement on text in non-English languages, while also being much faster and 50% cheaper.",
"cost": "Average cost: 0.025 Nano",
"costEstimate": 0.025,
"labelVariant": "outline",
"label": "Recommended",
"visible": false
},
// ...more text models
},
"image": {
"dall-e-3": {
"name": "DALL-E-3",
"model": "dall-e-3",
"engine": "dalle",
"description": "The current golden standard in image generation.",
"cost": {
"1024x1024": 0.04,
"1024x1792": 0.08,
"1792x1024": 0.08
},
"label": "Default",
"maxImages": 1,
"resolutions": [
{
"value": "1024x1024"
},
{
"value": "1024x1792",
"comment": "2x the price"
},
{
"value": "1792x1024",
"comment": "2x the price"
}
]
},
// ...more image models
}
}
}
Utility function to aid with creation of the Object field context
in the chat
function.
nanogpt.contextUser('Blue is my favorite color')
Returns an Object with the following format:
{
role: 'user',
content: 'Blue is my favorite color'
}
Utility function to aid with creation of the Object field context
in the chat
function.
nanogpt.contextAI('Very interesting! I will remember that.')
Returns an Object with the following format:
{
role: 'assistant',
content: 'Very interesting! I will remember that.'
}
You can find multiple NanoGPTJS usage examples in the examples folder.
Pull requests and opened issues are welcome!
As always when working with real world value, in this case Nano, be careful when using this library. The authors and contributors shall not be held liable for any use of this library's functionality, intentional or unintentional, that leads to an undesired lose of funds.
MIT