Skip to content

Commit 215cc5e

Browse files
committed
Starter sample
1 parent 0024468 commit 215cc5e

17 files changed

+5919
-2
lines changed

.eslintrc.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-disable */
2+
module.exports = {
3+
"extends": "standard",
4+
"rules": {
5+
"semi": [2, "always"],
6+
"indent": [2, 4],
7+
"no-return-await": 0,
8+
"space-before-function-paren": [2, {
9+
"named": "never",
10+
"anonymous": "never",
11+
"asyncArrow": "always"
12+
}],
13+
"template-curly-spacing": [2, "always"]
14+
}
15+
};

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
lib
3+
.env

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,62 @@
1-
# teams-chatgpt-js
2-
Sample bot to demonstrate how to integrate with the new gpt-3.5 turbo model that power ChatGPT
1+
# teams-chatgpt-js
2+
3+
Sample bot to demonstrate how to integrate with the new gpt-3.5 turbo model that power ChatGPT
4+
5+
This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and runs it via the newly released [gpt-3.5-turbo model](https://platform.openai.com/docs/guides/chat/introduction) that powers ChatGPT to respond to the user.
6+
7+
## Prerequisites
8+
9+
- [Node.js](https://nodejs.org) version 10.14.1 or higher
10+
11+
```bash
12+
# determine node version
13+
node --version
14+
```
15+
16+
## To run the bot
17+
18+
- Install modules
19+
20+
```bash
21+
npm install
22+
```
23+
24+
- Start the bot
25+
26+
```bash
27+
npm start
28+
```
29+
30+
## Testing the bot using Bot Framework Emulator
31+
32+
[Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel.
33+
34+
- Install the Bot Framework Emulator version 4.9.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases)
35+
36+
### Connect to the bot using Bot Framework Emulator
37+
38+
- Launch Bot Framework Emulator
39+
- File -> Open Bot
40+
- Enter a Bot URL of `http://localhost:3978/api/messages`
41+
42+
## Deploy the bot to Azure
43+
44+
To learn more about deploying a bot to Azure, see [Deploy your bot to Azure](https://aka.ms/azuredeployment) for a complete list of deployment instructions.
45+
46+
47+
## Further reading
48+
49+
- [OpenAI Chat Completion](https://platform.openai.com/docs/guides/chat/introduction)
50+
- [Bot Framework Documentation](https://docs.botframework.com)
51+
- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0)
52+
- [Dialogs](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-dialog?view=azure-bot-service-4.0)
53+
- [Gathering Input Using Prompts](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-prompts?view=azure-bot-service-4.0)
54+
- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0)
55+
- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
56+
- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0)
57+
- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest)
58+
- [Azure Portal](https://portal.azure.com)
59+
- [Language Understanding using LUIS](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/)
60+
- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0)
61+
- [Restify](https://www.npmjs.com/package/restify)
62+
- [dotenv](https://www.npmjs.com/package/dotenv)

bot.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const { ActivityHandler, MessageFactory } = require('botbuilder');
2+
const { Configuration, OpenAIApi } = require('openai');
3+
4+
class EchoBot extends ActivityHandler {
5+
6+
constructor() {
7+
8+
super();
9+
10+
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
11+
this.onMessage(async (context, next) => {
12+
13+
// Create an OpenAI API client.
14+
const configuration = new Configuration({
15+
apiKey: process.env.OPENAI_API_KEY,
16+
});
17+
const openai = new OpenAIApi(configuration);
18+
19+
// Start out by providing a system message to guide the conversation.
20+
// You can tweak your agent's personality by changing this message.
21+
if (! this.messages) {
22+
this.messages = [
23+
{"role": "system", "content": "You are a helpful assistant."},
24+
];
25+
}
26+
27+
// Add the user's message to the conversation.
28+
this.messages.push({"role": "user", "content": context.activity.text});
29+
30+
// Call OpenAI to get a response.
31+
const response = await openai.createChatCompletion({
32+
"model": "gpt-3.5-turbo",
33+
"messages": this.messages,
34+
})
35+
36+
// Add the bot's response to the conversation so we can provide it in the next request.
37+
const botMessage = response.data.choices[0].message;
38+
this.messages.push(botMessage);
39+
40+
// Only keep the last 10 messages in the conversation to avoid running over prompt limit.
41+
// Note: this is only good for demo. A more complete implementation would be to summarize
42+
// the conversation while staying within the token limit.
43+
// See https://platform.openai.com/docs/guides/chat/managing-tokens
44+
if (this.messages.length > 10) {
45+
this.messages.shift();
46+
}
47+
48+
// Send the bot's response to the user.
49+
const replyText = botMessage.content;
50+
await context.sendActivity(MessageFactory.text(replyText, replyText));
51+
52+
// By calling next() you ensure that the next BotHandler is run.
53+
await next();
54+
});
55+
56+
this.onMembersAdded(async (context, next) => {
57+
const membersAdded = context.activity.membersAdded;
58+
const welcomeText = 'Hello and welcome!';
59+
for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
60+
if (membersAdded[cnt].id !== context.activity.recipient.id) {
61+
await context.sendActivity(MessageFactory.text(welcomeText, welcomeText));
62+
}
63+
}
64+
// By calling next() you ensure that the next BotHandler is run.
65+
await next();
66+
});
67+
}
68+
}
69+
70+
module.exports.EchoBot = EchoBot;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"azureBotId": {
6+
"value": ""
7+
},
8+
"azureBotSku": {
9+
"value": "S1"
10+
},
11+
"azureBotRegion": {
12+
"value": "global"
13+
},
14+
"botEndpoint": {
15+
"value": ""
16+
},
17+
"appType": {
18+
"value": "MultiTenant"
19+
},
20+
"appId": {
21+
"value": ""
22+
},
23+
"UMSIName": {
24+
"value": ""
25+
},
26+
"UMSIResourceGroupName": {
27+
"value": ""
28+
},
29+
"tenantId": {
30+
"value": ""
31+
}
32+
}
33+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"appServiceName": {
6+
"value": ""
7+
},
8+
"existingAppServicePlanName": {
9+
"value": ""
10+
},
11+
"existingAppServicePlanLocation": {
12+
"value": ""
13+
},
14+
"newAppServicePlanName": {
15+
"value": ""
16+
},
17+
"newAppServicePlanLocation": {
18+
"value": ""
19+
},
20+
"newAppServicePlanSku": {
21+
"value": {
22+
"name": "S1",
23+
"tier": "Standard",
24+
"size": "S1",
25+
"family": "S",
26+
"capacity": 1
27+
}
28+
},
29+
"appType": {
30+
"value": "MultiTenant"
31+
},
32+
"appId": {
33+
"value": ""
34+
},
35+
"appSecret": {
36+
"value": ""
37+
},
38+
"UMSIName": {
39+
"value": ""
40+
},
41+
"UMSIResourceGroupName": {
42+
"value": ""
43+
},
44+
"tenantId": {
45+
"value": ""
46+
}
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Usage
2+
The BotApp must be deployed prior to AzureBot.
3+
4+
Command line:
5+
- az login
6+
- az deployment group create --resource-group <group-name> --template-file <template-file> --parameters @<parameters-file>
7+
8+
# parameters-for-template-BotApp-with-rg:
9+
10+
- **appServiceName**:(required) The Name of the Bot App Service.
11+
12+
- (choose an existingAppServicePlan or create a new AppServicePlan)
13+
- **existingAppServicePlanName**: The name of the App Service Plan.
14+
- **existingAppServicePlanLocation**: The location of the App Service Plan.
15+
- **newAppServicePlanName**: The name of the App Service Plan.
16+
- **newAppServicePlanLocation**: The location of the App Service Plan.
17+
- **newAppServicePlanSku**: The SKU of the App Service Plan. Defaults to Standard values.
18+
19+
- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.**
20+
21+
- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings.
22+
23+
- **appSecret**:(required for MultiTenant and SingleTenant) Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings.
24+
25+
- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication.
26+
27+
- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication.
28+
29+
- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to <Subscription Tenant ID>.
30+
31+
MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource
32+
33+
34+
35+
# parameters-for-template-AzureBot-with-rg:
36+
37+
- **azureBotId**:(required) The globally unique and immutable bot ID.
38+
- **azureBotSku**: The pricing tier of the Bot Service Registration. **Allowed values are: F0, S1(default)**.
39+
- **azureBotRegion**: Specifies the location of the new AzureBot. **Allowed values are: global(default), westeurope**.
40+
- **botEndpoint**: Use to handle client messages, Such as https://<botappServiceName>.azurewebsites.net/api/messages.
41+
42+
- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.**
43+
- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings.
44+
- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication.
45+
- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication.
46+
- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to <Subscription Tenant ID>.
47+
48+
MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource

0 commit comments

Comments
 (0)