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)
标签:url,image,base64,content,API,OpenAI,图像,reply,path From: https://www.cnblogs.com/Vicrooor/p/18227547