Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrating with Prompt Flow + PVA integration changes for Prompt Flow #3

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
870b0f8
Merge pull request #1 from samelhousseini/main
skaschimer May 2, 2023
a5d7a8d
Merge branch 'samelhousseini:main' into main
samelhousseini May 5, 2023
4a3366f
Merge branch 'samelhousseini:main' into main
samelhousseini May 5, 2023
7d44f63
Merge branch 'samelhousseini:main' into main
samelhousseini May 10, 2023
fa07e43
Merge branch 'samelhousseini:main' into main
samelhousseini May 10, 2023
e7017b4
Merge branch 'samelhousseini:main' into main
samelhousseini May 18, 2023
3b3ca41
Merge branch 'samelhousseini:main' into main
samelhousseini May 19, 2023
45c4d1a
Merge branch 'samelhousseini:main' into main
samelhousseini Jul 25, 2023
0486ad9
Merge branch 'samelhousseini:main' into main
samelhousseini Jul 25, 2023
485c192
Merge branch 'samelhousseini:main' into main
samelhousseini Aug 31, 2023
fd666a4
Merge branch 'samelhousseini:main' into main
samelhousseini Aug 31, 2023
f74d339
Merge branch 'samelhousseini:main' into main
samelhousseini Sep 4, 2023
d22108b
Merge branch 'samelhousseini:main' into main
samelhousseini Sep 4, 2023
9ca1055
Merge branch 'samelhousseini:main' into main
samelhousseini Sep 6, 2023
be2aada
Merge branch 'samelhousseini:main' into main
samelhousseini Sep 11, 2023
61e6541
Merge branch 'samelhousseini:main' into main
samelhousseini Sep 11, 2023
63da57f
Add new PDF for PromptFlow RESTful API + PVA
ofimbres Sep 19, 2023
c97dc5b
Replace company by Contoso
ofimbres Sep 19, 2023
958359a
Include PromptFlow Project
ofimbres Sep 19, 2023
b780692
Rename project temporally
ofimbres Sep 19, 2023
3efa6d3
Undo prompt flow renamed project
ofimbres Sep 19, 2023
b57683d
Replace company to a fictional company
ofimbres Sep 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions PVAbot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Build a Chatbot with PromptFlow endpoint + PVA

## What is PromptFlow?

