Skip to content

Commit ae906b0

Browse files
authored
Merge pull request #104 from ks6088ts-labs/feature/issue-103_image-inference-flow
add image qa flow with custom llm tool
2 parents 414a995 + 55b48c3 commit ae906b0

File tree

8 files changed

+150
-0
lines changed

8 files changed

+150
-0
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: 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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"package": {},
3+
"code": {
4+
"hello.py": {
5+
"type": "python",
6+
"inputs": {
7+
"connection": {
8+
"type": [
9+
"AzureOpenAIConnection"
10+
]
11+
},
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+
]
31+
}
32+
},
33+
"source": "hello.py",
34+
"function": "my_python_tool"
35+
}
36+
}
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"image": "../../../datasets/contoso-receipt.png"}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
$schema: https://azuremlschemas.azureedge.net/promptflow/latest/Flow.schema.json
2+
environment:
3+
python_requirements_txt: requirements.txt
4+
inputs:
5+
user_prompt:
6+
type: string
7+
default: Please extract texts from the image
8+
system_prompt:
9+
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
17+
outputs:
18+
output_prompt:
19+
type: string
20+
reference: ${image_qa.output}
21+
nodes:
22+
- name: image_qa
23+
type: python
24+
source:
25+
type: code
26+
path: hello.py
27+
inputs:
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}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import base64
2+
import io
3+
4+
from openai import AzureOpenAI
5+
from promptflow.connections import AzureOpenAIConnection
6+
from promptflow.contracts.multimedia import Image
7+
from promptflow.core import tool
8+
9+
10+
@tool
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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
promptflow

datasets/contoso-receipt.png

1.73 MB
Loading

0 commit comments

Comments
 (0)