0.0.79 • Published 4 months ago

@decloudlabs/sky-ai-accesspoint v0.0.79

Weekly downloads
-
License
-
Repository
-
Last release
4 months ago

Skynet AI Access Point

The sky-ai-accesspoint package provides a secure and efficient way to manage AI-related operations with integrated cost management and access control. It connects to Skynet's infrastructure and handles balance tracking for NFT-based access control.

Table of Contents

  1. Installation
  2. Environment Configuration
  3. Usage Guide
  4. API Reference

Installation

npm install @decloudlabs/sky-ai-accesspoint

Environment Configuration

The following environment variables are required:

# Blockchain Configuration
JSON_RPC_PROVIDER=<your-json-rpc-url>
WALLET_PRIVATE_KEY=<your-wallet-private-key>
SUBNET_ID=<your-subnet-id>

# Firebase Configuration
FIREBASE_PROJECT_ID=<your-firebase-project-id>
FIREBASE_CLIENT_EMAIL=<your-firebase-client-email>
FIREBASE_PRIVATE_KEY=<your-firebase-private-key>

# OpenAI Configuration
OPENAI_API_KEY=<your-openai-api-key>

Usage Guide

Basic Setup

Here's how to initialize and use the AI Access Point:

import express from "express";
import { initAIAccessPoint } from "@decloudlabs/sky-ai-accesspoint";
import SkyMainNodeJS from "@decloudlabs/skynet/lib/services/SkyMainNodeJS";

const app = express();
app.use(express.json());

// Define your natural language processing function
const runNaturalFunction = async (
  req: Request,
  res: Response,
  balanceRunMain: BalanceRunMain
) => {
  try {
    // Extract the request parameters
    const { messages } = req.body;

    // Call the AI model
    const response = await balanceRunMain.callAIModel(messages);

    // Send the response
    res.json(response);
  } catch (error) {
    console.error("Error processing request:", error);
    res.status(500).json({ error: "Failed to process request" });
  }
};

// Initialize the access point
const main = async () => {
  const skyNode = new SkyMainNodeJS(/* your config */);

  const env = {
    JSON_RPC_PROVIDER: process.env.JSON_RPC_PROVIDER!,
    WALLET_PRIVATE_KEY: process.env.WALLET_PRIVATE_KEY!,
    SUBNET_ID: process.env.SUBNET_ID!,
    FIREBASE_PROJECT_ID: process.env.FIREBASE_PROJECT_ID!,
    FIREBASE_CLIENT_EMAIL: process.env.FIREBASE_CLIENT_EMAIL!,
    FIREBASE_PRIVATE_KEY: process.env.FIREBASE_PRIVATE_KEY!,
    OPENAI_API_KEY: process.env.OPENAI_API_KEY!,
    SERVER_COST_CONTRACT_ADDRESS: "", // Will be set automatically
  };

  const balanceRunMain = await initAIAccessPoint(
    env,
    skyNode,
    app,
    runNaturalFunction,
    true // Set to true to enable automatic balance updates
  );

  app.listen(3000, () => {
    console.log("Server running on port 3000");
  });
};

main().catch(console.error);

Making Requests

The package exposes a /natural-request endpoint that handles AI requests. Here's how to use it:

// Example request
const request = {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: "Bearer <your-auth-token>",
  },
  body: JSON.stringify({
    accountNFT: {
      collectionID: "your-collection-id",
      nftID: "your-nft-id",
    },
    prompt: "process this request",
    userAuthPayload: {
      message: "1713033600000",
      signature: "0x123",
      userAddress: "0x123",
    }, // get this by calling skynet.appManager.getUrsulaAuth()
  }),
};

Cost Management

The package automatically handles cost tracking for NFT-based access. Costs are stored in Firebase and can be managed using the BalanceRunMain class:

// Example of adding costs to an NFT
const addCost = async (accountNFT, cost) => {
  const response = await balanceRunMain.addCost(accountNFT, cost);
  if (response.success) {
    console.log("Cost added successfully");
  } else {
    console.error("Failed to add cost:", response.data);
  }
};

API Reference

initAIAccessPoint

Initializes the AI Access Point with the necessary configuration and middleware.

function initAIAccessPoint(
  env: ENVDefinition,
  skyNode: SkyMainNodeJS,
  app: express.Application,
  runNaturalFunction: (
    req: Request,
    res: Response,
    balanceRunMain: BalanceRunMain
  ) => Promise<void>,
  runUpdate: boolean
): Promise<BalanceRunMain>;

BalanceRunMain

The main class that handles balance management and AI model interactions.

Key methods:

  • addCost(accountNFT: AccountNFT, cost: string): Add costs to an NFT
  • callAIModel(messages: ChatCompletionMessageParam[]): Make AI model calls
  • setup(): Initialize the service
  • update(): Run periodic balance updates

Security

The package includes built-in middleware for:

  • Authentication (protect middleware)
  • Balance checking (checkBalance middleware)
  • NFT ownership verification

All endpoints are protected and require proper authentication and authorization.

Error Handling

The package provides detailed error handling and logging:

try {
  const response = await balanceRunMain.addCost(accountNFT, cost);
  if (!response.success) {
    console.error("Operation failed:", response.data);
  }
} catch (error) {
  console.error("Unexpected error:", error);
}

For more examples and detailed documentation, visit our GitHub repository.

0.0.79

4 months ago

0.0.78

4 months ago

0.0.77

4 months ago

0.0.76

4 months ago

0.0.75

5 months ago

0.0.69

6 months ago

0.0.68

6 months ago

0.0.67

6 months ago

0.0.66

6 months ago

0.0.65

6 months ago

0.0.64

6 months ago

0.0.63

6 months ago

0.0.62

6 months ago

0.0.61

6 months ago

0.0.60

7 months ago

0.0.59

7 months ago

0.0.58

7 months ago

0.0.57

8 months ago

0.0.56

8 months ago

0.0.55

8 months ago

0.0.54

8 months ago

0.0.53

8 months ago

0.0.52

8 months ago

0.0.51

8 months ago

0.0.50

8 months ago

0.0.49

8 months ago

0.0.48

8 months ago

0.0.47

8 months ago

0.0.46

8 months ago

0.0.45

8 months ago

0.0.44

8 months ago

0.0.43

11 months ago

0.0.42

11 months ago

0.0.41

11 months ago

0.0.40

11 months ago

0.0.39

11 months ago

0.0.38

11 months ago

0.0.37

11 months ago

0.0.36

11 months ago

0.0.35

11 months ago

0.0.33

11 months ago

0.0.32

11 months ago

0.0.31

11 months ago

0.0.30

11 months ago

0.0.29

11 months ago

0.0.28

11 months ago

0.0.27

11 months ago

0.0.26

11 months ago

0.0.25

11 months ago

0.0.24

11 months ago

0.0.23

11 months ago

0.0.22

11 months ago

0.0.21

11 months ago

0.0.20

11 months ago

0.0.19

11 months ago

0.0.18

11 months ago

0.0.17

11 months ago

0.0.16

11 months ago

0.0.15

11 months ago

0.0.14

11 months ago

0.0.13

11 months ago

0.0.12

11 months ago

0.0.11

11 months ago

0.0.10

11 months ago

0.0.9

11 months ago

0.0.8

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago