Skip to main content

Dify AI: A Guide With Demo Project

Learn what Dify is and how to build an AI travel agent using its low-code drag-and-drop interface.
May 29, 2025  · 12 min read

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:

  1. Download or clone Dify from the official repository.
  2. 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.

Creating a black flow in Dify

To create the flow, we set a name and then hit the “Create” button:

Creating a Chatflow in Dify

This will create the following default flow:

The default Chatflow in Dify

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:

  1. Click the "Plugins" button in the top-right corner.

Adding the openai plugin on dify

  1. Click “Install from Marketplace.”

Accessing the marketplace on dify

  1. Type "openai" in the search box and click the OpenAI plugin.

Installing the OpenAI plugin on dify

  1. 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:

  1. Select the LLM node.

Selecting a Dify block

  1. Click the configuration icon next to the model name.

Configuring a LLM block on dify

  1. Click the model dropdown at the top.

The model dropdown

  1. Click "Model Provider Settings" at the bottom of the list.

Configuring the model provider on dify

  1. Click "Setup" in the API key section.

Model setup on dify

  1. Paste the API key and click “Save”.

Configuring the OpenAI API key on dify

Running the app

Now we can run the app by clicking the preview button:

Previewing the application.

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.

Sample chat on dify

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:

Adding a variable on dify

Let's create a variable to store the name of the user:

Variable configuration on dify

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:

Adding a block on dify

Then select the block you want to add. In this case, we select "IF/ELSE":

The IF/ELSE block on dify

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).

Controlling the flow with an IF/ELSE block on dify

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):

Results of flow control

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.

Parameter extractor block

We first add a Parameter Extractor block connected to the IF branch. Here's the block configuration:

Parameter extractor configuration on dify

  • The INPUT VARIABLE is set to sys.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.

Parameter configuration on dify

  • 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.

Variable assigner block

Make sure to assign the extracted name to the name variable.

Variable assigner configuration

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.

The name extractor flow

Here's an example interaction:

Sample chat with the name extractor flow in dify

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 of name.
  • 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:

Location extractor flow in dify

Here's a sample interaction:

Sample chat with the location extractor

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. 

The Code block

Configure it to receive a single parameter with the location value.

Configuring the code block

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.

Displaying the code block result

We now have a Chatflow that can tell us the weather forecast:

Sample what with the weather agent

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.

Activities variable configuration in dify

Here’s how we can create a flow to extract both variables:

The dual parameter extractor flow in dify

In this example, we start with a variable extraction block to extract both the location and the list of activities.

The dual parameter extractor configuration

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.

Requesting the values when the user didn't provide them.

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 travel agent LLM configuration.

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:

The final flow

Here's a sample interaction with the agent:

Sample interaction with the travel agent in dify

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.

Another sample interaction where the user directly provides the information.

Defining a custom greeting

You may notice that the agent started by greeting the user with a message:

Greeting message

To enable this, we need to activate the Conversation Opener feature.

Accessing Dify features

Enabling the greeting message on dify

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.

Accessing Dify tools

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.

Adding a tool block

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:

Introduction to AI Agents

Learn the fundamentals of AI agents, their components, and real-world use—no coding required.
Explore Course

François Aubry's photo
Author
François Aubry
LinkedIn
Full-stack engineer & founder at CheapGPT. Teaching has always been my passion. From my early days as a student, I eagerly sought out opportunities to tutor and assist other students. This passion led me to pursue a PhD, where I also served as a teaching assistant to support my academic endeavors. During those years, I found immense fulfillment in the traditional classroom setting, fostering connections and facilitating learning. However, with the advent of online learning platforms, I recognized the transformative potential of digital education. In fact, I was actively involved in the development of one such platform at our university. I am deeply committed to integrating traditional teaching principles with innovative digital methodologies. My passion is to create courses that are not only engaging and informative but also accessible to learners in this digital age.
Topics

Learn AI with these courses!

Track

AI Fundamentals

0 min
Discover the fundamentals of AI, dive into models like ChatGPT, and decode generative AI secrets to navigate the dynamic AI landscape.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

Tutorial

Flowise AI: A Guide With Demo Project

Learn how to build an AI agent that answers questions based on a CSV dataset using Flowise, a low-code workflow-based tool.
François Aubry's photo

François Aubry

12 min

Tutorial

Langflow: A Guide With Demo Project

Learn what Langflow is, how to install it, and how to build simple and custom AI agent workflows using Python.
François Aubry's photo

François Aubry

12 min

Tutorial

OpenAI's Audio API: A Guide With Demo Project

Learn how to build a voice-to-voice assistant using OpenAI's latest audio models and streamline your workflow using the Agents API.
François Aubry's photo

François Aubry

12 min

Tutorial

Lovable AI: A Guide With Demo Project

Learn how to build and publish a mobile app using Lovable AI, integrating it with Supabase for backend services and GitHub for version control.
François Aubry's photo

François Aubry

8 min

Tutorial

Mistral Agents API: A Guide With Demo Project

Learn how to build AI agents using Mistral's Agents API, and explore key concepts like tool usage, connectors, handoffs, and more.
Aashi Dutt's photo

Aashi Dutt

12 min

Tutorial

Google's Agent Development Kit (ADK): A Guide With Demo Project

Learn how to build a multi-agent travel assistant using Google's Agent Development Kit (ADK) and the Agent2Agent (A2A) protocol.
Aashi Dutt's photo

Aashi Dutt

12 min

See MoreSee More