---
title: "Node.js SDK"
description: "Using Liutong with the OpenAI Node.js SDK"
---

Liutong works out of the box with the [OpenAI Node.js SDK](https://github.com/openai/openai-node). Just change the base URL.

## Installation

```bash
npm install openai
```

## Setup

```javascript
import OpenAI from "openai";

const client = new OpenAI({
    baseURL: "https://api.liutong.llby.org/v1",
    apiKey: process.env.LIUTONG_API_KEY || "lt_your_api_key",
});
```

## Chat completion

```javascript
const response = await client.chat.completions.create({
    model: "crimson-falcon-4",
    messages: [
        { role: "system", content: "You are a helpful assistant." },
        { role: "user", content: "Explain closures in JavaScript." },
    ],
    temperature: 0.7,
    max_tokens: 512,
});

console.log(response.choices[0].message.content);
```

## Streaming

```javascript
const stream = await client.chat.completions.create({
    model: "crimson-falcon-4",
    messages: [{ role: "user", content: "Write a short story about a cat." }],
    stream: true,
});

for await (const chunk of stream) {
    const content = chunk.choices[0]?.delta?.content;
    if (content) process.stdout.write(content);
}
console.log();
```

## Embeddings

```javascript
const response = await client.embeddings.create({
    model: "jade-mole-4",
    input: "The quick brown fox jumps over the lazy dog.",
});

console.log(`Dimensions: ${response.data[0].embedding.length}`);
```

## Error handling

```javascript
try {
    const response = await client.chat.completions.create({
        model: "crimson-falcon-4",
        messages: [{ role: "user", content: "Hello" }],
    });
} catch (error) {
    if (error instanceof OpenAI.AuthenticationError) {
        console.error("Invalid API key");
    } else if (error instanceof OpenAI.APIError) {
        console.error(`API error: ${error.status} - ${error.message}`);
    }
}
```
