GraphRAG enhances Retrieval-Augmented Generation (RAG) by adding knowledge graph-based indexing, retrieval, and generation on top of traditional vector search. This addresses key limitations of standard RAG—insufficient context understanding, document similarity bias, and difficulty with complex relationships and multi-hop reasoning—enabling higher-quality responses for knowledge-intensive queries.
Integration solution: AnalyticDB for PostgreSQL + DTS RAGFlow. This solution integrates the graph analysis engine of AnalyticDB for PostgreSQL with the data processing capabilities of DTS RAGFlow, creating a complete pipeline from document ingestion to knowledge graph creation and AI chat. For more information, see Tutorial: Build a GraphRAG with DTS RAGFlow and AnalyticDB for PostgreSQL.
How it works
GraphRAG operates in three stages:
Indexing: A knowledge extraction model parses your documents, generates a knowledge graph, and saves it to the AnalyticDB for PostgreSQL graph analysis engine.
Retrieval: The model extracts keywords from a query and traverses the knowledge graph to find related subgraphs.
Generation: The query and the retrieved subgraph context are submitted to a Large Language Model (LLM) to generate a response.
Prerequisites
Before you begin, make sure you have:
An AnalyticDB for PostgreSQL 7.0 instance with minor engine version 7.2.1.3 or later. Versions 7.3.0.0 and 7.3.1.0 do not support the
adbpg_graphragextension.The required extensions installed. See Install extensions.
Note: To check your minor version, go to the Basic Information page of your instance in the AnalyticDB for PostgreSQL console. If the version is outdated, update the minor engine version.
Quick start
The following end-to-end example initializes GraphRAG, uploads a document, and runs a query.
Note: If you are using DMS, select both statements in the SQL window and click Execute to run them in the same session. Theinitializeandqueryoperations must run in the same session.
-- Step 1: Initialize the GraphRAG service (see Initialize the GraphRAG service for configuration options)
SELECT adbpg_graphrag.initialize(config json);
-- Step 2: Upload a document
SELECT adbpg_graphrag.upload(
'ProductInfo.txt',
'The Xiao Mi customer service system provides features such as question answering, leave requests, and knowledge base searches.'
);
-- Step 3: Query the knowledge graph
SELECT adbpg_graphrag.query('What features does the Xiao Mi system have?', 'hybrid');Install extensions
Three extensions are required, in this order: plpython3u, age, and adbpg_graphrag.
On instances with minor engine version 7.2.1.4 or later, all three are installed automatically. Skip to step 2 to configure ag_catalog in the search_path.
Install
plpython3u(installation guide). This extension is installed by default. To verify, run the following query in thepublicschema of your database:SELECT * FROM pg_extension WHERE extname = 'plpython3u';The following output confirms the extension is installed. No output means it is not installed in the
publicschema.oid | extname | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition -------+------------+----------+--------------+----------------+------------+-----------+-------------- 14674 | plpython3u | 10 | 11 | f | 1.0 | | (1 row)Install
age(installation guide). After installingage, addag_catalogto thesearch_pathto simplify queries. Choose one of the following methods:Session-level (applies to the current session only):
SET search_path TO "$user", public, ag_catalog;Database-level (permanent):
ALTER DATABASE <database_name> SET search_path TO "$user", public, ag_catalog;
(Optional) To grant other users access to the graph extension, use the initial account or a privileged user with the RDS_SUPERUSER role:
GRANT USAGE ON SCHEMA ag_catalog TO <username>;Install
adbpg_graphrag. This extension depends on bothplpython3uandage. Contact technical support for installation assistance.
Initialize the GraphRAG service
SELECT adbpg_graphrag.initialize(config json);Call adbpg_graphrag.initialize before uploading documents or running queries. It accepts a JSON object with the following parameters.
| Parameter | Description | Default |
|---|---|---|
llm_model | The LLM used for knowledge extraction and generation. | qwen-max-2025-01-25 |
llm_api_key | The API key required to invoke the large language model. | |
llm_url | The API endpoint URL of the LLM. By default, the API of Alibaba Cloud Model Studio is used. Note
| |
embedding_model | The embedding model used to generate vectors. | text-embedding-v3 |
language | The language GraphRAG uses for extraction. Supported values: English, Simplified Chinese. | English |
entity_types | The types of entity nodes to extract when building the knowledge graph. | — |
relationship_types | The types of relationship edges to extract when building the knowledge graph. | — |
first_node_content | (Tree mode only) The initial node response. | "Hello, how can I help you?" |
end_node_content | (Tree mode only) The final node response. | "Are there any other questions?" |
global_distance_threshold | (Tree mode only) The threshold for preferring a global search result over a local one. A value of 0.1 means the global result is selected if its similarity score is at least 0.1 higher than the local result. | 0.1 |
Model selection
Alibaba Cloud has tested three models with GraphRAG:
| Model | Response quality | Generation speed | Best for |
|---|---|---|---|
qwen-max | Highest | Slowest | Accuracy-critical use cases |
qwen-plus-latest | Medium | Medium | Balanced workloads |
qwen3-32b | Lowest | Fastest | Latency-sensitive use cases |
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",
"entity_types" : ["Business Scenario", "Product", "Feature", "Usage Logic", "Data Metric", "Data Caliber"],
"relationship_types": ["Contains", "Belongs To", "Uses", "Depends On", "Associates With", "Solves"]
}
$$
);Upload documents
SELECT adbpg_graphrag.upload(filename text, context text);After a file is uploaded, GraphRAG performs a series of tasks, such as text chunking, vector generation, and knowledge graph extraction.
| Parameter | Description |
|---|---|
filename | The name of the file. |
context | The full text content of the file. |
Example:
SELECT adbpg_graphrag.upload(
'ProductInfo.txt',
'The Xiao Mi customer service system provides features such as question answering, leave requests, and knowledge base searches.'
);Documents with images
The service has limited support for documents containing images. Before uploading, use a multimodal large language model (such as qwen2.5-vl-72b-instruct) to parse images into text descriptions. Use the following prompt:
You are a professional image reading assistant. Your task is to generate a detailed interpretation and a comprehensive summary for the provided image.
---Requirements---
1. If the image contains distinct modules, extract and provide as much detail as possible for each module.
2. Based on the extracted information, generate a summary for each module.
3. If the image contains charts, summarize the available details from the charts.
4. If the image contains meaningful icons, such as flag icons, pictograms, metaphorical icons, tool icons, and hybrid icons, also extract details from these icons.
5. Based on the extracted information, create a summary and provide useful global information.
---Note---
Write in the third person and include module names to ensure complete context.
*Identify the primary language of the image* (the language that makes up more than 80% of the text). Use the *primary language of the image* as the output language.Query for information
SELECT adbpg_graphrag.query(query_str text);
SELECT adbpg_graphrag.query(query_str text, query_mode text);| Parameter | Description |
|---|---|
query_str | The question to ask. |
query_mode | The retrieval strategy. Defaults to mix. See the query mode reference below. |
Example:
SELECT adbpg_graphrag.query('What features does the Xiao Mi system have?', 'hybrid');Query mode reference
Choose a query mode based on the type of question you are asking.
| Mode | Retrieval strategy | Best for | Example question |
|---|---|---|---|
mix (default) | Vector matching + knowledge graph | General-purpose queries; when you are unsure which mode to use | "Summarize the key topics in my documents." |
bypass | None (queries the LLM directly) | Questions answerable from the LLM's pre-trained knowledge, without needing your documents | "What is the capital of France?" |
naive | Vector search only | Simple factual lookups against specific passages | "What does the onboarding guide say about account creation?" |
local | Entity nodes in the knowledge graph | Questions about specific entities | "What are the capabilities of the customer service module?" |
global | Relationship edges in the knowledge graph | Questions about patterns across the whole dataset | "What are the most common themes across all product documents?" |
hybrid | Entity nodes + relationship edges | Questions requiring both entity detail and relationship context | "How does the customer service module interact with the knowledge base?" |
tree | Decision tree (Q&A tree structure) | Guided, multi-turn conversations following a predefined Q&A flow | "Walk me through the troubleshooting steps." |