Skip to content

JavaScript SDK Reference

The PaiTIENT Secure Model JavaScript SDK provides a powerful interface to the Secure Model Service, allowing you to deploy, manage, and use AI models in a HIPAA/SOC2 compliant environment.

Installation

bash
npm install paitient-secure-model
# or with yarn
yarn add paitient-secure-model

Client Initialization

javascript
const { SecureModelClient } = require('paitient-secure-model');

// Initialize from environment variables
const client = new SecureModelClient();

// Or specify credentials directly
const client = new SecureModelClient({
  apiKey: "your-api-key",
  clientId: "your-client-id",
  endpoint: "https://api.paitient.ai"  // Optional, defaults to production
});

Core Classes

SecureModelClient

The main client for interacting with the PaiTIENT Secure Model Service.

Constructor Options

OptionTypeDescriptionRequiredDefault
apiKeystringYour PaiTIENT API keyNo (if in env)process.env.PAITIENT_API_KEY
clientIdstringYour PaiTIENT client IDNo (if in env)process.env.PAITIENT_CLIENT_ID
endpointstringAPI endpointNo"https://api.paitient.ai"
timeoutnumberRequest timeout in msNo60000
maxRetriesnumberMax number of retriesNo3

Deployment Management

Deploy a Model

javascript
async function deployModel() {
  const deployment = await client.deploy({
    modelName: "ZimaBlueAI/HuatuoGPT-o1-8B",
    deploymentName: "clinical-assistant",
    useGpu: true,
    region: "us-west-2",
    replicas: 1,
    instanceType: "ml.g4dn.xlarge"
  });
  
  console.log(`Deployment ID: ${deployment.id}`);
  return deployment;
}

Deploy Options

OptionTypeDescriptionRequiredDefault
modelNamestringHuggingFace model nameNo"ZimaBlueAI/HuatuoGPT-o1-8B"
deploymentNamestringName for the deploymentYes-
useGpubooleanWhether to use GPUNofalse
regionstringAWS regionNo"us-west-2"
replicasnumberNumber of replicasNo1
instanceTypestringEC2 instance typeNoDepends on useGpu

Get Deployment Status

javascript
async function checkStatus(deploymentId) {
  const status = await client.getDeploymentStatus(deploymentId);
  console.log(`Status: ${status.status}`);
  console.log(`Message: ${status.message}`);
  return status;
}

List All Deployments

javascript
async function listDeployments() {
  const deployments = await client.listDeployments();
  deployments.forEach(deployment => {
    console.log(`ID: ${deployment.id}, Name: ${deployment.name}, Status: ${deployment.status}`);
  });
  return deployments;
}

Delete a Deployment

javascript
async function deleteDeployment(deploymentId) {
  await client.deleteDeployment(deploymentId);
  console.log("Deployment deleted successfully");
}

Text Generation

Generate Text

javascript
async function generateText(deploymentId, prompt) {
  const result = await client.generate({
    deploymentId: deploymentId,
    prompt: prompt,
    maxTokens: 500,
    temperature: 0.7,
    topP: 0.9,
    presencePenalty: 0,
    frequencyPenalty: 0
  });
  
  console.log(result.text);
  console.log(`Token usage: ${result.usage.totalTokens}`);
  return result;
}

Generate Options

OptionTypeDescriptionRequiredDefault
deploymentIdstringID of the deploymentYes-
promptstringInput text promptYes-
maxTokensnumberMax tokens to generateNo100
temperaturenumberSampling temperature (0-2)No0.7
topPnumberNucleus sampling parameter (0-1)No0.9
presencePenaltynumberPresence penalty (0-2)No0
frequencyPenaltynumberFrequency penalty (0-2)No0

Stream Text Generation

javascript
async function streamText(deploymentId, prompt) {
  const stream = await client.generateStream({
    deploymentId: deploymentId,
    prompt: prompt,
    maxTokens: 1000
  });
  
  for await (const chunk of stream) {
    process.stdout.write(chunk.text);
    // or handle the chunk in your application
  }
}

Subscription Management

Get Subscription Status

javascript
async function checkSubscription() {
  const subscription = await client.getSubscription();
  console.log(`Tier: ${subscription.tier}`);
  console.log(`Features: ${subscription.features.join(', ')}`);
  console.log(`Usage: ${subscription.currentUsage}/${subscription.usageLimit}`);
  return subscription;
}

LoRA Fine-tuning

Create a Fine-tuning Job

javascript
async function fineTuneModel(deploymentId, trainingFile) {
  const fineTuningJob = await client.createFineTuningJob({
    deploymentId: deploymentId,
    trainingFile: trainingFile,
    epochs: 3,
    learningRate: 3e-5,
    batchSize: 8
  });
  
  console.log(`Fine-tuning job ID: ${fineTuningJob.id}`);
  return fineTuningJob;
}

Get Fine-tuning Job Status

javascript
async function checkFineTuningStatus(jobId) {
  const status = await client.getFineTuningJobStatus(jobId);
  console.log(`Status: ${status.status}`);
  console.log(`Progress: ${status.progress}%`);
  return status;
}

