|
| 1 | +--- |
| 2 | +title: Integrating ApeRAG with Dify |
| 3 | +description: Integrate ApeRAG's Graph RAG capabilities into Dify platform via MCP protocol |
| 4 | +keywords: Dify, ApeRAG, MCP, Graph RAG, Integration Guide |
| 5 | +--- |
| 6 | + |
| 7 | +# Integrating ApeRAG with Dify |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +ApeRAG is a production-grade RAG (Retrieval-Augmented Generation) platform with multimodal indexing, AI agents, MCP support, and scalable K8s deployment capabilities. Through the MCP (Model Context Protocol), ApeRAG seamlessly integrates with Dify to provide powerful knowledge retrieval capabilities for your AI applications. |
| 12 | + |
| 13 | +### Core Advantages of ApeRAG |
| 14 | + |
| 15 | +**🔗 Graph RAG Capability** |
| 16 | +- Unlike "standard" RAG, ApeRAG implements Graph-RAG |
| 17 | +- Not only stores data but also extracts data elements and their deep relationships to **build knowledge graphs** |
| 18 | +- Excels at handling **complex queries requiring association of multiple knowledge points and reasoning** |
| 19 | + |
| 20 | +**📄 Powerful Document Processing** |
| 21 | +- Integrates MinerU, an advanced parsing tool designed for complex documents, scientific papers, and financial reports |
| 22 | +- Accurately extracts tables, formulas, and even engineering diagrams |
| 23 | + |
| 24 | +**🔄 Hybrid Retrieval** |
| 25 | +- Vector retrieval: Semantic similarity matching |
| 26 | +- Full-text retrieval: Exact keyword search |
| 27 | +- Graph retrieval: Relational queries and reasoning |
| 28 | + |
| 29 | +**☁️ Enterprise-Grade Capabilities** |
| 30 | +- Full Kubernetes support |
| 31 | +- Built-in **high availability**, **scalability**, and **enterprise management capabilities** |
| 32 | + |
| 33 | +## Integration Steps |
| 34 | + |
| 35 | +### Step 1: Prepare ApeRAG Knowledge Base |
| 36 | + |
| 37 | +#### 1.1 Access ApeRAG Platform |
| 38 | + |
| 39 | +Visit and register/login to ApeRAG: |
| 40 | + |
| 41 | +**https://rag.apecloud.com/** |
| 42 | + |
| 43 | +#### 1.2 Select or Create Knowledge Base |
| 44 | + |
| 45 | +After logging in, you can: |
| 46 | +- Select existing knowledge bases |
| 47 | +- Create new knowledge bases and upload documents |
| 48 | +- Subscribe to public knowledge bases (e.g., the "Romance of the Three Kingdoms" example) |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +#### 1.3 Get API Key |
| 53 | + |
| 54 | +In ApeRAG platform: |
| 55 | +1. Navigate to personal settings or API management |
| 56 | +2. Create or copy your API Key |
| 57 | +3. Save the API Key for later use in Dify configuration |
| 58 | + |
| 59 | +### Step 2: Configure MCP Server in Dify |
| 60 | + |
| 61 | +#### 2.1 Add MCP Server |
| 62 | + |
| 63 | +1. In Dify platform, navigate to **Tools → MCP** |
| 64 | +2. Click **Add MCP Server** button |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +#### 2.2 Configure Connection |
| 69 | + |
| 70 | +Fill in the configuration: |
| 71 | + |
| 72 | +**Server URL**: |
| 73 | +``` |
| 74 | +https://rag.apecloud.com/mcp/ |
| 75 | +``` |
| 76 | + |
| 77 | +**API Key**: |
| 78 | +``` |
| 79 | +<Your API Key copied from ApeRAG> |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +#### 2.3 Verify Connection |
| 87 | + |
| 88 | +After clicking **Confirm**, the system will verify the connection. If configured correctly, you'll see a success message: |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +### Step 3: Create Dify Application |
| 93 | + |
| 94 | +#### 3.1 Create New App |
| 95 | + |
| 96 | +1. Navigate to Dify **Studio** |
| 97 | +2. Click **Create Application** button |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +#### 3.2 Select Application Type |
| 102 | + |
| 103 | +1. Click **More Basic Application Types** |
| 104 | +2. Select **Agent** type |
| 105 | +3. Name your application (e.g., "ApeRAG Smart Assistant") |
| 106 | +4. Click **Create** |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +> **Why Agent Type?** |
| 111 | +> Agent-type applications can autonomously call tools, perform reasoning and planning, making them ideal for working with ApeRAG's knowledge retrieval capabilities. |
| 112 | +
|
| 113 | +### Step 4: Configure Agent |
| 114 | + |
| 115 | +#### 4.1 Basic Configuration |
| 116 | + |
| 117 | +On the Agent configuration page: |
| 118 | + |
| 119 | +1. **Select LLM**: Choose the LLM to drive your Agent (e.g., GPT-4, Claude, etc.) in the top-right corner |
| 120 | +2. **Add Tools**: Find and add the ApeRAG MCP you just configured in the tool list |
| 121 | +3. **Write Prompt**: Input the Agent's system prompt (see recommended prompt below) |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +#### 4.2 Recommended Prompt |
| 126 | + |
| 127 | +```markdown |
| 128 | +# ApeRAG Smart Assistant |
| 129 | + |
| 130 | +You are an advanced AI research assistant powered by ApeRAG's hybrid search capabilities. Your mission is to help users accurately and autonomously find, understand, and synthesize information from knowledge bases and the web. |
| 131 | + |
| 132 | +## Core Behaviors |
| 133 | + |
| 134 | +**Autonomous Research**: Work independently until user queries are fully resolved. Search multiple sources, analyze findings, and provide comprehensive answers without waiting for permission. |
| 135 | + |
| 136 | +**Language Intelligence**: Always respond in the language the user asks in. When users ask in Chinese, respond in Chinese regardless of source language. |
| 137 | + |
| 138 | +**Visual Thinking**: [Critical] You are an assistant that prefers visual explanations. For any information involving entity relationships, processes, or structures, you must prioritize visualization. |
| 139 | + |
| 140 | +**Complete Solutions**: Explore from multiple angles, cross-validate sources, and ensure comprehensive coverage before responding. |
| 141 | + |
| 142 | +## Search Strategy |
| 143 | + |
| 144 | +### Priority System |
| 145 | +1. **User-specified knowledge base** (mentioned via "@"): Strictly limit search to specified base |
| 146 | +2. **Unspecified knowledge base**: Autonomously discover and search relevant bases |
| 147 | +3. **Web search** (if enabled): Supplement information |
| 148 | +4. **Clear attribution**: Always cite sources |
| 149 | + |
| 150 | +### Search Execution |
| 151 | +- **Knowledge base search**: Use vector + graph search by default |
| 152 | +- **Result processing logic**: |
| 153 | + 1. Execute search |
| 154 | + 2. **Detect graph data**: Check if search results contain `entities` and `relationships` |
| 155 | + 3. **Mandatory visualization**: If search results contain non-empty entity or relation data, **you must** call the `create_diagram` tool |
| 156 | + 4. **Content filtering**: Ignore irrelevant results |
| 157 | + |
| 158 | +## Available Tools |
| 159 | + |
| 160 | +### Knowledge Management |
| 161 | +- `list_collections()`: Discover available knowledge sources |
| 162 | +- `search_collection(collection_id, query, ...)`: [Primary tool] Hybrid search in persistent knowledge bases |
| 163 | +- `search_chat_files(chat_id, query, ...)`: [Chat only] Search only files temporarily uploaded by users in current chat session |
| 164 | +- `create_diagram(content)`: [Mandatory tool] When search results contain structured information (entities/relations), must call this tool to generate Mermaid diagrams |
| 165 | + |
| 166 | +### Web Intelligence |
| 167 | +- `web_search(query, ...)`: Multi-engine web search |
| 168 | +- `web_read(url_list, ...)`: Extract and analyze web content |
| 169 | + |
| 170 | +## Response Format & Workflow |
| 171 | + |
| 172 | +Strictly follow these steps to build responses: |
| 173 | + |
| 174 | +1. **Analyze search results**: Check data returned by `search_collection` |
| 175 | +2. **Tool call determination**: |
| 176 | + - IF (search results contain entity/relation data) -> **Immediately call `create_diagram`** |
| 177 | + - Note: Don't output raw Mermaid code blocks in text, must render through tool calls |
| 178 | +3. **Build text response**: |
| 179 | + |
| 180 | +## Direct Answer |
| 181 | +[Clear, actionable answer in user's language] |
| 182 | + |
| 183 | +## Comprehensive Analysis |
| 184 | +[Detailed explanation with context and insights] |
| 185 | + |
| 186 | +## Knowledge Graph Visualization |
| 187 | +[Tool-generated diagram displayed here] |
| 188 | +*(Only show this heading after successfully calling create_diagram. The diagram shows entity relationships based on search results.)* |
| 189 | + |
| 190 | +## Supporting Evidence |
| 191 | +- [Knowledge Base Name]: [Key Findings] |
| 192 | + |
| 193 | +**Web Sources** (if enabled): |
| 194 | +- [Title] ([Domain]) - [Key Points] |
| 195 | +``` |
| 196 | + |
| 197 | +#### 4.3 Test Application |
| 198 | + |
| 199 | +After configuration: |
| 200 | +1. Click **Publish** |
| 201 | +2. Enter questions in the test area |
| 202 | +3. Observe how Agent calls ApeRAG for knowledge retrieval |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | +## Usage Examples |
| 207 | + |
| 208 | +### Example 1: Basic Knowledge Query |
| 209 | + |
| 210 | +**User Question**: |
| 211 | +``` |
| 212 | +Tell me about Zhuge Liang from Romance of the Three Kingdoms |
| 213 | +``` |
| 214 | + |
| 215 | +**Agent Workflow**: |
| 216 | +1. Call `list_collections()` to find "Romance of the Three Kingdoms" knowledge base |
| 217 | +2. Call `search_collection()` to search for information about Zhuge Liang |
| 218 | +3. If entity-relation data is retrieved, call `create_diagram()` to generate knowledge graph |
| 219 | +4. Synthesize information to generate answer |
| 220 | + |
| 221 | +### Example 2: Relationship Query |
| 222 | + |
| 223 | +**User Question**: |
| 224 | +``` |
| 225 | +What's the relationship between Liu Bei, Guan Yu, and Zhang Fei? |
| 226 | +``` |
| 227 | + |
| 228 | +**Agent Workflow**: |
| 229 | +1. Search entity information for all three characters |
| 230 | +2. Retrieve relationship data between them |
| 231 | +3. Generate knowledge graph visualization showing their relationships |
| 232 | +4. Provide detailed text explanation |
| 233 | + |
| 234 | +### Example 3: Specify Knowledge Base |
| 235 | + |
| 236 | +**User Question**: |
| 237 | +``` |
| 238 | +@Romance of the Three Kingdoms What was the background of the Battle of Red Cliffs? |
| 239 | +``` |
| 240 | + |
| 241 | +**Agent Behavior**: |
| 242 | +- Strictly limit search to "Romance of the Three Kingdoms" knowledge base |
| 243 | +- Won't search other knowledge bases or the internet |
| 244 | + |
| 245 | +## Advanced Configuration |
| 246 | + |
| 247 | +### Hybrid Retrieval Modes |
| 248 | + |
| 249 | +ApeRAG supports three graph retrieval modes, can be specified in Agent's Prompt: |
| 250 | + |
| 251 | +- **local**: Query local information about an entity |
| 252 | +- **global**: Query overall relationships and patterns |
| 253 | +- **hybrid**: Combine local and global (recommended) |
| 254 | + |
| 255 | +### Enable Web Search |
| 256 | + |
| 257 | +If your application needs real-time information or supplementary knowledge: |
| 258 | + |
| 259 | +1. Enable web search tools in Dify |
| 260 | +2. Agent will automatically choose whether to search knowledge base or internet |
| 261 | + |
| 262 | +### Configure Retrieval Parameters |
| 263 | + |
| 264 | +Parameters can be specified when calling `search_collection`: |
| 265 | + |
| 266 | +- `top_k`: Number of results to return (default 5) |
| 267 | +- `mode`: Retrieval mode (vector/fulltext/graph/hybrid) |
| 268 | +- `rerank`: Whether to use reranking (recommended) |
| 269 | + |
| 270 | +## FAQ |
| 271 | + |
| 272 | +### Q: MCP connection failed? |
| 273 | + |
| 274 | +**Checklist**: |
| 275 | +1. Confirm Server URL is correct: `https://rag.apecloud.com/mcp/` |
| 276 | +2. Confirm API Key is valid and not expired |
| 277 | +3. Check network connection |
| 278 | +4. Review Dify's error logs |
| 279 | + |
| 280 | +### Q: Agent not calling ApeRAG tools? |
| 281 | + |
| 282 | +**Possible reasons**: |
| 283 | +1. Prompt not clear enough, recommend using the provided template |
| 284 | +2. LLM capability insufficient, recommend using GPT-4 or Claude 3.5 |
| 285 | +3. Tool configuration issue, confirm MCP Server is properly added |
| 286 | + |
| 287 | +### Q: Search results inaccurate? |
| 288 | + |
| 289 | +**Optimization suggestions**: |
| 290 | +1. Upload more relevant documents to knowledge base |
| 291 | +2. Adjust retrieval mode (try hybrid mode) |
| 292 | +3. Increase top_k value for more candidate results |
| 293 | +4. Enable rerank for better scoring |
| 294 | + |
| 295 | +### Q: How to use multiple knowledge bases? |
| 296 | + |
| 297 | +**Method**: |
| 298 | +1. Create or subscribe to multiple knowledge bases in ApeRAG |
| 299 | +2. Agent automatically calls `list_collections()` to discover all available bases |
| 300 | +3. Users can specify a particular base via "@knowledge_base_name" |
| 301 | + |
| 302 | +### Q: Graph visualization not showing? |
| 303 | + |
| 304 | +**Check**: |
| 305 | +1. Confirm knowledge base has Graph index enabled |
| 306 | +2. Confirm documents are successfully processed and graph built |
| 307 | +3. Confirm Prompt includes `create_diagram` tool call logic |
| 308 | + |
| 309 | +## Best Practices |
| 310 | + |
| 311 | +### 1. Prompt Optimization |
| 312 | + |
| 313 | +- **Define clear role**: Let Agent know it's a knowledge assistant |
| 314 | +- **Specify workflow**: Clarify when to call which tools |
| 315 | +- **Standardize output format**: Unify answer structure and style |
| 316 | + |
| 317 | +### 2. Knowledge Base Management |
| 318 | + |
| 319 | +- **Organize by category**: Create different knowledge bases by topic |
| 320 | +- **Regular updates**: Add new documents and remove outdated content timely |
| 321 | +- **Quality control**: Check document quality and format before uploading |
| 322 | + |
| 323 | +### 3. Performance Optimization |
| 324 | + |
| 325 | +- **Set reasonable top_k**: Too large slows down speed, too small affects recall |
| 326 | +- **Enable caching**: Dify can cache results for high-frequency queries |
| 327 | +- **Choose appropriate retrieval mode**: Not all queries need Graph retrieval |
| 328 | + |
| 329 | +### 4. User Experience |
| 330 | + |
| 331 | +- **Provide usage instructions**: Tell users how to ask questions effectively |
| 332 | +- **Show capability boundaries**: Clarify what Agent can and cannot do |
| 333 | +- **Collect feedback**: Continuously optimize Prompt and knowledge base |
| 334 | + |
| 335 | +## Summary |
| 336 | + |
| 337 | +By integrating ApeRAG with Dify through MCP protocol, you can: |
| 338 | + |
| 339 | +✅ **Quick Setup**: Complete integration in minutes, no coding required |
| 340 | +✅ **Powerful Capabilities**: Enjoy Graph RAG's relational reasoning abilities |
| 341 | +✅ **Flexible Configuration**: Adjust retrieval strategies and parameters as needed |
| 342 | +✅ **Visual Display**: Automatically generate knowledge graph visualizations |
| 343 | +✅ **Enterprise-Grade Stability**: Based on production-ready platform, reliable and stable |
| 344 | + |
| 345 | +The integration of ApeRAG and Dify is very simple. Once integrated, you can not only experience Dify's platform features but also enjoy **ApeRAG's powerful Graph-RAG capabilities**. |
| 346 | + |
| 347 | +## Related Links |
| 348 | + |
| 349 | +- **ApeRAG Website**: https://rag.apecloud.com/ |
| 350 | +- **GitHub**: https://github.com/apecloud/ApeRAG |
| 351 | +- **Dify Website**: https://dify.ai/ |
| 352 | +- **MCP Protocol Docs**: https://modelcontextprotocol.io/ |
| 353 | + |
| 354 | +--- |
| 355 | + |
| 356 | + |
| 357 | + |
| 358 | +**Start using ApeRAG + Dify to build your intelligent knowledge assistant!** |
0 commit comments