Build a knowledge base
This procedure describes the successive steps for the creation of a relevant knowledge base for RAG. We start with one or multiple documents in various formats, and we deliver a single DuckDB file that is ready for semantic and keyword searches.
Understand the indexation process
The diagram below illustrates the preparation of a DuckDB knowledge base.

Each of the steps illustrated above are described in following sections:
- You start by selecting the reference documents that, together, constitute the explicit knowledge used by a specialized conversational agent.
- While
duckdb-vectorscan ingest PDF files, it is better to transcode reference documents to the Markdown format. This format is efficient for chunking, and it accepts structured meta-data. - Tag the documents, adding meta-information that scopes full pages, sections, or both, from the cheapest layer to the most expensive one.
- For the creation of the DuckDB database, each page is split in meaningful chunks. Context is preserved by prefixing chunks with selected tags.
- At the end of the process, you can control the content of the knowledge base and play with sample searches.
Let us dive a bit deeper into each of these steps.
Step 1: gather relevant documents
Start by scoping a domain of expertise. Write down, in a few lines, what the agent has to answer about, for whom, and what it must not answer. This statement is your filter: a document that does not help answer these questions stays out. A small, focused corpus retrieves better than a large and loose one.
Then collect documents from two kinds of sources, and take from both:
- External sources carry what the industry already agrees on: standards, regulations, vendor documentation, and well-known best practices.
- Internal sources carry what your organization knows: curated know-how, opinionated ways of working, and standard operating procedures. They are often the most valuable, because nobody else has written them down.
Many formats can be considered. In the end you need a folder of files, and Markdown is the preferred format. You find it in git repositories, wikis, and documentation sites. In a corporate environment, office automation produces PDF files and editable documents such as Word, Excel, PowerPoint, or equivalents. Put everything in one folder tree, and keep a note of where each file comes from, so that you can refresh it later.
Step 2: transcode documents to Markdown
duckdb-vectors ingests Markdown and PDF natively. The best scenario is still to transcode every file to Markdown. The challenge is to preserve the meaning and the structure of the source as much as possible, and more specifically:
- the text itself, assembled in consistent blocks
- the links, that can be used for the navigation of end-users
- the text attributes, to distinguish the nuances of bold and highlighted text
- the headers, that convey the document structure and that are used in semantic chunking
- the tables, that can be used as data sheets
- the diagrams, that can get a textual representation in mermaid format
- the pictures
End-users can consider three families of tools:
- Converters. markitdown converts PDF, Word, Excel, PowerPoint, HTML and more to Markdown from the command line. pandoc is a good choice for Word and HTML.
- Extraction services. For scanned or complex PDF files, an OCR or document-analysis service, such as Amazon Textract, recovers text and tables that a converter cannot read.
- Agentic transformation. A language model reads the converted page, or a picture of it, and repairs what converters get wrong: it rebuilds a table, describes a picture, and redraws a diagram in mermaid. Use it where fidelity matters, and review a sample of the output.
For a first pass, markitdown runs on demand with uvx:
$ uvx "markitdown[all]" handbook.docx -o handbook.mdThe transcoding procedure applies it to a folder of Word, PowerPoint and OpenDocument files, and lists what is lost on the way. The transcode-to-markdown skill runs the same procedure from a conversation with your agent, so that you can review the result on the spot. PDF files are indexed directly, so they need no conversion unless their structure matters to you.
Step 3: tag Markdown pages and sections
Tags are meta-information attached to a page or to a section. Chunking removes context, so a chunk can no longer say which product, which company or which contract it belongs to. A tag puts that context back. The tags of a page are also stored with its chunks, so that a plain SELECT can filter on page attributes. For example, to list the chunks of the policy pages:
SELECT source, title
FROM chunks
WHERE json_extract_string(metadata, '$.kind') = 'policy';Do not start with the most expensive way to tag. Go through the layers in the order of their cost, and stop at the first one that gives the answers that your users need. The troubleshooting procedure helps to decide.
- Project the metadata that you already have, such as a title, a product or a company, in front of each chunk. No model is called, and it takes minutes. The projection procedure does it.
- Measure on your own questions, before and after, so that a tag is kept on evidence. The evaluation procedure does it.
- Generate contextual tags with a model where the metadata is thin. A language model writes a short context for each section, once and offline, into a sidematter file next to each page. The generation procedure does it, for Markdown pages and for PDF files.
- Keep the tags fresh when pages change, with a check in continuous integration. The generation procedure covers it.
The tagged retrieval workbook answers the questions about the concepts: what a tag is, how a sidematter file is merged, and which keys are reserved.
Step 4: prefix chunks with tags before embedding
Indexing builds the DuckDB file, and it updates it when you run it again. Only the chunks whose text or tags changed are embedded again.
$ duckdb-vectors index ./pages --recursive --database knowledge.duckdbOptionally, write the settings once in the folder of the knowledge base, so that no command needs a flag. duckdb-vectors init ./pages writes a commented .duckdb-vectors.yaml, the manifest. Then duckdb-vectors index, search and status find it from any sub-folder.
Two options control how tags reach the embeddings:
--tag-field "label: $.path"declares one tag. The path is a JSONPath expression evaluated on the front matter of each page. Repeat the option for several tags. The order of the options is the order of the lines in the prefix.--tag-bm25also matches the tags in the exact-token search, so that a query that names a product finds chunks whose text never says it.
For example:
$ duckdb-vectors index ./pages --recursive --database knowledge.duckdb \
--tag-field "product: $.product.name" \
--tag-field "audience: $.audience[*]" \
--tag-field "context: $.retrieval.context" \
--tag-bm25The path $.audience[*] takes every item of a list, and $.retrieval.context takes the section context written by duckdb-vectors annotate. The tagged retrieval workbook lists the supported JSONPath expressions. Keep the prefix short: models cut input beyond their window, and a long prefix pushes the end of the chunk out of it. The projection procedure explains where the values come from, and the evaluation procedure tells you which tags to keep.
Step 5: check the knowledge base
The inspection procedure is the sub-procedure for this step. It goes further than the commands below: it reviews the coverage and the values of each tag, and the prefix that was embedded.
Show where the knowledge base stands, which means its pages and its database: the number of chunks, the embeddings model, the declared tags, the pages not yet indexed, and, with --sources, the indexed sources. It ends with the next command to run.
$ duckdb-vectors status --database knowledge.duckdbThen run sample searches, with questions that your users would really ask:
$ duckdb-vectors search "how do we roll back a release?" --database knowledge.duckdb --top-k 5To find out which tags help and which only add noise, measure them against a file of real queries, one per line:
$ duckdb-vectors tags --database knowledge.duckdb \
--tag-field "product: $.product.name" --queries my-queries.txtUse the same embeddings model for index, status and search: set it in the manifest, or pass the same --embeddings each time. If the sample searches return the wrong passages, change the tags or the transcoding, index again, and compare.
Next steps
- Transcode office documents: convert Word and PowerPoint files to Markdown, step 2.
- Troubleshoot a search that returns the wrong passages: pick the cheapest layer of tagging, step 3.
- Project tags in embeddings, evaluate retrieval and generate contextual tags: the layers of step 3.
- Inspect a knowledge base: review tags, their distribution, and search results, step 5.
- Tagged retrieval workbook: questions and answers on tags and the evidence behind them.
- User guide: the indexing and search reference.