UNPKG

@microsoft/teams-ai

Version:

SDK focused on building AI based applications for Microsoft Teams.

105 lines 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionResponseValidator = void 0; const JSONResponseValidator_1 = require("./JSONResponseValidator"); /** * Validates action calls returned by the model. */ class ActionResponseValidator { _actions = new Map(); _isRequired; _noun; _Noun; /** * Creates a new `ActionResponseValidator` instance. * @param {ChatCompletionAction[]} actions List of supported actions. * @param {boolean} isRequired Whether the response is required to call an action. * @param {string} noun Optional. Name of the action to use in feedback messages. Defaults to `action`. * @param {string} Noun Optional. Name of the action to use in feedback messages. Defaults to `Action`. */ constructor(actions, isRequired, noun = 'action', Noun = 'Action') { for (const action of actions) { this._actions.set(action.name, action); } this._isRequired = isRequired; this._noun = noun; this._Noun = Noun; } /** * Gets a list of the actions configured for the validator. * @returns {ChatCompletionAction[]} A list of the actions configured for the validator. */ get actions() { const list = []; this._actions.forEach((fn) => list.push(fn)); return list; } /** * Validates a response to a prompt. * @param {TurnContext} context Context for the current turn of conversation with the user. * @param {Memory} memory An interface for accessing state values. * @param {Tokenizer} tokenizer Tokenizer to use for encoding and decoding text. * @param {PromptResponse} response Response to validate. * @param {number} remaining_attempts Number of remaining attempts to validate the response. * @returns {Promise<Validation<ValidatedChatCompletionAction>>} A `Validation` object. */ async validateResponse(context, memory, tokenizer, response, remaining_attempts) { if (typeof response.message == 'object' && response.message.function_call) { // Ensure name is specified const function_call = response.message.function_call; if (!function_call.name) { return { type: 'Validation', valid: false, feedback: `${this._Noun} name missing. Specify a valid ${this._noun} name.` }; } // Ensure name valid if (!this._actions.has(function_call.name)) { return { type: 'Validation', valid: false, feedback: `Unknown ${this._noun} named "${function_call.name}". Specify a valid ${this._noun} name.` }; } // Validate arguments let parameters = {}; const action = this._actions.get(function_call.name); if (action.parameters) { const validator = new JSONResponseValidator_1.JSONResponseValidator(action.parameters, `No arguments were sent with called ${this._noun}. Call the "${function_call.name}" ${this._noun} with required arguments as a valid JSON object.`, `The ${this._noun} arguments had errors. Apply these fixes and call "${function_call.name}" ${this._noun} again:`); const args = function_call.arguments === '{}' ? undefined : (function_call.arguments ?? '{}'); const message = { role: 'assistant', content: args }; const result = await validator.validateResponse(context, memory, tokenizer, { status: 'success', message }, remaining_attempts); if (!result.valid) { return result; } else { parameters = result.value; } } // Return the validated action return { type: 'Validation', valid: true, value: { name: function_call.name, parameters: parameters } }; } else if (this._isRequired) { return { type: 'Validation', valid: false, feedback: `No ${this._noun} was specified. Call a ${this._noun} with valid arguments.` }; } // No action was called return { type: 'Validation', valid: true }; } } exports.ActionResponseValidator = ActionResponseValidator; //# sourceMappingURL=ActionResponseValidator.js.map