Skip to content

Commit

Permalink
Fix up ActionOutputMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
Corina Gum committed Aug 20, 2024
1 parent 3e42f82 commit 3348343
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion js/packages/teams-ai/src/AI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ export class AI<TState extends TurnState = TurnState> {
state.temp.inputFiles = [];

if (cmd.type === 'DO' && (cmd as PredictedDoCommand).actionId) {
state.deleteValue(state.temp.input);
state.deleteValue('temp.input');
} else {
state.temp.input = output;
}
Expand Down
23 changes: 15 additions & 8 deletions js/packages/teams-ai/src/prompts/ActionOutputMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ import { Message } from './Message';
import { PromptFunctions } from './PromptFunctions';
import { RenderedPromptSection } from './PromptSection';
import { PromptSectionBase } from './PromptSectionBase';
import { ActionCall } from '../types';

/**
* A section capable of rendering user input text and images as a user message.
*/
export class ActionOutputMessage extends PromptSectionBase {
private readonly _inputVariable: string;
private readonly _historyVariable: string;

/**
* Creates a new 'ActionOutputMessage' instance.
* @param {string} historyVariable Optional. Name of the variable containing the conversation history.
* @param {number} tokens Optional. Sizing strategy for this section. Defaults to `auto`.
* @param {string} inputVariable Optional. Name of the variable containing the user input text. Defaults to `input`.
*/
public constructor(tokens: number = -1, inputVariable = 'actionOutputs') {
public constructor(historyVariable = 'temp.history', tokens: number = -1) {
super(tokens, true, '\n', 'action: ');
this._inputVariable = inputVariable;
this._historyVariable = historyVariable;
}

/**
Expand All @@ -47,14 +48,20 @@ export class ActionOutputMessage extends PromptSectionBase {
tokenizer: Tokenizer,
maxTokens: number
): Promise<RenderedPromptSection<Message<any>[]>> {
const actionOutputs: Record<string, string> = memory.getValue(this._inputVariable) || {};
let actionOutputs: Record<string, string> = {};
let actionCalls: ActionCall[] = [];
const history: Message[] = memory.getValue(this._historyVariable) ?? [];
const messages: Message<string>[] = [];

for (const action in actionOutputs) {
if (history.length > 1) {
actionOutputs = memory.getValue('temp.actionOutputs');
actionCalls = history[history.length - 1].action_calls ?? [];
}
for (const actionCall of actionCalls) {
const message: Message<string> = {
role: 'tool',
content: actionOutputs[action],
action_call_id: action
content: actionOutputs[actionCall.id],
action_call_id: actionCall.id
};
messages.push(message);
}
Expand Down
4 changes: 3 additions & 1 deletion js/packages/teams-ai/src/prompts/PromptManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ export class PromptManager implements PromptFunctions {
sections.push(new UserMessage('{{$temp.input}}', this.options.max_input_tokens));
}
if (template.config.augmentation && template.config.augmentation.augmentation_type === 'tools') {
sections.push(new ActionOutputMessage(this.options.max_input_tokens));
const includeHistory: boolean = template.config.completion.include_history;
const historyVariable = includeHistory ? `conversation.${name}_history` : 'temp.history';
sections.push(new ActionOutputMessage(historyVariable));
}

// Create prompt
Expand Down

0 comments on commit 3348343

Please sign in to comment.