import os,base64
from openai import OpenAI
client = OpenAI(
api_key='xxx',
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
def encode_image(image_path):
with open(image_path,"rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
result = []
for file_name in ["food1.png","food2.png","food3.png"]:
image_path = os.path.join(os.path.dirname(__file__),file_name)
completion = client.chat.completions.create(
model="qwen-vl-max-latest",
messages=[
{
"role": "system",
"content": [{"type":"text","text":"You are a helpful assistant."}]
},
{
"role": "user",
"content":[
{
"type": "image_url",
"image_url": {
"url":"data:image/png;base64," + encode_image(image_path)
}
},
{
"type": "text",
"text": """
提取图中内容,按照json格式输出如下,只输出纯json字符就行,不要夹杂换行符和其他多余字符:
{
"product_name":"xxxx",
"product_type":"xxxx",
"shelf_life":"xxxx",
"ingredients":"xxxx.",
"product_standard_code":"xxxx",
"storage_conditions":"xxxx",
"food_production_license_number":"xxxx",
"production_date":"xxxx",
"manufacturer":"xxxx",
"address":"xXxx",
"phone":"xxxx",
"fax":"xXXX"
}
"""
}
]
}
]
)
result.append(completion.choices[0].message.content)
print(result)