Skip to content

Commit 414a995

Browse files
authored
Merge pull request #102 from ks6088ts-labs/feature/issue-69_promptflow-demo
add Prompt flow samples
2 parents a3ce554 + 4b7550f commit 414a995

31 files changed

+1466
-19
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Here are the preferred tools for development.
4040
| [8_streamlit_azure_openai_batch](./apps/8_streamlit_azure_openai_batch/README.md) | Call Azure OpenAI Batch API with Streamlit | ![8_streamlit_azure_openai_batch](./docs/images/8_streamlit_azure_openai_batch.main.png) |
4141
| [9_streamlit_azure_document_intelligence](./apps/9_streamlit_azure_document_intelligence/README.md) | Call Azure AI Document Intelligence API with Streamlit | ![9_streamlit_azure_document_intelligence](./docs/images/9_streamlit_azure_document_intelligence.main.png) |
4242
| [10_streamlit_batch_transcription](./apps/10_streamlit_batch_transcription/README.md) | Call Batch Transcription API with Streamlit | ![10_streamlit_batch_transcription](./docs/images/10_streamlit_batch_transcription.main.png) |
43+
| [11_promptflow](./apps/11_promptflow/README.md) | Get started with Prompt flow | No Image |
4344
| [99_streamlit_examples](./apps/99_streamlit_examples/README.md) | Code samples for Streamlit | ![99_streamlit_examples](./docs/images/99_streamlit_examples.explaindata.png) |
4445

4546
## How to run

