跳到正文
DEV Community· GageSterling2648·· 13 小時前AI 評分58

旅遊檢索中,作者為何選擇明確分塊邊界與引用優先策略

Why I Chose Explicit Chunk Boundaries for Travel Retrieval — A Citation-First Trade-off

AI 導讀

作者為旅遊行程規劃器採用按行程事實與引用單位劃分的文本區塊,再以元資料篩選少量區塊,讓每項行程陳述能對應到來源跨度。攝取流程先依標題、清單項目、表格列或段落識別結構邊界,再只在實體和有效時間相同時合併相鄰單位;重疊只用於補足代詞或局部例外。生成時要求每項陳述至少對應一個檢索跨度,若沒有跨度達到相關性門檻,便改為提問或明確表示沒有來源。

正文

Short answer: for a travel itinerary planner, I use chunks whose boundaries follow itinerary facts and citation units, then retrieve a small set of those chunks with metadata filters. Bigger windows are easier to build, but they blur which source supports a date, fare rule, or opening hour.

I learned this while reviewing an on-call incident pattern in production systems: a missed job is visible, but a duplicate delivery that looks plausible can survive for days. Retrieval has the same shape. A planner can return a beautiful day-by-day route with one unsupported detail hidden inside a large passage. The failure is quiet. The fix starts with boundaries, not with a larger model.

How should chunk boundaries support retrieval, grounding, and citation?

In my postmortems, I write down the smallest claim that must be replayed. For an itinerary, that might be “the museum is closed on Monday” or “the rail ticket is valid after 09:00.” Each claim needs a stable source span, a timestamp, and a location in the original document. A chunk that mixes three cities and six policy paragraphs cannot provide that audit trail. The invariant is simple: split where meaning, authority, or validity changes.

The invariant is simple: split where meaning, authority, or validity changes. Keep a booking rule together with its exceptions. Keep a venue's hours together with the timezone and effective date. Separate editorial suggestions from supplier terms. Store document_id, section, start_offset, end_offset, locale, and valid_from as metadata so a citation can point back to the exact text.

This is less tidy than fixed 500-token slices. It is also easier to explain when a traveler asks, “Why did you put this stop on Tuesday?”

That is the whole test.

I use a two-pass ingestion path. First, a parser finds structural boundaries: heading, list item, table row, or paragraph. Second, a size budget merges adjacent units only when they share an entity and validity window. Overlap is reserved for sentences that resolve a pronoun or define a local exception; blanket overlap creates duplicate evidence and noisy ranking.

Here is the shape of the contract in Go. It is deliberately boring: deterministic IDs make re-indexing idempotent, and the source span travels with the vector.

package chunks

import (
    "crypto/sha256"
    "encoding/hex"
    "fmt"
)

type Unit struct {
    DocumentID string
    Section    string
    Text       string
    Start      int
    End        int
    Entity     string
    ValidFrom  string
}

type Chunk struct {
    ID       string
    Text     string
    Metadata map[string]string
}

func Build(u Unit) Chunk {
    key := fmt.Sprintf("%s:%d:%d", u.DocumentID, u.Start, u.End)
    h := sha256.Sum256([]byte(key))
    return Chunk{
        ID:   hex.EncodeToString(h[:]),
        Text: u.Text,
        Metadata: map[string]string{
            "document_id": u.DocumentID,
            "section":     u.Section,
            "start":       fmt.Sprint(u.Start),
            "end":         fmt.Sprint(u.End),
            "entity":      u.Entity,
            "valid_from":  u.ValidFrom,
        },
    }
}

The index should return the chunk and its metadata, not just a similarity score. At generation time, require every itinerary assertion to map to at least one retrieved span. If no span clears the relevance threshold, emit a question or a bounded “I don't have a source for that” response. Hallucinated confidence is an operational defect, even when the HTTP request succeeded.

Operating the evidence trail after launch

Fixed-size windows are a useful baseline. They are fast, language-agnostic, and easy to batch. Their weakness is semantic leakage: a window can begin with a cancellation rule and end inside a different city's fare table. Sentence-only chunks improve precision but lose context for “it,” “the second day,” and list headings.

Parent-child retrieval is my compromise for long guides. Embed small child units, then expand to the parent section only after ranking. A graph of entities and dates helps when the query crosses flights, hotels, and attractions, but it adds synchronization work and another place for stale edges. I would not add that graph for a prototype with one document format.

I have also compared common hosted and self-managed vector stacks in design reviews. Their filtering syntax, update semantics, and payload limits differ; none removes the need to define an evidence unit. Pick the engine that fits your durability, latency, and compliance constraints. The boundary policy belongs in your application and test suite.

Here is a failure I now force into review. A source guide says a city pass covers “three museums,” then a footnote excludes the aquarium on public holidays. A naive splitter embeds the sentence and footnote separately; the planner retrieves the attractive sentence, cites it, and recommends the aquarium for a holiday Monday. With structural chunks, the footnote stays attached to the pass rule, while metadata marks the holiday exception and the source offset. The test query includes the date, not just the attraction name. We inspect the retrieved IDs, confirm that the exception appears in the context, and reject the answer if the citation points at the marketing paragraph. This check catches a boundary error before it becomes a support ticket, and it works regardless of which vector engine stores the payload.

The catch is scope. Explicit boundaries are a poor fit for unstructured chat logs, rapidly changing sensor streams, or a corpus where citations are not required. In those cases, fixed windows or a streaming summarizer may deliver better recall and lower ingestion overhead. Stick with a simpler splitter when the team cannot maintain source offsets and freshness metadata; an elegant policy without those fields is theater.

Your mileage may vary with multilingual text and tokenizers. I am not sure a single size budget works across every script, so I measure recall separately for each locale instead of assuming English results transfer. The acceptance test is concrete: can a reviewer trace each date, price condition, and opening hour in a generated itinerary to one current source span?

A runbook for operating the index

Start with a golden set of itinerary questions: “Is the gallery open Monday?”, “Can I use this pass after 9 AM?”, and “Which stop is near the station?” Label the supporting spans, then track recall@k, citation coverage, stale-hit rate, and duplicate-chunk rate after every parser change. Keep raw documents immutable; write a new parser version and re-index in the background. Swap the alias only after checks pass.

On-call alerts should distinguish retrieval empty, retrieval stale, and generation uncited. Log the query hash and chunk IDs, not traveler PII. A failed batch must be safe to retry: deterministic IDs and upserts prevent duplicate vectors, while a manifest records which document version was indexed. That is the same idempotency reflex I use for queues.

The recommendation is conditional, not universal: use explicit, citation-bearing boundaries when grounding is the primary decision axis for a travel itinerary planner. Measure the trade-off with your own corpus, and change the policy when the evidence trail stops matching the traveler-facing answer.

References

來源:DEV Community · dev.to