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
53.04k stars 7.74k forks source link

workflow api how to upload pdf? #9839

Closed llama-factory closed 1 month ago

llama-factory commented 1 month ago

Self Checks

Provide a description of requested docs changes

After setting up a workflow that requires uploading a PDF file, I don't know how to use the API to transfer the PDF file. I want to use this workflow in my backend service. I have consulted documents including issues for a long time, but still haven't found out how to access this workflow using the API. Please help. image

dosubot[bot] commented 1 month ago

Hello, @llama-factory! I'm here to help you with any bugs, questions, or contributions you need assistance with. Let's work together to solve any issues you encounter.

To upload a PDF file using the Dify workflow API in your backend service, you can follow these steps:

  1. Enable File Upload in the "Start" Node: In your workflow application, add a custom file variable in the "Start" node and name it pdf. This will allow users to upload a PDF file. The new comprehensive "File Upload" feature supports various file types, including documents like PDFs.

  2. Add a Document Extractor Node: After the file is uploaded, it is stored in the file variable. Since LLMs cannot directly read document contents, add a "Document Extractor" node to extract content from the uploaded PDF file. Use the pdf variable from the "Start" node as the input variable for the "Document Extractor" node. The document extractor will convert the PDF content into a string-type output variable.

  3. Process the Extracted Content with an LLM Node: Add an LLM node and use the output variable of the "Document Extractor" node in the system prompt. This allows the LLM to process the extracted text from the PDF file.

  4. Configure the End Node: Select the output variable of the LLM node in the end node to complete the workflow.

Here is a simplified code representation of the steps:

# Assuming you have a workflow setup with nodes

# Step 1: Add file variable in the "Start" node
file_variable = add_file_variable(node="Start", type="Single File", name="pdf")

# Step 2: Add Document Extractor node
document_extractor_node = add_document_extractor(input_variable=file_variable)

# Step 3: Process the extracted content in LLM node
llm_node = add_llm_node(system_prompt=document_extractor_node.output_variable)

# Step 4: Configure the end node
end_node = configure_end_node(output_variable=llm_node.output_variable)

This setup allows your backend service to handle PDF file uploads and process their content using the workflow API, enabling interactions such as ChatPDF [1].

Additionally, for the specific API endpoint to upload files, you can use the following:

This endpoint allows you to create a new document through a file based on existing Knowledge [2].

To continue talking to Dosu, mention @dosu.

llama-factory commented 1 month ago

Hello, @llama-factory! I'm here to help you with any bugs, questions, or contributions you need assistance with. Let's work together to solve any issues you encounter.

To upload a PDF file using the Dify workflow API in your backend service, you can follow these steps:

  1. Enable File Upload in the "Start" Node: In your workflow application, add a custom file variable in the "Start" node and name it pdf. This will allow users to upload a PDF file. The new comprehensive "File Upload" feature supports various file types, including documents like PDFs.
  2. Add a Document Extractor Node: After the file is uploaded, it is stored in the file variable. Since LLMs cannot directly read document contents, add a "Document Extractor" node to extract content from the uploaded PDF file. Use the pdf variable from the "Start" node as the input variable for the "Document Extractor" node. The document extractor will convert the PDF content into a string-type output variable.
  3. Process the Extracted Content with an LLM Node: Add an LLM node and use the output variable of the "Document Extractor" node in the system prompt. This allows the LLM to process the extracted text from the PDF file.
  4. Configure the End Node: Select the output variable of the LLM node in the end node to complete the workflow.

Here is a simplified code representation of the steps:

# Assuming you have a workflow setup with nodes

# Step 1: Add file variable in the "Start" node
file_variable = add_file_variable(node="Start", type="Single File", name="pdf")

# Step 2: Add Document Extractor node
document_extractor_node = add_document_extractor(input_variable=file_variable)

# Step 3: Process the extracted content in LLM node
llm_node = add_llm_node(system_prompt=document_extractor_node.output_variable)

# Step 4: Configure the end node
end_node = configure_end_node(output_variable=llm_node.output_variable)

This setup allows your backend service to handle PDF file uploads and process their content using the workflow API, enabling interactions such as ChatPDF [1].

