43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
import os
|
||
from openai import OpenAI
|
||
import base64
|
||
|
||
client = OpenAI(
|
||
api_key = 'd04d386a-7c67-4927-8251-171a236583a6',
|
||
base_url = "https://ark.cn-beijing.volces.com/api/v3",
|
||
)
|
||
def encode_image(image_path):
|
||
with open(image_path, "rb") as image_file:
|
||
return base64.b64encode(image_file.read()).decode('utf-8')
|
||
# 将图片转为Base64编码
|
||
|
||
# 需要传给大模型的图片
|
||
image_path = "ch.jpg"
|
||
base64_image = encode_image(image_path)
|
||
# Image input:
|
||
response = client.chat.completions.create(
|
||
model="doubao-1-5-vision-pro-32k-250115",
|
||
messages=[
|
||
{
|
||
"role": "user",
|
||
"content": [
|
||
{
|
||
"type": "text",
|
||
"text": "图中是几个语文题目,帮我分析一下,我是否做对了",
|
||
},
|
||
{
|
||
"type": "image_url",
|
||
"image_url": {
|
||
# 需要注意:传入Base64编码前需要增加前缀 data:image/{图片格式};base64,{Base64编码}:
|
||
# PNG图片:"url": f"data:image/png;base64,{base64_image}"
|
||
# JEPG图片:"url": f"data:image/jpeg;base64,{base64_image}"
|
||
# WEBP图片:"url": f"data:image/webp;base64,{base64_image}"
|
||
"url": f"data:image/jpg;base64,{base64_image}"
|
||
},
|
||
},
|
||
],
|
||
}
|
||
],
|
||
)
|
||
|
||
print(response.choices[0].message.content) |