npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nanogptjs

v1.0.1

Published

Interact with NanoGPT's API for pay-per-prompt interaction with AI models

Downloads

137

Readme

NanoGPTJS

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.

Installation

npm install --save nanogptjs

Sample Usage

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.
})()

Setup

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'
})

API

chat(String prompt)

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?')

Arguments

  • prompt (String, required) - This is the prompt that will be sent to the text model.

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
  }
}

chat(Object config)

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
})

Arguments

  • config (Object, required)) - This is a configuration Object containing the following fields:
    • prompt (String, required) - This is the prompt that will be sent to the text model.
    • model (String, optional) - The text model to use. This field is optional if the defaultModel field is set when creating your nanogptjs instance.
    • context (Array, optional) - Provide context (chat history) to your chat prompt. Send context in an alternating user-to-AI sequence. Use the utility functions 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
  }
}

image(Object config)

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
})

Arguments

  • config (Object, required) - This is a configuration Object containing the following fields:
    • prompt (String, required) - This is the prompt for what you want to see in your generated image.
    • width (Integer, optional) - The width of the image you want to generate.
    • height (Integer, optional) - The height of the image you want to generate.
    • negativePrompt (String, optional) - This is the prompt for what you don't want to see in your generated image.
    • steps (Integer, optional) - Number of generation steps. Higher is better but more expensive. Aim for 25-30.
    • sampler (String, optional) - Sampler that will be used to denoise your image during generation. For advanced users.
    • scale (Number, optional) - Determines how much the image generation process follows the text prompt. For advanced users.

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
  }
}

imageBatch(Object config)

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
})

Arguments

  • config (Object, required) - This is a configuration Object containing the following fields:
    • batchSize (Number, required) - Specify how many images to generate. The more images, the higher the Nano cost.
    • prompt (String, required) - This is the prompt for what you want to see in your generated image.
    • width (Integer, optional) - The width of the image you want to generate.
    • height (Integer, optional) - The height of the image you want to generate.
    • negativePrompt (String, optional) - This is the prompt for what you don't want to see in your generated image.
    • steps (Integer, optional) - Number of generation steps. Higher is better but more expensive. Aim for 25-30.
    • sampler (String, optional) - Sampler that will be used to denoise your image during generation. For advanced users.
    • scale (Number, optional) - Determines how much the image generation process follows the text prompt. For advanced users

Returns

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
  }
}

account()

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'
}

balance()

Retrieves your current NanoGPT account balance.

let balance = await nanogpt.balance()

Returns a Number

4.0678

models()

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
    }
  }
}

contextUser(String content)

Utility function to aid with creation of the Object field context in the chat function.

nanogpt.contextUser('Blue is my favorite color')

Arguments

  • content (String, required) - Content that the user previously supplied via chat prompt.

Returns an Object with the following format:

{
  role: 'user',
  content: 'Blue is my favorite color'
}

contextAI(String content)

Utility function to aid with creation of the Object field context in the chat function.

nanogpt.contextAI('Very interesting! I will remember that.')

Arguments

  • content (String, required) - Content that the AI previously replied with.

Returns an Object with the following format:

{
  role: 'assistant',
  content: 'Very interesting! I will remember that.'
}

Examples

You can find multiple NanoGPTJS usage examples in the examples folder.

Feedback

Pull requests and opened issues are welcome!

Disclaimer

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.

License

MIT