NanoGPTJS

NanoGPTJS is a Node.js library designed to interact with NanoGPT’s API. This library provides an easy way to integrate NanoGPT’s capabilities into your JavaScript applications.

Overview

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

You can install the library using npm:

npm install nanogptjs

Basic Usage

const NanoGPT = require('nanogptjs');

// Initialize with your API key
const nanogpt = new NanoGPT('your-api-key');

async function chatExample() {
  try {
    const response = await nanogpt.chat({
      model: 'chatgpt-4o-latest',
      messages: [
        { role: 'system', content: 'You are a helpful assistant.' },
        { role: 'user', content: 'What is the capital of France?' }
      ]
    });
    
    console.log(response);
  } catch (error) {
    console.error('Error:', error);
  }
}

chatExample();

Features

  • Chat Completions: Generate text responses using various AI models
  • Image Generation: Create images from text prompts
  • Model Selection: Choose from a wide range of available models
  • Balance Management: Check your NanoGPT balance and manage transactions

API Methods

Chat

const response = await nanogpt.chat({
  model: 'chatgpt-4o-latest',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'Hello, how are you?' }
  ],
  temperature: 0.7,
  max_tokens: 150
});

Generate Image

const response = await nanogpt.generateImage({
  prompt: 'A beautiful sunset over the ocean',
  model: 'recraft-v3',
  width: 1024,
  height: 1024
});

Check Balance

const balance = await nanogpt.checkBalance();
console.log('USD Balance:', balance.usd_balance);
console.log('Nano Balance:', balance.nano_balance);

Error Handling

The library provides robust error handling to manage API response errors:

try {
  const response = await nanogpt.chat({
    model: 'chatgpt-4o-latest',
    messages: [
      { role: 'user', content: 'Hello!' }
    ]
  });
} catch (error) {
  console.error('Status:', error.status);
  console.error('Message:', error.message);
}

Resources