-
Notifications
You must be signed in to change notification settings - Fork 901
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Example] Add example flow for activate config (#525)
# Description Add example flow for `activate config`. (if-else scenario) - if pass content safety check ![image](https://github.com/microsoft/promptflow/assets/111329184/c224dd18-e795-4e1f-9a6b-190f42172ce1) - else ![image](https://github.com/microsoft/promptflow/assets/111329184/f2cea225-a020-41fc-897b-1a210ec6ab53) # All Promptflow Contribution checklist: - [x] **The pull request does not introduce [breaking changes]** - [ ] **CHANGELOG is updated for new features, bug fixes or other significant changes.** - [x] **I have read the [contribution guidelines](../CONTRIBUTING.md).** ## General Guidelines and Best Practices - [x] Title of the pull request is clear and informative. - [x] There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, [see this page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md). ### Testing Guidelines - [x] Pull request includes test coverage for the included changes. --------- Co-authored-by: Peiwen Gao <[email protected]>
- Loading branch information
1 parent
aeaad5c
commit 5ae7ffb
Showing
12 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
.github/workflows/samples_flows_standard_conditional_flow_for_if_else.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# This code is autogenerated. | ||
# Code is generated by running custom script: python3 readme.py | ||
# Any manual changes to this file may cause incorrect behavior. | ||
# Any manual changes will be overwritten if the code is regenerated. | ||
|
||
name: samples_flows_standard_conditional_flow_for_if_else | ||
on: | ||
schedule: | ||
- cron: "8 21 * * *" # Every day starting at 5:8 BJT | ||
pull_request: | ||
branches: [ main ] | ||
paths: [ examples/flows/standard/conditional-flow-for-if-else/**, examples/*requirements.txt, .github/workflows/samples_flows_standard_conditional_flow_for_if_else.yml ] | ||
workflow_dispatch: | ||
|
||
jobs: | ||
samples_readme_ci: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
- name: Setup Python 3.9 environment | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: "3.9" | ||
- name: Generate config.json | ||
run: echo ${{ secrets.TEST_WORKSPACE_CONFIG_JSON }} > ${{ github.workspace }}/examples/config.json | ||
- name: Prepare requirements | ||
working-directory: examples | ||
run: | | ||
if [[ -e requirements.txt ]]; then | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
fi | ||
- name: Prepare dev requirements | ||
working-directory: examples | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r dev_requirements.txt | ||
- name: Refine .env file | ||
working-directory: examples/flows/standard/conditional-flow-for-if-else | ||
run: | | ||
AOAI_API_KEY=${{ secrets.AOAI_API_KEY_TEST }} | ||
AOAI_API_ENDPOINT=${{ secrets.AOAI_API_ENDPOINT_TEST }} | ||
AOAI_API_ENDPOINT=$(echo ${AOAI_API_ENDPOINT//\//\\/}) | ||
if [[ -e .env.example ]]; then | ||
echo "env replacement" | ||
sed -i -e "s/<your_AOAI_key>/$AOAI_API_KEY/g" -e "s/<your_AOAI_endpoint>/$AOAI_API_ENDPOINT/g" .env.example | ||
mv .env.example .env | ||
fi | ||
- name: Create run.yml | ||
working-directory: examples/flows/standard/conditional-flow-for-if-else | ||
run: | | ||
gpt_base=${{ secrets.AOAI_API_ENDPOINT_TEST }} | ||
gpt_base=$(echo ${gpt_base//\//\\/}) | ||
if [[ -e run.yml ]]; then | ||
sed -i -e "s/\${azure_open_ai_connection.api_key}/${{ secrets.AOAI_API_KEY_TEST }}/g" -e "s/\${azure_open_ai_connection.api_base}/$gpt_base/g" run.yml | ||
fi | ||
- name: Azure Login | ||
uses: azure/login@v1 | ||
with: | ||
creds: ${{ secrets.AZURE_CREDENTIALS }} | ||
- name: Extract Steps examples/flows/standard/conditional-flow-for-if-else/README.md | ||
working-directory: ${{ github.workspace }} | ||
run: | | ||
python scripts/readme/extract_steps_from_readme.py -f examples/flows/standard/conditional-flow-for-if-else/README.md -o examples/flows/standard/conditional-flow-for-if-else | ||
- name: Cat script | ||
working-directory: examples/flows/standard/conditional-flow-for-if-else | ||
run: | | ||
cat bash_script.sh | ||
- name: Run scripts | ||
working-directory: examples/flows/standard/conditional-flow-for-if-else | ||
run: | | ||
export aoai_api_key=${{secrets.AOAI_API_KEY_TEST }} | ||
export aoai_api_endpoint=${{ secrets.AOAI_API_ENDPOINT_TEST }} | ||
export test_workspace_sub_id=${{ secrets.TEST_WORKSPACE_SUB_ID }} | ||
export test_workspace_rg=${{ secrets.TEST_WORKSPACE_RG }} | ||
export test_workspace_name=${{ secrets.TEST_WORKSPACE_NAME }} | ||
bash bash_script.sh | ||
- name: Pip List for Debug | ||
if : ${{ always() }} | ||
working-directory: examples/flows/standard/conditional-flow-for-if-else | ||
run: | | ||
pip list | ||
- name: Upload artifact | ||
if: ${{ always() }} | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: artifact | ||
path: examples/flows/standard/conditional-flow-for-if-else/bash_script.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
examples/flows/standard/conditional-flow-for-if-else/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Conditional flow for if-else scenario | ||
|
||
This example is a conditonal flow for if-else scenario. | ||
|
||
In this flow, it checks if an input query passes content safety check. If it's denied, we'll return a default response; otherwise, we'll call LLM to get a response and then summarize the final results. | ||
|
||
The following are two execution situations of this flow: | ||
- if input query passes content safety check: | ||
![content_safety_check_passed](content_safety_check_passed.png) | ||
- else: | ||
![content_safety_check_failed](content_safety_check_failed.png) | ||
|
||
**Notice**: The `content_safety_check` and `llm_result` node in this flow are dummy nodes that do not actually use the conten safety tool and LLM tool. You can replace them with the real ones. Learn more: [LLM Tool](https://microsoft.github.io/promptflow/reference/tools-reference/llm-tool.html) | ||
|
||
By following this example, you will learn how to create a conditional flow using the `activate config`. | ||
|
||
## Prerequisites | ||
|
||
Install promptflow sdk and other dependencies: | ||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
## Run flow | ||
|
||
- Test flow | ||
```bash | ||
# test with default input value in flow.dag.yaml | ||
pf flow test --flow . | ||
|
||
# test with flow inputs | ||
pf flow test --flow . --inputs question="What is Prompt flow?" | ||
``` | ||
|
||
- Create run with multiple lines of data | ||
```bash | ||
# create a random run name | ||
run_name="conditional_flow_for_if_else_"$(openssl rand -hex 12) | ||
|
||
# create run | ||
pf run create --flow . --data ./data.jsonl --stream --name $run_name | ||
``` | ||
|
||
- List and show run metadata | ||
```bash | ||
# list created run | ||
pf run list | ||
|
||
# show specific run detail | ||
pf run show --name $run_name | ||
|
||
# show output | ||
pf run show-details --name $run_name | ||
|
||
# visualize run in browser | ||
pf run visualize --name $run_name | ||
``` |
8 changes: 8 additions & 0 deletions
8
examples/flows/standard/conditional-flow-for-if-else/content_safety_check.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from promptflow import tool | ||
import random | ||
|
||
|
||
@tool | ||
def content_safety_check(text: str) -> str: | ||
# You can use a content safety node to replace this tool. | ||
return random.choice([True, False]) |
Binary file added
BIN
+38.7 KB
...les/flows/standard/conditional-flow-for-if-else/content_safety_check_failed.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
BIN
+38.7 KB
...les/flows/standard/conditional-flow-for-if-else/content_safety_check_passed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions
2
examples/flows/standard/conditional-flow-for-if-else/data.jsonl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{"question": "What is Prompt flow?"} | ||
{"question": "What is ChatGPT?"} |
6 changes: 6 additions & 0 deletions
6
examples/flows/standard/conditional-flow-for-if-else/default_result.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from promptflow import tool | ||
|
||
|
||
@tool | ||
def default_result(question: str) -> str: | ||
return f"I'm not familiar with your query: {question}." |
47 changes: 47 additions & 0 deletions
47
examples/flows/standard/conditional-flow-for-if-else/flow.dag.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
$schema: https://azuremlschemas.azureedge.net/promptflow/latest/Flow.schema.json | ||
inputs: | ||
question: | ||
type: string | ||
default: What is Prompt flow? | ||
outputs: | ||
answer: | ||
type: string | ||
reference: ${generate_result.output} | ||
nodes: | ||
- name: content_safety_check | ||
type: python | ||
source: | ||
type: code | ||
path: content_safety_check.py | ||
inputs: | ||
text: ${inputs.question} | ||
- name: llm_result | ||
type: python | ||
source: | ||
type: code | ||
path: llm_result.py | ||
inputs: | ||
question: ${inputs.question} | ||
activate: | ||
when: ${content_safety_check.output} | ||
is: true | ||
- name: default_result | ||
type: python | ||
source: | ||
type: code | ||
path: default_result.py | ||
inputs: | ||
question: ${inputs.question} | ||
activate: | ||
when: ${content_safety_check.output} | ||
is: false | ||
- name: generate_result | ||
type: python | ||
source: | ||
type: code | ||
path: generate_result.py | ||
inputs: | ||
llm_result: ${llm_result.output} | ||
default_result: ${default_result.output} | ||
environment: | ||
python_requirements_txt: requirements.txt |
9 changes: 9 additions & 0 deletions
9
examples/flows/standard/conditional-flow-for-if-else/generate_result.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from promptflow import tool | ||
|
||
|
||
@tool | ||
def generate_result(llm_result="", default_result="") -> str: | ||
if llm_result: | ||
return llm_result | ||
else: | ||
return default_result |
10 changes: 10 additions & 0 deletions
10
examples/flows/standard/conditional-flow-for-if-else/llm_result.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from promptflow import tool | ||
|
||
|
||
@tool | ||
def llm_result(question: str) -> str: | ||
# You can use an LLM node to replace this tool. | ||
return ( | ||
"Prompt flow is a suite of development tools designed to streamline " | ||
"the end-to-end development cycle of LLM-based AI applications." | ||
) |
2 changes: 2 additions & 0 deletions
2
examples/flows/standard/conditional-flow-for-if-else/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
promptflow | ||
promptflow-tools |