Build your first AI workflow with Dify

Build a simple chatbot that enriches model responses with information about countries.

ToolDify
Format
10 minEnglish
Updated 10 min

What you will create

Build a small chatbot that answers from a limited country table rather than asking a model to invent a fact sheet. This teaching workflow separates deterministic lookup in a Code node from phrasing in an LLM node. It is neither an encyclopedia nor a production-ready service.

Before you start

Use an authorized Dify workspace with a model provider already configured. Check costs and quotas before testing: model calls may be billed. Never put a secret key in code or prompts. This exercise uses neither personal data nor an external geography API.

Choose a Chatflow application, which supports conversation and the Answer node. Labels may differ on older installations. The steps below describe a proposed configuration to verify in your environment, not an account of an interface test.

Step-by-step

  1. Create a Chatflow and connect nodes in this order: User Input, Code, LLM, Answer. Initially ask the user to type only a country name.
  2. Select Python in Code. Declare a text input called country and bind it to the latest user message using the variable selector. Declare a text output called record, then paste the code below.
  3. In LLM, select a model available in your workspace. Add the instruction below and insert the Code node’s record output using the variable selector. Do not type a made-up variable path as plain text.
  4. In Answer, insert the LLM node’s text output. Keep conversation memory disabled for this single-country-per-request exercise.
  5. Use preview for the checking cases. Inspect the Code output first, then the model’s text. Do not publish a version that adds facts absent from the table.

Example to adapt

This minimal table contains two countries and their capitals. It accepts France, Japon or Japan; full sentences are deliberately not interpreted.

def main(country: str) -> dict:
    key = (country or "").strip().casefold()
    aliases = {"france": "FR", "japon": "JP", "japan": "JP"}
    data = {"FR": "France | Paris", "JP": "Japan | Tokyo"}
    record = data.get(aliases.get(key, ""), "NOT_FOUND")
    return {"record": record}

Enter this instruction in LLM, followed by the record variable: “Rephrase only the supplied record as one English sentence stating the country and its capital. Add no other fact. If the record is NOT_FOUND, reply exactly: Country absent from the demonstration table. Type France or Japan.”

This separation tells you where to fix an error. An unknown country must remain unknown even if the model knows its capital. An instruction alone is not an absolute guarantee, however: for strictly deterministic output, eventually prefer a response built directly from the data without free-form rephrasing.

Check your result

  • France: Code must return France | Paris.
  • JAPON surrounded by spaces: the output must be Japan | Tokyo.
  • Canada: expect NOT_FOUND even though the country exists.
  • France and Japan: expect NOT_FOUND rather than an arbitrary choice.
  • A new request must not reuse the country from the previous turn.
  • The final response must not add population, currency or travel advice.

Common problems

Variable error: check country at the input and record at the output. Correct code, wrong answer: inspect the prompt and the variable inserted into LLM. No visible text: check the connection to Answer. Model unavailable: review provider configuration and quotas with the administrator without putting a key into content. Every country is rejected: verify that country receives the user’s message rather than its literal variable name.

Next steps

Read the Dify overview. Then add one country and its corresponding checking case. Expand the accepted vocabulary only after deciding how to handle typos, ambiguous requests and missing data.