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

相关推荐
崔庆才丨静觅8 分钟前
Sora Videos Generation API 对接说明
api·sora
humors22127 分钟前
四步生成喜欢的图片
人工智能·ai·图片·背景·祝福·头像
技术小甜甜33 分钟前
[AI 工程实践] 远程调用 Ollama 报错解析:如何解决“本地文件找不到”的误区
ai·自动化·llm·agent·ollama·’人工智能·aider
Damon小智1 小时前
【TextIn大模型加速器 + 火山引擎】跨国药企多语言手册智能翻译系统设计与实现
人工智能·ai·ocr·agent·火山引擎
540_5401 小时前
ADVANCE Day33
人工智能·python·机器学习
水龙吟啸1 小时前
基于Orbbec-Gemini深度相机与SFM-2D to 3D重建算法、手部识别视觉算法、Unity运动控制的3D水果切割游戏
python·深度学习·神经网络·c#·游戏引擎·3d视觉·3d重建
BBB努力学习程序设计2 小时前
深入理解 Python 中的深浅拷贝(Shallow Copy & Deep Copy):避免数据引用的 “坑”
python
BBB努力学习程序设计2 小时前
深入理解 Python 中的闭包(Closure):封装状态的函数式编程利器
python
澜莲花3 小时前
python图色之opencv基础---验证码实战
开发语言·python·opencv
import_random3 小时前
[python]dataframe二维数据 -- > 三维数据
python