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-modelClient 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
| Option | Type | Description | Required | Default |
|---|---|---|---|---|
apiKey | string | Your PaiTIENT API key | No (if in env) | process.env.PAITIENT_API_KEY |
clientId | string | Your PaiTIENT client ID | No (if in env) | process.env.PAITIENT_CLIENT_ID |
endpoint | string | API endpoint | No | "https://api.paitient.ai" |
timeout | number | Request timeout in ms | No | 60000 |
maxRetries | number | Max number of retries | No | 3 |
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
| Option | Type | Description | Required | Default |
|---|---|---|---|---|
modelName | string | HuggingFace model name | No | "ZimaBlueAI/HuatuoGPT-o1-8B" |
deploymentName | string | Name for the deployment | Yes | - |
useGpu | boolean | Whether to use GPU | No | false |
region | string | AWS region | No | "us-west-2" |
replicas | number | Number of replicas | No | 1 |
instanceType | string | EC2 instance type | No | Depends 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
| Option | Type | Description | Required | Default |
|---|---|---|---|---|
deploymentId | string | ID of the deployment | Yes | - |
prompt | string | Input text prompt | Yes | - |
maxTokens | number | Max tokens to generate | No | 100 |
temperature | number | Sampling temperature (0-2) | No | 0.7 |
topP | number | Nucleus sampling parameter (0-1) | No | 0.9 |
presencePenalty | number | Presence penalty (0-2) | No | 0 |
frequencyPenalty | number | Frequency penalty (0-2) | No | 0 |
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:
| Variable | Description |
|---|---|
PAITIENT_API_KEY | Your API key |
PAITIENT_CLIENT_ID | Your client ID |
PAITIENT_ENDPOINT | API endpoint |
PAITIENT_TIMEOUT | Request timeout in ms |
PAITIENT_MAX_RETRIES | Maximum number of retries |
PAITIENT_RETRY_DELAY | Delay between retries in ms |
PAITIENT_LOG_LEVEL | Log 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();