langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
51.16k stars 7.37k forks source link

Invalid upload file #8573

Closed jiegod closed 1 month ago

jiegod commented 1 month ago

Self Checks

Dify version

0.8.0

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

i use upload api upload file then use file id post chat-message,return Invalid upload file image image

✔️ Expected Behavior

return success

❌ Actual Behavior

{ "code": "invalid_param", "message": "Invalid upload file", "status": 400 }

dosubot[bot] commented 1 month ago

I found a similar issue and discussions that might be relevant to your problem:

  1. Why this not work - Unsolved [1].
  2. I can't upload the image on dify workflow via api - Unsolved [2].
  3. 知识库上传文件报错 - Unsolved [3].
  4. api request error - Closed [4].

The error you're encountering, "Invalid upload file," with a 400 status code, is likely due to the create_chat_message method in the ChatClient class sending the file IDs as part of the JSON payload, while the API expects the files to be uploaded as multipart/form-data.

To resolve this, ensure that the files are uploaded correctly using the file_upload method and then use the returned file IDs in the create_chat_message method. Here is an example of how you can do this:

  1. Upload the files using the file_upload method:

    <?php
    
    require 'vendor/autoload.php';
    
    use GuzzleHttp\Client;
    
    class DifyClient {
        // ... (existing code)
    
        public function file_upload($user, $files) {
            $data = ['user' => $user];
            $options = [
                'multipart' => $this->prepareMultipart($data, $files)
            ];
    
            return $this->file_client->request('POST', 'files/upload', $options);
        }
    
        protected function prepareMultipart($data, $files) {
            $multipart = [];
            foreach ($data as $key => $value) {
                $multipart[] = [
                    'name' => $key,
                    'contents' => $value
                ];
            }
    
            foreach ($files as $file) {
                $multipart[] = [
                    'name' => 'file',
                    'contents' => fopen($file['tmp_name'], 'r'),
                    'filename' => $file['name']
                ];
            }
    
            return $multipart;
        }
    }
    
    class ChatClient extends DifyClient {
        public function create_chat_message($inputs, $query, $user, $response_mode = 'blocking', $conversation_id = null, $files = null) {
            $data = [
                'inputs' => $inputs,
                'query' => $query,
                'user' => $user,
                'response_mode' => $response_mode,
            ];
            if ($conversation_id) {
                $data['conversation_id'] = $conversation_id;
            }
            if ($files) {
                $data['files'] = $files;
            }
    
            return $this->send_request('POST', 'chat-messages', $data, null, $response_mode === 'streaming');
        }
    }
    
    // Usage example
    $api_key = 'your_api_key';
    $base_url = 'https://api.dify.ai/v1/';
    $user = 'your_user_id';
    $files = [
        ['tmp_name' => '/path/to/file1.txt', 'name' => 'file1.txt'],
        ['tmp_name' => '/path/to/file2.txt', 'name' => 'file2.txt']
    ];
    
    $client = new ChatClient($api_key, $base_url);
    
    // Step 1: Upload files
    $response = $client->file_upload($user, $files);
    $file_ids = json_decode($response->getBody(), true)['file_ids'];
    
    // Step 2: Create chat message with file IDs
    $inputs = ['input1' => 'value1'];
    $query = 'Your query here';
    $response_mode = 'blocking';
    $conversation_id = null;
    
    $response = $client->create_chat_message($inputs, $query, $user, $response_mode, $conversation_id, $file_ids);
    echo $response->getBody();
  2. Ensure the files being uploaded are of the correct type and format as expected by the Dify API. The supported image formats are png, jpg, jpeg, webp, and gif [5][6][7][8].

  3. Set the transfer_method parameter to local_file when using the uploaded file ID in the create_chat_message method. This ensures that the API correctly interprets the file ID as a locally uploaded file [9].

By following these steps, you should be able to resolve the "Invalid upload file" error.

To continue talking to Dosu, mention @dosu.

crazywoola commented 1 month ago

QQ_1726912155814

The user is missing in this api request.

chaozZLF commented 1 month ago

你需要在最后传入userid而且两者要一致

zhangrenliang1 commented 2 weeks ago

哈哈,我也遇到这个问题。捣鼓了半天原来要userid一样。我还以为有bug的