Build An Article Reader Using File Upload
Last updated
Last updated
Author: Steven Lynn. Dify Technical Writer.
In Dify, you can use the knowledge base to allow agent to obtain accurate information from a large amount of text content. However, in many cases, the local files provided are not large enough to warrant the use of the knowledge base. In such cases, you can use the file upload feature to directly provide local files as context for the LLM to read.
In this experiment, we will build the article reader as a case study. This assistant will ask questions based on the uploaded document, helping users to read papers and other materials with those questions in mind.
File upload usage
Basic usage of Chatflow
Prompt writing skill
Iteration node usage
Doc extractor and list operator usage
Create a Chatflow in Dify. Make sure you have added a model provider and have sufficient quota.
In this experiment, at least four types of nodes are required: start node, document extractor node, LLM node, and answer node.
In the start node, you need to add a file variable. File upload is supported in v0.10.0 Dify, allowing you to add files as variable.
For more information on file upload, please read: File Upload
In the start node, you need to add a file variable and check the document in the supported file types.
Some readers might notice the sys.files
in the system variables, which are files or file lists uploaded by users in the dialog box.
The difference between creating your own file variables is that this feature requires enabling file upload in the functions and setting the upload file types, and each time a new file is uploaded in the dialog, this variable will be overwritten.
Please choose the appropriate file upload method according to your business scenario.
LLM cannot read files directly. This is a common misconception among many users when they first use file upload, as they might think simply using the file as a variable in an LLM node would work. However, in reality, the LLM reads nothing from file variables.
Thus, Dify introduced the doc extractor, which can extract text from the file variable and output it as a text variable.
In this experiment, two LLM nodes need to be designed: structure extraction and question generation.
The structure extraction node can extract the structure of the original text, summarizing key content.
The prompts are as follow:
The question generation node can summarize the issues of the article from the content summarized by the structure extraction node, assisting the reader in thinking through the questions during the reading process.
The prompts are as follow:
To handle multiple uploaded files, an iterative node is needed.
The iterative node is similar to the while loop in many programming languages, except that Dify has no conditional restrictions, and the input variable can only be of type array
(list). The reason is that Dify will execute all the content in the list until it is done.
Therefore, you need to adjust the file variable in the start node to an array
type, i.e., a file list.
In Question 1, some readers might notice that Dify will process all files before ending the loop, while in some cases, only a part of the files need to be operated on, not all. For this issue, you can process the file list in Dify using the list operation node. List operations can operate on all array-type variables, not just file lists.
For example, limit the analysis to only document-type files and sort the files to be processed in order of file names.
Before the iterative node, add a list operation, adjust the filter condiftion and order by, then change the input of the iterative node to the output of the list operation node.