Skip to content

Inspect a knowledge base ​

This sub-procedure details step 5 of the build procedure. A knowledge manager looks inside a DuckDB knowledge base to check its content, to review the tags and their distribution, and to try searches before agents rely on it. Run it after each indexing, and again when the pages or the tags change.

What do you need? ​

  • A knowledge base built with the build procedure, here knowledge.duckdb.
  • The same embeddings model as the one used for indexing, for every verb below. Set it once as embeddings in the manifest, or pass the same --embeddings each time. A different model embeds queries in a different space and returns noise.
  • For the SQL steps, the duckdb Python package, which duckdb-vectors already installs. Open the file read-only, and not while another process is indexing it.
  • A file of real questions, one per line, for steps 4 and 6. Questions taken from a log or from your users are the best. Do not write them after looking at the tags.

Step 1: check the content ​

The status verb counts the chunks and, with --sources, lists the sources. Declare the tags in the environment to see what the current settings expect:

shell
$ DUCKDB_VECTORS_TAG_FIELDS="product: $.product.name; audience: $.audience[*]" \
    duckdb-vectors status --database knowledge.duckdb

Check that every page you expect is among the sources, that the number of chunks is plausible for the volume of text, and that tag_fields shows the tags you meant. A page that is missing was not transcoded, or sits outside the folder that you indexed.

Step 2: check the coverage of tags ​

A tag is useful only on the pages that carry it. Write a small script, for instance inspect_tags.py, that opens the file read-only and runs the SQL of this procedure:

python
import duckdb

con = duckdb.connect("knowledge.duckdb", read_only=True)
rows = con.sql(
    """
    SELECT count(DISTINCT source) AS pages,
           count(DISTINCT source) FILTER (
               json_extract_string(metadata, '$.product.name') IS NOT NULL
           ) AS tagged
    FROM chunks
    """
).fetchall()
print(rows)

The metadata column holds the front matter of each page as JSON, with section metadata merged in, so any path that you give to --tag-field works here too. List the pages that lack the tag, and fill in their front matter or leave them out on purpose:

sql
SELECT DISTINCT source
FROM chunks
WHERE json_extract_string(metadata, '$.product.name') IS NULL
ORDER BY source;

A tag that covers fewer than half of the pages helps little: most chunks get no context from it.

Step 3: review the values of each tag ​

Count the pages for each value of a tag. A tag works when values repeat across related pages, and it fails when the same thing is written in several ways:

sql
SELECT json_extract_string(metadata, '$.product.name') AS value,
       count(DISTINCT source) AS pages,
       count(*) AS chunks
FROM chunks
GROUP BY value
ORDER BY pages DESC;

Read the list from top to bottom and look for:

  • Variants of one value, such as Enterprise, enterprise plan and Ent.. Pick one spelling, and fix the front matter at the source.
  • A single value shared by every page. It separates nothing, so drop the tag.
  • A different value on every page. A title can still match words of a query, while an opaque code cannot.
  • Empty answers such as Other or None, mostly written by language models. Remove them, since a tag that says nothing tells a search nothing.

Do the same for derived tags, under $.retrieval. For section contexts, which differ on every section, read a sample instead: pick ten sections, and check that each context names what its section loses when it is cut from its page.

Step 4: read the tags report ​

The tags verb computes the figures of every declared tag over the whole index, and gives a verdict for each: effective or decorative. Run it with your questions, because the verdict relies on them:

shell
$ duckdb-vectors tags --database knowledge.duckdb \
    --tag-field "product: $.product.name" \
    --tag-field "kind: $.kind" \
    --queries my-queries.txt

The fields of the report, the rule of the verdict and its limits are described once, in the projection procedure. In short, the report tells you which tags to try and which to drop first. It cannot tell you whether a tag helps your searchers, and its verdict is provisional. Its warnings list also says when prefix and chunk probably exceed the input window of the model, which step 5 looks at.

Step 5: look at the embedded prefix ​

The tags column stores the prefix of each chunk, as it was embedded. Read a few, and find the longest:

sql
SELECT chunk_id, tags, length(tags) AS prefix_characters
FROM chunks
ORDER BY prefix_characters DESC
LIMIT 5;

Models cut input beyond their window, at 512 tokens for every model of fastembed 0.9.1, and at 128 for the two paraphrase-multilingual models. Four characters make about one token. A prefix of several hundred characters, added to a long chunk, pushes the end of the chunk out of the window. Shorten the prefix by dropping tags that the report calls decorative. See How long can the prefix be?.

Step 6: try searches ​

Run the questions of your file, one by one, and read the hits:

shell
$ duckdb-vectors search "how do we roll back a release?" \
    --database knowledge.duckdb --top-k 5

Each hit carries the raw chunk, its metadata, a score, and its rank in the vector search (vector_rank) and in the keyword search (text_rank). Check three things:

  • The right passage is among the first hits. If not, note the question, because it is a test case for the next iteration.
  • A hit that comes from the keyword search only (no vector_rank) shows that an exact term did the work. If tags were meant to do it, check that you indexed with --tag-bm25.
  • Two near-identical passages from different pages are told apart. This is what tags are for. If the wrong one comes first, the tag that separates them is missing or too weak.

To compare two tag sets on the same pages, index each in its own file, and run the same questions on both:

shell
$ duckdb-vectors index ./pages --recursive --database plain.duckdb
$ duckdb-vectors index ./pages --recursive --database tagged.duckdb \
    --tag-field "product: $.product.name" --tag-bm25
$ duckdb-vectors search "refund window for the enterprise plan" --database plain.duckdb --top-k 3
$ duckdb-vectors search "refund window for the enterprise plan" --database tagged.duckdb --top-k 3

A handful of questions shows a problem. It does not measure a gain: for that, use the questions and the measure of the evaluation procedure.

Step 7: iterate ​

  1. Declare the tags you think matter, including doubtful ones.
  2. Index, then run steps 1 to 5.
  3. Remove the tags that the report marks as decorative, or fix the front matter behind them. A sparse tag may only need to be filled in on more pages.
  4. Index again. Only the chunks whose prefix changed are embedded again.
  5. Run your questions before and after, and compare the hits.

Next steps ​