chore: import upstream snapshot with attribution
tools_continuous_delivery / Private PyPI non-main branch release (push) Has been skipped
tools_continuous_delivery / Private PyPI main branch release (push) Failing after 2m42s
Publish Promptflow Doc / Build (push) Has been cancelled
Publish Promptflow Doc / Deploy (push) Has been cancelled
Flake8 Lint / flake8 (push) Has been cancelled
Spell check CI / Spell_Check (push) Has been cancelled
tools_continuous_delivery / Private PyPI non-main branch release (push) Has been skipped
tools_continuous_delivery / Private PyPI main branch release (push) Failing after 2m42s
Publish Promptflow Doc / Build (push) Has been cancelled
Publish Promptflow Doc / Deploy (push) Has been cancelled
Flake8 Lint / flake8 (push) Has been cancelled
Spell check CI / Spell_Check (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
# Conditional flow for switch scenario
|
||||
|
||||
This example is a conditional flow for switch scenario.
|
||||
|
||||
By following this example, you will learn how to create a conditional flow using the `activate config`.
|
||||
|
||||
## Flow description
|
||||
|
||||
In this flow, we set the background to the search function of a certain mall, use `activate config` to implement switch logic and determine user intent based on the input queries to achieve dynamic processing and generate user-oriented output.
|
||||
|
||||
- The `classify_with_llm` node analyzes user intent based on input query and provides one of the following results: "product_recommendation," "order_search," or "product_info".
|
||||
- The `class_check` node generates the correctly formatted user intent.
|
||||
- The `product_recommendation`, `order_search`, and `product_info` nodes are configured with activate config and are only executed when the output from `class_check` meets the specified conditions.
|
||||
- The `generate_response` node generates user-facing output.
|
||||
|
||||
For example, as the shown below, the input query is "When will my order be shipped" and the LLM node classifies the user intent as "order_search", resulting in both the `product_info` and `product_recommendation` nodes being bypassed and only the `order_search` node being executed, and then generating the outputs.
|
||||
|
||||

|
||||
|
||||
## Prerequisites
|
||||
|
||||
Install promptflow sdk and other dependencies:
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## Setup connection
|
||||
|
||||
Prepare your Azure OpenAI resource follow this [instruction](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal) and get your `api_key` if you don't have one.
|
||||
|
||||
Note in this example, we are using [chat api](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/chatgpt?pivots=programming-language-chat-completions), please use `gpt-35-turbo` or `gpt-4` model deployment.
|
||||
|
||||
Create connection if you haven't done that. Ensure you have put your azure OpenAI endpoint key in [azure_openai.yml](../../../connections/azure_openai.yml) file.
|
||||
```bash
|
||||
# Override keys with --set to avoid yaml file changes
|
||||
pf connection create -f ../../../connections/azure_openai.yml --name open_ai_connection --set api_key=<your_api_key> api_base=<your_api_base>
|
||||
```
|
||||
|
||||
Note in [flow.dag.yaml](flow.dag.yaml) we are using connection named `open_ai_connection`.
|
||||
```bash
|
||||
# show registered connection
|
||||
pf connection show --name open_ai_connection
|
||||
```
|
||||
|
||||
## 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 query="When will my order be shipped?"
|
||||
```
|
||||
|
||||
- Create run with multiple lines of data
|
||||
```bash
|
||||
# create a random run name
|
||||
run_name="conditional_flow_for_switch_"$(openssl rand -hex 12)
|
||||
|
||||
# create run
|
||||
pf run create --flow . --data ./data.jsonl --column-mapping query='${data.query}' --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
|
||||
```
|
||||
@@ -0,0 +1,8 @@
|
||||
from promptflow.core import tool
|
||||
|
||||
|
||||
@tool
|
||||
def class_check(llm_result: str) -> str:
|
||||
intentions_list = ["order_search", "product_info", "product_recommendation"]
|
||||
matches = [intention for intention in intentions_list if intention in llm_result.lower()]
|
||||
return matches[0] if matches else "unknown"
|
||||
@@ -0,0 +1,11 @@
|
||||
# system:
|
||||
There is a search bar in the mall APP and users can enter any query in the search bar.
|
||||
|
||||
The user may want to search for orders, view product information, or seek recommended products.
|
||||
|
||||
Therefore, please classify user intentions into the following three types according to the query: product_recommendation, order_search, product_info
|
||||
|
||||
Please note that only the above three situations can be returned, and try not to include other return values.
|
||||
|
||||
# user:
|
||||
The user's query is {{query}}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 60 KiB |
@@ -0,0 +1,3 @@
|
||||
{"query": "When will my order be shipped?"}
|
||||
{"query": "Can you help me find information about this T-shirt?"}
|
||||
{"query": "Can you recommend me a useful prompt tool?"}
|
||||
@@ -0,0 +1,69 @@
|
||||
$schema: https://azuremlschemas.azureedge.net/promptflow/latest/Flow.schema.json
|
||||
inputs:
|
||||
query:
|
||||
type: string
|
||||
default: When will my order be shipped?
|
||||
outputs:
|
||||
response:
|
||||
type: string
|
||||
reference: ${generate_response.output}
|
||||
nodes:
|
||||
- name: classify_with_llm
|
||||
type: llm
|
||||
source:
|
||||
type: code
|
||||
path: classify_with_llm.jinja2
|
||||
inputs:
|
||||
deployment_name: gpt-35-turbo
|
||||
max_tokens: 128
|
||||
query: ${inputs.query}
|
||||
connection: open_ai_connection
|
||||
api: chat
|
||||
- name: class_check
|
||||
type: python
|
||||
source:
|
||||
type: code
|
||||
path: class_check.py
|
||||
inputs:
|
||||
llm_result: ${classify_with_llm.output}
|
||||
- name: order_search
|
||||
type: python
|
||||
source:
|
||||
type: code
|
||||
path: order_search.py
|
||||
inputs:
|
||||
query: ${inputs.query}
|
||||
activate:
|
||||
when: ${class_check.output}
|
||||
is: order_search
|
||||
- name: product_info
|
||||
type: python
|
||||
source:
|
||||
type: code
|
||||
path: product_info.py
|
||||
inputs:
|
||||
query: ${inputs.query}
|
||||
activate:
|
||||
when: ${class_check.output}
|
||||
is: product_info
|
||||
- name: product_recommendation
|
||||
type: python
|
||||
source:
|
||||
type: code
|
||||
path: product_recommendation.py
|
||||
inputs:
|
||||
query: ${inputs.query}
|
||||
activate:
|
||||
when: ${class_check.output}
|
||||
is: product_recommendation
|
||||
- name: generate_response
|
||||
type: python
|
||||
source:
|
||||
type: code
|
||||
path: generate_response.py
|
||||
inputs:
|
||||
order_search: ${order_search.output}
|
||||
product_info: ${product_info.output}
|
||||
product_recommendation: ${product_recommendation.output}
|
||||
environment:
|
||||
python_requirements_txt: requirements.txt
|
||||
@@ -0,0 +1,8 @@
|
||||
from promptflow.core import tool
|
||||
|
||||
|
||||
@tool
|
||||
def generate_response(order_search="", product_info="", product_recommendation="") -> str:
|
||||
default_response = "Sorry, no results matching your search were found."
|
||||
responses = [order_search, product_info, product_recommendation]
|
||||
return next((response for response in responses if response), default_response)
|
||||
@@ -0,0 +1,7 @@
|
||||
from promptflow.core import tool
|
||||
|
||||
|
||||
@tool
|
||||
def order_search(query: str) -> str:
|
||||
print(f"Your query is {query}.\nSearching for order...")
|
||||
return "Your order is being mailed, please wait patiently."
|
||||
@@ -0,0 +1,7 @@
|
||||
from promptflow.core import tool
|
||||
|
||||
|
||||
@tool
|
||||
def product_info(query: str) -> str:
|
||||
print(f"Your query is {query}.\nLooking for product information...")
|
||||
return "This product is produced by Microsoft."
|
||||
@@ -0,0 +1,7 @@
|
||||
from promptflow.core import tool
|
||||
|
||||
|
||||
@tool
|
||||
def product_recommendation(query: str) -> str:
|
||||
print(f"Your query is {query}.\nRecommending products...")
|
||||
return "I recommend promptflow to you, which can solve your problem very well."
|
||||
@@ -0,0 +1,2 @@
|
||||
promptflow
|
||||
promptflow-tools
|
||||
Reference in New Issue
Block a user