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

相关推荐
weixin1997010801627 分钟前
加盟网 item_search - 根据关键词获取行业列表接口对接全攻略:从入门到精通
java·python
喵手27 分钟前
Python爬虫实战:采集巨潮资讯网等上市公司公告数据,通过智能关键词匹配技术识别分红、回购、停牌等重要信息(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集巨潮资讯数据·智能匹配识别分红、回购等信息·csv导出+sqlite
cyforkk27 分钟前
11、Java 基础硬核复习:常用类和基础API的核心逻辑与面试考点
java·python·面试
小鸡吃米…30 分钟前
机器学习 —— 数据缩放
人工智能·python·机器学习
JHC00000038 分钟前
智能体造论子--简单封装大模型输出审核器
开发语言·python·机器学习
diediedei38 分钟前
Python字典与集合:高效数据管理的艺术
jvm·数据库·python
【赫兹威客】浩哥38 分钟前
可食用野生植物数据集构建与多版本YOLO模型训练实践
开发语言·人工智能·python
气可鼓不可泄39 分钟前
将dmpython 封装在容器镜像里
数据库·python
m0_5613596741 分钟前
超越Python:下一步该学什么编程语言?
jvm·数据库·python
2301_810730101 小时前
python第三次作业
开发语言·python