Artificial intelligence technology continues to evolve, and chat apps are becoming increasingly feature-rich. Recently, the Dify platform launched a notable update, its newly released chat application can realize data visualization and analysis directly in the conversation, bringing users a more intuitive and efficient communication experience. Although the title of the article mentions that the function is "as effective as ChatGPT", there is still a gap in the actual application, which is more focused on technology demonstration and function exploration. The article lacks explanation of the principle, recommended reading Dify Workflows and AI Intelligent Assistants Reinvent the Enterprise Data Adoption Model The
ChatGPT's data visualization solution
In terms of data visualization, some leading AI products, such as ChatGPT, have been actively explored. It is reported that ChatGPT's data visualization solution is also to convert user commands into Python code, which is executed in the background and then displayed in the foreground. However, the pie chart generated by it has compatibility problems in Chinese display.
Dify Data Visualization Solution for Low-Code Platforms
Dify The platform takes a different approach by leveraging its low-code features so that users can quickly build chat applications with data visualization capabilities without writing complex code.
on the basis of ChatGPT Dify platform provides a simple solution: adjust the previously used Pandas code instructions to directly generate Pyecharts code, run in the background sandbox environment, get the generated images or HTML files. This is a relatively simple way to realize.
However, this paper will focus on Dify, a low-code intelligent body design platform, to show how easy it is to visualize data, so we will continue to optimize our previous workflow design to take full advantage of Dify's built-in tools.
Dify platform has a built-in chart generation tool that supports pie charts, bar charts, line graphs and other chart types. Users can intuitively understand the parameter requirements of these three charting tools, all of which are in string format and in the same format.
Workflow optimization: data format conversion
In order to match the output of the workflow to the parameter requirements of the Dify charting tool, the workflow needs to be optimized.
The following JSON data is an example of the output from the workflow in the previous tutorial, and is in a format that cannot be used directly in the Dify charting tool.
{"status": "success", "data_result": {"Schools": 2, "Internet": 1, "Logistics": 1, "Hardware": 1, "Educational Institutions": 1, "Traditional Retail": 1, "Traditional Media": 1, "Manufacturing": 1}}
Here you choose to use Python code for the data format conversion, and to do so you need to add a code execution node to the workflow and populate it with the appropriate Python code.
This Python code can be generated with the help of the Big Language Model, the built-in code generator in the Dify platform, just by clearly describing the requirements. Here's an example of the prompts used to generate the code.
{"status": "success", "data_result": {"school": 2, "internet": 1, "logistics": 1, "hardware": 1, "educational institutions": 1, "traditional retail": 1, "traditional media": 1, "manufacturing": 1}} Write a piece of code for the Pytho function on the side, which accepts json input, splices all the key values of data_result into a string with ";" separating each key, assigns them to the variable exl_ key, splices all the value values of data_result into a string with ";" separating each key, assigns them to the variable exl_ key, and splices all the value values of data_result into a string with ";" separating each key. result, accept json input, splice all key values of data_result into a string, with ";" separating each key, assign to variable exl_ key, splice all value values of data_result into a string, with ";" separating each value, assign to variable exl_value, exl_key exl_value returns
There are some minor issues with the code generated directly by the Dify code generator, for example the return result contains only one variable, whereas the Dify pie chart tool requires two. Therefore, the generated code needs to be modified to ensure that the return
statement returns a result of type dictionary (dict).
import json
def main(data: dict) -> dict.
data = json.loads(data)
exl_key = ";".join(data["data_result"].keys())
exl_value = ";".join(map(str, data["data_result"].values()))
return {'exl_key': exl_key, 'exl_value': exl_value}
There is a potential problem with the above code, if the data format generated by the large model does not match the expected {key: value, key: value}
structure, the code execution will report an error. This issue will be explored in a subsequent article, this article first focuses on the implementation of the function.
In addition, the format of the input variables received by the code execution node is also prone to errors. The default input is a string, which requires the use of the json
library to perform a forced type conversion. Otherwise, the following error will occur.
Workflow operation and results presentation
The workflow can now be run in its entirety to see the final results.
At this point, the workflow has been successfully configured to generate pie chart results based on the user's data analysis needs.
Workflow extensions: support for multiple chart types
However, if the user needs to generate bar charts or line graphs, the current workflow needs to be extended.
The idea of optimization is: firstly, we need to add a "Parameter Extraction" node, so that the big model can identify the needs of visualization charts, such as pie charts or bar charts, from user commands. Second, a "conditional branching" node should be added between the data processing and pie chart visualization nodes, so that different branching paths can be selected according to the parameter extraction results. The final workflow design is shown in the figure below.
Below we describe in detail how to configure the parameter extractor. As you can see from the configuration interface, it contains the following three main aspects:
- input variable: Specifies that the large model extracts parameters from the user command text.
- extraction parameter: Defines the parameters to be extracted, i.e. whether the user instruction contains chart type information. If relevant information is extracted, it is assigned to a new variable
exl_type
The - directives:: Cue words sent by the parameter extractor to the large model. The parameter extraction task is relatively simple for large models.
In this way, subsequent "conditional branching" nodes will be organized by the big model based on the extracted parameters. exl_type
The "Conditional Branch" node appears to be complex, but the principle is simple. The "Conditional Branching" node looks complicated, but the principle is simple: according to the type of chart output from the big model, select the corresponding branch to generate the corresponding chart.
Next, we demonstrate how the overall workflow works. The results show good results, indicating that the workflow can be applied to the Big Model Chat application.
Publish workflows as Dify tools and integrate into Agent applications
In order to integrate this workflow into Dify, you first need to publish it as a built-in tool of Dify. In the workflow interface, click the Publish button in the upper right corner, and then select Publish as Tool.
Note: This is a critical step. As a rule of thumb, workflows that are published as tools must have their parameter format set to text-input
type. Only then, in a Dify Agent application, will the Big Model be able to correctly invoke the tool via a Function Call. Otherwise, the big model will ignore the tool. Therefore, it is important to adjust the input variables of the workflow as shown in the following figure text-input
type before publishing it as a tool.
Once you have completed the above steps, you can integrate your data analytics workflow into Dify as a Function Call. Next, create an Agent application in Dify. If you are not familiar with Agent application, you can follow the steps first, and then we will understand its principle and application step by step. After that, I will write an article to compare the difference between different types of applications in Dify.
Next, simply add the published tool to the Agent application. At this point, the configuration is complete. Preview and debug directly to see the final result.
Through the above steps, the Dify platform successfully integrates chat applications with data visualization functions, providing users with a new interactive experience. With Dify's low-code features, users can quickly build powerful and intelligent applications and enjoy the convenience of AI technology without the need for in-depth coding.