@aristech-org/nlp-client
Version:
A Node.js client library for the Aristech NLP Service
465 lines (464 loc) • 16 kB
JavaScript
import * as grpc from '@grpc/grpc-js';
import { FunctionRequest, NLPServerClient, RunFunctionsRequest, SyncDBsRequest, } from './generated/nlp_server.js';
import { GetContentRequest, GetIntentsRequest, GetKeywordsRequest, GetScoreLimitsRequest, RemoveContentRequest, UpdateContentRequest, } from './generated/intents.js';
import { AddProjectRequest, GetEmbeddingModelsRequest, GetProjectsRequest, RemoveProjectRequest, UpdateProjectRequest, } from './generated/projects.js';
import { AddDocumentsRequest, GetDocumentsRequest, GetDocumentStatusRequest, ProcessDocumentsRequest, RemoveDocumentsRequest, UpdateDocumentsRequest } from './generated/documents.js';
import fs from 'fs';
// Re-export generated types
export * as NlpServer from './generated/nlp_server.js';
export * as Projects from './generated/projects.js';
export * as Intents from './generated/intents.js';
export class NlpClient {
cOptions;
constructor(options) {
this.cOptions = options;
}
/**
* List all available functions on the server
* @param request An optional request object
* @returns A promise that resolves with an array of function messages
*/
async listFunctions(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = FunctionRequest.create(request);
const stream = client.getFunctions(req);
const functions = [];
stream.on('data', (data) => {
functions.push(data);
});
stream.on('end', () => {
res(functions);
});
stream.on('error', (err) => {
rej(err);
});
});
}
/**
* Performs a processing request with the given request
* @param request The request object
* @returns A promise that resolves with the response object
*/
async runFunctions(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = RunFunctionsRequest.create(request);
client.runFunctions(req, (err, response) => {
if (err) {
rej(err);
}
else {
res(response);
}
});
});
}
/**
* Performs a processing request with the given request
* @deprecated Use `runFunctions` instead
* @param request The request object
* @returns A promise that resolves with the response object
*/
async process(request) {
return this.runFunctions(request);
}
/**
* Get all projects available on the server
* @param request An optional request object
* @returns A promise that resolves with an array of projects
*/
async listProjects(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = GetProjectsRequest.create(request);
const stream = client.getProjects(req);
const projects = [];
stream.on('data', (data) => {
projects.push(data);
});
stream.on('end', () => {
res(projects);
});
stream.on('error', (err) => {
rej(err);
});
});
}
/**
* Add a new project to the server
* @param project The project to add
* @returns A promise that resolves with the added project
*/
async addProject(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = AddProjectRequest.create(request);
client.addProject(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
/**
* Update a project on the server
* @param project The project to update
* @returns A promise that resolves with the updated project
*/
async updateProject(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = UpdateProjectRequest.create(request);
client.updateProject(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
/**
* Remove a project from the server
* @param projectId The id of the project to remove
* @returns A promise that resolves when the project was removed
*/
async removeProject(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = RemoveProjectRequest.create(request);
client.removeProject(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
/**
* Get all intents for a given project
* @param request The request object
* @returns A promise that resolves with the response objects
*/
async listIntents(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = GetIntentsRequest.create(request);
const stream = client.getIntents(req);
const intents = [];
stream.on('data', (data) => {
intents.push(data);
});
stream.on('end', () => {
res(intents);
});
stream.on('error', (err) => {
rej(err);
});
});
}
/**
* This function allows to find out good thresholds by providing prompts that should match
* an intent and negative prompts that should not match an intent.
*
* @param request The request object
* @returns A promise that resolves with the response object
*/
async getScoreLimits(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = GetScoreLimitsRequest.create(request);
client.getScoreLimits(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
/**
* List all available embedding models on the server
* @param request An optional request object
* @returns A promise that resolves with an array of embedding models
*/
async listEmbeddingModels(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = GetEmbeddingModelsRequest.create(request);
const stream = client.getEmbeddingModels(req);
const models = [];
stream.on('data', (data) => {
models.push(data);
});
stream.on('end', () => {
res(models);
});
stream.on('error', (err) => {
rej(err);
});
});
}
/**
* Triggers a database synchronization on the server.
* @param request An optional request object
* @returns A promise that resolves with the synchronization response
*/
async syncDbs(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = SyncDBsRequest.create(request);
client.syncDBs(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
/**
* Get all keywords for a given project
* @param request The request object
* @returns A promise that resolves with the response objects
*/
async getKeywords(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = GetKeywordsRequest.create(request);
const stream = client.getKeywords(req);
const keywords = [];
stream.on('data', (data) => {
keywords.push(data);
});
stream.on('end', () => {
res(keywords);
});
stream.on('error', (err) => {
rej(err);
});
});
}
/**
* Get the content of a given id
* @param request The request object
* @returns A promise that resolves with the response objects
*/
async getContent(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = GetContentRequest.create(request);
const stream = client.getContent(req);
let response = [];
stream.on('data', (data) => {
response.push(data);
});
stream.on('end', () => {
res(response);
});
stream.on('error', (err) => {
rej(err);
});
});
}
/**
* Updates content inside the vector database
* @param request The request object
* @returns A promise that resolves with the response object
*/
async updateContent(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = UpdateContentRequest.create(request);
client.updateContent(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
/**
* Removes content from the vector database
* @param request The request object
* @returns A promise that resolves when the content was removed
*/
async removeContent(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = RemoveContentRequest.create(request);
client.removeContent(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
/**
* Adds documents to a given project.
* @param request The request object
* @return A promise that resolves with the response object
*/
async addDocuments(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = AddDocumentsRequest.create(request);
client.addDocuments(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
/**
* Gets all documents for a given project.
* @param request The request object
* @return A promise that resolves with the response object
*/
async getDocuments(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = GetDocumentsRequest.create(request);
client.getDocuments(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
/**
* Processes spefified documents in a project.
* @param request The request object
* @return A promise that resolves with the response object
*/
async processDocuments(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = ProcessDocumentsRequest.create(request);
client.processDocuments(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
/**
* Updates documents in a project.
* @param request The request object
* @return A promise that resolves with the response object
*/
async updateDocuments(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = UpdateDocumentsRequest.create(request);
client.updateDocuments(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
/**
* Removes documents from a project.
* @param request The request object
* @return A promise that resolves with the response object
*/
async removeDocuments(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = RemoveDocumentsRequest.create(request);
client.removeDocuments(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
/**
* Gets the processing status of a document in a project.
* @param request The request object
* @return A promise that resolves with the response object
*/
async getDocumentStatus(request) {
return new Promise((res, rej) => {
const client = this.getClient();
const req = GetDocumentStatusRequest.create(request);
client.getDocumentStatus(req, (err, response) => {
if (err) {
rej(err);
return;
}
res(response);
});
});
}
getClient() {
const { rootCert: rootCertPath, rootCertContent, auth, grpcClientOptions, } = this.cOptions;
let host = this.cOptions.host || 'localhost:8523';
let ssl = this.cOptions.ssl === true;
let rootCert = null;
if (rootCertContent) {
rootCert = Buffer.from(rootCertContent);
}
else if (rootCertPath) {
rootCert = fs.readFileSync(rootCertPath);
}
const sslExplicit = typeof this.cOptions.ssl === 'boolean' || !!rootCert;
const portRe = /[^:]+:([0-9]+)$/;
if (portRe.test(host)) {
// In case a port was provided but ssl was not specified
// ssl is assumed when the port matches 8524
const [, portStr] = host.match(portRe);
const hostPort = parseInt(portStr, 10);
if (!sslExplicit) {
if (hostPort === 8524) {
ssl = true;
}
else {
ssl = false;
}
}
}
else {
// In case no port was provided, depending on the ssl settings
// at the default non ssl port 8523 or ssl port 8524
if (sslExplicit && ssl) {
host = `${host}:8524`;
}
else {
host = `${host}:8523`;
}
}
let creds = grpc.credentials.createInsecure();
if (ssl || rootCert) {
creds = grpc.credentials.createSsl(rootCert);
if (auth) {
const callCreds = grpc.credentials.createFromMetadataGenerator((_, cb) => {
const meta = new grpc.Metadata();
meta.add('token', auth.token);
meta.add('secret', auth.secret);
cb(null, meta);
});
creds = grpc.credentials.combineChannelCredentials(creds, callCreds);
}
}
return new NLPServerClient(host, creds, grpcClientOptions);
}
}