Automatically extract and write back document tags using the Knowledge Q&A service, enabling an end-to-end workflow—from document ingestion to tag-based retrieval.
This practice leverages the retrieval and generation capabilities of the Knowledge Q&A service. A large language model (LLM) automatically extracts tags from all document chunks and writes them back to the document—establishing an end-to-end pipeline from ingestion → automatic tagging → tag-based retrieval. The entire process is completed in the console and is fully reproducible.
Solution Overview
Background
Manual document tagging suffers from three key issues:
- Inability to keep pace with ingestion speed: During bulk document ingestion, manual tagging lags far behind ingestion throughput.
- Latency between ingestion and usability: Documents remain unavailable for tag-based search and distribution until manually tagged.
- Inconsistent tag standards: Human annotators interpret content differently, leading to inconsistent tags across similar documents—and degrading retrieval quality.
Applicable Scenarios
| Scenario | Description | Value of Automatic Tagging |
|---|---|---|
| Enterprise Document Management | Tag contracts, project documentation, and technical manuals with attributes such as department, project, topic, and classification level | Improves search efficiency and supports permission control and compliance traceability |
| Knowledge Asset Consolidation | Tag technical documents, research reports, and industry whitepapers with domain, tech stack, and use cases | Structures scattered materials, enabling precise tag-based Q&A and recommendation |
| Marketing Asset Management | Tag product manuals, marketing plans, and customer case studies with product line, industry, and scenario | Enables rapid asset discovery and supports tag-based filtering and reuse |
Technical Principle
The Knowledge Q&A service is fundamentally a RAG (Retrieval-Augmented Generation) system: it retrieves relevant chunks first, then uses an LLM to generate answers based on those chunks. By shifting the generation objective from answering questions to extracting tags, we achieve automatic tagging: assign a unique temporary tag to the target document, filter retrieval exclusively to that document’s chunks via tag filtering, prompt the LLM to output structured tags, and finally write them back.
Three critical implementation points:
- Single-document retrieval: Bailian’s tag filtering operates on the
tagsfield—not document IDs. Assigning a unique tag (e.g.,auto_tag_temp) to the target document ensures only its chunks are retrieved. - Maximize coverage: Disable the ranking model and set recall count to maximum so as many chunks as possible enter the LLM context.
- Structured output: Use prompt engineering to constrain LLM output to parseable, directly writable tag arrays.
Step-by-Step Implementation
This practice demonstrates using the Bailian Technical Documentation Sample (92 technical documents about the Bailian platform), taking "First Call to Tongyi Qwen API" as the example document. You may substitute your own documents.
The following tags will be written back automatically—no pre-creation required:
| Tag | Description | Example Values |
|---|---|---|
product | Product the document belongs to | product_Bailian |
topic | Core subject of the document | topic_API_call |
difficulty | Entry-level / Intermediate / Expert | difficulty_Beginner |
keyword | Key terms (multi-value) | keyword_Environment_Variable, keyword_curl |
1. Create a Knowledge Base and Import Documents
Navigate to Data Ingestion → Knowledge Management, click Create, and select Premium Edition (required for higher recall limits). Follow the three-step wizard:
- Basic Info: Enter knowledge base name and description. Select Document Search as knowledge base type and Basic Document Q&A as use case.
- Select Data: Configure step-by-step: choose connector (default: File Connector), select Upload Files as data source, configure categories, and upload the sample ZIP file in the File Upload section.
- Index Settings: Keep defaults and complete creation.
The Add Tag option at the bottom of the Select Data step applies tags batch-wise to all uploaded files—and cannot isolate individual documents. Unique per-document tags (e.g.,
auto_tag_temp) are added separately in the next step. Tags are free-text; no prior business tag definition is needed.Data selection step: connector, data source, category configuration, file upload

