0.1.0 • Published 5 months ago

@tanolabs/shield v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Tano Shield

A JavaScript client library for interacting with the Tano Shield AI security services.

Installation

npm install @tanolabs/shield
# or
yarn add @tanolabs/shield

Note: This package uses ES modules (type: "module") and requires Node.js 14.0.0 or higher. If you're using CommonJS modules, you'll need to use dynamic imports or update your project to use ES modules.

Features

  • AI Issue Detector: Protect your AI applications from harmful inputs and outputs
  • PII Redaction: Detect and redact personally identifiable information

Usage

Quick Start

import TanoShield from '@tanolabs/shield';

// Initialize the client with your API key
const tanoShield = new TanoShield({
  apiKey: 'your-api-key',
});

// Use the services
async function example() {
  try {
    // Check user input with the Issue Detector
    const firewallResult = await tanoShield.firewall.checkInput({
      system_prompt: "You are a helpful AI assistant",
      user_input: "Tell me about AI security",
      response: "I can provide information about AI security...",
      event_id: "conversation-12345",
      securityChecks: [
        "sycophancy",
        "faithfulness",
        "harmfulness",
        "implausible_output",
        "information_disclosure",
        "jailbreak"
      ]
    });
    console.log('Issue Detector check result:', firewallResult);
    
    // Redact PII from text
    const piiResult = await tanoShield.pii.redactText({
      text: 'My email is john.doe@example.com and my phone is 555-123-4567',
    });
    console.log('Redacted text:', piiResult.redactedText);
  } catch (error) {
    console.error('Error:', error);
  }
}

example();

Using Individual Services

You can also use each service independently:

import { FirewallClient, PIIClient } from '@tanolabs/shield';

// Initialize just the Issue Detector client
const firewall = new FirewallClient({
  apiKey: 'your-api-key',
});

// Check user input
const result = await firewall.checkInput({
  user_input: 'User message here',
  system_prompt: 'You are a helpful assistant',
});

API Reference

TanoShield

The main client that provides access to all services.

const tanoShield = new TanoShield({
  apiKey: 'your-api-key',
  baseUrl: 'https://api.example.com', // Optional
  timeout: 30000, // Optional (default: 30000)
  version: 'v1', // Optional (default: 'v1')
});

Properties

  • firewall: FirewallClient instance
  • pii: PIIClient instance
  • CREDIT_CONSTANTS: Credit cost constants

Methods

  • setApiKey(apiKey): Update the API key for all services

FirewallClient (Issue Detector)

Client for the AI Issue Detector service.

Methods

  • checkInput(params): Check user input for security issues
    • params.system_prompt: The system prompt (optional)
    • params.user_input: The user input to check (required)
    • params.response: The AI response to check (optional)
    • params.event_id: A unique identifier for the event (optional)
    • params.securityChecks: Array of security checks to perform (optional)
  • getSettings(): Get Issue Detector settings
  • updateSettings(settings): Update Issue Detector settings
  • getHistory(options): Get Issue Detector call history
  • getAnalytics(options): Get Issue Detector analytics

PIIClient

Client for the PII Redaction service.

Methods

  • redactText({ text, entityTypes, maskMode }): Redact PII from text
  • detectPII({ text, entityTypes }): Detect PII in text without redacting
  • getSupportedEntityTypes(): Get supported PII entity types

Credit System

Tano Shield uses a credit system for API usage. Each API call consumes credits based on the service used.

import { CREDIT_CONSTANTS } from '@tanolabs/shield';

console.log(CREDIT_CONSTANTS.FREE_CREDITS_PER_MONTH); // 1000
console.log(CREDIT_CONSTANTS.FIREWALL_CHECK_COST);    // 1
console.log(CREDIT_CONSTANTS.PII_REDACTION_COST);     // 1

Error Handling

The library provides custom error classes for different types of errors:

import { TanoShieldError, AuthenticationError, ApiRequestError } from '@tanolabs/shield';

try {
  await tanoShield.firewall.checkInput({ user_input: 'test' });
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.error('Authentication failed:', error.message);
  } else if (error instanceof ApiRequestError) {
    console.error('API request failed:', error.message, error.status);
  } else if (error instanceof TanoShieldError) {
    console.error('Tano Shield error:', error.message, error.code);
  } else {
    console.error('Unknown error:', error);
  }
}

Example API Call

Here's an example of how to use the Issue Detector with curl:

curl -X POST https://api.tanolabs.com/api/v1/firewall-check \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
  "prompt": "Tell me about AI security",
  "system_prompt": "You are a helpful AI assistant",
  "user_input": "Tell me about AI security",
  "response": "I can provide information about AI security...",
  "event_id": "conversation-12345",
  "securityChecks": ["sycophancy", "faithfulness", "harmfulness", "implausible_output", "information_disclosure", "jailbreak"]
}'

License

MIT

0.1.0

5 months ago