Skip to content

Commit 55b48c3

Browse files
committed
implement image qa flow
1 parent 670ed51 commit 55b48c3

File tree

7 files changed

+122
-46
lines changed

7 files changed

+122
-46
lines changed

apps/11_promptflow/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,32 @@ $ pf run create \
177177
$ pf run show-details --name $RUN_NAME
178178
```
179179

180+
### image_qa
181+
182+
To run the image QA flow with GPT-4o, we customize an LLM tool.
183+
Following documents provide more details:
184+
185+
- docs: [Customizing an LLM Tool](https://microsoft.github.io/promptflow/how-to-guides/develop-a-tool/customize_an_llm_tool.html)
186+
- example codes: [promptflow/examples/flows/chat/chat-with-image](https://github.com/microsoft/promptflow/tree/main/examples/flows/chat/chat-with-image)
187+
188+
With the image QA flow sample, you can ask questions about an image and get answers from the model.
189+
190+
```shell
191+
cd apps/11_promptflow/image_qa
192+
193+
# Create run with multiple lines data
194+
$ RUN_NAME=image_qa-$(date +%s)
195+
$ pf run create \
196+
--name $RUN_NAME \
197+
--flow . \
198+
--data ./data.jsonl \
199+
--column-mapping image='${data.image}' \
200+
--stream
201+
202+
# Show run details
203+
$ pf run show-details --name $RUN_NAME
204+
```
205+
180206
## References
181207

182208
- [Prompt flow > repos](https://github.com/microsoft/promptflow)
Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
{
2-
"package": {},
3-
"code": {
4-
"hello.jinja2": {
5-
"type": "prompt",
6-
"inputs": {
7-
"text": {
8-
"type": [
9-
"string"
10-
]
11-
}
12-
}
2+
"package": {},
3+
"code": {
4+
"hello.py": {
5+
"type": "python",
6+
"inputs": {
7+
"connection": {
8+
"type": [
9+
"AzureOpenAIConnection"
10+
]
1311
},
14-
"hello.py": {
15-
"type": "python",
16-
"inputs": {
17-
"input1": {
18-
"type": [
19-
"string"
20-
]
21-
}
22-
},
23-
"function": "my_python_tool"
12+
"image": {
13+
"type": [
14+
"image"
15+
]
16+
},
17+
"model": {
18+
"type": [
19+
"string"
20+
]
21+
},
22+
"system_prompt": {
23+
"type": [
24+
"string"
25+
]
26+
},
27+
"user_prompt": {
28+
"type": [
29+
"string"
30+
]
2431
}
32+
},
33+
"source": "hello.py",
34+
"function": "my_python_tool"
2535
}
36+
}
2637
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"text": "Hello World!"}
1+
{"image": "../../../datasets/contoso-receipt.png"}
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
$schema: https://azuremlschemas.azureedge.net/promptflow/latest/Flow.schema.json
2+
environment:
3+
python_requirements_txt: requirements.txt
24
inputs:
3-
text:
5+
user_prompt:
6+
type: string
7+
default: Please extract texts from the image
8+
system_prompt:
49
type: string
10+
default: You are an excellent OCR tool
11+
image:
12+
type: image
13+
default: ../../../datasets/contoso-receipt.png
14+
model:
15+
type: string
16+
default: gpt-4o
517
outputs:
618
output_prompt:
719
type: string
8-
reference: ${echo_my_prompt.output}
20+
reference: ${image_qa.output}
921
nodes:
10-
- name: hello_prompt
11-
type: prompt
12-
source:
13-
type: code
14-
path: hello.jinja2
15-
inputs:
16-
text: ${inputs.text}
17-
- name: echo_my_prompt
22+
- name: image_qa
1823
type: python
1924
source:
2025
type: code
2126
path: hello.py
2227
inputs:
23-
input1: ${hello_prompt.output}
24-
environment:
25-
python_requirements_txt: requirements.txt
28+
connection: open_ai_connection
29+
image: ${inputs.image}
30+
system_prompt: ${inputs.system_prompt}
31+
user_prompt: ${inputs.user_prompt}
32+
model: ${inputs.model}

apps/11_promptflow/image_qa/hello.jinja2

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,48 @@
1-
# ---------------------------------------------------------
2-
# Copyright (c) Microsoft Corporation. All rights reserved.
3-
# ---------------------------------------------------------
1+
import base64
2+
import io
43

4+
from openai import AzureOpenAI
5+
from promptflow.connections import AzureOpenAIConnection
6+
from promptflow.contracts.multimedia import Image
57
from promptflow.core import tool
68

7-
# The inputs section will change based on the arguments of the tool function, after you save the code
8-
# Adding type to arguments and return value will help the system show the types properly
9-
# Please update the function name/signature per need
10-
119

1210
@tool
13-
def my_python_tool(input1: str) -> str:
14-
return "Prompt: " + input1
11+
def my_python_tool(
12+
connection: AzureOpenAIConnection,
13+
image: Image,
14+
model: str,
15+
system_prompt: str,
16+
user_prompt: str,
17+
) -> str:
18+
image_stream = io.BytesIO(image)
19+
encoded_image = base64.b64encode(image_stream.read()).decode("utf-8")
20+
21+
client = AzureOpenAI(
22+
api_key=connection.api_key,
23+
api_version=connection.api_version,
24+
azure_endpoint=connection.api_base,
25+
)
26+
response = client.chat.completions.create(
27+
model=model,
28+
messages=[
29+
{
30+
"role": "system",
31+
"content": system_prompt,
32+
},
33+
{
34+
"role": "user",
35+
"content": [
36+
{
37+
"type": "image_url",
38+
"image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"},
39+
},
40+
{
41+
"type": "text",
42+
"text": user_prompt,
43+
},
44+
],
45+
},
46+
],
47+
)
48+
return response.choices[0].message.content

datasets/contoso-receipt.png

1.73 MB
Loading

0 commit comments

Comments
 (0)