All Products
Search
Document Center

AnalyticDB:Building a guided conversational chatbot with GraphRAG

Last Updated:Dec 15, 2025

This guide shows you how to use the decision tree mode in the AnalyticDB for PostgreSQL GraphRAG service to build a multi-turn, guided conversational agent. This topic describes how to build a guided questioning chatbot using the GraphRAG service of AnalyticDB for PostgreSQL. This solution is ideal for customer support scenarios, such as troubleshooting smart appliances or guiding users through complex processes. By using a decision tree, the agent can ask clarifying questions to diagnose a user's problem and provide a precise solution efficiently.

Background

In the smart electronics industry represented by white household appliances, the demand for consultation-type customer service systems is increasingly prominent. These systems aim to accurately identify problems that users encounter during product use through multi-turn questioning, and provide practical solutions accordingly. This not only effectively improves user experience but also further optimizes after-sales service processes. In a highly competitive market environment, these intelligent customer service platforms play a key role with their excellent problem diagnosis capabilities and efficient service response mechanisms.

When handling common user issues, industry experts typically construct one or more decision tree models. These decision trees are designed based on specific application scenarios and narrow down broad questions through follow-up questioning dialogues with users, ultimately locating specific situations. This hierarchical analysis method enables the transition from macro issues to micro-specific contexts, providing users with precise and targeted solutions.

Version limitations

AnalyticDB for PostgreSQL 7.0 instances with kernel version 7.2.1.3 or later.

Note

You can view the minor version on the Basic Information page of an instance in the AnalyticDB for PostgreSQL console. If your instance does not meet the required versions, update the minor version of the instance.

Prerequisites

  1. Verify the plpython3u extension This extension is usually installed by default. To verify, connect to your database and run:

    SELECT * FROM pg_extension WHERE extname = 'plpython3u';

    The following result indicates that the plpython3u extension is successfully installed. If no result is returned, the extension is not installed in the public schema of the specified database.

      oid  |  extname   | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition 
    -------+------------+----------+--------------+----------------+------------+-----------+--------------
     14674 | plpython3u |       10 |           11 | f              | 1.0        |           | 
    (1 row)
  2. Install the age extension.

    1. The ag_catalog schema is added to the search path specified by the search_path parameter to simplify queries. You can use one of the following methods:

      • Session-level configuration.

        SET search_path TO public, ag_catalog;
      • Database-level permanent configuration.

        ALTER DATABASE <database_name> SET search_path TO public, ag_catalog;
    2. (Optional) The initial account or a privileged account that has the RDS_SUPERUSER permission is used to grant other users access to the ag_catalog schema.

      GRANT USAGE ON SCHEMA ag_catalog TO <username>;
  3. To install the adbpg_graphrag extension, please contact technical support for assistance.

    The adbpg_graphrag extension depends on the plpython3u and age extensions. Before installing this extension, make sure the dependent extensions are installed.

How multi-turn interaction works

Decision tree

A decision tree is a tree-like structure where the root node is the starting point of the tree representing the entire dataset, internal nodes represent judgment conditions for features (attributes), branches represent output results of certain judgment conditions, and leaf nodes are the final decision results. A simple air conditioner customer service decision tree is shown in the following figure.

image

GraphRAG service

AnalyticDB for PostgreSQL GraphRAG service provides a decision tree-based model for implementing multi-turn interactive guided conversations. This model improves the efficiency and accuracy of information retrieval and conversation management through a series of carefully designed questions that gradually refine user needs or problems, enhancing the human-computer interaction experience.

Procedure

Step1: Initialize the service for tree mode

Before uploading your tree, you must initialize the GraphRAG service. For tree mode, you can specify custom greetings and closing messages.

Syntax

SELECT adbpg_graphrag.initialize(config json);

Parameters

The adbpg_graphrag.initialize function takes a JSON variable as an input parameter to configure the GraphRAG service. The configuration parameters are described as follows:

Parameter

Description

llm_model

Specifies the large language model used by GraphRAG. The default is qwen-max-2025-01-25.

llm_api_key

The API key required to call the large language model.

llm_url

The API operation address of the large language model. By default, the API of Alibaba Cloud Model Studio is used.

Note

embedding_model

Specifies the embedding model used by GraphRAG. The default is text-embedding-v3.

language

