Skip to content

Commit 595f611

Browse files
authored
Merge pull request #1 from Azure-Samples/wenjwang/promptflowsamples
AzureSQLPromptFlow Samples Initial check in
2 parents fd0695b + 700ab6b commit 595f611

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+10916
-0
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Ignore Visual Studio temporary files, build results, and
2+
## files generated by popular Visual Studio add-ons.
3+
##
4+
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5+
6+
.env
7+
AzureSQLPromptFlowSamples/src/sql-promptflow-demo/configs/flow_config.json
8+
AzureSQLPromptFlowSamples/src/sql-promptflow-demo/configs/key_config_local.json
9+
AzureSQLPromptFlowSamples/src/sql-promptflow-demo/promptflow/flow.dag.yaml
10+
AzureSQLPromptFlowSamples/src/sql-promptflow-demo/evaluation/flow.dag.yaml
11+
12+
AzureSQLPromptFlowSamples/src/sql-promptflow-demo/deployment/deployment.yaml
13+
AzureSQLPromptFlowSamples/src/sql-promptflow-demo/deployment/endpoint.yaml
14+
AzureSQLPromptFlowSamples/src/sql-promptflow-demo/deployment/model.yaml
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
# Azure SQL Promptflow Samples
2+
3+
This is the code repository for Azure SQL Promptflow Demo. It demonstrates how to create a chatbot that can query the data from your own Azure SQL database.
4+
5+
## Prerequisites
6+
- Azure subscription
7+
- Azure Cognitive Search service
8+
- Azure Machine Learning workspace
9+
- Azure SQL Server with Sample AdventureWorksLT database
10+
- Azure Open AI service
11+
12+
and the following python packages
13+
14+
## Developers: Note
15+
16+
This works with the latest promptflow version as of 10/31/2023, the further improvement of promptflow will be updated in the future.
17+
18+
## Getting started
19+
20+
### Install VSCode and Promptflow
21+
22+
- You need to install Visual studio Code: [Here](https://code.visualstudio.com/).
23+
- You also need to install the PromptFlow extension, available here: [Prompt Flow community ecosystem](https://learn.microsoft.com/azure/machine-learning/prompt-flow/community-ecosystem?view=azureml-api-2).
24+
25+
### Install python packages
26+
27+
**Warning:** As far as we know, the only vesion of python currently working is python 3.9 environment.
28+
29+
And install the packages by:
30+
31+
```bash
32+
pip install -r requirements.txt
33+
```
34+
35+
(Optional, install VS code extension: [https://marketplace.visualstudio.com/items?itemName=ms-python.python])
36+
37+
### Filling in configurations needed
38+
39+
Create configure files in the `src/sql-promptflow-demo/configs` based on the sample files. You will need to set up Azure Open AI (AOAI), Azure Congnitive Search (ACS) and Azure SQL Server with sample AdentureWorksLT database.
40+
41+
42+
Note that we have two models, `aoai_deployment_name` and `aoai_agent_deployment_name`. Those can be the same, but however:
43+
44+
1. `aoai_deployment_name` is used for the final answer produced by the model.
45+
2. `aoai_agent_deployment_name` is used to reformulate the question you are asking adequately.
46+
47+
Default build_image: `mcr.microsoft.com/azureml/promptflow/promptflow-runtime:latest`
48+
49+
50+
### Sending secret keys to Key Vault
51+
52+
The AML workspace used to deploy this flow comes with a default key vault for storing secret keys. Navigate to the AML workspace in Azure Portal and in the Overview pane find the associated key vault. Grab its URI and add it as the `keyvault_uri` key within your team config.
53+
54+
For the file `src/sql-promptflow-demo/configs/key_config_local.json` in the copilot dir (file not tracked by git) and add the following JSON structure and save:
55+
56+
```json
57+
{
58+
"aoai-api-key": "YOUR_OPENAI_KEY",
59+
"aoai-api-key-embed": "YOUR_OPENAI_KEY",
60+
"acs-key": "YOUR_ACS_KEY",
61+
"connectionString": "SQL_CONNECTION_STRING",
62+
}
63+
```
64+
65+
You can copy `config_local_sample.json` as well.
66+
67+
These secrets are used to access ACS, AOAI, and Azure SQL database. They will be uploaded with setup.py is run.
68+
69+
Now your secrets should be in key vault, and the flow can access these secrets locally via your `az login` auth and in deployment via the default key vault association with AML.
70+
71+
**IMPORTANT:** you need to give yourself access to the Key Vault.
72+
73+
- You can do this by navigating to the Key Vault on the Azure Portal
74+
- Click on *Access policies* And *+ Create*.
75+
- Add yourself with all secret privileges.
76+
77+
**Note:** secrets in Azure Key Vault must consist of lowercase alphanumeric characters or dashes (-). Because of this, existing config_local.json values (prior to Key Vault) need to be renamed to fit this format (lowercase, replace `_` with `-`).
78+
79+
80+
### Run setup.py to set up the flow
81+
82+
```powershell
83+
python setup.py
84+
```
85+
In case you experience authentication errors, replace `credential = DefaultAzureCredential()` with `credential = DefaultAzureCredential(exclude_shared_token_cache_credential=True)`. You will need to replace it in promptflow modules as well.
86+
### Test the flow
87+
88+
```bash
89+
# run chat flow with default question in flow.dag.yaml
90+
python -m promptflow._cli.pf flow test --flow . --interactive
91+
```
92+
93+
Alternatively, you can open in VS code the `flow.dag.yaml`, and there is a little "run all" icon that looks like >> at the top right, you can click it to run.
94+
95+
### Batch run and evaluate the flow
96+
Prepare batch run data `data\batch_run_data.jsonl` and run the following command to batch run the flow and evaluate the results.
97+
98+
```bash
99+
python batch_run_and_eval.py
100+
```
101+
102+
### Deploy the flow on local
103+
104+
See below, go to the parent directory first!
105+
106+
```bash
107+
# go to parent directory
108+
cd ..
109+
# run chat flow with default question in flow.dag.yaml
110+
python -m promptflow._cli.pf flow serve --source promptflow <this is the folder name> --port 8080 --host localhost
111+
```
112+
113+
Then it can be tested with (use git bash):
114+
115+
```bash
116+
curl http://localhost:8080/score --data '{"chat_history":[], "question":"Why my jobs are running slow?", "team_name":"test"}' -X POST -H "Content-Type: application/json"
117+
```
118+
119+
The above doesn't work in Windows Powershell, this alternate command with Invoke-WebRequest worked instead:
120+
121+
```powershell
122+
$uri = "http://localhost:8080/score"
123+
$body = @{
124+
chat_history = @()
125+
question = "Why my jobs are running slow?"
126+
team_name = "test"
127+
} | ConvertTo-Json
128+
129+
$headers = @{
130+
"Content-Type" = "application/json"
131+
}
132+
133+
$response = Invoke-WebRequest -Uri $uri -Method POST -Body $body -Headers $headers
134+
135+
# Display the response (if needed)
136+
$response.Content
137+
```
138+
139+
### Run the flow on cloud
140+
141+
The same flow can be executed on cloud, and the code will be uploaded to AML workspace. After that, you can deploy using the portal's UI. The job can be found from the output of the run.py as well as from the portal.
142+
143+
1. To run, one need to create runtime first. And in the config.json, put the runtime name there.
144+
2. You need to create two connections from portal, one is for AzureOpenAI connection with `azure_open_ai_connection_name` name that you specified in `configs/flow_config_sample.json`. And the other is customized connection that contains key for ACS and SQL database with name `SQLDB_connection_name` that you specified in `configs/flow_config_sample.json`. Here is the detailed steps. [reference](https://learn.microsoft.com/en-us/azure/machine-learning/prompt-flow/tools-reference/python-tool?view=azureml-api-2#how-to-consume-custom-connection-in-python-tool).
145+
146+
For `azure_open_ai_connection_name`, you will need to create a connection to store the AOAI model parameters for the chat.
147+
148+
For `SQLDB_connection_name`, you will need to create a connection to store the SQL database connection string.
149+
150+
```json
151+
{
152+
"OPENAI_API_BASE_EMBED": "https://{LLM}.openai.azure.com/",
153+
"connectionString": "SQL_CONNECTION_STRING",
154+
"OPENAI_API_KEY_EMBED": "YOUR_OPENAI_KEY",
155+
"OPENAI_API_VERSION": "2023-03-15-preview",
156+
"search-key": "YOUR_ACS_KEY",
157+
}
158+
```
159+
160+
```bash
161+
# go to parent directory
162+
cd ..
163+
# run chat flow with default question in flow.dag.yaml
164+
python run.py
165+
```
166+
167+
### Deploy on cloud
168+
169+
For set up the promptflow, the user should have run the `setup.py` file, which will modify the workload file (`flow.dag.yaml`).
170+
171+
By running the script of `deploy.py` step by step, one can create an endpoint for the flow in their azure machine learning workspace.
172+
The `deploy.py` script will produce various deployment files under the `Deployments/` folder from tempaltes.
173+
174+
**IMPORTANT**: there are two next steps you need to take to finialize setting up the PromptFlow endpoint.
175+
176+
1. As for running on the cloud (see above), you will need to manually create the required connections on your PromptFlow endpoint.
177+
CF [Create necessary connections](https://promptflow.azurewebsites.net/community/cloud/local-to-cloud.html#create-necessary-connections) for reference.
178+
The name of the connection should match what you have set in your configuration file.
179+
2. Once deployed, you need to allow the endpoint to use the connection that's stored in Azure ML.
180+
1. For this, navigate to your *Azure ML workspace* on the *Azure Portal*.
181+
2. Click on *Access control (IAM)* and click *+ Add*, *Role assignment*.
182+
3. In the role list, select *AzureML Data Scientist* and click *Next*.
183+
4. Click on *Managed identity*, *+ Select members*.
184+
5. Select your subscription and under *Managed identity* select *All system-assigned managed identities*.
185+
6. Search for an identity with the same name as your deployment endpoint and add it.
186+
7. Click *save*.
187+
3. In addition, you will need to add the endpoint to access the default AzureML Key Vault for secrets
188+
1. Grab the application ID by searching the endpoint name in Azure Portal, going to the "Identity" tab and finding the object (principal) ID
189+
2. Navigate to Key Vault (can find in Overview of AzureML workspace)
190+
3. Goto *Access policies* and click *Create*
191+
4. Select *Get* under *Secret permissions*. This is all the endpoints will need.
192+
5. Hit Next
193+
6. Enter the app ID for the endpoint or search for it.
194+
7. Finalize the access policy
195+
8. The bot should now be able to access Key Vault
196+
197+
**Note:** `config_local.json` is not copied to AzureML (security risk). A tmp directory is first created without this file and uploaded with the model.
198+
199+
### Generate a docker
200+
201+
See below, go to the parent directory first!
202+
203+
```bash
204+
# go to parent directory
205+
cd ..
206+
# run chat flow with default question in flow.dag.yaml
207+
python -m promptflow._cli.pf flow export --source d:\Repos\DRICopilot0725\DRICopilot\src\core\copilot\promptflow <this is the folder name> --output d:\Repos --format docker
208+
```
209+
210+
### Troubleshooting
211+
212+
If running "az ml" results in the extension not being recognized, you may need to upgrade you azure cli: [https://learn.microsoft.com/azure/machine-learning/how-to-configure-cli?view=azureml-api-2&tabs=public]
213+
214+
From the documentation: "The new Machine Learning extension requires Azure CLI version >=2.38.0. Ensure this requirement is met"
215+
216+
After upgrading, run these commands to refresh things:
217+
218+
```bash
219+
az extension remove -n azure-cli-ml
220+
az extension remove -n ml
221+
az extension add -n ml
222+
```
223+
224+
### References
225+
- [Auzre SQL Database documentation](https://docs.microsoft.com/en-us/azure/azure-sql/database)
226+
- [Azure Cognitive Search documentation](https://docs.microsoft.com/en-us/azure/search/)
227+
- [Azure Machine Learning documentation](https://docs.microsoft.com/en-us/azure/machine-learning/)
228+
- [Azure Open AI documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/)
229+
- [PromptFlow documentation](https://learn.microsoft.com/en-us/azure/machine-learning/prompt-flow/overview-what-is-prompt-flow?view=azureml-api-2/)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
azure-ai-ml
2+
azure-core
3+
azure-identity
4+
azure-keyvault
5+
azure-kusto-data
6+
azure-search-documents>=11.4.0b11
7+
azure-storage-blob
8+
pyodbc
9+
bs4
10+
jinja2
11+
jsbeautifier
12+
langchain
13+
logger
14+
matplotlib
15+
openai
16+
pandas
17+
plotly
18+
promptflow>=0.1.0b8
19+
promptflow-tools
20+
python-dotenv
21+
ipywidgets
22+
scikit-learn
23+
scipy
24+
tiktoken
25+
tenacity
26+
tqdm
27+
28+
types-PyYAML
29+
types-requests
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
connectionString=""
2+
OPENAI_API_KEY_AZURE_Embeddings=
3+
OPENAI_API_BASE_Embeddings=""
4+
AZURE_SEARCH_SERVICE_ENDPOINT=""
5+
AZURE_SEARCH_KEY=

0 commit comments

Comments
 (0)