2. Assign a Unique Temporary Tag to the Target Document
Since tag filtering is our mechanism for single-document retrieval, first assign a unique temporary tag (e.g., auto_tag_temp) to the document you wish to tag:
- Console: Go to Data Ingestion → Knowledge Management, click View Details for the knowledge base created above, locate the target document in the Document List, click More → Tags, enter
auto_tag_tempin the tag management modal, press Enter, then confirm. - API: Call
batchUpdateFileTagto write["auto_tag_temp"]into the document’stagsarray.
3. Configure the Knowledge Q&A Service
- Navigate to Knowledge Services → Knowledge Q&A, click Create, and bind the knowledge base from Step 1.
- Select a generation model. Tag extraction demands strong comprehension and instruction-following capability—use a high-performance model (e.g.,
qwen3.7-plus). - Click Expand Knowledge Base Configuration, and set retrieval parameters as follows:
| Parameter | Setting | Reason |
|---|---|---|
| Tag Filtering | Input or select the unique tag auto_tag_temp | Ensures only chunks from this document are retrieved |
| Initial Vector Retrieval TopK | Increase (≥ number of document chunks; default 50) | Maximizes inclusion of this document’s chunks in candidate pool |
| Initial Keyword Retrieval TopK | Increase (≥ number of document chunks; default 50) | Covers keyword-based retrieval path |
| Ranking Model | Select Do Not Use Model | Prevents reranking from discarding relevant chunks |
| Max Recall Count | Set to maximum 20 | Maximizes number of chunks passed to final LLM |
Knowledge base config: tag filter = unique tag, TopK increased, ranking model disabled, max recall = 20

4. Configure the Tagging Prompt
Paste the tagging prompt template into the Prompt input box on the configuration page. The service automatically concatenates retrieved chunks into context—no placeholder syntax needed. The prompt must explicitly instruct the model to output a parseable, writeable field_value tag array:
5. Execute Tagging via Console
In the Debug panel on the right side of the configuration page, enter a tagging instruction (e.g., Please extract tags for this document) and send. Verify that the LLM’s returned tag array matches expectations.
Debug panel: input tagging instruction, model returns tag array

agent_id). Then invoke the Knowledge Q&A API to submit tagging requests (responses are SSE-streamed; see interface docs for parsing). Finally, proceed to Step 6 to write tags back. For batch workflows, see Notes for ordering guidance.
6. Write Tags Back & Enable Tag-Based Retrieval
Once the LLM outputs the tag array, call batchUpdateFileTag to persist tags (using OVERWRITE mode replaces the temporary auto_tag_temp):
fileId via listFile, or manually write tags via More → Tags in the console’s document list. After writing, enable precise retrieval by selecting corresponding tags under Knowledge Base Configuration → Tag Filtering, in either Knowledge Search or Q&A services.
Document list shows applied tags; tag filtering in KB config enables precise recall

Notes
Tags Are String Arrays
Bailian tags are untyped string arrays. Allowed characters: Chinese, English letters (case-sensitive), digits, underscores (_), hyphens (-). Colons (:), spaces, and other symbols are forbidden. Max length per tag: 32 chars; max tags per document: 100. Semantic meaning is encoded via field_name_value (e.g., product_Bailian). Multi-value fields (e.g., keywords) become separate strings. Tags are free-text—no pre-registration required.
Single-Document Retrieval Relies on Unique Tags
Bailian’s retrieval and Q&A do not support document-ID filtering—only tags-based filtering. Thus, single-document tagging requires assigning a unique temporary tag (e.g., auto_tag_temp) per document. Tags added during the Select Data step apply in bulk to all uploaded files—and cannot isolate individual documents.
Constrained by Max Recall Limit
The Knowledge Q&A service caps retrievable chunks per request at 20. Documents with >20 chunks cannot be fully processed in one call—split them or use batched tagging.
Tag Filtering Is a Static, Service-Level Configuration
Tag filtering in Q&A services is fixed upon publishing the configuration—it does not vary per request. For batch tagging multiple documents, choose one of two approaches:
| Approach | How To | Use Case |
|---|---|---|
| Shared Temporary Tag | Assign same temp tag (e.g., auto_tag_temp) to all docs; tag sequentially; overwrite with business tags afterward | Small batches, serial execution |
| Dedicated Services | Create one Q&A service per document (or group), each configured with its own tag filter | Large-scale parallel tagging |
Frequently Asked Questions
Why does tagging only cover part of the document?Confirm ranking model is disabled and both TopK and max recall are maximized. For documents with >20 chunks, split or batch. Why does the model output extra text—not just the tag array?
Strengthen prompt constraints requiring only the JSON array. Before parsing, extract the first
[...] substring from the response.
Why does tag write-back fail?Check for unsupported characters (colons, spaces) and ensure no tag exceeds 32 characters.