Query @azure/openai with images?

题意 :使用图像与@azure/openai进行交互或查询

问题背景:

On chat.openai.com I can upload an image and ask chatgpt a question about it, with the existing openai and @azure/openai api however there doesn't seem to be a way to do this? The ChatCompletion object in both cases only take text prompts.

在chat.openai.com上,我可以上传一张图片并就它向ChatGPT提问,但是使用现有的openai和@azure/openai API时,似乎没有办法做到这一点?在这两种情况下,ChatCompletion对象都只接受文本提示。

Is this feautre supported at an api level?

这个特性在API级别上得到支持吗?

问题解决:

With OpenAI you just include your image as part of the message that you supply. Here is a piece from the code I use, which works whether you have an image or not:

在使用OpenAI时,你只需要将你的图像作为你提供消息的一部分包含进来。下面是我使用的一段代码,无论你是否有图像,它都能正常工作。

cs 复制代码
if image != '':
    # Get base64 string
    base64_image = encode_image(image)
    content = [
        {
            "type": "text",
            "text": your_prompt
        },
        {
            "type": "image_url",
            "image_url": {
                "url": f"data:image/jpeg;base64,{base64_image}"
            }
        }
    ]
else:
    content = your_prompt
messages.append({"role": "user", "content": content})

And then 然后

cs 复制代码
payload = {
    "model": model_name,
    "temperature": temperature,
    "max_tokens": tokens,
    "messages": messages
}

where encode_image() is defined: encode_image() 函数是在哪里定义的?

cs 复制代码
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

Currently you need to target OpenAI model gpt-4-vision-preview. Update: As @Michael suggests, it also works with gpt-4o.

目前你需要将目标设定为OpenAI的模型gpt-4-vision-preview。更新:如@Michael所建议的,它也适用于gpt-4o

相关推荐
huisheng_qaq32 分钟前
【Python基础篇-09】深入理解python的闭包与装饰器
python·闭包
软件开发技术深度爱好者1 小时前
中学生学习工具:多学科自学启导者(轻量版)实现
学习·ai·技术实践
编码者卢布1 小时前
【App Service 】WebJobs 多实例实验:谁在运行,什么时候运行?
开发语言·python
quantdash_cc2 小时前
Python 获取实时行情后如何进行批量筛选?从全市场快照到策略候选池
开发语言·python·数据分析·量化交易·股票数据·quantdash
小蜗 strong2 小时前
和电脑猜拳(随机程序应用)
服务器·前端·python
zhanghaha13142 小时前
AI Agent_5 AI 大语言模型基础(LLM)
ai
感谢地心引力3 小时前
来试试我做的音乐播放器吧
ai·rust·音乐·navidrome·music·tarui
华研前沿标杆游学3 小时前
企业标杆游学|走进东莞OPPO总部✨探秘智造与品牌出海
python
大侠归来3 小时前
C语言内存管理:从栈到堆的完整指南
c语言·开发语言·python
fundroid3 小时前
Android AI 开发,真正拉开差距的是工程闭环
android·ai·大模型·agent