The default language used by GraphRAG. The default is English.

  • English: English.

  • Simplified Chinese: Simplified Chinese.

entity_types

Specifies the types of entity nodes to extract when parsing documents to generate knowledge graphs.

relationship_types

Specifies the types of relationship edges to extract when parsing documents to generate knowledge graphs.

first_node_content

When using the decision tree mode (tree), specifies the initial node response. The default is "Hello, how can I help you?"

end_node_content

When using the decision tree mode (tree), specifies the final node response. The default is "Do you have any other questions?"

global_distance_threshold

When using the decision tree mode (tree), specifies the threshold for selecting global search results among local and global results. The default is 0.1, which means that when the similarity between the global search and the customer's reply is greater than 0.1 compared to the local search, the global search result is selected.

Example

SELECT adbpg_graphrag.initialize(
	$$
	{
		"llm_model" : "qwen-max-2025-01-25",
		"llm_api_key" : "sk-****************",
		"llm_url" : "https://dashscope.aliyuncs.com/compatible-mode/v1",
		"embedding_model" : "text-embedding-v3",
		"language" : "Simplified Chinese",
      	        "first_node_content" : "Hello, how can I help you?",
      	        "end_node_content" : "Do you have any other questions?",
      	        "global_distance_threshold" : "0.1"
	}
	$$
);

Step 2: Upload your decision tree

You can upload your formatted decision tree directly as a string or from a table in your database. The service uses the upload_decision_tree function.

In addition to external import, the system supports importing decision tree data in Markdown format. In Markdown format text, indentation is used to represent the hierarchical relationships between nodes and branches.

The air conditioner customer service decision tree converted to Markdown format is as follows.

Hello, how can I help you?
    The air conditioner's cooling effect is poor
        Is the machine not blowing air or not cooling
            The machine is not cooling
                Please provide the operating mode of the air conditioner
                    Cooling mode
                        Has the filter not been cleaned for a long time
                            Yes
                                Please clean the filter and then observe the cooling performance of the air conditioner
                            No
                                Please call our repair hotline, a maintenance engineer will visit for inspection
                    Non-cooling mode
                        Please switch to cooling mode
            The machine is not blowing air
                Does the air conditioner display a sensor fault
                    Yes
                        Please power off the air conditioner for 3 minutes and restart it, to see if it returns to normal
                    No
                        Please call our repair hotline, a maintenance engineer will visit for inspection
    No more issues
        Complete

Upload directly from a string

Syntax

SELECT adbpg_graphrag.upload_decision_tree(original_content text, root_content text) FROM documents_text;

Parameters

  • original_content: Decision tree information excluding the root node.

  • root_content: Root node information of the decision tree.

Example

