From 3be7254254e926c8e6b97a7461676d1f0495e874 Mon Sep 17 00:00:00 2001 From: Chaoyi Yuan Date: Tue, 2 Jan 2024 10:36:56 +0800 Subject: [PATCH] update chef bot to accept user feedback --- js/samples/04.ai.a.teamsChefBot/src/index.ts | 32 +++++++++++++++++-- .../src/responseFormatter.ts | 9 +++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/js/samples/04.ai.a.teamsChefBot/src/index.ts b/js/samples/04.ai.a.teamsChefBot/src/index.ts index f58301027..2b01e4943 100644 --- a/js/samples/04.ai.a.teamsChefBot/src/index.ts +++ b/js/samples/04.ai.a.teamsChefBot/src/index.ts @@ -5,6 +5,7 @@ import { config } from 'dotenv'; import * as path from 'path'; import * as restify from 'restify'; +import * as fs from 'fs'; // Import required bot services. // See https://aka.ms/bot-services to learn more about the different parts of a bot. @@ -72,8 +73,10 @@ import { addResponseFormatter } from './responseFormatter'; import { VectraDataSource } from './VectraDataSource'; // eslint-disable-next-line @typescript-eslint/no-empty-interface -interface ConversationState {} -type ApplicationTurnState = TurnState; +interface ConversationState { + history: string[]; +} +export type ApplicationTurnState = TurnState; if (!process.env.OPENAI_KEY && !process.env.AZURE_OPENAI_KEY) { throw new Error('Missing environment variables - please check that OPENAI_KEY or AZURE_OPENAI_KEY is set.'); @@ -87,7 +90,7 @@ const model = new OpenAIModel({ // Azure OpenAI Support azureApiKey: process.env.AZURE_OPENAI_KEY!, - azureDefaultDeployment: 'gpt-3.5-turbo', + azureDefaultDeployment: 'gpt-35-turbo', azureEndpoint: process.env.AZURE_OPENAI_ENDPOINT!, azureApiVersion: '2023-03-15-preview', @@ -138,6 +141,29 @@ app.ai.action(AI.FlaggedOutputActionName, async (context: TurnContext, state: Ap return AI.StopCommandName; }); +app.turn("beforeTurn", async (context: TurnContext, state: ApplicationTurnState) => { + if (state.conversation.history === undefined) { + state.conversation.history = []; + } + if (context.activity.type === "message") { + if (state.conversation.history.length > 10) { + state.conversation.history.shift(); // Remove oldest message + } + state.conversation.history.push(context.activity.text); + } + return true; +}); + +app.messageReactions("reactionsAdded", async (context: TurnContext, state: ApplicationTurnState) => { + if (context.activity.reactionsAdded!.filter((reaction) => reaction.type === "no").length > 0) + { + const message = `Following user input does not satisfy user's requirement ${state.conversation.history[state.conversation.history.length -2]}. The response is ${state.conversation.history[state.conversation.history.length -1]}.`; + fs.appendFile('feedback.txt', message + '\n', (err) => { + if (err) throw err; + }); + } +}); + // Listen for incoming server requests. server.post('/api/messages', async (req, res) => { // Route received a request to adapter for processing diff --git a/js/samples/04.ai.a.teamsChefBot/src/responseFormatter.ts b/js/samples/04.ai.a.teamsChefBot/src/responseFormatter.ts index 4bba60b27..cefd1847e 100644 --- a/js/samples/04.ai.a.teamsChefBot/src/responseFormatter.ts +++ b/js/samples/04.ai.a.teamsChefBot/src/responseFormatter.ts @@ -1,10 +1,11 @@ import { Application, AI, PredictedSayCommand } from '@microsoft/teams-ai'; +import {ApplicationTurnState} from './index'; /** * * @param app */ -export function addResponseFormatter(app: Application): void { +export function addResponseFormatter(app: Application): void { app.ai.action(AI.SayCommandActionName, async (context, state, data) => { // Replace markdown code blocks with
 tags
         let addTag = false;
@@ -33,6 +34,12 @@ export function addResponseFormatter(app: Application): void {
 
         // Send response
         const formattedResponse = output.join('\n');
+        if (context.activity.type === "message") {
+            if (state.conversation.history.length > 10) {
+                state.conversation.history.shift(); // Remove oldest message
+            }
+            state.conversation.history.push(formattedResponse);
+        }
         await context.sendActivity(formattedResponse);
 
         return '';