0.0.79 • Published 6 months ago

@decloudlabs/sky-ai-accesspoint v0.0.79

Weekly downloads
-
License
-
Repository
-
Last release
6 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

6 months ago

0.0.78

6 months ago

0.0.77

6 months ago

0.0.76

6 months ago

0.0.75

6 months ago

0.0.69

7 months ago

0.0.68

7 months ago

0.0.67

7 months ago

0.0.66

7 months ago

0.0.65

7 months ago

0.0.64

7 months ago

0.0.63

7 months ago

0.0.62

7 months ago

0.0.61

7 months ago

0.0.60

8 months ago

0.0.59

8 months ago

0.0.58

8 months ago

0.0.57

9 months ago

0.0.56

9 months ago

0.0.55

9 months ago

0.0.54

9 months ago

0.0.53

9 months ago

0.0.52

9 months ago

0.0.51

9 months ago

0.0.50

9 months ago

0.0.49

9 months ago

0.0.48

10 months ago

0.0.47

10 months ago

0.0.46

10 months ago

0.0.45

10 months ago

0.0.44

10 months ago

0.0.43

12 months ago

0.0.42

1 year ago

0.0.41

1 year ago

0.0.40

1 year ago

0.0.39

1 year ago

0.0.38

1 year ago

0.0.37

1 year ago

0.0.36

1 year ago

0.0.35

1 year ago

0.0.33

1 year ago

0.0.32

1 year ago

0.0.31

1 year ago

0.0.30

1 year ago

0.0.29

1 year ago

0.0.28

1 year ago

0.0.27

1 year ago

0.0.26

1 year ago

0.0.25

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago