Closed llama-factory closed 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:
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.
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.
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.
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.
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:
- Enable File Upload in the "Start" Node: In your workflow application, add a custom file variable in the "Start" node and name it
- 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
- 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.
- 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.
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:
Enable File Upload by Adding File Variables:
Use a Document Extractor 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.
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:
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.
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.
To pass a PDF file when using the API of a workflow in Dify, follow these steps:
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.
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.
To pass a PDF file when using the API of a workflow in Dify, follow these steps:
- 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.
- Use the API Endpoint: Use the
POST /workflows/run
endpoint to execute the workflow. Include the PDF file in thefiles
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
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.