@felipenmoura/laya-node-client
v1.0.1
Published
Client for laya-node
Maintainers
Readme
Laya Node Client
The official Node.js client for integrating the Laya Text Classification Model into your applications. This package provides a secure, server-side HTTP client that connects directly to a running laya-node inference server.
This should be used alongside with laya-node, a node service (wrapper) you can start in your own insfrastructure, container or local machine.
Features
- Server-Side Security: Explicitly blocks client-side execution to prevent leaking your
SECRET_API_KEY. - Router Compatibility: Exposes a
Routerclass matching the Laya Python SDK, so your application logic reads exactly like the official documentation. - Dynamic State Support: Supports both simple string payloads and complex dictionary states for your predictions.
- Native Fetch: Zero-dependency architecture relying on Node's native
fetchAPI.
Installation
npm install @felipenmoura/laya-node-client
# or
pnpm add @felipenmoura/laya-node-clientGetting Started
First, ensure you have the laya-node server running locally or deployed.
For example, by using the node wrapper or starting the inference server in your own server.
Importing and Instantiating
You can import either Router or LayaClient (they are functionally identical, but Router is recommended to match Laya's standard API terminology). The client supports both CommonJS (require) and ES Modules (import).
// Using CommonJS
const { Router } = require('@felipenmoura/laya-node-client');
// Using ES Modules (Named Import)
import { Router } from "@felipenmoura/laya-node-client";
// Using ES Modules (Default Import)
import LayaClient from "@felipenmoura/laya-node-client";// Initialize the router pointing to your running server
const router = new Router({
url: 'http://localhost:4000', // URL of your laya-node server
apiKey: 'your_secret_api_key' // Optional, if you configured one in your on premise server
});Making Predictions
Laya's API takes a state (the text/data to analyze) and questions (a schema defining what you want to extract).
1. Simple String State
const analyzeIssue = async () => {
const result = await router.predict(
"The app crashes every time I try to open the settings menu. Please fix this!",
{
"issue_type": {
"type": "choice",
"instructions": "What kind of issue is the user reporting?",
"criteria": {
"bug": "crash, error, or broken feature",
"feature_request": "asking for a new capability",
"question": "how-to or general inquiry"
}
}
}
);
console.log(result.answers.issue_type.choice); // "bug"
};2. Dictionary State
If you want to feed multiple data points (like an email subject and body), pass an object as the state. The Router natively handles translating this structure!
const analyzeEmail = async () => {
const result = await router.predict(
{
"subject": "Cobrança duplicada",
"body": "Fui cobrado duas vezes este mês. Cancelem a minha assinatura e devolvam o dinheiro."
},
{
"department": {
"type": "choice",
"instructions": "Which department should handle this request?",
"criteria": {
"billing": "invoices, payments, refunds",
"technical": "bugs, outages, system errors"
}
},
"churn_risk": {
"type": "noul",
"instructions": "Does the user threaten to cancel or leave?"
}
}
);
console.log(result.answers.department.choice); // "billing"
// The Router will automatically detect the language (Portuguese) and use the multilingual model!
console.log(result.routing.model); // "multilingual"
};Running the Built-in Tests
To verify that the client connects to your laya-node server successfully, you can run the interactive test script included in the repository.
- Clone this repository and navigate to the
laya-node-clientdirectory. - Run the test script using Node:
node test.js - The prompt will ask you for your server URL and API key. It will then perform English and Portuguese text classification tests against your server!
Security Notes
If the server requires the use of SECRET_API_KEY, then this client must pass it and it will ONLY work on server side, as you should NEVER leak your SECRET keys into the client side.
License
This project is licensed under the MIT License.
