MartialBE / one-hub

OpenAI 接口管理 & 分发系统,改自songquanpeng/one-api。支持更多模型,加入统计页面,完善非openai模型的函数调用。
https://one-hub.xiao5.info/
Apache License 2.0
1.14k stars 232 forks source link

接入qwen模型上传图片base64报错 #364

Closed caster-Q closed 1 month ago

caster-Q commented 1 month ago

例行检查

问题描述 接入阿里的千问视觉理解模型,使用HTTP方式上传图片没有问题,正常返回结果;使用本地图片base64编码上传报错

复现步骤 申请个人测试qwen的apikey,然后运行代码,随便传入一个本地的图片文件

from openai import OpenAI
import os
import base64

#  base 64 编码格式
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

def get_response(image_path):
    base64_image = encode_image(image_path)
    client = OpenAI(
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    )
    completion = client.chat.completions.create(
        model="qwen-vl-plus",
        messages=[
            {
              "role": "user",
              "content": [
                {
                  "type": "image_url",
                  "image_url": {
                    "url": f"data:image/jpeg;base64,{base64_image}"
                  }
                },
                {
                  "type": "text",
                  "text": "这是什么"
                }
              ]
            }
          ]
        )
    print(completion.model_dump_json())

if __name__=='__main__':
    get_response("test.png")

预期结果

{
  "id": "chatcmpl-a2076b9a-32db-9b07-8fb1-0dfb2122108f",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "message": {
        "content": "这张图片显示了一位女士和一只狗在海滩上互动。她们似乎正在享受彼此的陪伴,狗狗坐在沙滩上伸出爪子与这位女士握手或玩耍。\n\n背景中可以看到海浪拍打着海岸线,并且有日落时分柔和光线照射下的天空。这给人一种宁静而温馨的感觉,可能是在傍晚或者清晨的时候拍摄的照片。这种场景通常象征着友谊、爱以及人与宠物之间的深厚情感连接。",
        "refusal": null,
        "role": "assistant",
        "function_call": null,
        "tool_calls": null
      }
    }
  ],
  "created": 1727252174,
  "model": "qwen-vl-plus",
  "object": "chat.completion",
  "service_tier": null,
  "system_fingerprint": null,
  "usage": {
    "completion_tokens": 92,
    "prompt_tokens": 1275,
    "total_tokens": 1367,
    "completion_tokens_details": null
  }
}

实际结果结果 实际结果报错,未能识别到有效的URL: 企业微信截图_b9deb8b6-8011-4828-b212-af8606b79803 正常直接访问qwen的截图 image

MartialBE commented 1 month ago

因为阿里只能URL,不支持base64