Node.js SDK Overview
The PaiTIENT Secure Model Service Node.js SDK provides a powerful interface for deploying, managing, and using AI models in a HIPAA/SOC2 compliant environment.
Installation
Install the SDK using npm:
npm install paitient-secure-modelOr using yarn:
yarn add paitient-secure-modelFor detailed installation instructions, see the Installation Guide.
Authentication
Before using the SDK, you need to set up authentication:
const { PaiTIENTClient } = require('paitient-secure-model');
// Using environment variables (recommended)
const client = new PaiTIENTClient();
// Or with explicit credentials
const client = new PaiTIENTClient({
apiKey: 'your-api-key',
clientId: 'your-client-id'
});Basic Usage
Deploying a Model
// Deploy a model
async function deployModel() {
try {
const deployment = await client.createDeployment({
modelName: 'ZimaBlueAI/HuatuoGPT-o1-8B',
deploymentName: 'clinical-assistant',
computeType: 'gpu',
instanceType: 'g4dn.xlarge'
});
console.log(`Deployment ID: ${deployment.id}`);
console.log(`Status: ${deployment.status}`);
// Wait for deployment to be ready
await deployment.waitUntilReady();
console.log('Deployment is ready!');
} catch (error) {
console.error('Deployment error:', error);
}
}
deployModel();Generating Text
// Generate text from a deployed model
async function generateText(deploymentId) {
try {
const response = await client.generateText({
deploymentId,
prompt: 'What are the potential side effects of metformin?',
maxTokens: 500,
temperature: 0.7
});
console.log(response.text);
} catch (error) {
console.error('Generation error:', error);
}
}
generateText('dep_12345abcde');Managing Deployments
// List all deployments
async function listDeployments() {
try {
const deployments = await client.listDeployments();
for (const dep of deployments) {
console.log(`${dep.id}: ${dep.name} - ${dep.status}`);
}
} catch (error) {
console.error('Error listing deployments:', error);
}
}
// Get a specific deployment
async function getDeployment(deploymentId) {
try {
const deployment = await client.getDeployment(deploymentId);
console.log(deployment);
} catch (error) {
console.error('Error getting deployment:', error);
}
}
// Update a deployment
async function updateDeployment(deploymentId) {
try {
await client.updateDeployment(deploymentId, {
minReplicas: 2,
maxReplicas: 5
});
console.log('Deployment updated successfully');
} catch (error) {
console.error('Error updating deployment:', error);
}
}
// Delete a deployment
async function deleteDeployment(deploymentId) {
try {
await client.deleteDeployment(deploymentId);
console.log('Deployment deleted successfully');
} catch (error) {
console.error('Error deleting deployment:', error);
}
}Advanced Features
Streaming Responses
For long-form text generation, streaming the response can provide a better user experience:
// Stream the response as it's generated
async function streamText(deploymentId) {
try {
const stream = await client.generateTextStream({
deploymentId,
prompt: 'Write a detailed summary of diabetes management techniques.',
maxTokens: 1000
});
// Using event emitter pattern
stream.on('data', (chunk) => {
process.stdout.write(chunk.text);
});
stream.on('end', () => {
console.log('\nStream completed');
});
stream.on('error', (error) => {
console.error('Stream error:', error);
});
// Alternatively, using async iteration
/*
for await (const chunk of stream) {
process.stdout.write(chunk.text);
}
console.log('\nStream completed');
*/
} catch (error) {
console.error('Streaming error:', error);
}
}
streamText('dep_12345abcde');Batch Processing
For processing multiple prompts efficiently:
// Define a list of prompts
const prompts = [
'What are the symptoms of hypertension?',
'What are common treatments for type 2 diabetes?',
'Explain the mechanism of action for statins.'
];
// Process in batch
async function batchGenerate(deploymentId, prompts) {
try {
const results = await client.generateTextBatch({
deploymentId,
prompts,
maxTokens: 300
});
for (let i = 0; i < results.length; i++) {
console.log(`Prompt ${i+1}: ${prompts[i]}`);
console.log(`Response: ${results[i].text}`);
console.log();
}
} catch (error) {
console.error('Batch generation error:', error);
}
}
batchGenerate('dep_12345abcde', prompts);Fine-tuning Models
The SDK provides support for fine-tuning models:
// Prepare training data
async function fineTuneModel() {
try {
// Prepare the training file
const trainFile = await client.prepareFineTuningFile({
filePath: 'training_data.jsonl',
validationSplit: 0.1
});
// Start fine-tuning job
const fineTuneJob = await client.createFineTuningJob({
model: 'ZimaBlueAI/HuatuoGPT-o1-8B',
trainingFile: trainFile.id,
epochs: 3
});
// Wait for fine-tuning to complete
await fineTuneJob.waitUntilComplete();
console.log('Fine-tuning completed!');
// Deploy fine-tuned model
const deployment = await client.createDeployment({
modelName: fineTuneJob.fineTunedModel,
deploymentName: 'custom-clinical-assistant'
});
console.log(`Deployed fine-tuned model: ${deployment.id}`);
} catch (error) {
console.error('Fine-tuning error:', error);
}
}
fineTuneModel();Secure Multi-tenancy
For applications serving multiple clients, the SDK supports secure multi-tenancy:
// Initialize with multi-tenant support
const client = new PaiTIENTClient({ multiTenant: true });
// Generate text for a specific tenant
async function generateForTenant(deploymentId, tenantId) {
try {
const response = await client.generateText({
deploymentId,
prompt: 'What are the potential side effects of metformin?',
tenantId // Ensures strong isolation
});
console.log(response.text);
} catch (error) {
console.error('Generation error:', error);
}
}
generateForTenant('dep_12345abcde', 'tenant_12345');Error Handling
The SDK includes specific error classes for different types of errors:
const { PaiTIENTClient } = require('paitient-secure-model');
const {
PaiTIENTApiError,
AuthenticationError,
ResourceNotFoundError,
RateLimitError
} = require('paitient-secure-model/errors');
const client = new PaiTIENTClient();
async function handleErrors() {
try {
const deployment = await client.getDeployment('non-existent-id');
} catch (error) {
if (error instanceof ResourceNotFoundError) {
console.log(`Resource not found: ${error.message}`);
console.log(`Request ID for troubleshooting: ${error.requestId}`);
} else if (error instanceof AuthenticationError) {
console.log(`Authentication failed: ${error.message}`);
} else if (error instanceof RateLimitError) {
console.log(`Rate limit exceeded, retry after ${error.retryAfter} seconds`);
} else if (error instanceof PaiTIENTApiError) {
console.log(`API error: ${error.code} - ${error.message}`);
} else {
console.log(`Unexpected error: ${error.message}`);
}
}
}
handleErrors();Logging
Control the SDK's logging level:
const { PaiTIENTClient } = require('paitient-secure-model');
// Enable debug logging
const client = new PaiTIENTClient({
logLevel: 'debug' // 'debug', 'info', 'warn', 'error'
});TypeScript Support
The SDK includes full TypeScript definitions:
import { PaiTIENTClient, DeploymentOptions, GenerationOptions } from 'paitient-secure-model';
const client = new PaiTIENTClient();
async function createTypedDeployment() {
const deploymentOptions: DeploymentOptions = {
modelName: 'ZimaBlueAI/HuatuoGPT-o1-8B',
deploymentName: 'clinical-assistant',
computeType: 'gpu',
instanceType: 'g4dn.xlarge'
};
const deployment = await client.createDeployment(deploymentOptions);
const generationOptions: GenerationOptions = {
deploymentId: deployment.id,
prompt: 'What are the potential side effects of metformin?',
maxTokens: 500,
temperature: 0.7
};
const response = await client.generateText(generationOptions);
console.log(response.text);
}Promise vs Callback API
The SDK provides both Promise-based and callback-based APIs:
// Promise API (recommended)
client.createDeployment({
modelName: 'ZimaBlueAI/HuatuoGPT-o1-8B',
deploymentName: 'clinical-assistant'
})
.then(deployment => console.log(deployment.id))
.catch(error => console.error(error));
// Callback API
client.createDeployment({
modelName: 'ZimaBlueAI/HuatuoGPT-o1-8B',
deploymentName: 'clinical-assistant'
}, (error, deployment) => {
if (error) {
console.error(error);
} else {
console.log(deployment.id);
}
});SDK Reference
For complete details on all classes and methods, see the Node.js SDK API Reference.
Next Steps
- Learn about Deployment
- Explore Text Generation
- Understand Fine-tuning
- Review Security Best Practices