@microsoft/teams-ai
Version:
SDK focused on building AI based applications for Microsoft Teams.
84 lines • 3.29 kB
JavaScript
;
/**
* @module teams-ai
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActionAugmentationSection = void 0;
const yaml_1 = require("yaml");
const prompts_1 = require("../prompts");
/**
* A prompt section that renders a list of actions to the prompt.
*/
class ActionAugmentationSection extends prompts_1.PromptSectionBase {
_text;
_tokens;
_actions = new Map();
/**
* Creates a new `ActionAugmentationSection` instance.
* @param {ChatCompletionAction[]} actions List of actions to render.
* @param {string} callToAction Text to display after the list of actions.
*/
constructor(actions, callToAction) {
super(-1, true, '\n\n');
// Convert actions to an ActionList
const actionList = { actions: {} };
for (let i = 0; i < actions.length; i++) {
const action = actions[i];
this._actions.set(action.name, action);
actionList.actions[action.name] = {};
if (action.description) {
actionList.actions[action.name].description = action.description;
}
if (action.parameters) {
const params = action.parameters;
actionList.actions[action.name].parameters =
params.type == 'object' && !params.additionalProperties ? params.properties : params;
}
}
// Build augmentation text
this._text = `${(0, yaml_1.stringify)(actionList)}\n\n${callToAction}`;
}
/**
* @returns {Map<string, ChatCompletionAction>} Map of action names to actions.
*/
get actions() {
return this._actions;
}
/**
* Renders the prompt section as a list of `Message` objects.
* @param {TurnContext} context - Context for the current turn of conversation.
* @param {Memory} memory - Interface for accessing state variables.
* @param {PromptFunctions} functions - Functions for rendering prompts.
* @param {Tokenizer} tokenizer - Tokenizer to use for encoding/decoding text.
* @param {number} maxTokens - Maximum number of tokens allowed for the rendered prompt.
* @returns {Promise<RenderedPromptSection<Message<string>[]>>} The rendered prompt section.
*/
renderAsMessages(context, memory, functions, tokenizer, maxTokens) {
// Tokenize text on first use
if (!this._tokens) {
this._tokens = tokenizer.encode(this._text);
}
// Check for max tokens
if (this._tokens.length > maxTokens) {
const trimmed = this._tokens.slice(0, maxTokens);
return Promise.resolve({
output: [{ role: 'system', content: tokenizer.decode(trimmed) }],
length: trimmed.length,
tooLong: true
});
}
else {
return Promise.resolve({
output: [{ role: 'system', content: this._text }],
length: this._tokens.length,
tooLong: false
});
}
}
}
exports.ActionAugmentationSection = ActionAugmentationSection;
//# sourceMappingURL=ActionAugmentationSection.js.map