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

相关推荐
一路向阳~负责的男人2 分钟前
PyTorch / CUDA 是什么?它们的关系?
人工智能·pytorch·python
aloha_7898 分钟前
乐信面试准备
java·spring boot·python·面试·职场和发展·maven
云和数据.ChenGuang12 分钟前
CANN实现语音积分程序的测试
ide·macos·ai·cann·人工智能识别语音
火云洞红孩儿13 分钟前
零基础:100个小案例玩转Python软件开发!第六节:英语教学软件
开发语言·python
2401_8414956413 分钟前
深度卷积生成对抗网络(DCGAN)
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·深度卷积生成对抗网络
忧郁的橙子.20 分钟前
26期_01_Pyhton函数进阶
python
充值修改昵称24 分钟前
数据结构基础:B+树如何优化数据库性能
数据结构·b树·python·算法
AI殉道师25 分钟前
FastScheduler:让 Python 定时任务变得优雅简单
开发语言·python
Elastic 中国社区官方博客31 分钟前
Agent Builder 现已正式发布:在几分钟内发布上下文驱动的 agents
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索
小二·36 分钟前
Python Web 开发进阶实战:AI 伦理审计平台 —— 在 Flask + Vue 中构建算法偏见检测与公平性评估系统
前端·人工智能·python