Diagnose and improve RAG quality across indexing, retrieval, and generation stages
RAG (Retrieval Augmented Generation) quality depends on three stages:
Before optimizing, you need a quantifiable evaluation standard to measure whether changes are effective.
Prepare at least 100 test cases, each containing a question and expected result. Cover the following types:
Use the QA service debug panel or the chat API to run the full evaluation set. Record the retrieved chunks and answers for each case. This report serves as the reference baseline for all subsequent optimizations.
Analyze underperforming cases from the baseline report and apply the corresponding solutions below.
The knowledge base contains relevant content, but retrieval fails to match it.
The knowledge base has relevant content, but retrieval returns unrelated chunks.
Improper chunking strategies lead to the following issues:
Chunks are retrieved but ranking is suboptimal, leading to poor final results.
Correct chunks are retrieved, but the model generates inaccurate answers.
When binding multiple knowledge bases, these strategies can improve joint retrieval quality:
After each configuration change:
- Indexing: Document parsing, chunking, and vectorization
- Retrieval: Matching and recalling relevant chunks from the index
- Generation: The LLM generates answers based on retrieved content and the query
Step 1: Establish a Baseline
Before optimizing, you need a quantifiable evaluation standard to measure whether changes are effective.
Build an Evaluation Set
Prepare at least 100 test cases, each containing a question and expected result. Cover the following types:
| Type | Example |
|---|---|
| Factual | "What is the warranty period for Product X?" |
| Comparative | "Compare the main differences between Product X and Product Y" |
| Tutorial | "How to install Product X?" |
| Analytical | "Why has Product X's sales been increasing over the past 3 months?" |
Run a Baseline Test
Use the QA service debug panel or the chat API to run the full evaluation set. Record the retrieved chunks and answers for each case. This report serves as the reference baseline for all subsequent optimizations.
Step 2: Diagnose and Improve
Analyze underperforming cases from the baseline report and apply the corresponding solutions below.
2.1 Retrieval Failure: Relevant Knowledge Not Found
The knowledge base contains relevant content, but retrieval fails to match it.
Add missing content to the knowledge base
Add missing content to the knowledge base
If the knowledge base lacks relevant information, the model cannot answer the question. Check whether the knowledge base covers the topics in your evaluation cases and supplement missing content.
Optimize source document formatting
Optimize source document formatting
Document format issues may cause key content to be lost during parsing:
- Ensure clear heading hierarchy and content structure
- Remove watermarks
- Avoid complex tables with merged or cross-page cells
- Prefer Markdown format; convert PDF/DOCX to Markdown before importing
Match query language
Match query language
If queries are primarily in English or another language, source documents should use the same language. For technical terms, consider multilingual annotation.
Resolve entity ambiguity
Resolve entity ambiguity
Standardize different expressions for the same entity. For example, unify "ML", "Machine Learning", and "机器学习" to a single form.
You can use an LLM to help standardize terminology. For long documents, split into parts and process sequentially.
Enable Query Rewriting
Enable Query Rewriting
In multi-turn conversations, user input may be incomplete or lack context. Query rewriting automatically completes the query based on conversation history, improving retrieval matching.Example: User inputs
Phone X1, system rewrites to Please provide specifications for all versions of Phone X1 currently on sale.Enable the Query Rewriting toggle in the per-knowledge-base configuration of Knowledge Retrieval or Knowledge QA.2.2 Retrieval Failure: Irrelevant Results Returned
The knowledge base has relevant content, but retrieval returns unrelated chunks.
Use tag filtering
Use tag filtering
Typical problem: The knowledge base contains multiple categories of documents, and results from category B appear when searching category A.Solution: Add tags to documents and filter by tags during retrieval.Setup:
- During import: Add tags in step 3 of Upload Data
- After import: Edit tags from the "More" menu in the document list
- Set Tag Filter in per-knowledge-base configuration of retrieval or QA services
- Specify
tagsin API request parameters
Enable metadata extraction
Enable metadata extraction
Typical problem: Multiple documents with similar structure (e.g., all containing a "Feature Overview" section) cause retrieval to return content from the wrong document.Solution: Define document metadata so the system performs structured search before vector retrieval, precisely locating the target document.How it works:
- Extract metadata from the query (e.g.,
{"key": "name", "value": "Phone X1"}) - Filter chunks containing that metadata
- Perform vector retrieval on the filtered results
2.3 Incomplete Chunks
Improper chunking strategies lead to the following issues:
| Issue | Symptom | Impact |
|---|---|---|
| Chunks too short | A single chunk contains only one sentence, missing context | Cannot match relevant content during retrieval |
| Chunks too long | A single chunk mixes multiple topics | Returns irrelevant information during recall |
| Semantic truncation | Chunks are forcibly cut in the middle of a sentence | Retrieved content is incomplete |
Use smart chunking
Use smart chunking
Smart chunking adaptively selects chunk boundaries based on semantic relevance rather than fixed lengths:
- The system first divides the document into paragraphs using built-in sentence markers
- Then determines chunk boundaries based on semantic relevance between paragraphs
Manually review and fix chunks
Manually review and fix chunks
Automatic chunking may still produce unexpected truncations or parsing errors (e.g., spaces parsed as
%20). Spot-check chunk quality after import.View chunks in the Chunk Details tab of the knowledge base detail page. Edit and fix any issues directly. Fixed content takes effect immediately for retrieval.Fixes only affect indexed chunks, not source documents. Re-importing requires re-checking.
Manually add or delete chunks
Manually add or delete chunks
For edge cases that automatic chunking cannot handle:
- Add chunk: Supplement missing key information or additional context. Select the parent document, max 6000 characters
- Delete chunk: Remove chunks with incorrect or duplicate content without affecting other chunks from the same document
2.4 Poor Reranking
Chunks are retrieved but ranking is suboptimal, leading to poor final results.
Adjust similarity threshold
Adjust similarity threshold
The similarity threshold filters out chunks scoring below the threshold after reranking.
- Too high: May discard all relevant chunks, leaving the model without sufficient information
- Too low: May include many low-relevance chunks, introducing noise
Increase max recall count
Increase max recall count
Max recall count determines how many chunks are returned. For complex questions requiring summarization, enumeration, or comparison, providing more quality chunks (e.g., K=20) usually works better than just 5.However, too many chunks may exceed the model's input length limit, causing content truncation.Configuration: Max Recall Count (1~20) in per-knowledge-base configuration.
Choose reranking model and mode
Choose reranking model and mode
The reranking model reorders initially recalled chunks. Available modes:
| Mode | Use Case |
|---|---|
| QA mode (default) | User asks a complete question, expects an answer from chunks |
| Similarity mode | Query and chunks share similar phrasing, e.g., finding historical records |
| Custom advanced mode | Special ranking rules needed, e.g., "prioritize content from the last year" |
- Per-KB config: Expand the knowledge base config panel in retrieval or QA services
- Global reranking: Configure in Knowledge Retrieval — Global Config
2.5 Model Comprehension Issues
Correct chunks are retrieved, but the model generates inaccurate answers.
Switch generation model
Switch generation model
Typical problem: Answers appear to be crudely assembled; the model fails to understand the relationship between knowledge and the question.Different models vary in instruction following, knowledge comprehension, and long-text handling:
Switch the Model in the QA service configuration page.
| Scenario | Recommended Model |
|---|---|
| Simple information lookup and summary | Smaller models (e.g., qwen3-flash) |
| Complex logical reasoning | Larger models (e.g., qwen3-max) |
| Queries requiring many chunks | Models with larger context windows |
Optimize prompts
Optimize prompts
Typical problem: Results don't follow requirements or lack completeness.Three common optimization methods:Method 1: Constrain output contentMethod 2: Add few-shot examplesInclude input-output examples in the prompt to guide the model's format and style.Method 3: Separate knowledge contentClearly separate instructions and knowledge base content with delimiter markers so the model can better understand the structure.
Enable rejection policy
Enable rejection policy
Typical problem: Answers include the model's general knowledge rather than strictly using the knowledge base.Enable the Rejection toggle in the QA service configuration. When retrieval results are insufficient, the model refuses to answer and returns a custom response instead of fabricating an answer.Configuration: Rejection toggle in Knowledge QA.
Adjust model parameters
Adjust model parameters
Typical problem: Same question produces different answers each time, or answers are too similar when variety is desired.Adjust the temperature parameter:
icon next to the model in the QA service configuration page.
- Higher values: More diverse output, suitable for creative writing and brainstorming
- Lower values: More deterministic output, suitable for factual queries and technical documentation
2.6 Multi-Knowledge-Base Optimization
When binding multiple knowledge bases, these strategies can improve joint retrieval quality:
| Strategy | Description |
|---|---|
| Set knowledge base weights/priority | Assign higher weights or rankings to core knowledge bases so their results dominate the final ranking |
| Enable knowledge base routing | The system automatically determines which bases to query based on intent, reducing interference from irrelevant bases. Configure in Knowledge Retrieval — Global Config |
| Choose reranking model mode | QA mode for Q&A scenarios; Similarity mode for finding semantically similar passages; Custom advanced mode for special ranking needs |
Step 3: Continuous Iteration
After each configuration change:
- Re-evaluate: Rerun the same evaluation set to quantify improvements
- Compare: Check against the baseline report to confirm which issues were resolved and whether new issues were introduced
- Expand coverage: Add newly discovered bad cases to the evaluation set