Additionally, for the specific API endpoint to upload files, you can use the following:

  • Create documents from files Request example:
    curl --location --request POST 'https://api.dify.ai/v1/datasets/{dataset_id}/document/create_by_file' \
    --header 'Authorization: Bearer {api_key}' \
    --form 'data="{"indexing_technique":"high_quality","process_rule":{"rules":{"pre_processing_rules":[{"id":"remove_extra_spaces","enabled":true},{"id":"remove_urls_emails","enabled":true}],"segmentation":{"separator":"###","max_tokens":500}},"mode":"custom"}}";type=text/plain' \
    --form 'file=@"/path/to/file"'

This endpoint allows you to create a new document through a file based on existing Knowledge [2].

To continue talking to Dosu, mention @dosu.

Do you mean that if the workflow API I want to access requires file upload, do I first need to use the interface https://api.dify.ai/v1/datasets to upload a PDF file? But I don't want to create datasets. I just want to upload a temporary file for this access only.

dosubot[bot] commented 1 month ago

To upload a temporary file in Dify without creating a dataset, you can use the file upload feature in the workflow. Here's how you can handle temporary file uploads:

  1. Enable File Upload by Adding File Variables:

    • In your workflow, add a file variable in the "Start" node. You can choose "Single File" as the field type for the variable. This allows you to upload a PDF file temporarily for processing.
  2. Use a Document Extractor Node:

    • After uploading the file, use a "Document Extractor" node to convert the PDF content into text. This text can then be processed by an LLM node.

These steps allow you to handle temporary file uploads without the need to create a dataset. This method is suitable for scenarios where you need to process files temporarily during a workflow execution [1].

To continue talking to Dosu, mention @dosu.

llama-factory commented 1 month ago

To upload a temporary file in Dify without creating a dataset, you can use the file upload feature in the workflow. Here's how you can handle temporary file uploads:

  1. Enable File Upload by Adding File Variables:

    • In your workflow, add a file variable in the "Start" node. You can choose "Single File" as the field type for the variable. This allows you to upload a PDF file temporarily for processing.
  2. Use a Document Extractor Node:

    • After uploading the file, use a "Document Extractor" node to convert the PDF content into text. This text can then be processed by an LLM node.

These steps allow you to handle temporary file uploads without the need to create a dataset. This method is suitable for scenarios where you need to process files temporarily during a workflow execution [1].

To continue talking to Dosu, mention @dosu.

Yes, I know. I have already built a workflow using this method. But what I don't know now is how to pass a PDF file when using the API of this workflow.

dosubot[bot] commented 1 month ago

To pass a PDF file when using the API of a workflow in Dify, follow these steps:

  1. Add File Variables in the "Start" Node: Ensure your workflow's "Start" node includes a file variable, such as "Single File," to accept the PDF.

  2. Use the API Endpoint: Use the POST /workflows/run endpoint to execute the workflow. Include the PDF file in the files array of the request body.

Here's an example of how to structure your API request using cURL:

curl -X POST '${props.appDetail.api_base_url}/workflows/run' \
--header 'Authorization: Bearer {api_key}' \
--header 'Content-Type: application/json' \
--data-raw '{
    "inputs": {},
    "response_mode": "streaming",
    "user": "abc-123",
    "files": [
        {
            "type": "document",
            "transfer_method": "local_file",
            "upload_file_id": "your_upload_file_id"
        }
    ]
}'

Make sure to replace {api_key} with your actual API key and "your_upload_file_id" with the ID of the uploaded PDF file. This setup allows you to upload and process a PDF file temporarily using the workflow API [1].

To continue talking to Dosu, mention @dosu.

llama-factory commented 1 month ago

To pass a PDF file when using the API of a workflow in Dify, follow these steps:

  1. Add File Variables in the "Start" Node: Ensure your workflow's "Start" node includes a file variable, such as "Single File," to accept the PDF.
  2. Use the API Endpoint: Use the POST /workflows/run endpoint to execute the workflow. Include the PDF file in the files array of the request body.

Here's an example of how to structure your API request using cURL:

curl -X POST '${props.appDetail.api_base_url}/workflows/run' \
--header 'Authorization: Bearer {api_key}' \
--header 'Content-Type: application/json' \
--data-raw '{
    "inputs": {},
    "response_mode": "streaming",
    "user": "abc-123",
    "files": [
        {
            "type": "document",
            "transfer_method": "local_file",
            "upload_file_id": "your_upload_file_id"
        }
    ]
}'

Make sure to replace {api_key} with your actual API key and "your_upload_file_id" with the ID of the uploaded PDF file. This setup allows you to upload and process a PDF file temporarily using the workflow API [1].

To continue talking to Dosu, mention @dosu.

How can I obtain the upload_file_id? @dosu @crazywoola