基于OpenAI API接口向GPT4v上传图像进行图像分析
import openai import base64 from openai import OpenAI client = OpenAI( api_key='xxxxxxxxx', base_url='https://api.openai.com/v1'#可根据镜像站修改 ) #图片转base64函数 def encode_image(image_path): with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode('utf-8') #输入图片路径 image_path = "xxxxxx" #原图片转base64 base64_image = encode_image(image_path) #提交信息至GPT4o response = client.chat.completions.create( model="xxx",#选择模型 messages=[ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content":[ { "type": "text", "text": "xxxxxxxxxxxxxxxxxx" }, { "type": "image_url", "image_url":{ "url": f"data:image/jpeg;base64,{base64_image}" } }, ] } ], stream=True, ) reply = "" for res in response: content = res.choices[0].delta.content if content: reply += content print(content) print('reply:',reply)