dify icon indicating copy to clipboard operation
dify copied to clipboard

When you access glm4v through the API, the image ID cannot be recognized

Open shatang123 opened this issue 1 year ago • 1 comments

Self Checks

  • [X] This is only for bug report, if you would like to ask a question, please head to Discussions.
  • [X] I have searched for existing issues search for existing issues, including closed ones.
  • [X] I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • [X] Please do not modify this template :) and fill in all the required fields.

Dify version

0.6.15

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

下面是我请求的代码

import requests

api_key = 'app-69M5xKCZs70EMgtSKGhXl2YZ' 
conversation_id = ""
file_headers = {
    'Authorization': f'Bearer {api_key}'
}
file_path = "354.jpg"
files = {'file': (file_path, open(file_path, 'rb'), 'image/jpeg')}
data = {'user':" abc-123"}
response = requests.post("http:/xxxxxxx/v1/files/upload", headers=file_headers, files=files, data=data)
file_id = response.json()["id"]
print(f"file: {response.json()}")

headers = {
    'Authorization': f'Bearer {api_key}',
    'Content-Type': 'application/json'
}
data = {
    "inputs": "",
    "query": "你看到了什么?",
    "response_mode": "blocking",
    "conversation_id":conversation_id,
    "user": "abc-123",
    "files": [
      {
        "type": "image",
        "transfer_method": "local_file",
        "upload_file_id": file_id
      }
    ]
    
}
response = requests.post("http://xxxxxxx/v1/chat-messages", headers=headers, json=data)
response.json()

✔️ Expected Behavior

成功返回数据

❌ Actual Behavior

实际接口返回上传文件格式不正确 file: {'id': '9184d15c-c816-4ee7-ab81-cbcf6c27a2f2', 'name': '354.jpg', 'size': 56899, 'extension': 'jpg', 'mime_type': 'image/jpeg', 'created_by': '4724e8ce-799a-4552-914a-1be524810f1b', 'created_at': 1723270441} {'code': 'invalid_param', 'message': 'Invalid upload file', 'status': 400}

shatang123 avatar Aug 10 '24 06:08 shatang123

Hello, @shatang123! I'm here to assist you with any bugs, questions, or contributions while we wait for a human maintainer. Let's get to the bottom of this together!

The error message "Invalid upload file" indicates that the upload_file_id parameter is not being recognized correctly. Here are a few things to check and ensure:

  1. File Upload API: Make sure the file is uploaded correctly and the file_id is valid. The response from the file upload should include the file's ID, name, size, extension, mime-type, created_by (end-user ID), and creation timestamp.

  2. File Type and MIME Type: Ensure that the file type and MIME type are supported. The valid file formats and MIME types supported by the /v1/files/upload endpoint are:

    • Formats: png, jpg, jpeg, webp, gif, svg
    • MIME types: image/png, image/jpeg, image/webp, image/gif, image/svg+xml [1][2][3][4][5].
  3. API Endpoint: Verify that the API endpoint URLs are correct and accessible.

  4. File Association: Ensure that the upload_file_id is associated with the correct tenant and user. The file must be uploaded by the same user who is making the request to the /v1/chat-messages endpoint [1][6][7][2][3].

  5. Configuration Limits: Check if there are any specific configuration limits, such as the number of files allowed and the transfer methods enabled.

Here is a revised version of your code with some additional checks:

import requests

api_key = 'app-69M5xKCZs70EMgtSKGhXl2YZ' 
conversation_id = ""
file_headers = {
    'Authorization': f'Bearer {api_key}'
}
file_path = "354.jpg"
files = {'file': (file_path, open(file_path, 'rb'), 'image/jpeg')}
data = {'user': "abc-123"}
response = requests.post("http://xxxxxxx/v1/files/upload", headers=file_headers, files=files, data=data)
file_id = response.json().get("id")
print(f"file: {response.json()}")

if not file_id:
    raise ValueError("File upload failed, no file ID returned")

headers = {
    'Authorization': f'Bearer {api_key}',
    'Content-Type': 'application/json'
}
data = {
    "inputs": "",
    "query": "你看到了什么?",
    "response_mode": "blocking",
    "conversation_id": conversation_id,
    "user": "abc-123",
    "files": [
        {
            "type": "image",
            "transfer_method": "local_file",
            "upload_file_id": file_id
        }
    ]
}
response = requests.post("http://xxxxxxx/v1/chat-messages", headers=headers, json=data)
print(response.json())

Ensure that the file upload is successful and the file_id is correctly obtained before making the request to the /v1/chat-messages endpoint. If the issue persists, double-check the file type and MIME type, and ensure that the file is correctly associated with the user.

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Aug 10 '24 06:08 dosubot[bot]

Because you are using the wrong user.

file_path = "354.jpg"
files = {'file': (file_path, open(file_path, 'rb'), 'image/jpeg')}
data = {'user':" abc-123"}
response = requests.post("http:/xxxxxxx/v1/files/upload", headers=file_headers, files=files, data=data)
file_id = response.json()["id"]
print(f"file: {response.json()}")

See this user, you have a blank space in it.

crazywoola avatar Aug 12 '24 01:08 crazywoola