Track
Dify is a user-friendly platform that helps you build AI applications with low or no code required. It works by dragging and dropping blocks with different functions. Information flows through the blocks and is processed and combined to generate answers.
In this article, I'll explain the fundamentals of Dify by walking you through the steps to create an AI travel agent.
We keep our readers updated on the latest in AI by sending out The Median, our free Friday newsletter that breaks down the week’s key stories. Subscribe and stay sharp in just a few minutes a week:
What Is Dify?
Dify is a platform designed to simplify the development of AI applications without requiring extensive coding skills. It provides a user-friendly, low-code environment where users can build apps by dragging and dropping different components.
Imagine Dify as a digital toolbox filled with ready-made blocks that can be pieced together to create functional software. This approach makes technology more accessible, allowing people with limited programming knowledge to bring their ideas to life.
Each type of block has inputs and transforms these into different outputs, depending on the block type. Information is passed around and transformed from block to block, producing the final result.
Getting Started With Dify
We have two ways of using Dify:
- Local setup.
- Using the cloud version.
In this tutorial, we use the cloud version. The cloud version of Dify offers a free plan, so it's possible to follow this tutorial without needing a paid subscription.
If you're interested in the local setup, the easiest way is to:
- Download or clone Dify from the official repository.
- Run Dify using Docker as described in the Quick Start section of the repository's README.
Creating a Chatflow With Dify
Dify provides several types of apps. We are going to focus on building a Chatflow app, which is an AI flow where users use a chat interface to interact with the agent.
To create a new Chatflow app, first click "Create from blank" and select the "Chatflow" option.
To create the flow, we set a name and then hit the “Create” button:
This will create the following default flow:
This flow has three blocks:
- The start block is responsible for starting the flow. In the case of a Chatflow, the flow is started with the user sending a message.
- The LLM node takes as input the user message and sends it to an LLM,
gpt-4
in this case. - The final node is used to display a message in the chat. In this example, it is connected to the LLM node, so the output of the LLM node is sent to the chat.
These three nodes together effectively create an AI chat similar to ChatGPT. Before running this app, we need to install and set up the OpenAI plugin.
Installing and setting up the OpenAI plugin
To install the OpenAI plugin, do:
- Click the "Plugins" button in the top-right corner.
- Click “Install from Marketplace.”
- Type "openai" in the search box and click the OpenAI plugin.
- Select the OpenAI plugin and click “Install.”
Next, we need to set up the OpenAI API key. If you don't have one yet, create it here. Note that using the OpenAI API isn't free, so you'll need to associate a payment method with the API key.
Once we have a key, we can configure it by:
- Select the LLM node.
- Click the configuration icon next to the model name.
- Click the model dropdown at the top.
- Click "Model Provider Settings" at the bottom of the list.
- Click "Setup" in the API key section.
- Paste the API key and click “Save”.
Running the app
Now we can run the app by clicking the preview button:
Right now, it's not very interesting as it's only an AI chatbot. Remember that it won’t do anything until the user sends a message.
Creating variables
Dify makes it possible to store the state of the application by assigning values to variables that are accessible to all blocks.
To create a variable, click the variable button:
Let's create a variable to store the name of the user:
This configuration created a variable named name
that stores a string (text). We left the default value empty, so initially the variable will be an empty string.
There are several ways we can populate it, but the most common way is to use an AI block to automatically figure it out from the conversation. We’ll learn how to do that later. First, we learn how to check the value of a variable to control the flow of the application.
IF/ELSE blocks
To check the value of a variable, we can use an IF/ELSE block. This block is used to execute different actions depending on the value of a variable.
To add a new block, click the "+" button at the bottom:
Then select the block you want to add. In this case, we select "IF/ELSE":
Then we click the block to configure it by selecting the name
variable we created before.
To test it, we add two Answer blocks. These blocks are used to display a message in the chat. We connect one of them to the IF
output (which runs when the condition is true) and the other to the ELSE
output (which runs when the condition is false).
For the IF
output, I set the string "Sorry, I don't know your name." while for the ELSE
, I set "Hello, {name}!" We can use the curly braces to inject a variable into a string. This will also be useful later on when building prompts.
We can try this Chatflow by clicking the "Preview" button. Note that a Chatflow is triggered by a user message, so we need to send a message first, like "Hi." Here are the results depending on whether or not the name is set (I set it manually in the interface variable for the second example):
Variable assigner and parameter extractor blocks
We can use AI to identify and extract the values of variables using a Parameter Extractor block. This block uses an LLM to extract information from the conversation.
We first add a Parameter Extractor block connected to the IF
branch. Here's the block configuration:
- The
INPUT VARIABLE
is set tosys.query
, which corresponds to the user message. This means that the Parameter Extractor block will try to extract the value from the user message. - In the
EXTRACTION PARAMETERS
, we define the information we are trying to extract. Unfortunately, we can't directly specify a variable here, so we need to redefine that information by clicking the “+” button.
- The
INSTRUCTION
field is the prompt to guide the LLM on what to do. - Next, we add a Variable Assigner block to assign the value extracted by the Parameter Extractor (if any) to the
name
variable.
Make sure to assign the extracted name
to the name
variable.
After this step, the name
variable might still be undefined because it could be the case that the user didn't provide their name. So we add another IF/ELSE block to check. If the name is set, we greet the user. Otherwise, we ask for their name.
Here's an example interaction:
This flow illustrates the fundamentals of Dify workflows. No matter what the user's message is, if the name isn't defined, it tries to extract the name from the message. If it fails, it asks for it. Otherwise, it just greets the user by their name.
At this point, there’s no LLM block, so the agent won’t do anything else.
Code block
Code blocks can be used to execute custom Python code. Let's modify the previous flow to extract the user location instead of the user name. Then we will use a Code block to make a request to a weather API and send the weather forecast to the user.
- Create a new variable named
location
. - Modify the IF/ELSE blocks to check for
location
instead ofname
. - Modify the Parameter Extractor block to extract the location instead of the name.
- Modify the Variable Assigner block to assign the extracted value to the
location
variable. - Modify the Answer blocks to ask for the location and display the location, respectively.
After these steps, the flow should look like this:
Here's a sample interaction:
The next step is to add a Code block after we have the location. This block makes a request to the OpenWeather API to ask for the weather forecast for that location.
This API is free to use, provided we don't make too many requests.
Replace the second Answer block with a Code block.
Configure it to receive a single parameter with the location
value.
For the Python code, here's how we can use the requests
package to get the weather from the OpenWeather API:
import requests
API_KEY = "PASTE_YOUR_API_KEY_HERE"
def main(location: str) -> str:
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
"q": location,
"appid": API_KEY,
"units": "metric",
}
response = requests.get(base_url, params=params)
data = response.json()
if response.status_code == 200:
weather_desc = data["weather"][0]["description"]
temp = data["main"]["temp"]
return {
"result": f"Current weather in {location}: {weather_desc}, Temperature: {temp}°C"
}
else:
return {
"result": "Could not retrieve weather data",
}
Finally, we connect the output of the Code node to an Answer node to display it to the user.
We now have a Chatflow that can tell us the weather forecast:
Project: Creating a Travel Planner AI Agent
We now have all the tools we need to build an AI travel agent. The possibilities are endless, but to avoid overcomplicating this tutorial, we'll keep it simple.
The agent will focus on planning a single day of the trip. It will start by gathering the user's location and the types of activities they like to do. To do this, we use two variables:
location
: Store the location where the user is traveling.activities
: An array of strings storing the activities the user wants to do.
Here’s how we can create a flow to extract both variables:
In this example, we start with a variable extraction block to extract both the location
and the list of activities
.
Then we use IF/ELSE blocks to check whether each of these parameters was extracted and update the variables if they were. Contrary to our previous examples, this makes it possible for the user to update the values.
At the end of the value extraction and variable assignment, we add another IF/ELSE block to check whether there are missing values. If something is missing, we explicitly ask the user to provide it.
If both location
and activities
are defined, we use the Code block we created before to get the weather in the given location. Finally, we provide the weather, the location, and the activities to an LLM block to plan out the user’s day.
The LLM block is configured with a system prompt that tells the model what to do. The location, activities, and weather are injected into it. Here’s the final part of the flow:
Here's a sample interaction with the agent:
We see in the response that, because of the Code node, the agent can provide the weather to the user and take it into account when planning the activities.
Because of the way the flow was set up, if the user provides the information straight away in the first message, the agent will directly provide a plan for the day.
Defining a custom greeting
You may notice that the agent started by greeting the user with a message:
To enable this, we need to activate the Conversation Opener feature.
Dify Tools
In this tutorial, we learned the fundamental building blocks of Dify. One aspect we didn't cover was tools.
In Dify, tools are powerful extensions that allow your AI agents to interact with the outside world beyond the limitations of a language model. While an LLM can reason and generate responses based on its training data and prompt context, it doesn't have real-time access to live data or external services.
Tools solve this by enabling your agent to make API calls, query databases, perform calculations, retrieve documents, and more, essentially acting as a bridge between your AI and external information sources.
You can configure tools in Dify through the platform's visual interface by specifying the tool type (such as HTTP requests, code interpreters, or third-party plugins), the input/output schema, and any authentication requirements. Once set up, these tools can be used in your agent flow via Tool blocks, which allow you to pass data to and from the tool and feed the results into LLM nodes or other logic blocks.
This makes your agents not only more intelligent but also action-oriented, capable of providing up-to-date, contextually relevant answers by interacting with real-world services.
In our example, we used a code block to make a request to the weather API. An alternative would have been to create a tool and provide that tool to the LLM.
Conclusion
In this tutorial, we've explored some of the fundamental blocks you will need to begin working with Dify. However, Dify offers many more features and capabilities to discover.
To solidify your understanding, a great exercise would be to enhance the travel agent we created. Consider adding features such as internet search capabilities to find local activities online, which can enrich the agent’s functionality and provide more dynamic suggestions.
If you're keen on AI agent builders like Dify, I recommend checking out our other tutorials for further learning: