├── .gitignore ├── CODE_OF_CONDUCT.md ├── Dockerfile ├── LICENSE ├── README.md ├── SECURITY.md ├── SUPPORT.md ├── environment.yml └── streamlit_app ├── config.yml ├── images ├── MSFT.jpg ├── Microsoft_logo.png ├── Microsoft_logo.svg ├── bot.png └── openai.png ├── llm_bot.py ├── main.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Microsoft Open Source Code of Conduct 2 | 3 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). 4 | 5 | Resources: 6 | 7 | - [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/) 8 | - [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) 9 | - Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns 10 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mambaorg/micromamba:0.15.3 2 | USER root 3 | RUN apt-get update && DEBIAN_FRONTEND=“noninteractive” apt-get install -y --no-install-recommends \ 4 | nginx \ 5 | ca-certificates \ 6 | apache2-utils \ 7 | certbot \ 8 | python3-certbot-nginx \ 9 | sudo \ 10 | cifs-utils \ 11 | && \ 12 | rm -rf /var/lib/apt/lists/* 13 | RUN apt-get update && apt-get -y install cron 14 | RUN mkdir /opt/chatbot 15 | RUN chmod -R 777 /opt/chatbot 16 | WORKDIR /opt/chatbot 17 | USER micromamba 18 | EXPOSE 8000 19 | COPY environment.yml environment.yml 20 | RUN micromamba install -y -n base -f environment.yml && \ 21 | micromamba clean --all --yes 22 | COPY streamlit_app streamlit_app 23 | ENTRYPOINT ["streamlit", "run"] 24 | CMD ["./streamlit_app/main.py","--server.port","8000","--theme.base","dark"] 25 | 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Azure-streamlit-chatbot 2 | 3 | --- 4 | A sample demonstrating how to deploy a streamlit-chatbot (LLM-powered) User Interface in Azure for demo purposes. This sample is in python and uses [Streamlit](https://streamlit.io/) + [Azure Container Registry (ACR)](https://azure.microsoft.com/en-us/products/container-registry) + [Azure Web App](https://azure.microsoft.com/en-us/products/app-service/containers?activetab=pivot:deploytab) to deploy a chat interface. 5 | 6 | ![image](./streamlit_app/images/bot.png) 7 | 8 | 9 | ## Pre-requisites 10 | 11 | 1. Azure CLI install instructions [here](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) 12 | 2. Azure web app 13 | 3. Azure Container Registry 14 | 15 | ## Links 16 | 17 | - https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps 18 | - https://towardsdatascience.com/deploying-a-streamlit-web-app-with-azure-app-service-1f09a2159743 19 | 20 | ## Folder structure 21 | 22 | ```bash 23 | . 24 | ├── Dockerfile 25 | ├── README.md 26 | ├── environment.yml 27 | └── streamlit_app 28 | ├── config.yaml 29 | ├── images 30 | ├── llm_bot.py 31 | ├── main.py 32 | └── requirements.txt 33 | ``` 34 | 35 | 1. This sample can be run locally (without the need for containerization) or as a Docker container. To deploy to Azure, we build the container remotely using ACR and deploy using Web app. 36 | 2. The streamlit app is defined in a folder `streamlit_app/` 37 | 1. `main.py` is the main access point and contains the front-end 38 | 2. `llm_bot.py` contains the chatbot response logic. 39 | 3. `config.yml` containes all configurations such as title, logos etc. 40 | 3. `Dockerfile` and `environment.yml` define how the container should be built 41 | 42 | ## Usage 43 | 44 | The sample can be run locally OR deployed to Azure web app as a Docker container. 45 | 46 | 1. **To run the streamlit app locally:** 47 | 1. In a terminal `streamlit run ./streamlit_app/main.py --server.port 8000` 48 | 2. open a web browser at `localhost:8000` 49 | 2. **To deploy to Azure web app as a Docker container** 50 | 1. *Optional* if already setup: Create the ACR and web-app services. Note: any of the `, , etc. are arbitrarily defined` 51 | 1. Ensure you are creating [your services on the correct subscription](https://learn.microsoft.com/en-us/cli/azure/manage-azure-subscriptions-azure-cli) 52 | 1. `az account list --output table` to list all subscriptions available to you 53 | 2. `az account show --output table` to show which subscription is currently set to 54 | 3. `az account set --subscription "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"` to set a subsrciption using a subscription-id 55 | 2. Create a container registry ` az acr create --name --resource-group --sku basic --admin-enabled true` 56 | 3. Create a web app service plan `az appservice plan create --resource-group --name --location eastus --is-linux --sku B1` (note: B1 sku is needed for websockets - required by streamlit) 57 | 2. Build the docker container remotely (this will upload and build the container in your ACR service) `az acr build --registry --resource-group --image bot .` 58 | 3. Create a web app using the built container `az webapp create --resource-group --plan --name -i .azurecr.io/bot:latest` 59 | 4. Configure your web app to listen to port 8000 and set a longer container_start_time_limit `az webapp config appsettings set --resource-group --name --settings WEBSITES_PORT=8000 WEBSITES_CONTAINER_START_TIME_LIMIT=1800 ` 60 | 5. Open a browser at `.azurewebsites.net` (note: it may take a few minutes to load the first time the container is started) 61 | 62 | 3. **Optional: Build and run Docker container locally** (requires [docker installed locally](https://docs.docker.com/engine/install/)) 63 | 1. build the docker container `docker build -t bot:v1 .` 64 | 2. run the docker container locally `docker run --rm -p 8000:8000 bot:v1` 65 | 3. open a web browser and type `localhost:8000` 66 | 67 | ## Updating & troubleshooting: 68 | 69 | In order to redeploy the bot, all you need to do is rebuild a container on ACR, and recreate the web app (you can reuse the same webapp previously created) 70 | 71 | 1. Re-Build the docker container remotely (this will upload and build the container in your ACR service) `az acr build --registry --resource-group --image bot .` 72 | 2. Re-create a web app `az webapp create -g -p -n -i .azurecr.io/bot:latest ` 73 | 3. Container logs can be handy fro troubleshooting, link on how to access [these](https://stackoverflow.com/questions/52245077/where-can-i-find-docker-container-logs-for-azure-app-service) in the web app on portal.azure.com (look for `log stream on the left`) -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Security 4 | 5 | Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/). 6 | 7 | If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/opensource/security/definition), please report it to us as described below. 8 | 9 | ## Reporting Security Issues 10 | 11 | **Please do not report security vulnerabilities through public GitHub issues.** 12 | 13 | Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/opensource/security/create-report). 14 | 15 | If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/opensource/security/pgpkey). 16 | 17 | You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://aka.ms/opensource/security/msrc). 18 | 19 | Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue: 20 | 21 | * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.) 22 | * Full paths of source file(s) related to the manifestation of the issue 23 | * The location of the affected source code (tag/branch/commit or direct URL) 24 | * Any special configuration required to reproduce the issue 25 | * Step-by-step instructions to reproduce the issue 26 | * Proof-of-concept or exploit code (if possible) 27 | * Impact of the issue, including how an attacker might exploit the issue 28 | 29 | This information will help us triage your report more quickly. 30 | 31 | If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/opensource/security/bounty) page for more details about our active programs. 32 | 33 | ## Preferred Languages 34 | 35 | We prefer all communications to be in English. 36 | 37 | ## Policy 38 | 39 | Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/opensource/security/cvd). 40 | 41 | 42 | -------------------------------------------------------------------------------- /SUPPORT.md: -------------------------------------------------------------------------------- 1 | # TODO: The maintainer of this repo has not yet edited this file 2 | 3 | **REPO OWNER**: Do you want Customer Service & Support (CSS) support for this product/project? 4 | 5 | - **No CSS support:** Fill out this template with information about how to file issues and get help. 6 | - **Yes CSS support:** Fill out an intake form at [aka.ms/onboardsupport](https://aka.ms/onboardsupport). CSS will work with/help you to determine next steps. 7 | - **Not sure?** Fill out an intake as though the answer were "Yes". CSS will help you decide. 8 | 9 | *Then remove this first heading from this SUPPORT.MD file before publishing your repo.* 10 | 11 | # Support 12 | 13 | ## How to file issues and get help 14 | 15 | This project uses GitHub Issues to track bugs and feature requests. Please search the existing 16 | issues before filing new issues to avoid duplicates. For new issues, file your bug or 17 | feature request as a new Issue. 18 | 19 | For help and questions about using this project, please **REPO MAINTAINER: INSERT INSTRUCTIONS HERE 20 | FOR HOW TO ENGAGE REPO OWNERS OR COMMUNITY FOR HELP. COULD BE A STACK OVERFLOW TAG OR OTHER 21 | CHANNEL. WHERE WILL YOU HELP PEOPLE?**. 22 | 23 | ## Microsoft Support Policy 24 | 25 | Support for this **PROJECT or PRODUCT** is limited to the resources listed above. 26 | -------------------------------------------------------------------------------- /environment.yml: -------------------------------------------------------------------------------- 1 | name: demo_azure 2 | channels: 3 | - conda-forge 4 | - defaults 5 | dependencies: 6 | - python=3.9 7 | - pip 8 | - pip: 9 | - streamlit 10 | - requests 11 | - pyyaml -------------------------------------------------------------------------------- /streamlit_app/config.yml: -------------------------------------------------------------------------------- 1 | --- 2 | streamlit: 3 | title: "Applied AI team's bot" 4 | tab_title: "LLM bot" 5 | logo: "./streamlit_app/images/Microsoft_logo.png" 6 | page_icon: "./streamlit_app/images/Microsoft_logo.png" 7 | avatar: "./streamlit_app/images/openai.png" 8 | assistant_intro_message: "Hi there :wave:, I'm an AI assistant powered by Large Language Models. How can I help?" 9 | about: "this is a demo bot, author: Applied AI team" 10 | azure: 11 | dummy: "test" -------------------------------------------------------------------------------- /streamlit_app/images/MSFT.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/azure-streamlit-chatbot/256a138a6900e35af2656b5fb3487eacf94713dd/streamlit_app/images/MSFT.jpg -------------------------------------------------------------------------------- /streamlit_app/images/Microsoft_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/azure-streamlit-chatbot/256a138a6900e35af2656b5fb3487eacf94713dd/streamlit_app/images/Microsoft_logo.png -------------------------------------------------------------------------------- /streamlit_app/images/Microsoft_logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /streamlit_app/images/bot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/azure-streamlit-chatbot/256a138a6900e35af2656b5fb3487eacf94713dd/streamlit_app/images/bot.png -------------------------------------------------------------------------------- /streamlit_app/images/openai.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/azure-streamlit-chatbot/256a138a6900e35af2656b5fb3487eacf94713dd/streamlit_app/images/openai.png -------------------------------------------------------------------------------- /streamlit_app/llm_bot.py: -------------------------------------------------------------------------------- 1 | # Description: This file contains the logic for the LLM bot 2 | def dummy_bot(msg): 3 | return "Dummy bot: I'm a dummy bot" 4 | 5 | def echo_bot(msg): 6 | return 'Echo: {}'.format(msg) 7 | 8 | def simple_llm(msg): 9 | return 'Simple LLM: {}'.format(msg) -------------------------------------------------------------------------------- /streamlit_app/main.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | import yaml 3 | from llm_bot import dummy_bot, echo_bot #contains logic for bot's response 4 | 5 | # Read config yaml file 6 | with open('./streamlit_app/config.yml', 'r') as file: 7 | config = yaml.safe_load(file) 8 | #print(config) 9 | title = config['streamlit']['title'] 10 | avatar = { 11 | 'user': None, 12 | 'assistant': config['streamlit']['avatar'] 13 | } 14 | 15 | # Set page config 16 | st.set_page_config( 17 | page_title=config['streamlit']['tab_title'], 18 | page_icon=config['streamlit']['page_icon'], 19 | ) 20 | 21 | # Set sidebar 22 | st.sidebar.title("About") 23 | st.sidebar.info(config['streamlit']['about']) 24 | 25 | # Set logo 26 | st.image(config['streamlit']['logo'], width=50) 27 | 28 | # Set page title 29 | st.title(title) 30 | 31 | # Initialize chat history 32 | if "messages" not in st.session_state: 33 | st.session_state.messages = [] 34 | st.session_state.messages.append({ 35 | "role": "assistant", 36 | "content": config['streamlit']['assistant_intro_message'] 37 | }) 38 | 39 | # Display chat messages from history on app rerun 40 | for message in st.session_state.messages: 41 | with st.chat_message(message["role"], avatar=avatar[message["role"]]): 42 | st.markdown(message["content"]) 43 | 44 | # React to user input 45 | if prompt := st.chat_input("Send a message"): 46 | # Add user message to chat history 47 | st.session_state.messages.append({"role": "user", "content": prompt}) 48 | # Display user message in chat message container 49 | with st.chat_message("user"): 50 | st.markdown(prompt) 51 | # Get bot response 52 | response = echo_bot(prompt) 53 | with st.chat_message("assistant", avatar=config['streamlit']['avatar']): 54 | st.markdown(response) 55 | # Add assistant response to chat history 56 | st.session_state.messages.append({"role": "assistant", "content": response}) 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /streamlit_app/requirements.txt: -------------------------------------------------------------------------------- 1 | streamlit 2 | requests 3 | pyyaml --------------------------------------------------------------------------------