TypeScript / Node.js API Reference¶
Installation¶
Client¶
Constructor¶
import { LlmClient } from 'liter-llm';
const client = new LlmClient({
apiKey: string,
baseUrl?: string,
modelHint?: string,
maxRetries?: number, // default: 3
timeoutSecs?: number, // default: 60
});
| Parameter | Type | Default | Description |
|---|---|---|---|
apiKey |
string |
required | API key for authentication |
baseUrl |
string \| undefined |
undefined |
Override provider base URL |
modelHint |
string \| undefined |
undefined |
Hint for provider auto-detection (e.g. "groq/llama3-70b") |
maxRetries |
number \| undefined |
3 |
Retries on 429 / 5xx responses |
timeoutSecs |
number \| undefined |
60 |
Request timeout in seconds |
Methods¶
All methods are async and return Promises. Request and response objects use camelCase keys (converted automatically from the snake_case wire format).
chat(request)¶
Send a chat completion request.
const resp = await client.chat({
model: "gpt-4",
messages: [{ role: "user", content: "Hi" }],
});
console.log(resp.choices[0].message.content);
chatStream(request)¶
Collect all streaming chat completion chunks into an array. The full SSE stream is consumed on the Rust side before the Promise resolves.
const chunks = await client.chatStream({
model: "gpt-4",
messages: [{ role: "user", content: "Hi" }],
});
for (const chunk of chunks) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
embed(request)¶
Send an embedding request.
listModels()¶
List available models from the provider.
imageGenerate(request)¶
Generate an image from a text prompt.
speech(request)¶
Generate speech audio from text. Returns a Buffer of raw audio bytes.
transcribe(request)¶
Transcribe audio to text.
moderate(request)¶
Check content against moderation policies.
rerank(request)¶
Rerank documents by relevance to a query.
createFile(request)¶
Upload a file.
retrieveFile(fileId)¶
Retrieve metadata for a file by ID.
deleteFile(fileId)¶
Delete a file by ID.
listFiles(query?)¶
List files, optionally filtered.
fileContent(fileId)¶
Retrieve the raw content of a file. Returns a Buffer.
createBatch(request)¶
Create a new batch job.
retrieveBatch(batchId)¶
Retrieve a batch by ID.
listBatches(query?)¶
List batches, optionally filtered.
cancelBatch(batchId)¶
Cancel an in-progress batch.
createResponse(request)¶
Create a new response via the Responses API.
retrieveResponse(id)¶
Retrieve a response by ID.
cancelResponse(id)¶
Cancel an in-progress response.
Module Functions¶
version()¶
Returns the library version string.
Types¶
Response objects are plain JavaScript objects with camelCase keys.
ChatCompletionResponse¶
| Field | Type | Description |
|---|---|---|
id |
string |
Response ID |
model |
string |
Model used |
choices |
Choice[] |
Completion choices |
usage |
Usage \| undefined |
Token usage |
created |
number |
Unix timestamp |
ChatCompletionChunk¶
| Field | Type | Description |
|---|---|---|
id |
string |
Response ID |
model |
string |
Model used |
choices |
StreamChoice[] |
Stream choices with deltas |
usage |
Usage \| undefined |
Token usage (final chunk only) |
Error Handling¶
Errors are thrown as JavaScript Error objects. The message includes a bracketed label for the error category:
try {
await client.chat({ model: "gpt-4", messages: [] });
} catch (err) {
// "[Authentication] Invalid API key"
// "[RateLimited] Too many requests"
// "[BadRequest] Messages must not be empty"
console.error(err.message);
}
Error categories: Authentication, RateLimited, BadRequest, ContextWindowExceeded, ContentPolicy, NotFound, ServerError, ServiceUnavailable, Timeout, Network, Streaming, EndpointNotSupported, InvalidHeader, Serialization.