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

相关推荐
OliverZhao10 分钟前
iPhoto:基于 Python + PySide6 的高性能 macOS 风格照片管理器
python
SoRound12 分钟前
【Shopee Games AI 模型使用经验】年度总结之 ------ 识别人脸特征,生成动漫形象
python·openai
郝学胜-神的一滴15 分钟前
机器学习特征预处理:缺失值处理全攻略
人工智能·python·程序人生·机器学习·性能优化·sklearn
rgeshfgreh15 分钟前
Python闭包:函数记住状态的秘密
开发语言·python
金智维科技官方16 分钟前
金智维出席2025年粤港澳大湾区人工智能与机器人产业大会,AI数字员工和智能体成关注焦点
人工智能·ai·智能体·数字员工
乂爻yiyao22 分钟前
LLM Agent 增强架构
ai
有为少年25 分钟前
PyTorch 的统计三剑客:bucketize, bincount 与 histogram
pytorch·python·学习·机器学习·统计
sunsunyu0333 分钟前
基于OpenCV的图像重复检测算法实战
python·计算机视觉
enjoy编程43 分钟前
Spring-AI 脱离 IDE 的束缚:OpenCode 让 AI 开发回归终端本源
人工智能·spring·ai·claude·gemini·claude code·opencode
码农三叔43 分钟前
(8-3-02)自动驾驶中的无地图环境路径探索:D* Lite路径规划系统(2)
人工智能·python·机器人·自动驾驶·路径规划·d star lite