passages

package
v2.8.42 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package passages splits extracted Markdown into heading-aware chunks and ranks them against a query with BM25. It backs `POST /v1/extract` ("BM25-ranked passages across 1..20 URLs").

This is the Go home of the Cloudflare hub's chunker and ranker (chunkMarkdownForSearch and rankChunksInMemory in hub/src/search-index.ts). The hub parity test (cmd/edgefetch/hub_parity_test.go) holds the two to the same answers until the hub stops processing pages (docs/HUB-NODE-PROCESSING-PLAN.md). Lengths count runes, which matches the hub's UTF-16 lengths for all text outside the astral planes.

Index

Constants

View Source
const (
	// DefaultChunkChars is the target chunk size, in characters.
	DefaultChunkChars = 1500
	// DefaultChunkOverlap is how much trailing prose a chunk repeats from the one before it.
	DefaultChunkOverlap = 150
)

Variables

This section is empty.

Functions

func QueryTerms

func QueryTerms(query string) []string

QueryTerms are the distinct lowercase terms of at least two characters in a query, at most twelve.

func Tokens

func Tokens(c Chunk) []string

Tokens are the lowercase terms of a chunk's heading and text.

Types

type Chunk

type Chunk struct {
	Ordinal int     `json:"ordinal"`
	Heading string  `json:"heading"`
	Text    string  `json:"text"`
	Score   float64 `json:"score,omitempty"`
}

Chunk is one passage of a document.

func Rank

func Rank(chunks []Chunk, query string, topK int) []Chunk

Rank scores chunks against a query with BM25 (k1 1.5, b 0.75, a 1.35 boost for terms in the heading, times the boilerplate weight) and returns the best topK, best first. With no usable query terms it returns the first topK, each scored 1.

func Split

func Split(markdown string, targetChars, overlapChars int) []Chunk

Split splits Markdown into ~targetChars chunks on block and sentence boundaries. Every chunk carries its nearest heading, no chunk cuts inside a `[link](url)` or inline code, and a chunk never starts mid-sentence.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL