- Introduction
- LLM Validation & Customization Handler Structure
- Writing LLM Validation & Customization Handler Functions
- Supported Events
A Large Language Model Validation & Customization Handler (LLMVCH) enables you to perform custom validations on the request and the response. In addition, it allows you to customize any bot message created by the invokeLLM
component before it is sent to the user. Finally, you can implement a handler method that fires when the user selects the standard Submit
action in a bot response generated by the invokeLLM
component.
The LLMVCH is deployed as part of a component service, and is configured against an LLM service under skill settings in the configuration tab.
The transformation handler exports two objects: the metadata
object that provides the name of the component and the eventHandlerType
(which should be set to LlmComponent
), and the handlers
object that contains the event handler functions.
module.exports = {
metadata: {
name: 'myLlmComponentHandler',
eventHandlerType: 'LlmComponent'
},
handlers: {
/**
* Handler to validate the request payload
* @param {ValidateRequestEvent} event
* @param {LlmComponentContext} context - LLM context, see https://oracle.github.io/bots-node-sdk/LlmComponentContext.html
* @returns {boolean} returns true when payload is valid
*/
validateRequestPayload: async (event, context) => {
if (context.getCurrentTurn() === 1 && context.isJsonValidationEnabled()) {
context.addJSONSchemaFormattingInstruction();
}
return true;
},
/**
* Handler to validate the response payload
* @param {ValidateResponseEvent} event
* @param {LlmComponentContext} context - LLM context, see https://oracle.github.io/bots-node-sdk/LlmComponentContext.html
* @returns {boolean} returns true when payload is valid
*/
validateResponsePayload: async (event, context) => {
let errors = event.allValidationErrors || [];
if (errors.length > 0) {
return context.handleInvalidResponse(errors);
}
return true;
},
/**
* Handler to change the candidate bot messages that will be sent to the user
* @param {ChangeBotMessagesLlmEvent} event
* @param {LlmComponentContext} context - LLM context, see https://oracle.github.io/bots-node-sdk/LlmComponentContext.html
* @returns {NonRawMessage[]} returns list of bot messages
*/
changeBotMessages: async (event, context) => {
return event.messages;
},
/**
* Handler that fires when the Submit action is executed by the user.
* Use this handler to add your custom logic to process the LLM response.
* @param {SubmitEvent} event
* @param {LlmComponentContext} context - LLM context, see https://oracle.github.io/bots-node-sdk/LlmComponentContext.html
*/
submit: async (event, context) => {
}
}
};
If needed, you can define the metadata
and handlers
members as functions rather than as an objects.
In TypeScript, the event handler class implements the LlmComponentHandler
interface. This interface requires both of the following methods:
- The
metadata
method that returns an object of typeLlmComponentHandlerMetadata
. - The
handlers
method that returns an object of typeLlmComponentHandlers
.
import { LlmComponentContext, LlmComponentHandler, LlmComponentHandlers, LlmComponentHandlerMetadata, ValidateRequestEvent, ValidateResponseEvent
, ChangeBotMessagesLlmEvent, SubmitEvent, NonRawMessage } from '@oracle/bots-node-sdk/typings/lib2';
export class MyComponentHandler implements LlmComponentHandler {
public metadata(): LlmComponentHandlerMetadata {
return {
name: 'myLlmComponentHandler',
eventHandlerType: 'LlmComponent'
};
}
public handlers(): LlmComponentHandlers {
return {
/**
* Handler to validate the request payload
* @param {ValidateRequestEvent} event
* @param {LlmComponentContext} context - LLM context, see https://oracle.github.io/bots-node-sdk/LlmComponentContext.html
* @returns {boolean} returns true when payload is valid
*/
validateRequestPayload: async (event: ValidateRequestEvent, context: LlmComponentContext): Promise<boolean> => {
if (context.getCurrentTurn() === 1 && context.isJsonValidationEnabled()) {
context.addJSONSchemaFormattingInstruction();
}
return true;
},
/**
* Handler to validate the response payload
* @param {ValidateResponseEvent} event
* @param {LlmComponentContext} context - LLM context, see https://oracle.github.io/bots-node-sdk/LlmComponentContext.html
* @returns {boolean} returns true when payload is valid
*/
validateResponsePayload: async (event: ValidateResponseEvent, context: LlmComponentContext): Promise<boolean> => {
let errors = event.allValidationErrors || [];
if (errors.length > 0) {
return context.handleInvalidResponse(errors);
}
return true;
},
/**
* Handler to change the candidate bot messages that will be sent to the user
* @param {ChangeBotMessagesLlmComponent} event
* @param {LlmComponentContext} context - LLM context, see https://oracle.github.io/bots-node-sdk/LlmComponentContext.html
* @returns {NonRawMessage[]} returns list of bot messages
*/
changeBotMessages: async (event: ChangeBotMessagesLlmEvent, context: LlmComponentContext): Promise<NonRawMessage[]> => {
return event.messages;
},
/**
* Handler that fires when the Submit action is executed by the user.
* Use this handler to add your custom logic to process the LLM response.
* @param {SubmitEvent} event
* @param {LlmComponentContext} context - LLM context, see https://oracle.github.io/bots-node-sdk/LlmComponentContext.html
*/
submit: async (event: SubmitEvent, context: LlmComponentContext): Promise<void> => {
}
};
}
}
The first argument of each event method is the event
object. The properties available in this object depend on the type of event.
See the list of supported entity events for information on which properties are available with which event.
The second argument of each event method is the context
object. This object references the LlmComponentContext that provides access to convenience methods you can use to create your event handler logic.
TIP: if you are using a JavaScript IDE like Visual Studio Code, you can get code insight and code completion support by defining the types used in the event handlers as follows in your JavaScript handler file:
const { LlmComponentContext, ValidateRequestEvent, ValidateResponseEvent , ChangeBotMessagesLlmEvent, SubmitEvent
, NonRawMessage } = require ('@oracle/bots-node-sdk/typings/lib2');
When using TypeScript, you will automatically get code completion support if your IDE supports it.
The table below lists the event methods that can be implemented:
Event | Description | Event Properties |
---|---|---|
validateRequestPayload |
A handler that can be used to validate the request body. |
|
validateResponsePayload |
A handler that can be used to validate the response body. |
|
changeBotMessages |
A handler that can be used to customize the bot response generated by the LLM state. |
|
submit |
Handler that fires when the Submit standard action is executed by the user. |
None |