UNPKG

@aristech-org/nlp-client

Version:

A Node.js client library for the Aristech NLP Service

299 lines (298 loc) 10.3 kB
import * as grpc from '@grpc/grpc-js'; import { FunctionRequest, NLPServerClient, RunFunctionsRequest, } from './generated/nlp_server.js'; import { GetContentRequest, GetIntentsRequest, GetScoreLimitsRequest, RemoveContentRequest, UpdateContentRequest, } from './generated/intents.js'; import { AddProjectRequest, GetProjectsRequest, RemoveProjectRequest, UpdateProjectRequest, } from './generated/projects.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); }); }); } /** * Get the content of a given id * @param request The request object * @returns A promise that resolves with the response object */ async getContent(request) { return new Promise((res, rej) => { const client = this.getClient(); const req = GetContentRequest.create(request); const stream = client.getContent(req); let response = null; stream.on('data', (data) => { response = data; }); stream.on('end', () => { if (response !== null) { res(response); } else { rej(new Error('No response received')); } }); 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); }); }); } 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); } }