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

相关推荐
刘马想放假3 小时前
CANviz 使用指南|Linux 安装、DBC 解码、实时曲线与 J1939 实战
linux·python·websocket
2601_967987374 小时前
python判断变量名不要调用任何函数实现
python·函数·语法·判断·变量名
卷无止境5 小时前
SALib敏感性分析的python库学习笔记
后端·python
duansamve6 小时前
Python3+Flask+PostgreSQL18+Vue3项目生产环境用docker如何部署?
python·docker·flask
特立独行的猫A6 小时前
少 3 个环节、少 1 次 ABI、少 3500 行代码:cj-tauri 与 Ionic/Capacitor 的鸿蒙桥接机制对比
java·后端·python
特立独行的猫A6 小时前
cj-tauri vs Rust/Tauri 同款播放器实测:编译 21 秒 vs 11 分钟,磁盘 12 MB vs 1.2 GB
java·后端·python
duansamve7 小时前
详解Flask中Session和Cookie
python·flask
威哥爱编程8 小时前
模拟面试智能体开发实战 04:面试聊到哪算哪?用状态机把整场面试编排起来
python·agent·harmonyos
威哥爱编程8 小时前
模拟面试智能体开发实战 03:AI 面试官太客气?人设 Prompt 让它像个真考官
python·agent·harmonyos
量化分析码农8 小时前
股票数据接口返回字段逐项解析:从在线验证到 Python 数据校验
python