Build a custom NLU connector or integrate a custom NLU engine with your own JavaScript code.
The Code connector allows
you to integrate an NLU engine that isn’t natively supported by Cognigy,
or to build a highly customized integration with a supported NLU engine
that goes beyond the default connector’s capabilities.To use this connector, follow these steps:
Go to Advanced > NLU Connectors, click Create NLU connector, and select the Code connector type.
Add your JavaScript code to the JavaScript or TypeScript code section and activate the Enable Code Transformer toggle.
Code example with description
createNluCodeTransformer({ /** * This function connects to an external NLU service, for example, Dialogflow or LUIS, * and returns the processed result. * @param text The message text from the user. * @param data Any additional data passed with the message. * @param language The language of the input message. * @returns nluResult Processed result from the external NLU service. */ getNluResult: async ({ text, data, language }) => { // Example: Make an HTTP request to an external NLU API, for example, Dialogflow or LUIS. const response = await fetch('https://api.externalnlu.com/analyze', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ query: text, language: language, }), }); // Parse the response from the NLU API const nluResponse = await response.json(); // Construct the NLU result based on the response from the NLU service let nluResult = { intent: nluResponse.intent || null, // Extracted intent from the NLU response type: nluResponse.type || null, // The type of the Intent (optional) slots: nluResponse.entities || {}, // Extracted entities as Slots (parameters) intentScore: nluResponse.intentConfidence || 0, // Confidence score of the Intent nlu: { intentId: nluResponse.intentId || null, // Unique ID for the detected Intent intentFlow: nluResponse.intentFlow || null // Flow associated with the Intent (if available) } }; // Return the transformed data and NLU result return { data, nluResult }; }});
Description
Function and its parameters
createNluCodeTransformer
This function creates a transformer that asynchronously sends the message (text) to an external NLU service for analysis. The function processes the response from the service, populates the nluResult object with values such as intent, type, slots, and intentScore, and returns the data object unchanged, unless explicitly modified.
text
The message received from the Endpoint, such as user input, that needs to be analyzed by the NLU service.
data
The data object associated with the message, containing additional metadata or context about the request. This object can be modified or passed through unchanged.
language
The language code of the locale, representing the language in which the message is processed. For example, en for English, de for German.
NLU Result Initialization
Initial values set for the NLU result object to ensure structure before any processing occurs.
intent
Initially null, it will be populated with the intent detected from the message, for example, book_flight or greet_user, after processing.
type
Initially null, it refers to the type of intent, for example, action or informational. It’s set based on the NLU service’s response.
slots
Initially {}, this object holds the extracted parameters (slots) that are associated with the detected intent, for example, destination, date, and class.
intentScore
Initially 0, it represents the confidence score (ranging from 0 to 1) indicating how confident the NLU service is in the detected intent.
nlu
An object containing metadata about the intent:
intentId: initially null, the unique identifier for the detected intent.
intentFlow: initially null, the flow associated with the intent.
Return Object
The function returns the data and nluResult.
data
The unchanged data object passed in, which may contain context or additional information.
nluResult
The object returned after processing, containing the final intent, slots, intentScore, and any other details extracted from the message using the NLU logic.