apps/11_promptflow/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/.promptflow/*

apps/11_promptflow/README.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# Getting Started with Prompt flow
2+
3+
This application explains how to get started with [Prompt flow](https://github.com/microsoft/promptflow), a Python library that provides a simple and easy way to build conversational AI applications.
4+
5+
## Prerequisites
6+
7+
- Python 3.10 or later
8+
- Azure OpenAI Service
9+
10+
## Overview
11+
12+
Prompt flow is a suite of development tools designed to streamline the end-to-end development cycle of LLM-based AI applications, from ideation, prototyping, testing, evaluation to production deployment and monitoring. It makes prompt engineering much easier and enables you to build LLM apps with production quality.
13+
14+
## Usage
15+
16+
1. Get the API key for Azure OpenAI Service
17+
1. Copy [.env.template](../../.env.template) to `.env` in the same directory
18+
1. Set credentials in `.env`
19+
1. Run scripts in the [apps/11_promptflow](./) directory
20+
21+
Set up the environment and install dependencies:
22+
23+
```shell
24+
# Create a virtual environment
25+
$ python -m venv .venv
26+
27+
# Activate the virtual environment
28+
$ source .venv/bin/activate
29+
30+
# Install dependencies
31+
$ pip install -r requirements.txt
32+
```
33+
34+
## Examples
35+
36+
[Prompt flow > Quick start](https://microsoft.github.io/promptflow/how-to-guides/quick-start.html) provides a quick start guide to Prompt flow.
37+
38+
### [chat_minimal](https://github.com/microsoft/promptflow/tree/main/examples/flex-flows/chat-minimal)
39+
40+
**Run as normal Python script**
41+
42+
```shell
43+
$ python apps/11_promptflow/chat_minimal/main.py
44+
```
45+
46+
**Run from CLI**
47+
48+
```shell
49+
$ cd apps/11_promptflow/chat_minimal
50+
51+
# Test flow
52+
$ pf flow test \
53+
--flow main:chat \
54+
--inputs question="What's the capital of France?"
55+
56+
# Test flow: multi turn, access to http://localhost:{EPHEMERAL_PORT}
57+
$ pf flow test \
58+
--flow main:chat \
59+
--ui
60+
61+
# Create run with multiple lines data
62+
$ pf run create \
63+
--flow main:chat \
64+
--data ./data.jsonl \
65+
--column-mapping question='${data.question}' \
66+
--stream
67+
```
68+
69+
### playground_chat
70+
71+
```shell
72+
cd apps/11_promptflow
73+
74+
# Initialize a new flow
75+
$ pf flow init \
76+
--flow playground_chat \
77+
--type chat
78+
79+
$ cd playground_chat
80+
81+
# Set parameters
82+
$ CONNECTION_NAME=open_ai_connection
83+
$ AZURE_OPENAI_KEY=<your_api_key>
84+
$ AZURE_OPENAI_ENDPOINT=<your_api_endpoint>
85+
86+
# List connections
87+
$ pf connection list
88+
89+
90+
# Delete connection (if needed)
91+
$ pf connection delete \
92+
--name $CONNECTION_NAME
93+
94+
# Create connection
95+
$ pf connection create \
96+
--file azure_openai.yaml \
97+
--set api_key=$AZURE_OPENAI_KEY \
98+
--set api_base=$AZURE_OPENAI_ENDPOINT \
99+
--name $CONNECTION_NAME
100+
101+
# Show connection
102+
$ pf connection show \
103+
--name $CONNECTION_NAME
104+
105+
# Interact with chat flow
106+
$ pf flow test \
107+
--flow . \
108+
--interactive
109+
110+
# Test flow
111+
$ pf flow test \
112+
--flow . \
113+
--inputs question="What's the capital of France?"
114+
115+
# Create run with multiple lines data
116+
$ RUN_NAME=playground_chat-$(date +%s)
117+
$ pf run create \
118+
--name $RUN_NAME \
119+
--flow . \
120+
--data ./data.jsonl \
121+
--column-mapping question='${data.question}' \
122+
--stream
123+
124+
# Show run details
125+
$ pf run show-details --name $RUN_NAME
126+
```
127+
128+
### playground_evaluation
129+
130+
```shell
131+
cd apps/11_promptflow
132+
133+
# Initialize a new flow
134+
$ pf flow init \
135+
--flow playground_evaluation \
136+
--type evaluation
137+
138+
$ cd playground_evaluation
139+
140+
# Create run with multiple lines data
141+
$ RUN_NAME=playground_evaluation-$(date +%s)
142+
$ pf run create \
143+
--name $RUN_NAME \
144+
--flow . \
145+
--data ./data.jsonl \
146+
--column-mapping \
147+
groundtruth='${data.groundtruth}' \
148+
prediction='${data.prediction}' \
149+
--stream
150+
151+
# Show run details
152+
$ pf run show-details --name $RUN_NAME
153+
```
154+
155+
### playground_standard
156+
157+
```shell
158+
cd apps/11_promptflow
159+
160+
# Initialize a new flow
161+
$ pf flow init \
162+
--flow playground_standard \
163+
--type standard
164+
165+
$ cd playground_standard
166+
167+
# Create run with multiple lines data
168+
$ RUN_NAME=playground_standard-$(date +%s)
169+
$ pf run create \
170+
--name $RUN_NAME \
171+
--flow . \
172+
--data ./data.jsonl \
173+
--column-mapping text='${data.text}' \
174+
--stream
175+
176+
# Show run details
177+
$ pf run show-details --name $RUN_NAME
178+
```
179+
180+
## References
181+
182+
- [Prompt flow > repos](https://github.com/microsoft/promptflow)
183+
- [Prompt flow > documents](https://microsoft.github.io/promptflow/)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: Minimal Chat
3+
model:
4+
api: chat
5+
configuration:
6+
type: azure_openai
7+
azure_deployment: gpt-4o
8+
parameters:
9+
temperature: 0.2
10+
max_tokens: 1024
11+
inputs:
12+
question:
13+
type: string
14+
sample:
15+
question: "What is Prompt flow?"
16+
---
17+
18+
system:
19+
You are a helpful assistant.
20+
21+
user:
22+
{{question}}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"question": "What is Prompt flow?"}
2+
{"question": "What is ChatGPT? Please explain with consise statement"}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pathlib import Path
2+
3+
from dotenv import load_dotenv
4+
from promptflow.core import Prompty
5+
from promptflow.tracing import start_trace, trace
6+
7+
BASE_DIR = Path(__file__).absolute().parent
8+
9+
10+
@trace
11+
def chat(question: str) -> str:
12+
load_dotenv()
13+
prompty = Prompty.load(source=f"{BASE_DIR}/chat.prompty")
14+
return prompty(question=question)
15+
16+
17+
if __name__ == "__main__":
18+
start_trace()
19+
20+
result = chat("What's the capital of France?")
21+
print(result)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.env
2+
__pycache__/
3+
.promptflow/*
4+
!.promptflow/flow.tools.json
5+
.runs/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"package": {},
3+
"code": {
4+
"chat.jinja2": {
5+
"type": "llm",
6+
"inputs": {
7+
"question": {
8+
"type": [
9+
"string"
10+
]
11+
},
12+
"chat_history": {
13+
"type": [
14+
"string"
15+
]
16+
}
17+
},
18+
"description": "Chat with Chatbot"
19+
}
20+
}
21+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Chat flow
2+
Chat flow is designed for conversational application development, building upon the capabilities of standard flow and providing enhanced support for chat inputs/outputs and chat history management. With chat flow, you can easily create a chatbot that handles chat input and output.
3+
4+
## Create connection for LLM tool to use
5+
You can follow these steps to create a connection required by a LLM tool.
6+
7+
Currently, there are two connection types supported by LLM tool: "AzureOpenAI" and "OpenAI". If you want to use "AzureOpenAI" connection type, you need to create an Azure OpenAI service first. Please refer to [Azure OpenAI Service](https://azure.microsoft.com/en-us/products/cognitive-services/openai-service/) for more details. If you want to use "OpenAI" connection type, you need to create an OpenAI account first. Please refer to [OpenAI](https://platform.openai.com/) for more details.
8+
9+
```bash
10+
# Override keys with --set to avoid yaml file changes
11+
# Create OpenAI connection
12+
pf connection create --file openai.yaml --set api_key=<your_api_key> --name open_ai_connection
13+
14+
# Create azure OpenAI connection
15+
# pf connection create --file azure_openai.yaml --set api_key=<your_api_key> api_base=<your_api_base> --name open_ai_connection
16+
```
17+
18+
Note in [flow.dag.yaml](flow.dag.yaml) we are using connection named `open_ai_connection`.
19+
```bash
20+
# show registered connection
21+
pf connection show --name open_ai_connection
22+
```
23+
Please refer to connections [document](https://promptflow.azurewebsites.net/community/local/manage-connections.html) and [example](https://github.com/microsoft/promptflow/tree/main/examples/connections) for more details.
24+
25+
## Develop a chat flow
26+
27+
The most important elements that differentiate a chat flow from a standard flow are **Chat Input**, **Chat History**, and **Chat Output**.
28+
29+
- **Chat Input**: Chat input refers to the messages or queries submitted by users to the chatbot. Effectively handling chat input is crucial for a successful conversation, as it involves understanding user intentions, extracting relevant information, and triggering appropriate responses.
30+
31+
- **Chat History**: Chat history is the record of all interactions between the user and the chatbot, including both user inputs and AI-generated outputs. Maintaining chat history is essential for keeping track of the conversation context and ensuring the AI can generate contextually relevant responses. Chat History is a special type of chat flow input, that stores chat messages in a structured format.
32+
33+
- **Chat Output**: Chat output refers to the AI-generated messages that are sent to the user in response to their inputs. Generating contextually appropriate and engaging chat outputs is vital for a positive user experience.
34+
35+
A chat flow can have multiple inputs, but Chat History and Chat Input are required inputs in chat flow.
36+
37+
## Interact with chat flow
38+
39+
Promptflow CLI provides a way to start an interactive chat session for chat flow. Customer can use below command to start an interactive chat session:
40+
41+
```
42+
pf flow test --flow <flow_folder> --interactive
43+
```
44+
45+
After executing this command, customer can interact with the chat flow in the terminal. Customer can press **Enter** to send the message to chat flow. And customer can quit with **ctrl+C**.
46+
Promptflow CLI will distinguish the output of different roles by color, <span style="color:Green">User input</span>, <span style="color:Gold">Bot output</span>, <span style="color:Blue">Flow script output</span>, <span style="color:Cyan">Node output</span>.
47+
48+
> =========================================<br>
49+
> Welcome to chat flow, <You-flow-name>.<br>
50+
> Press Enter to send your message.<br>
51+
> You can quit with ctrl+C.<br>
52+
> =========================================<br>
53+
> <span style="color:Green">User:</span> What types of container software there are<br>
54+
> <span style="color:Gold">Bot:</span> There are several types of container software available, including:<br>
55+
> 1. Docker: This is one of the most popular containerization software that allows developers to package their applications into containers and deploy them across different environments.<br>
56+
> 2. Kubernetes: This is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications.<br>
57+
>
58+
> <span style="color:Green">User:</span> What's the different between them<br>
59+
> <span style="color:Gold">Bot:</span> The main difference between the various container software systems is their functionality and purpose. Here are some key differences between them:<br>
60+
> 1. Docker is more focused on container packaging and deployment, while Kubernetes is more focused on container orchestration and management.<br>
61+
> 2. Kubernetes: Kubernetes is a container orchestration tool that helps manage and deploy containers at scale. It automates the deployment, scaling, and management of containerized applications across multiple hosts.<br>
62+
63+
If customer adds "--verbose" in the pf command, the output of each step will be displayed.
64+
65+
> =========================================<br>
66+
> Welcome to chat flow, Template Chat Flow.<br>
67+
> Press Enter to send your message.<br>
68+
> You can quit with ctrl+C.<br>
69+
> =========================================<br>
70+
> <span style="color:Green">User:</span> What types of container software there are<br>
71+
> <span style="color:Cyan">chat:</span> There are several types of container software available, including:<br>
72+
> 1. Docker: A popular container platform that is widely used in the industry.<br>
73+
> 2. Kubernetes: A container orchestration tool that helps manage and deploy containers at scale.<br>
74+
>
75+
> <span style="color:Gold">Bot:</span> There are several types of container software available, including:<br>
76+
> 1. Docker: A popular container platform that is widely used in the industry.<br>
77+
> 2. Kubernetes: A container orchestration tool that helps manage and deploy containers at scale.<br>
78+
>
79+
> <span style="color:Green">User:</span> What's the different between them<br>
80+
> <span style="color:Cyan">chat:</span> The main differences between container software are in their architecture, feature sets, and use cases. Here are some brief explanations of the differences between the examples I listed:<br>
81+
> 1. Docker: Docker is a container platform that is widely used for building, packaging, and deploying containerized applications. It is known for its ease of use, portability, and large ecosystem of tools and services.<br>
82+
> 2. Kubernetes: Kubernetes is a container orchestration tool that helps manage and deploy containers at scale. It automates the deployment, scaling, and management of containerized applications across multiple hosts.<br>
83+
>
84+
> <span style="color:Gold">Bot:</span> The main differences between container software are in their architecture, feature sets, and use cases. Here are some brief explanations of the differences between the examples I listed:<br>
85+
> 1. Docker: Docker is a container platform that is widely used for building, packaging, and deploying containerized applications. It is known for its ease of use, portability, and large ecosystem of tools and services.<br>
86+
> 2. Kubernetes: Kubernetes is a container orchestration tool that helps manage and deploy containers at scale. It automates the deployment, scaling, and management of containerized applications across multiple hosts.<br>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
$schema: https://azuremlschemas.azureedge.net/promptflow/latest/AzureOpenAIConnection.schema.json
2+
name: open_ai_connection
3+
type: azure_open_ai
4+
api_key: "<user-input>"
5+
api_base: "<user-input>"
6+
api_type: "azure"
7+
api_version: "2024-07-01-preview"

0 commit comments

Comments
 (0)