-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathapp.ts
117 lines (102 loc) · 3.48 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {
OpenAIModel,
PromptManager,
ActionPlanner,
Application,
TurnState,
TeamsAdapter,
FeedbackLoopData
} from '@microsoft/teams-ai';
import { DefaultAzureCredential, getBearerTokenProvider } from '@azure/identity';
import { ConfigurationServiceClientCredentialFactory, MemoryStorage, TurnContext } from 'botbuilder';
import axios from 'axios';
import path from 'path';
import debug from 'debug';
const error = debug('azureopenai:app:error');
error.log = console.log.bind(console);
interface ConversationState {}
type ApplicationTurnState = TurnState<ConversationState>;
const adapter = new TeamsAdapter(
{},
new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.BOT_ID,
MicrosoftAppPassword: process.env.BOT_PASSWORD,
MicrosoftAppTenantId: process.env.BOT_TENANT_ID,
MicrosoftAppType: process.env.BOT_TYPE
})
);
// Create AI components
const model = new OpenAIModel({
// Azure OpenAI Support
azureDefaultDeployment: process.env.AZURE_OPENAI_DEPLOYMENT!,
azureEndpoint: process.env.AZURE_OPENAI_ENDPOINT!,
azureApiVersion: '2024-02-15-preview',
azureADTokenProvider: getBearerTokenProvider(
new DefaultAzureCredential(),
'https://cognitiveservices.azure.com/.default'
),
// Request logging
logRequests: true,
useSystemMessages: true,
requestConfig: {
headers: {
'x-ms-useragent': adapter.userAgent
}
}
});
const prompts = new PromptManager({
promptsFolder: path.join(__dirname, '../src/prompts')
});
const planner = new ActionPlanner({
model,
prompts,
defaultPrompt: 'chat'
});
// Define storage and application
const storage = new MemoryStorage();
export const app = new Application<ApplicationTurnState>({
adapter,
storage,
ai: {
planner: planner,
enable_feedback_loop: true
}
});
app.conversationUpdate('membersAdded', async (context: TurnContext) => {
await context.sendActivity(
"Welcome! I'm a conversational bot that can tell you about your data. You can also type `/clear` to clear the conversation history."
);
});
app.message('/clear', async (context: TurnContext, state: TurnState) => {
state.deleteConversationState();
await context.sendActivity("New chat session started: Previous messages won't be used as context for new queries.");
});
app.error(async (context: TurnContext, err: any) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights.
error(`[onTurnError] unhandled error: ${err}`);
if (err instanceof axios.AxiosError) {
error(err.toJSON());
error(err.response?.data);
} else {
error(err);
}
// Send a trace activity, which will be displayed in Bot Framework Emulator
await context.sendTraceActivity(
'OnTurnError Trace',
`${err}`,
'https://www.botframework.com/schemas/error',
'TurnError'
);
// Send a message to the user
await context.sendActivity('The bot encountered an error or bug.');
await context.sendActivity('To continue to run this bot, please fix the bot source code.');
});
app.feedbackLoop(async (_context: TurnContext, _state: TurnState, feedbackLoopData: FeedbackLoopData) => {
if (feedbackLoopData.actionValue.reaction === 'like') {
console.log('👍');
} else {
console.log('👎');
}
});