@microsoft/teams-ai
Version:
SDK focused on building AI based applications for Microsoft Teams.
86 lines • 3.74 kB
JavaScript
;
/**
* @module teams-ai
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolsAugmentation = void 0;
/**
* The 'tools' augmentation is for enabling server-side action/tools calling.
* In the Teams AI Library, the equivalent to OpenAI's 'tools' functionality is called an 'action'.
* More information about OpenAI's tools can be found at [OpenAI API docs](https://platform.openai.com/docs/api-reference/chat/create#chat-create-tool_choice).
*
* Therefore, tools/actions are defined in `actions.json`, and when 'tools' augmentation is set in `config.json`, the LLM model can specify which action(s) to call.
* To avoid using server-side tool-calling, do not set augmentation to 'tools' in `config.json`.
* Server-side tool-calling is not compatible with other augmentation types.
*/
class ToolsAugmentation {
/**
* @returns {PromptSection|undefined} Returns an optional prompt section for the augmentation.
*/
createPromptSection() {
return undefined;
}
/**
* 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<string>} response - Response to validate.
* @param {number} remaining_attempts Number of remaining attempts to validate the response.
* @returns {Validation} A `Validation` object.
*/
validateResponse(context, memory, tokenizer, response, remaining_attempts) {
return Promise.resolve({
type: 'Validation',
valid: true
});
}
/**
* Creates a plan given validated response value.
* @param {TurnContext} context Context for the current turn of conversation.
* @param {Memory} memory An interface for accessing state variables.
* @param {PromptResponse<string | ActionCall[]>} response The validated and transformed response for the prompt.
* @returns {Promise<Plan>} The created plan.
*/
createPlanFromResponse(context, memory, response) {
const commands = [];
if (response.message && response.message.action_calls) {
const actionToolCalls = response.message.action_calls;
for (const toolCall of actionToolCalls) {
let parameters = {};
if (toolCall.function.arguments && toolCall.function.arguments.trim() !== '') {
try {
parameters = JSON.parse(toolCall.function.arguments);
}
catch (err) {
console.warn(`ToolsAugmentation: Error parsing tool arguments for ${toolCall.function.name}:`, err);
console.warn('Arguments:', toolCall.function.arguments);
}
}
const doCommand = {
type: 'DO',
action: toolCall.function.name,
actionId: toolCall.id,
parameters: parameters
};
commands.push(doCommand);
}
return Promise.resolve({ type: 'plan', commands });
}
return Promise.resolve({
type: 'plan',
commands: [
{
type: 'SAY',
response: response.message
}
]
});
}
}
exports.ToolsAugmentation = ToolsAugmentation;
//# sourceMappingURL=ToolsAugmentation.js.map