List Fine-tuning Jobs

javascript
async function listFineTuningJobs() {
  const jobs = await client.listFineTuningJobs();
  jobs.forEach(job => {
    console.log(`ID: ${job.id}, Model: ${job.baseModel}, Status: ${job.status}`);
  });
  return jobs;
}

Error Handling

The SDK provides specific error classes for different types of errors:

javascript
const { 
  SecureModelClient,
  AuthenticationError,
  ValidationError,
  ResourceNotFoundError,
  RateLimitError,
  ServiceError
} = require('paitient-secure-model');

async function handleErrors() {
  try {
    // Your SDK operations here
    await client.deploy({
      deploymentName: "my-deployment"
    });
  } catch (error) {
    if (error instanceof AuthenticationError) {
      console.error("Authentication failed:", error.message);
    } else if (error instanceof ValidationError) {
      console.error("Validation error:", error.message);
    } else if (error instanceof ResourceNotFoundError) {
      console.error("Resource not found:", error.message);
    } else if (error instanceof RateLimitError) {
      console.error("Rate limit exceeded:", error.message);
      console.log(`Retry after ${error.retryAfter} seconds`);
    } else if (error instanceof ServiceError) {
      console.error("Service error:", error.message);
    } else {
      console.error("Unexpected error:", error);
    }
  }
}

Advanced Usage

Custom Headers

javascript
const client = new SecureModelClient({
  apiKey: "your-api-key",
  clientId: "your-client-id",
  headers: {
    'X-Custom-Header': 'custom-value'
  }
});

Set Request Timeout

javascript
const client = new SecureModelClient({
  apiKey: "your-api-key",
  clientId: "your-client-id",
  timeout: 120000  // 2 minutes
});

Retry Configuration

javascript
const client = new SecureModelClient({
  apiKey: "your-api-key",
  clientId: "your-client-id",
  maxRetries: 5,
  retryDelay: 1000  // ms
});

TypeScript Support

The SDK includes TypeScript definitions. Import types as follows:

typescript
import { 
  SecureModelClient, 
  DeploymentOptions, 
  GenerateOptions,
  DeploymentStatus,
  GenerationResult
} from 'paitient-secure-model';

// Example with types
const deployOptions: DeploymentOptions = {
  modelName: "ZimaBlueAI/HuatuoGPT-o1-8B",
  deploymentName: "typescript-deployment",
  useGpu: true
};

async function deploy(): Promise<void> {
  const client = new SecureModelClient();
  const deployment = await client.deploy(deployOptions);
  const status: DeploymentStatus = await client.getDeploymentStatus(deployment.id);
  console.log(status);
}

Environment Variables

The SDK recognizes the following environment variables:

VariableDescription
PAITIENT_API_KEYYour API key
PAITIENT_CLIENT_IDYour client ID
PAITIENT_ENDPOINTAPI endpoint
PAITIENT_TIMEOUTRequest timeout in ms
PAITIENT_MAX_RETRIESMaximum number of retries
PAITIENT_RETRY_DELAYDelay between retries in ms
PAITIENT_LOG_LEVELLog level (debug, info, warn, error)

Complete Example

Here's a complete example that deploys a model, waits for it to be ready, and generates text:

javascript
const { SecureModelClient } = require('paitient-secure-model');

async function main() {
  try {
    // Initialize client
    const client = new SecureModelClient({
      apiKey: process.env.PAITIENT_API_KEY,
      clientId: process.env.PAITIENT_CLIENT_ID
    });
    
    // Deploy model
    console.log("Deploying model...");
    const deployment = await client.deploy({
      deploymentName: "example-deployment",
      useGpu: true
    });
    
    console.log(`Deployment ID: ${deployment.id}`);
    
    // Check deployment status
    let status = await client.getDeploymentStatus(deployment.id);
    console.log(`Initial status: ${status.status}`);
    
    // Wait for deployment to be ready
    while (status.status !== "DEPLOYED") {
      if (status.status === "FAILED") {
        console.error(`Deployment failed: ${status.message}`);
        return;
      }
      
      console.log(`Waiting for deployment... Current status: ${status.status}`);
      await new Promise(resolve => setTimeout(resolve, 30000)); // Wait 30 seconds
      status = await client.getDeploymentStatus(deployment.id);
    }
    
    console.log("Deployment successful!");
    
    // Generate text
    console.log("Generating text...");
    const result = await client.generate({
      deploymentId: deployment.id,
      prompt: "Explain the importance of HIPAA compliance in healthcare AI applications.",
      maxTokens: 500
    });
    
    console.log("\nGenerated Text:");
    console.log(result.text);
    console.log(`\nToken usage: ${result.usage.totalTokens}`);
    
    // Check subscription
    const subscription = await client.getSubscription();
    console.log(`\nSubscription tier: ${subscription.tier}`);
    console.log(`Usage: ${subscription.currentUsage}/${subscription.usageLimit}`);
    
  } catch (error) {
    console.error("Error:", error);
  }
}

main();

Released under the MIT License.