Langtrain
Langtrain Docs
DocsAPI ReferenceSDK Reference
AppChat
GitHubDiscord

Node.js SDK

The official Node.js SDK for training, deploying, and managing AI models with Langtrain.

Node 18+
TypeScript
Streaming

Installation

Install the SDK with npm, yarn, or pnpm. Node.js 18+ is required.
python
1npm install langtrain-ai
2
3# With yarn
4yarn add langtrain-ai
5
6# With pnpm
7pnpm add langtrain-ai

Authentication

Configure your API key to authenticate with Langtrain.
python
1import Langtrain from 'langtrain-ai';
2
3// Option 1: Environment variable (recommended)
4// LANGTRAIN_API_KEY=your-api-key
5
6// Option 2: Direct initialization
7const client = new Langtrain({
8 apiKey: 'your-api-key',
9});
10
11// Option 3: From environment
12const client = new Langtrain({
13 apiKey: process.env.LANGTRAIN_API_KEY,
14});

LoRA Training

Fine-tune models using LoRA for efficient, memory-friendly training.
python
1import { LoRATrainer } from 'langtrain-ai';
2
3const trainer = new LoRATrainer({
4 model: 'meta-llama/Llama-3.3-8B',
5 outputDir: './my-model',
6});
7
8// Train on your data
9await trainer.train('training_data.jsonl');
10
11// Save and upload the trained model
12await trainer.save();
13await trainer.push('my-custom-model');

Dataset Management

Upload and manage training datasets programmatically.
python
1import { Dataset } from 'langtrain-ai';
2
3// Upload a dataset
4const dataset = await Dataset.upload({
5 filePath: 'data.jsonl',
6 name: 'customer-support-v1',
7});
8
9console.log('Dataset ID:', dataset.id);
10console.log('Rows:', dataset.rowCount);
11
12// List all datasets
13const datasets = await Dataset.list();
14datasets.forEach(ds => {
15 console.log(`- ${ds.name} (${ds.status})`);
16});

Training Jobs

Create and monitor fine-tuning jobs on Langtrain Cloud.
python
1import { TrainingJob } from 'langtrain-ai';
2
3// Create a training job
4const job = await TrainingJob.create({
5 modelId: 'llama-3.3-8b',
6 datasetId: dataset.id,
7 config: {
8 method: 'qlora',
9 epochs: 3,
10 learningRate: 2e-4,
11 batchSize: 4,
12 },
13});
14
15// Poll for completion
16while (['pending', 'running'].includes(job.status)) {
17 await job.refresh();
18 console.log(`Status: ${job.status}, Progress: ${job.progress}%`);
19 await new Promise(r => setTimeout(r, 30_000));
20}
21
22console.log('Training completed:', job.modelId);

Inference

Generate text with your trained models.
python
1import { Model } from 'langtrain-ai';
2
3const model = await Model.load('my-custom-model');
4
5// Generate text
6const response = await model.generate({
7 prompt: 'Explain machine learning',
8 maxTokens: 200,
9 temperature: 0.7,
10});
11console.log(response);
12
13// Chat interface
14const chatResponse = await model.chat([
15 { role: 'user', content: 'Hello!' }
16]);
17console.log(chatResponse.content);
18
19// Streaming
20for await (const chunk of model.stream('Tell me a story')) {
21 process.stdout.write(chunk);
22}

TypeScript Support

Full TypeScript types are included out of the box.
python
1import Langtrain, {
2 type TrainingConfig,
3 type GenerateOptions,
4 type ChatMessage,
5} from 'langtrain-ai';
6
7const config: TrainingConfig = {
8 method: 'qlora',
9 epochs: 3,
10 learningRate: 2e-4,
11};
12
13const messages: ChatMessage[] = [
14 { role: 'system', content: 'You are a helpful assistant.' },
15 { role: 'user', content: 'What is fine-tuning?' },
16];
17
18const client = new Langtrain();
19const response = await client.chat(messages, { model: 'my-model' });

Error Handling

Handle errors gracefully with typed exception classes.
python
1import {
2 LangtrainError,
3 AuthenticationError,
4 RateLimitError,
5 ValidationError,
6 NotFoundError,
7} from 'langtrain-ai';
8
9try {
10 const job = await TrainingJob.create({ ... });
11} catch (err) {
12 if (err instanceof AuthenticationError) {
13 console.error('Invalid API key');
14 } else if (err instanceof RateLimitError) {
15 console.error(`Rate limited — retry in ${err.retryAfter}s`);
16 } else if (err instanceof ValidationError) {
17 console.error('Invalid config:', err.message);
18 } else if (err instanceof NotFoundError) {
19 console.error('Model or dataset not found');
20 }
21}