SELECT adbpg_graphrag.upload_decision_tree('The air conditioner's cooling effect is poor
        Is the machine not blowing air or not cooling
            The machine is not cooling
                Please provide the operating mode of the air conditioner
                    Cooling mode
                        Has the filter not been cleaned for a long time
                            Yes
                                Please clean the filter and then observe the cooling performance of the air conditioner
                            No
                                Please call our repair hotline, a maintenance engineer will visit for inspection
                    Non-cooling mode
                        Please switch to cooling mode
            The machine is not blowing air
                Does the air conditioner display a sensor fault
                    Yes
                        Please power off the air conditioner for 3 minutes and restart it, to see if it returns to normal
                    No
                        Please call our repair hotline, a maintenance engineer will visit for inspection
    No more issues
        Complete', 'Hello, how can I help you?');

Upload from a table

  1. Create a table and insert decision tree data.

    CREATE TABLE documents_text (id integer, original_content text);
    INSERT INTO documents_text VALUES (1, 'The air conditioner's cooling effect is poor
            Is the machine not blowing air or not cooling
                The machine is not cooling
                    Please provide the operating mode of the air conditioner
                        Cooling mode
                            Has the filter not been cleaned for a long time
                                Yes
                                    Please clean the filter and then observe the cooling performance of the air conditioner
                                No
                                    Please call our repair hotline, a maintenance engineer will visit for inspection
                        Non-cooling mode
                            Please switch to cooling mode
                The machine is not blowing air
                    Does the air conditioner display a sensor fault
                        Yes
                            Please power off the air conditioner for 3 minutes and restart it, to see if it returns to normal
                        No
                            Please call our repair hotline, a maintenance engineer will visit for inspection
        No more issues
            Complete');
  2. Import decision tree data from the table.

    Syntax

    SELECT adbpg_graphrag.upload_decision_tree(original_content text, root_content text) FROM documents_text WHERE id = 1;

    Parameters

    • original_content: Decision tree information excluding the root node.

    • root_content: Root node information of the decision tree.

    Example

    SELECT adbpg_graphrag.upload_decision_tree(original_content, 'Hello, how can I help you?') FROM documents_text;

Step 3: Conduct a multi-turn conversation

Normal conversation process

Initiate a multi-turn conversation following the decision tree flow. The conversation process is as follows.

  1. First round of conversation

    SELECT adbpg_graphrag.query('Hello','tree');

    Response

              query           
    --------------------------
     Hello, how can I help you?
    (1 row)
  2. Second round of conversation

    SELECT adbpg_graphrag.query('The air conditioner is not cooling','tree');

    Response

             query          
    ------------------------
     Is the machine not blowing air or not cooling
    (1 row)
  3. Third round of conversation

    SELECT adbpg_graphrag.query('The air conditioner is not blowing air','tree');

    Response

             query          
    ------------------------
     Does the air conditioner display a sensor fault
    (1 row)
  4. Fourth round of conversation

    SELECT adbpg_graphrag.query('Yes','tree');

    Response

                      query                  
    -----------------------------------------
     Please power off the air conditioner for 3 minutes and restart it, to see if it returns to normal
    (1 row)
  5. Fifth round of conversation

    SELECT adbpg_graphrag.query('OK','tree');

    Response

              query           
    --------------------------
     Do you have any other questions?
    (1 row)

Jump navigation conversation process

Dynamic query mechanism: In customer interaction strategy, Alibaba Cloud adopts a combination of local exploration and global retrieval dynamic query mechanism to shorten conversation paths and optimize conversation flow.

  • Handling process

    Based on the customer's answer, the system evaluates the relevance of the response content to global information in the decision tree. If the analysis shows that the customer's answer has higher semantic similarity with global search results, it triggers a jump navigation process, directly linking to the associated global node.

  • Benefits

    Enhances user experience by skipping redundant intermediate steps and ensures quick and accurate resolution of customer issues. This strategy precisely captures customer intent, achieving flexibility and efficiency in intelligent interaction system design.

  1. First round of conversation

    SELECT adbpg_graphrag.query('Hello','tree');

    Response

              query           
    --------------------------
     Hello, how can I help you?
    (1 row)
  2. Second round of conversation

    SELECT adbpg_graphrag.query('The machine is not blowing air','tree');

    Response

             query          
    ------------------------
     Does the air conditioner display a sensor fault
    (1 row)
  3. Third round of conversation

    SELECT adbpg_graphrag.query('It shows F1 error code','tree');

    Response

                       query                    
    --------------------------------------------
     Please call our repair hotline, a maintenance engineer will visit for inspection
    (1 row)
  4. Fourth round of conversation

    SELECT adbpg_graphrag.query('OK','tree');

    Response

              query           
    --------------------------
     Do you have any other questions?
    (1 row)

Reset conversation status

To start a new conversation flow, use the following command.

SELECT adbpg_graphrag.reset_tree_query();

Response

       reset_tree_query        
-------------------------------
 Successful! Reset Tree Query.
(1 row)

Step 4: Modify the decision tree

After importing a decision tree, you can modify it by adding or deleting subtrees.

Decision tree operations depend on the entity_id value of nodes. You can obtain this value through the following methods:

  • Visualization tool Age-Viewer: Hover the mouse over the corresponding node to view its entity_id value.

  • Cypher:

    SELECT * from cypher('chunk_entity_relation', $$
            MATCH (V)-[R:DIRECTED]-(V2)
            RETURN V,R,V2
    $$) as (V agtype, R agtype, V2 agtype);

Add a subtree

Syntax

SELECT adbpg_graphrag.append_decision_tree(context text, root_node_id text);

Parameters

  • context: Subtree information.

  • root_node_id: Node ID entity_id.

Delete a subtree

Syntax

SELECT adbpg_graphrag.delete_decision_tree(root_node_entity text);

Parameters

root_node_entity: Node ID entity_id. Deletes the subtree under this ID.