From 0ab9d9b529382b56aebf3c8f7247ab842c75d390 Mon Sep 17 00:00:00 2001 From: Jegadeesh V Date: Mon, 18 Nov 2024 16:17:22 +0530 Subject: [PATCH 1/2] [New Sample] Bot Streaming in python for Microsoft Teams --- samples/bot-streaming/python/.gitignore | 14 ++ .../python/.vscode/extensions.json | 6 + .../bot-streaming/python/.vscode/launch.json | 69 +++++++ .../python/.vscode/settings.json | 3 + .../bot-streaming/python/.vscode/tasks.json | 78 ++++++++ samples/bot-streaming/python/README.md | 141 ++++++++++++++ samples/bot-streaming/python/app.py | 94 ++++++++++ .../python/appManifest/icon-color.png | Bin 0 -> 3415 bytes .../python/appManifest/icon-outline.png | Bin 0 -> 407 bytes .../python/appManifest/manifest.json | 53 ++++++ .../bot-streaming/python/assets/sample.json | 68 +++++++ samples/bot-streaming/python/bots/__init__.py | 3 + .../python/bots/streaming_bot.py | 176 ++++++++++++++++++ samples/bot-streaming/python/config.py | 21 +++ .../bot-streaming/python/infra/azure.bicep | 42 +++++ .../python/infra/azure.parameters.json | 18 ++ .../bot-streaming/python/models/streaming.py | 15 ++ samples/bot-streaming/python/requirements.txt | 4 + .../python/resources/CardTemplate.json | 12 ++ .../bot-streaming/python/teamsapp.local.yml | 78 ++++++++ samples/bot-streaming/python/teamsapp.yml | 9 + 21 files changed, 904 insertions(+) create mode 100644 samples/bot-streaming/python/.gitignore create mode 100644 samples/bot-streaming/python/.vscode/extensions.json create mode 100644 samples/bot-streaming/python/.vscode/launch.json create mode 100644 samples/bot-streaming/python/.vscode/settings.json create mode 100644 samples/bot-streaming/python/.vscode/tasks.json create mode 100644 samples/bot-streaming/python/README.md create mode 100644 samples/bot-streaming/python/app.py create mode 100644 samples/bot-streaming/python/appManifest/icon-color.png create mode 100644 samples/bot-streaming/python/appManifest/icon-outline.png create mode 100644 samples/bot-streaming/python/appManifest/manifest.json create mode 100644 samples/bot-streaming/python/assets/sample.json create mode 100644 samples/bot-streaming/python/bots/__init__.py create mode 100644 samples/bot-streaming/python/bots/streaming_bot.py create mode 100644 samples/bot-streaming/python/config.py create mode 100644 samples/bot-streaming/python/infra/azure.bicep create mode 100644 samples/bot-streaming/python/infra/azure.parameters.json create mode 100644 samples/bot-streaming/python/models/streaming.py create mode 100644 samples/bot-streaming/python/requirements.txt create mode 100644 samples/bot-streaming/python/resources/CardTemplate.json create mode 100644 samples/bot-streaming/python/teamsapp.local.yml create mode 100644 samples/bot-streaming/python/teamsapp.yml diff --git a/samples/bot-streaming/python/.gitignore b/samples/bot-streaming/python/.gitignore new file mode 100644 index 0000000000..e8442994dd --- /dev/null +++ b/samples/bot-streaming/python/.gitignore @@ -0,0 +1,14 @@ +# TeamsFx files +env/.env.*.user +env/.env.local +appManifest/build/ + +# python virtual environment +.venv/ + +# misc +.env +.deployment/ + +# tmp files +__pycache__/ \ No newline at end of file diff --git a/samples/bot-streaming/python/.vscode/extensions.json b/samples/bot-streaming/python/.vscode/extensions.json new file mode 100644 index 0000000000..bf8c33db9c --- /dev/null +++ b/samples/bot-streaming/python/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "TeamsDevApp.ms-teams-vscode-extension", + "ms-python.python", + ] +} \ No newline at end of file diff --git a/samples/bot-streaming/python/.vscode/launch.json b/samples/bot-streaming/python/.vscode/launch.json new file mode 100644 index 0000000000..6d66d8beb8 --- /dev/null +++ b/samples/bot-streaming/python/.vscode/launch.json @@ -0,0 +1,69 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Launch App (Edge)", + "type": "msedge", + "request": "launch", + "url": "https://teams.microsoft.com/l/app/${{local:TEAMS_APP_ID}}?installAppPackage=true&webjoin=true&${account-hint}", + "cascadeTerminateToConfigurations": [ + "Python: Run App Locally" + ], + "presentation": { + "group": "all", + "hidden": true + }, + "internalConsoleOptions": "neverOpen" + }, + { + "name": "Launch App (Chrome)", + "type": "chrome", + "request": "launch", + "url": "https://teams.microsoft.com/l/app/${{local:TEAMS_APP_ID}}?installAppPackage=true&webjoin=true&${account-hint}", + "cascadeTerminateToConfigurations": [ + "Python: Run App Locally" + ], + "presentation": { + "group": "all", + "hidden": true + }, + "internalConsoleOptions": "neverOpen" + }, + { + "name": "Python: Run App Locally", + "type": "debugpy", + "request": "launch", + "program": "${workspaceFolder}/app.py", + "cwd": "${workspaceFolder}", + "console": "integratedTerminal" + } + ], + "compounds": [ + { + "name": "Debug (Edge)", + "configurations": [ + "Launch App (Edge)", + "Python: Run App Locally" + ], + "preLaunchTask": "Prepare Teams App Resources", + "presentation": { + "group": "all", + "order": 1 + }, + "stopAll": true + }, + { + "name": "Debug (Chrome)", + "configurations": [ + "Launch App (Chrome)", + "Python: Run App Locally" + ], + "preLaunchTask": "Prepare Teams App Resources", + "presentation": { + "group": "all", + "order": 2 + }, + "stopAll": true + } + ] +} \ No newline at end of file diff --git a/samples/bot-streaming/python/.vscode/settings.json b/samples/bot-streaming/python/.vscode/settings.json new file mode 100644 index 0000000000..3014fd9cf0 --- /dev/null +++ b/samples/bot-streaming/python/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "debug.onTaskErrors": "abort" +} diff --git a/samples/bot-streaming/python/.vscode/tasks.json b/samples/bot-streaming/python/.vscode/tasks.json new file mode 100644 index 0000000000..2161094dcc --- /dev/null +++ b/samples/bot-streaming/python/.vscode/tasks.json @@ -0,0 +1,78 @@ +// This file is automatically generated by Teams Toolkit. +// The teamsfx tasks defined in this file require Teams Toolkit version >= 5.0.0. +// See https://aka.ms/teamsfx-tasks for details on how to customize each task. +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Prepare Teams App Resources", + "dependsOn": [ + "Validate prerequisites", + "Start local tunnel", + "Provision", + "Deploy" + ], + "dependsOrder": "sequence" + }, + { + // Check all required prerequisites. + // See https://aka.ms/teamsfx-tasks/check-prerequisites to know the details and how to customize the args. + "label": "Validate prerequisites", + "type": "teamsfx", + "command": "debug-check-prerequisites", + "args": { + "prerequisites": [ + "m365Account", // Sign-in prompt for Microsoft 365 account, then validate if the account enables the sideloading permission. + "portOccupancy" // Validate available ports to ensure those debug ones are not occupied. + ], + "portOccupancy": [ + 3978, // app service port + ] + } + }, + { + // Start the local tunnel service to forward public URL to local port and inspect traffic. + // See https://aka.ms/teamsfx-tasks/local-tunnel for the detailed args definitions. + "label": "Start local tunnel", + "type": "teamsfx", + "command": "debug-start-local-tunnel", + "args": { + "type": "dev-tunnel", + "ports": [ + { + "portNumber": 3978, + "protocol": "http", + "access": "public", + "writeToEnvironmentFile": { + "endpoint": "BOT_ENDPOINT", // output tunnel endpoint as BOT_ENDPOINT + "domain": "BOT_DOMAIN" // output tunnel domain as BOT_DOMAIN + } + } + ], + "env": "local" + }, + "isBackground": true, + "problemMatcher": "$teamsfx-local-tunnel-watch" + }, + { + // Create the debug resources. + // See https://aka.ms/teamsfx-tasks/provision to know the details and how to customize the args. + "label": "Provision", + "type": "teamsfx", + "command": "provision", + "args": { + "env": "local" + } + }, + { + // Build project. + // See https://aka.ms/teamsfx-tasks/deploy to know the details and how to customize the args. + "label": "Deploy", + "type": "teamsfx", + "command": "deploy", + "args": { + "env": "local" + } + } + ] +} \ No newline at end of file diff --git a/samples/bot-streaming/python/README.md b/samples/bot-streaming/python/README.md new file mode 100644 index 0000000000..582a7238e6 --- /dev/null +++ b/samples/bot-streaming/python/README.md @@ -0,0 +1,141 @@ +--- +page_type: sample +description: This sample app can be use to streaming scenarios in Teams using Azure Open AI and Bot Framework v4 for personal scope. +products: +- office-teams +languages: +- python +extensions: + contentType: samples + createdDate: "18-11-2024 13:38:25" +urlFragment: officedev-microsoft-teams-samples-bot-streaming-python +--- + +# Teams Conversation Bot + +This bot has been created using [Bot Framework](https://dev.botframework.com) and [Azure Open AI](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource?pivots=web-portal) as a secondary/alternative option to using [Teams AI SDK](https://github.com/microsoft/teams-ai/tree/main/js/samples/04.ai-apps/i.teamsChefBot-streaming). + +Its main purpose is to demonstrate how to build a bot connected to an LLM and send messages through Teams. + +## Included Features +* Bots +* Adaptive Cards +* Streaming + +## Interaction with bot +![BotConversation](Images/BotConversation.gif) + +## Try it yourself - experience the App in your Microsoft Teams client +Please find below demo manifest which is deployed on Microsoft Azure and you can try it yourself by uploading the app package (.zip file link below) to your teams and/or as a personal app. (Sideloading must be enabled for your tenant, [see steps here](https://docs.microsoft.com/microsoftteams/platform/concepts/build-and-test/prepare-your-o365-tenant#enable-custom-teams-apps-and-turn-on-custom-app-uploading)). + +**Teams Conversation Bot:** [Manifest](/samples/bot-conversation/csharp/demo-manifest/bot-conversation.zip) + +## Prerequisites + +- Microsoft Teams is installed and you have an account +- [Python SDK](https://www.python.org/downloads/) min version 3.6 +- [dev tunnel](https://learn.microsoft.com/en-us/azure/developer/dev-tunnels/get-started?tabs=windows) or [ngrok](https://ngrok.com/) latest version or equivalent tunnelling solution + + +## Run the app (Using Teams Toolkit for Visual Studio Code) + +The simplest way to run this sample in Teams is to use Teams Toolkit for Visual Studio Code. + +1. Ensure you have downloaded and installed [Visual Studio Code](https://code.visualstudio.com/docs/setup/setup-overview) +1. Install the [Teams Toolkit extension](https://marketplace.visualstudio.com/items?itemName=TeamsDevApp.ms-teams-vscode-extension) and [Python Extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) +1. Select **File > Open Folder** in VS Code and choose this samples directory from the repo +1. Press **CTRL+Shift+P** to open the command box and enter **Python: Create Environment** to create and activate your desired virtual environment. Remember to select `requirements.txt` as dependencies to install when creating the virtual environment. +1. Using the extension, sign in with your Microsoft 365 account where you have permissions to upload custom apps +1. Select **Debug > Start Debugging** or **F5** to run the app in a Teams web client. +1. In the browser that launches, select the **Add** button to install the app to Teams. + +> If you do not have permission to upload custom apps (sideloading), Teams Toolkit will recommend creating and using a Microsoft 365 Developer Program account - a free program to get your own dev environment sandbox that includes Teams. + +## Run the app (Manually Uploading to Teams) + +> Note these instructions are for running the sample on your local machine, the tunnelling solution is required because +the Teams service needs to call into the bot. + +1) Clone the repository + + ```bash + git clone https://github.com/OfficeDev/Microsoft-Teams-Samples.git + ``` + +2) Run ngrok - point to port 3978 + + ```bash + ngrok http 3978 --host-header="localhost:3978" + ``` + + Alternatively, you can also use the `dev tunnels`. Please follow [Create and host a dev tunnel](https://learn.microsoft.com/en-us/azure/developer/dev-tunnels/get-started?tabs=windows) and host the tunnel with anonymous user access command as shown below: + + ```bash + devtunnel host -p 3978 --allow-anonymous + ``` + +3) Create [Azure Bot resource resource](https://docs.microsoft.com/azure/bot-service/bot-service-quickstart-registration) in Azure + - Use the current `https` URL you were given by running the tunneling application. Append with the path `/api/messages` used by this sample + - Ensure that you've [enabled the Teams Channel](https://docs.microsoft.com/azure/bot-service/channel-connect-teams?view=azure-bot-service-4.0) + - __*If you don't have an Azure account*__ you can use this [Azure free account here](https://azure.microsoft.com/free/) + +4) In a terminal, go to `samples\bot-conversation` + +5) Activate your desired virtual environment + +6) Install dependencies by running ```pip install -r requirements.txt``` in the project folder. + +7) Update the `config.py` configuration for the bot to use the Microsoft App Id and App Password from the Bot Framework registration. (Note the App Password is referred to as the "client secret" in the azure portal and you can always create a new client secret anytime.) + +8) __*This step is specific to Teams.*__ + - **Edit** the `manifest.json` contained in the `appManifest` folder to replace your Microsoft App Id (that was created when you registered your bot earlier) *everywhere* you see the place holder string `${{AAD_APP_CLIENT_ID}}` and `${{TEAMS_APP_ID}}` (depending on the scenario the Microsoft App Id may occur multiple times in the `manifest.json`) + - **Zip** up the contents of the `appManifest` folder to create a `manifest.zip` + - **Upload** the `manifest.zip` to Teams (in the Apps view click "Upload a custom app") + +9) Run your bot with `python app.py` + +## Interacting with the bot + +You can interact with this bot by sending it a message, or selecting a command from the command list. The bot will respond to the following strings. + +1. **Show Welcome** + - **Result:** The bot will send the welcome card for you to interact with + - **Valid Scopes:** personal, group chat, team chat +2. **MentionMe** + - **Result:** The bot will respond to the message and mention the user + - **Valid Scopes:** personal, group chat, team chat +3. **MessageAllMembers** + - **Result:** The bot will send a 1-on-1 message to each member in the current conversation (aka on the conversation's roster). + - **Valid Scopes:** personal, group chat, team chat + +You can select an option from the command list by typing ```@TeamsConversationBot``` into the compose message area and ```What can I do?``` text above the compose area. + +## Running the sample + +The bot initialization message +![Message](Images/1.prompts.png) + +The bot will send the welcome card for you to interact with +![WelcomeCard](Images/2.welcome.png) + +The bot will respond to the message and mention the user +![MentionMe](Images/3.mention.png) + +The bot initialization message +![MessageAllMembers](Images/4.message-to-all.png) + +## Deploy the bot to Azure + +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. + +# Further reading + +- [Bot Framework Documentation](https://docs.botframework.com) +- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) +- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0) +- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0) +- [Messages in bot conversations](https://learn.microsoft.com/microsoftteams/platform/bots/how-to/conversations/conversation-messages?tabs=dotnet) +- [Azure Open AI Client Library Documentation](https://learn.microsoft.com/en-us/dotnet/api/overview/azure/ai.openai-readme?view=azure-dotnet) +- [Stream message through REST API](https://learn.microsoft.com/en-us/microsoftteams/platform/bots/streaming-ux?branch=pr-en-us-10850&tabs=csharp#stream-message-through-rest-api) + + \ No newline at end of file diff --git a/samples/bot-streaming/python/app.py b/samples/bot-streaming/python/app.py new file mode 100644 index 0000000000..4d4ecfe6b9 --- /dev/null +++ b/samples/bot-streaming/python/app.py @@ -0,0 +1,94 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import sys +import traceback +import uuid +from datetime import datetime +from http import HTTPStatus + +from aiohttp import web +from aiohttp.web import Request, Response, json_response +from botbuilder.core import ( + BotFrameworkAdapterSettings, + TurnContext, + BotFrameworkAdapter, +) +from botbuilder.core.integration import aiohttp_error_middleware +from botbuilder.schema import Activity, ActivityTypes + +from bots import TeamsConversationBot +from config import DefaultConfig + +CONFIG = DefaultConfig() + +# Create adapter. +# See https://aka.ms/about-bot-adapter to learn more about how bots work. +SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD) +ADAPTER = BotFrameworkAdapter(SETTINGS) + + +# Catch-all for errors. +async def on_error(context: TurnContext, error: Exception): + # This check writes out errors to console log .vs. app insights. + # NOTE: In production environment, you should consider logging this to Azure + # application insights. + print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) + traceback.print_exc() + + # Send a message to the user + await context.send_activity("The bot encountered an error or bug.") + await context.send_activity( + "To continue to run this bot, please fix the bot source code." + ) + # Send a trace activity if we're talking to the Bot Framework Emulator + if context.activity.channel_id == "emulator": + # Create a trace activity that contains the error object + trace_activity = Activity( + label="TurnError", + name="on_turn_error Trace", + timestamp=datetime.utcnow(), + type=ActivityTypes.trace, + value=f"{error}", + value_type="https://www.botframework.com/schemas/error", + ) + # Send a trace activity, which will be displayed in Bot Framework Emulator + await context.send_activity(trace_activity) + + +ADAPTER.on_turn_error = on_error + +# If the channel is the Emulator, and authentication is not in use, the AppId will be null. +# We generate a random AppId for this case only. This is not required for production, since +# the AppId will have a value. +APP_ID = SETTINGS.app_id if SETTINGS.app_id else uuid.uuid4() + +# Create the Bot +BOT = TeamsConversationBot(CONFIG) + + +# Listen for incoming requests on /api/messages. +async def messages(req: Request) -> Response: + # Main bot message handler. + if "application/json" in req.headers["Content-Type"]: + body = await req.json() + else: + return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE) + + activity = Activity().deserialize(body) + auth_header = req.headers["Authorization"] if "Authorization" in req.headers else "" + + response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn) + if response: + return json_response(data=response.body, status=response.status) + return Response(status=HTTPStatus.OK) + + +APP = web.Application(middlewares=[aiohttp_error_middleware]) +APP.router.add_post("/api/messages", messages) + +if __name__ == "__main__": + try: + web.run_app(APP, host="localhost", port=CONFIG.PORT) + except Exception as error: + raise error diff --git a/samples/bot-streaming/python/appManifest/icon-color.png b/samples/bot-streaming/python/appManifest/icon-color.png new file mode 100644 index 0000000000000000000000000000000000000000..b8cf81afbe2f5bafd8563920edfadb78b7b71be6 GIT binary patch literal 3415 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 Bp2+|J literal 0 HcmV?d00001 diff --git a/samples/bot-streaming/python/appManifest/manifest.json b/samples/bot-streaming/python/appManifest/manifest.json new file mode 100644 index 0000000000..809594b7e7 --- /dev/null +++ b/samples/bot-streaming/python/appManifest/manifest.json @@ -0,0 +1,53 @@ +{ + "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.14/MicrosoftTeams.schema.json", + "manifestVersion": "1.14", + "version": "1.0.0", + "id": "${{TEAMS_APP_ID}}", + "packageName": "com.teams.sample.teamsstreamingbot", + "developer": { + "name": "TeamsStreamingBot", + "websiteUrl": "https://www.microsoft.com", + "privacyUrl": "https://www.teams.com/privacy", + "termsOfUseUrl": "https://www.teams.com/termsofuser" + }, + "icons": { + "outline": "icon-outline.png", + "color": "icon-color.png" + }, + "name": { + "short": "Streaming Bot", + "full": "Streaming Bot - This sample showcases the conversational streaming token scenario." + }, + "description": { + "short": "This sample showcases the conversational streaming token scenario.", + "full": "This sample showcases the conversational streaming token scenario for teams bot in personal scope." + }, + "accentColor": "#FFFFFF", + "bots": [ + { + "botId": "${{AAD_APP_CLIENT_ID}}", + "scopes": [ + "personal" + ], + "isNotificationOnly": false, + "supportsCalling": false, + "supportsVideo": false, + "supportsFiles": false, + "commandLists": [ + { + "scopes": [ + "personal" + ], + "commands": [] + } + ] + } + ], + "permissions": [ + "identity", + "messageTeamMembers" + ], + "validDomains": [ + "${{BOT_DOMAIN}}" + ] +} \ No newline at end of file diff --git a/samples/bot-streaming/python/assets/sample.json b/samples/bot-streaming/python/assets/sample.json new file mode 100644 index 0000000000..4e200bb688 --- /dev/null +++ b/samples/bot-streaming/python/assets/sample.json @@ -0,0 +1,68 @@ +[ + { + "name": "officedev-microsoft-teams-samples-bot-streaming-python", + "source": "officeDev", + "title": "streaming Bot using Bot Framework v4", + "shortDescription": "This sample showcases the conversational streaming token scenario.", + "url": "https://github.com/OfficeDev/Microsoft-Teams-Samples/tree/main/samples/bot-streaming/python", + "longDescription": [ + "This sample showcases the conversational streaming token scenario for teams bot in personal scope." + ], + "creationDateTime": "2024-11-15", + "updateDateTime": "2024-11-15", + "products": [ + "Teams" + ], + "metadata": [ + { + "key": "TEAMS-SAMPLE-SOURCE", + "value": "OfficeDev" + }, + { + "key": "TEAMS-SERVER-LANGUAGE", + "value": "python" + }, + { + "key": "TEAMS-SERVER-PLATFORM", + "value": "python" + }, + { + "key": "TEAMS-FEATURES", + "value": "bot" + } + ], + "thumbnails": [ + { + "type": "image", + "order": 100, + "url": "https://raw.githubusercontent.com/OfficeDev/Microsoft-Teams-Samples/main/samples/bot-streaming/python/Images/bot-streaming.gif", + "alt": "Solution UX showing Streaming Bot using python" + } + ], + "authors": [ + { + "gitHubAccount": "OfficeDev", + "pictureUrl": "https://avatars.githubusercontent.com/u/6789362?s=200&v=4", + "name": "OfficeDev" + } + ], + "references": [ + { + "name": "Teams developer documentation", + "url": "https://aka.ms/TeamsPlatformDocs" + }, + { + "name": "Teams developer questions", + "url": "https://aka.ms/TeamsPlatformFeedback" + }, + { + "name": "Teams development videos from Microsoft", + "url": "https://aka.ms/sample-ref-teams-vids-from-microsoft" + }, + { + "name": "Teams development videos from the community", + "url": "https://aka.ms/community/videos/m365powerplatform" + } + ] + } +] \ No newline at end of file diff --git a/samples/bot-streaming/python/bots/__init__.py b/samples/bot-streaming/python/bots/__init__.py new file mode 100644 index 0000000000..5cd9b8e920 --- /dev/null +++ b/samples/bot-streaming/python/bots/__init__.py @@ -0,0 +1,3 @@ +from .streaming_bot import TeamsConversationBot + +__all__ = ["TeamsConversationBot"] \ No newline at end of file diff --git a/samples/bot-streaming/python/bots/streaming_bot.py b/samples/bot-streaming/python/bots/streaming_bot.py new file mode 100644 index 0000000000..3361abb9f2 --- /dev/null +++ b/samples/bot-streaming/python/bots/streaming_bot.py @@ -0,0 +1,176 @@ +import json +import os +import time +from botbuilder.core import TurnContext, MessageFactory +from botbuilder.core.teams import TeamsActivityHandler +from botbuilder.schema import Activity, ActivityTypes, Attachment, Entity +from openai import AzureOpenAI +from models.streaming import StreamType, ChannelData + +class TeamsConversationBot(TeamsActivityHandler): + def __init__(self, config): + # Initialize configuration settings + self._app_id = config.APP_ID + self._app_password = config.APP_PASSWORD + self._app_tenant_id = config.APP_TENANT_ID + self._endpoint = config.AZURE_OPENAI_ENDPOINT + self._key = config.AZURE_OPENAI_KEY + self._deployment = config.AZURE_OPENAI_DEPLOYMENT + + self._client = AzureOpenAI( + api_key=self._key, + api_version="2024-07-01-preview", + azure_endpoint=self._endpoint + ) + self.adaptive_card_template = "./Resources/CardTemplate.json" + + async def on_message_activity(self, turn_context: TurnContext): + user_input = turn_context.activity.text.strip().lower() + content_builder = [] + stream_sequence = 1 + rps = 1000 # Rate per second limit + start_time = time.time() + temperature=0.7 + frequency_penalty=0 + presence_penalty=0 + + try: + # Initial informative message + channel_data = ChannelData( + streamType=StreamType.INFORMATIVE.value, + streamSequence=stream_sequence + ) + stream_id = await self.build_and_send_streaming_activity( + turn_context, "Getting the information...", channel_data + ) + + # Prepare messages for the chat completion request + messages = [ + {"role": "system", "content": "You are an AI great at storytelling."}, + {"role": "user", "content": user_input}, + ] + + # Send request to chat client with streaming enabled + chat_response = self._client.chat.completions.create( + model=self._deployment, + messages=messages, + temperature=temperature, + frequency_penalty=frequency_penalty, + presence_penalty=presence_penalty, + stream=True # Set stream=True to get a streaming response + ) + + # Debug: Check response structure + print(f"Chat response started: {chat_response}") + + # Use a synchronous for loop to read the chunks from the response stream + for chunk in chat_response: + stream_sequence = stream_sequence + 1 + # Check if the chunk has valid choices + if len(chunk.choices) > 0: + choice_delta = chunk.choices[0].delta # Assuming one choice + delta_content = choice_delta.content + + # Handle the finish reason for the final chunk + if chunk.choices[0].finish_reason != None: + finish_reason = chunk.choices[0].finish_reason + if finish_reason: + channel_data = ChannelData( + streamType=StreamType.FINAL.value, + streamSequence=stream_sequence, + streamId=stream_id, + ) + await self.build_and_send_streaming_activity( + turn_context, "".join(content_builder), channel_data + ) + break + + # Append and send content incrementally + if delta_content: + content_builder.append(delta_content) + if content_builder and (time.time() - start_time > 1 / rps): + channel_data = ChannelData( + streamType=StreamType.STREAMING.value, + streamSequence=stream_sequence, + streamId=stream_id, + ) + await self.build_and_send_streaming_activity( + turn_context, "".join(content_builder), channel_data + ) + start_time = time.time() # Reset time + else: + # Handle case where 'choices' is missing or empty + print(f"Warning: 'choices' is empty or missing in chunk: {chunk}") + # Add logic to either retry or stop if empty chunks persist + + except Exception as ex: + await turn_context.send_activity(MessageFactory.text(str(ex))) + + + async def build_and_send_streaming_activity(self, turn_context: TurnContext, text: str, channel_data): + is_stream_final = channel_data.streamType == StreamType.FINAL.value + channel_data_dict = { + "streamId": channel_data.streamId, + "streamType": channel_data.streamType, + "streamSequence": channel_data.streamSequence + } + streaming_activity = Activity( + type=ActivityTypes.message if is_stream_final else ActivityTypes.typing, + id=channel_data.streamId, + channel_data=channel_data_dict + ) + + streaming_activity.entities = [{ + "type": "streaminfo", + "streamId": channel_data.streamId, + "streamType": channel_data.streamType, + "streamSequence": channel_data.streamSequence + }] + + if text: + streaming_activity.text = text + + # For the final stream, add an Adaptive Card attachment + if is_stream_final: + # Build the adaptive card + with open(self.adaptive_card_template) as template_file: + adaptive_card_template = json.load(template_file) + adaptive_card = adaptive_card_template.copy() + adaptive_card["body"][0]["text"] = text + attachment = Attachment( + content_type="application/vnd.microsoft.card.adaptive", + content=adaptive_card + ) + + streaming_activity.attachments = [attachment] + streaming_activity.text = "This is what I've got:" + + # Send the streaming activity + return await self.send_streaming_activity_async(turn_context, streaming_activity) + + + # Helper function to send streaming activity + async def send_streaming_activity_async(self, turn_context: TurnContext, streaming_activity): + try: + activity_dict = streaming_activity.__dict__ + print(json.dumps(activity_dict, indent=4)) + streaming_response = await turn_context.send_activity(streaming_activity) + return streaming_response.id + except Exception as ex: + error_message = f"Error while sending streaming activity: {str(ex)}" + await turn_context.send_activity(MessageFactory.text(error_message)) + raise Exception(error_message) + + async def on_installation_update(self, turn_context: TurnContext): + if turn_context.activity.conversation.conversation_type == "channel": + await turn_context.send_activity( + MessageFactory.text( + f"Welcome to the streaming bot. The streaming feature is not available for channels yet." + ) + ) + else: + await turn_context.send_activity( + MessageFactory.text( + "Welcome! You can ask a question, and I'll stream the response." + ) + ) \ No newline at end of file diff --git a/samples/bot-streaming/python/config.py b/samples/bot-streaming/python/config.py new file mode 100644 index 0000000000..71e022f78e --- /dev/null +++ b/samples/bot-streaming/python/config.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os + +""" Bot Configuration """ + + +class DefaultConfig: + """ Bot Configuration """ + + PORT = 3978 + APP_ID = os.environ.get("MicrosoftAppId", "<>") + APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "<>") + APP_TENANT_ID = os.getenv("MICROSOFT_APP_TENANT_ID", "") + + # Azure OpenAI settings + AZURE_OPENAI_ENDPOINT = os.getenv("AzureOpenAIEndpoint", "") + AZURE_OPENAI_KEY = os.getenv("AzureOpenAIKey", "") + AZURE_OPENAI_DEPLOYMENT = os.getenv("AzureOpenAIDeployment", "") diff --git a/samples/bot-streaming/python/infra/azure.bicep b/samples/bot-streaming/python/infra/azure.bicep new file mode 100644 index 0000000000..8734cb547b --- /dev/null +++ b/samples/bot-streaming/python/infra/azure.bicep @@ -0,0 +1,42 @@ +@maxLength(20) +@minLength(4) +@description('Used to generate names for all resources in this file') +param resourceBaseName string + +@description('Required when create Azure Bot service') +param botAadAppClientId string + +param botAppDomain string + +@maxLength(42) +param botDisplayName string + +param botServiceName string = resourceBaseName +param botServiceSku string = 'F0' + +// Register your web service as a bot with the Bot Framework +resource botService 'Microsoft.BotService/botServices@2021-03-01' = { + kind: 'azurebot' + location: 'global' + name: botServiceName + properties: { + displayName: botDisplayName + endpoint: 'https://${botAppDomain}/api/messages' + msaAppId: botAadAppClientId + msaAppType: 'MultiTenant' + msaAppTenantId: '' + } + sku: { + name: botServiceSku + } +} + +// Connect the bot service to Microsoft Teams +resource botServiceMsTeamsChannel 'Microsoft.BotService/botServices/channels@2021-03-01' = { + parent: botService + location: 'global' + name: 'MsTeamsChannel' + properties: { + channelName: 'MsTeamsChannel' + } +} diff --git a/samples/bot-streaming/python/infra/azure.parameters.json b/samples/bot-streaming/python/infra/azure.parameters.json new file mode 100644 index 0000000000..7474499bdb --- /dev/null +++ b/samples/bot-streaming/python/infra/azure.parameters.json @@ -0,0 +1,18 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "resourceBaseName": { + "value": "bot${{RESOURCE_SUFFIX}}" + }, + "botAadAppClientId": { + "value": "${{AAD_APP_CLIENT_ID}}" + }, + "botAppDomain": { + "value": "${{BOT_DOMAIN}}" + }, + "botDisplayName": { + "value": "TeamsConversationBot" + } + } + } \ No newline at end of file diff --git a/samples/bot-streaming/python/models/streaming.py b/samples/bot-streaming/python/models/streaming.py new file mode 100644 index 0000000000..5ce333ae4b --- /dev/null +++ b/samples/bot-streaming/python/models/streaming.py @@ -0,0 +1,15 @@ +from enum import Enum +from pydantic import BaseModel, Field +from typing import Optional + +# Enum for StreamType +class StreamType(str, Enum): + INFORMATIVE = "Informative" + STREAMING = "Streaming" + FINAL = "Final" + +# ChannelData model using Pydantic +class ChannelData(BaseModel): + streamId: Optional[str] = Field(default=None, alias="streamId") + streamType: Optional[str] = Field(default=None, alias="streamType") + streamSequence: Optional[int] = Field(default=None, alias="streamSequence") \ No newline at end of file diff --git a/samples/bot-streaming/python/requirements.txt b/samples/bot-streaming/python/requirements.txt new file mode 100644 index 0000000000..411fb23c76 --- /dev/null +++ b/samples/bot-streaming/python/requirements.txt @@ -0,0 +1,4 @@ +requests==2.31.0 +botbuilder-integration-aiohttp>=4.16.2 +openai +pydantic \ No newline at end of file diff --git a/samples/bot-streaming/python/resources/CardTemplate.json b/samples/bot-streaming/python/resources/CardTemplate.json new file mode 100644 index 0000000000..68d1e6c5e8 --- /dev/null +++ b/samples/bot-streaming/python/resources/CardTemplate.json @@ -0,0 +1,12 @@ +{ + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "type": "AdaptiveCard", + "version": "1.5", + "body": [ + { + "type": "TextBlock", + "wrap": true, + "text": "${finaltStreamText}" + } + ] +} \ No newline at end of file diff --git a/samples/bot-streaming/python/teamsapp.local.yml b/samples/bot-streaming/python/teamsapp.local.yml new file mode 100644 index 0000000000..53a5bf5219 --- /dev/null +++ b/samples/bot-streaming/python/teamsapp.local.yml @@ -0,0 +1,78 @@ +# yaml-language-server: $schema=https://aka.ms/teams-toolkit/v1.2/yaml.schema.json +# Visit https://aka.ms/teamsfx-v5.0-guide for details on this file +# Visit https://aka.ms/teamsfx-actions for details on actions +version: v1.2 + +additionalMetadata: + sampleTag: Microsoft-Teams-Samples:bot-conversation-python + +provision: + # Creates a new Azure Active Directory (AAD) app to authenticate users if the environment variable that stores clientId is empty + - uses: aadApp/create + with: + name: teamsConversationBot-aad # Note: when you run aadApp/update, the AAD app name will be updated based on the definition in manifest. If you don't want to change the name, make sure the name in AAD manifest is the same with the name defined here. + generateClientSecret: true # If the value is false, the action will not generate client secret for you + signInAudience: "AzureADMultipleOrgs" # Multitenant + writeToEnvironmentFile: # Write the information of created resources into environment file for the specified environment variable(s). + clientId: AAD_APP_CLIENT_ID + clientSecret: SECRET_AAD_APP_CLIENT_SECRET # Environment variable that starts with `SECRET_` will be stored to the .env.{envName}.user environment file + objectId: AAD_APP_OBJECT_ID + tenantId: AAD_APP_TENANT_ID + authority: AAD_APP_OAUTH_AUTHORITY + authorityHost: AAD_APP_OAUTH_AUTHORITY_HOST + + # Creates a Teams app + - uses: teamsApp/create + with: + # Teams app name + name: teamsConversationBot${{APP_NAME_SUFFIX}} + # Write the information of created resources into environment file for + # the specified environment variable(s). + writeToEnvironmentFile: + teamsAppId: TEAMS_APP_ID + + - uses: arm/deploy # Deploy given ARM templates parallelly. + with: + subscriptionId: ${{AZURE_SUBSCRIPTION_ID}} # The AZURE_SUBSCRIPTION_ID is a built-in environment variable. TeamsFx will ask you select one subscription if its value is empty. You're free to reference other environment varialbe here, but TeamsFx will not ask you to select subscription if it's empty in this case. + resourceGroupName: ${{AZURE_RESOURCE_GROUP_NAME}} # The AZURE_RESOURCE_GROUP_NAME is a built-in environment variable. TeamsFx will ask you to select or create one resource group if its value is empty. You're free to reference other environment varialbe here, but TeamsFx will not ask you to select or create resource grouop if it's empty in this case. + templates: + - path: ./infra/azure.bicep + parameters: ./infra/azure.parameters.json + deploymentName: Create-resources-for-bot + bicepCliVersion: v0.9.1 # Teams Toolkit will download this bicep CLI version from github for you, will use bicep CLI in PATH if you remove this config. + + # Validate using manifest schema + - uses: teamsApp/validateManifest + with: + # Path to manifest template + manifestPath: ./appManifest/manifest.json + + # Build Teams app package with latest env value + - uses: teamsApp/zipAppPackage + with: + # Path to manifest template + manifestPath: ./appManifest/manifest.json + outputZipPath: ./appManifest/build/appManifest.${{TEAMSFX_ENV}}.zip + outputJsonPath: ./appManifest/build/manifest.${{TEAMSFX_ENV}}.json + # Validate app package using validation rules + - uses: teamsApp/validateAppPackage + with: + # Relative path to this file. This is the path for built zip file. + appPackagePath: ./appManifest/build/appManifest.${{TEAMSFX_ENV}}.zip + + # Apply the Teams app manifest to an existing Teams app in + # Teams Developer Portal. + # Will use the app id in manifest file to determine which Teams app to update. + - uses: teamsApp/update + with: + # Relative path to this file. This is the path for built zip file. + appPackagePath: ./appManifest/build/appManifest.${{TEAMSFX_ENV}}.zip + +deploy: + # Generate runtime environment variables + - uses: file/createOrUpdateEnvironmentFile + with: + target: ./.env + envs: + MicrosoftAppId: ${{AAD_APP_CLIENT_ID}} + MicrosoftAppPassword: ${{SECRET_AAD_APP_CLIENT_SECRET}} \ No newline at end of file diff --git a/samples/bot-streaming/python/teamsapp.yml b/samples/bot-streaming/python/teamsapp.yml new file mode 100644 index 0000000000..991ae7b515 --- /dev/null +++ b/samples/bot-streaming/python/teamsapp.yml @@ -0,0 +1,9 @@ +# yaml-language-server: $schema=https://aka.ms/teams-toolkit/v1.2/yaml.schema.json +# Visit https://aka.ms/teamsfx-v5.0-guide for details on this file +# Visit https://aka.ms/teamsfx-actions for details on actions +version: v1.2 + +additionalMetadata: + sampleTag: Microsoft-Teams-Samples:bot-conversation-python + +environmentFolderPath: ./env From 7ada24b49f53f7db58661e8689d3acc78a1e497a Mon Sep 17 00:00:00 2001 From: Jegadeesh V Date: Mon, 18 Nov 2024 16:22:40 +0530 Subject: [PATCH 2/2] adding env file --- samples/bot-streaming/python/env/.env.local | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 samples/bot-streaming/python/env/.env.local diff --git a/samples/bot-streaming/python/env/.env.local b/samples/bot-streaming/python/env/.env.local new file mode 100644 index 0000000000..c636ced2a5 --- /dev/null +++ b/samples/bot-streaming/python/env/.env.local @@ -0,0 +1,19 @@ +# This file includes environment variables that can be committed to git. It's gitignored by default because it represents your local development environment. + +# Built-in environment variables +TEAMSFX_ENV=local + +# Generated during provision, you can also add your own variables. If you're adding a secret value, add SECRET_ prefix to the name so Teams Toolkit can handle them properly +BOT_ENDPOINT= +BOT_DOMAIN= +AAD_APP_CLIENT_ID= +AAD_APP_OBJECT_ID= +AAD_APP_TENANT_ID= +AAD_APP_OAUTH_AUTHORITY= +AAD_APP_OAUTH_AUTHORITY_HOST= +TEAMS_APP_ID= +TEAMS_APP_TENANT_ID= +RESOURCE_SUFFIX= +AZURE_SUBSCRIPTION_ID= +AZURE_RESOURCE_GROUP_NAME= +APP_NAME_SUFFIX=local \ No newline at end of file