Skip to content

Commit

Permalink
update chef bot to accept user feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
blackchoey committed Jan 2, 2024
1 parent a71c391 commit 3be7254
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
32 changes: 29 additions & 3 deletions js/samples/04.ai.a.teamsChefBot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<ConversationState>;
interface ConversationState {
history: string[];
}
export type ApplicationTurnState = TurnState<ConversationState>;

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.');
Expand All @@ -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',

Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion js/samples/04.ai.a.teamsChefBot/src/responseFormatter.ts
Original file line number Diff line number Diff line change
@@ -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<ApplicationTurnState>): void {
app.ai.action<PredictedSayCommand>(AI.SayCommandActionName, async (context, state, data) => {
// Replace markdown code blocks with <pre> tags
let addTag = false;
Expand Down Expand Up @@ -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 '';
Expand Down

0 comments on commit 3be7254

Please sign in to comment.