Prompt flow is a powerful feature within Azure Machine Learning that streamlines the development, evaluation, and continuous integration and deployment (CI/CD) of prompt engineering projects. It empowers data scientists and LLM application developers with an experience that combines natural language prompts, templating language, a list of built-in tools and Python code.
You can learn more about it here: [What is Azure Machine Learning prompt flow](https://learn.microsoft.com/en-us/azure/machine-learning/prompt-flow/overview-what-is-prompt-flow?view=azureml-api-2)

## What is PVA (Power Virtual Agents)?

Microsoft Power Virtual Agents empowers everyone to create intelligent conversational bots - from citizen developers,
business users to professional developers alike - to seamlessly build secure and scalable virtual agents in an integrated
building platform. You can learn more about it here: [Power Virtual Agents overview](https://learn.microsoft.com/en-us/power-virtual-agents/fundamentals-what-is-power-virtual-agents)

## How to get started

1. You will first need to setup your PromptFlow endpoint. To do this, please setup your custom environment here with your use case:
[Deploy a prompt flow using Docker | Github](https://github.com/microsoft/promptflow/blob/main/docs/how-to-guides/deploy-a-flow/deploy-using-docker.md). You can test your endpoint using Postman client. Keep your configuration handy. For example:

Request:

```text
POST https://promptflow-webapp-hack.azurewebsites.net/score

body:
{ "chat_input": "How to verify a customer?", "chat_history": [] }
```

Response:

```text
body:
{
"answer": "To verify a customer, you need to follow the Account Verification Process - BCSS. The process involves determining the verification method based on the caller's type and workgroup, asking the caller to provide their full name, CTN, account name or company name, and validating the information provided using FaST or Clarify. If the caller is unable to complete all verification requirements, they are unverified and have General Access. (Source: Account Verification Process - BCSS)",
"context": "Content: What I Need to Know\n\nAdhere to all VID Business Rules found in Account Verification Process - BCSS.\n\nDetermine Verification Method\n\nAsk the caller to provide:¶\r\nCaller's full name ContosoUID¶\r\nIf Screen pop is available, the ContosoUID can be confirmed instead of requesting it be provided.Identify the caller's Business Unit in Webphone. If the caller does not have a Business Unit listed, move up in the hierarchy until a Business Unit is visible.¶\r\n¶\n\nContinue verification based on the Business Unit.¶\r\nWhen the Business Unit starts with Contoso Business, follow Contoso Business (BCSS/Enterprise/GBS) or Finance.When the Business Unit does not start with Contoso Business, follow Non-Contoso Business - All Retail/Virtual Sales Experience (formerly DMDR/Mobility Sales)/Contoso Right to You.¶\n\nNon-Contoso Business - All Retail/Virtual Sales ..."
}
```

1. As a next step, you will need a PVA environment. Easiest way to get access to such an environment is to create a tenant from [demos.microsoft.com](demos.microsoft.com). Go to My Environments|Create Tenant. You want a D365 Customer Engagement tenant - it comes with the licenses you need.

![PVA Custom Environment 1](docs/pva-custom-environment-1.png)

![PVA Custom Environment 2](docs/pva-custom-environment-2.png)

![PVA Custom Environment 3](docs/pva-custom-environment-3.png)

You will receive a set of credentials to access your custom environment. Go to [powervirtualagents.microsoft](https://powervirtualagents.microsoft.com/en-us/signin/) and use these new credentials.

![PVA Custom Environment 4](docs/pva-custom-environment-4.png)

## Now that you are all set-up, let's get started

1. Sign-in the Power Virtual Agents page with your existing account. Make sure your new environment is selected.

![PVA Select Environment](docs/pva-select-environment.png)

1. One the next step, click "Create" to create a new bot. Then, "Try the unified canvas (preview)". Provide a name for your bot.
E.g. "demo_bot", then "Create".

![PVA Create Bot](docs/pva-create-bot.png)

Your new bot will be created after a few moments.

1. Once it's created, you will land on the following page.

![PVA Bot Overview](docs/pva-bot-overview.png)

1. On the Topics menu, select "System", and select "Fallback". We need to create a global variable for the chat_history like the following.

![PVA Fallback 1](docs/pva-fallback-1.png)

1. At the end of this workflow, click on the "+" symbol under the node you just added. Go to "Call an action > Create a flow".

![PVA Fallback 2](docs/pva-fallback-2.png)

1. This action will redirect you to Power Automate, a service that helps you create automated workflows between your
apps and services to synchronize files, get notifications, collect data, and more (find out more here: [Get Started with Power Automate - Power Automate | Microsoft Learn](https://learn.microsoft.com/en-us/power-automate/getting-started)), in order create
your flow. On the top "Power virtual agents", add two inputs and provide a name for it, e.g. text_prompt and chat_history. Leave the values of the variables as-is.

![Power Automate 1](docs/power-automate-1.png)

1. Since Power Automate inputs does not understand stringified arrays, we need to parse it before using it as an array. Search for "Parse Json". You can add the ""

![Power Automate 2](docs/power-automate-2.png)

![Power Automate 3](docs/power-automate-3.png)

1. Click the + symbol after the node you just modified, select "Add an action", type "HTTP" and select the simple option
available.

![Power Automate 4](docs/power-automate-4.png)

1. Populate with the PromptFlow details as in the image below. Make sure the "Body" and the text_prompt variables are specified.

![Power Automate 5](docs/power-automate-5.png)

1. Next, click on the "+" icon to add a new action and search for "Parse Json". As in the previous steps, add the "Body" variable in the content. For the Schema, add the following (you don't need to click on "Generate from sample" option):

![Power Automate 6](docs/power-automate-6.png)

1. In order to build up the chat history, we need to perform a few variable operations.

Search for initialize variable and cast the array variable from "Convert history to array" output

![Power Automate 7](docs/power-automate-7.png)

Search for append to array variable using the text_prompt input and the Parse Json output

![Power Automate 8](docs/power-automate-8.png)

Search for compose and initialize the max history length constant

![Power Automate 9](docs/power-automate-9.png)

Search for compose and initialize the max history length constant. Use `length(variables('chat_history'))`

![Power Automate 10](docs/power-automate-10.png)

Search for compose and calculate the number of items to skip. Use `max(0, sub(outputs('Current_History_Length'), outputs('Max_History_Length')))`

![Power Automate 11](docs/power-automate-11.png)

Search for compose and skip the number items of the previous image. Use `skip(variables('chat_history'), outputs('Skip_items'))`

![Power Automate 12](docs/power-automate-12.png)

Search for compose and create a new array for chat_history.

![Power Automate 13](docs/power-automate-13.png)

1. The final step is to write the output of the REST API request into a variable, in this case we called it "final_text". Click
on the text field for assigning the value, right next to it. Do the same with "chat_history" using last output. A new window will pop-up.

![Power Automate 14](docs/power-automate-14.png)

1. Now, coming back to Power Virtual Agents page, in fallback topic sets the inputs and outputs to call the workflow.

![Power Fallback 2](docs/pva-fallback-2.png)

1. Print the "final_text" message at the end of the workflow.

![Power Fallback 3](docs/pva-fallback-3.png)

## Let's try the bot

Click on the Test your bot option on the bottom left of your screen. You can ask whatever you want. The following screenshot
shows an example of a conversation.
Binary file added PVAbot.pdf
Binary file not shown.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,19 @@ The below are troubleshooting steps in case you face any problems with the solut
<br/>
<br/>

# Prompt Flow Project
As part of this solution, we have included an [Azure ML Prompt Flow](https://learn.microsoft.com/en-us/azure/machine-learning/prompt-flow/overview-what-is-prompt-flow?view=azureml-api-2) project.

This is the [Contoso Q&A Chat](./prompt-flow-project/contoso-qna-chat/README.md) that can be served on a RESTful API. We will use this API as part of the Chatbot integration with Power Virtual Agents in the next step.

Make sure to deploy explore and/or deploy this flow using Docker. More details, [here](https://github.com/microsoft/promptflow/blob/main/docs/how-to-guides/deploy-a-flow/deploy-using-docker.md).


# Interfacing with the Solution: Chatbot and Postman
For both cases below, please note that the "BotQnAHTTPFunc" Azure Function returns plain text. This could be easily changed to return the response in JSON format:

1. In this repo, there's a general guide "[AzureOpenAIandPVAbot.pdf](AzureOpenAIandPVAbot.pdf)" to build chat bots using Power Virtual Agents. This guide can be adapted and used to build a bot that will interface with the already deployed "BotQnAHTTPFunc" Azure Function in the Function App. The returned link for the document below has been hidden:
1. In this repo, there's a general guide "[AzureOpenAIandPVAbot.pdf](AzureOpenAIandPVAbot.pdf)" to build chat bots using Power Virtual Agents. Thi guide can be adapted and used to build a bot that will interface with the already deployed "BotQnAHTTPFunc" Azure Function in the Function App. However, we provide another guide "[PVAbot.pdf](./PVAbot.pdf)" specifically for this project that can be integrated with a PromptFlow integration with RESTful API.
The returned link for the document below has been hidden:
<br/>
<br />
<p align="center">
Expand Down
Binary file added images/power-automate-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/power-automate-9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pva-bot-overview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pva-create-bot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pva-custom-environment-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pva-custom-environment-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pva-custom-environment-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pva-custom-environment-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pva-fallback-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pva-fallback-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pva-fallback-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pva-fallback-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pva-select-environment.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pva-topic-1.png
Binary file added images/pva-topic-2.png
Binary file added images/pva-topic-3.png
Binary file added prompt-flow-project/.DS_Store
Binary file not shown.
16 changes: 16 additions & 0 deletions prompt-flow-project/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM mcr.microsoft.com/devcontainers/miniconda:0-3

# Copy environment.yml (if found) to a temp location so we update the environment. Also
# copy "noop.txt" so the COPY instruction does not fail if no environment.yml exists.
COPY environment.yml* .devcontainer/noop.txt /tmp/conda-tmp/
RUN if [ -f "/tmp/conda-tmp/environment.yml" ]; then umask 0002 && /opt/conda/bin/conda env update -n base -f /tmp/conda-tmp/environment.yml; fi \
&& rm -rf /tmp/conda-tmp

# [Optional] Uncomment to install a different version of Python than the default
# RUN conda install -y python=3.6 \
# && pip install --no-cache-dir pipx \
# && pipx reinstall-all

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>
31 changes: 31 additions & 0 deletions prompt-flow-project/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/miniconda
{
"name": "Miniconda (Python 3)",
"build": {
"context": "..",
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"extensions": [
"prompt-flow.prompt-flow"
]
}
}

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "python --version",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
3 changes: 3 additions & 0 deletions prompt-flow-project/.devcontainer/noop.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This file is copied into the container along with environment.yml* from the
parent folder. This is done to prevent the Dockerfile COPY instruction from
failing if no environment.yml is found.
2 changes: 2 additions & 0 deletions prompt-flow-project/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
152 changes: 152 additions & 0 deletions prompt-flow-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
Loading