Documentation
¶
Overview ¶
Package rerank re-orders fused web-search candidates with a hosted cross-encoder reranker (OpenRouter's /api/v1/rerank, which serves Cohere and Voyage/MongoDB rerank models).
Both hubs use it: the self-hosted Go hub calls Rerank directly, and the Cloudflare Worker reaches BuildRequest and Blend through the WASM ops rerank_prepare / rerank_blend, making only the HTTPS call itself. Reranking is advisory: any failure, timeout or malformed reply leaves the reciprocal-rank-fusion order unchanged.
Index ¶
- Constants
- func AllowedModel(model string) bool
- func AllowedModels() []string
- func Blend(n int, scores []Score, weight float64) (order []int, blended []float64)
- func BuildRequest(model, query string, cands []Candidate) ([]byte, error)
- func Document(c Candidate) string
- func Head(n int) int
- func Rerank(ctx context.Context, client *http.Client, apiKey, model, query string, ...) ([]int, []float64, error)
- type Candidate
- type Score
Constants ¶
const ( // MaxCandidates bounds how many fused hits are sent; reranking deeper than this rarely changes the top results. MaxCandidates = 25 // DefaultWeight is the share of the final score taken from the reranker; the rest comes from the fused rank, so // two engines agreeing on a hit still counts when the reranker is unsure. DefaultWeight = 0.7 // Timeout bounds the rerank call; search falls back to the fused order past it. Timeout = 2500 * time.Millisecond )
const DefaultModel = "cohere/rerank-5-fast"
DefaultModel is used when reranking is enabled without naming a model.
const Endpoint = "https://openrouter.ai/api/v1/rerank"
Endpoint is OpenRouter's rerank API.
Variables ¶
This section is empty.
Functions ¶
func AllowedModel ¶
AllowedModel reports whether model is a supported rerank model ID.
func AllowedModels ¶
func AllowedModels() []string
AllowedModels returns the supported model IDs, sorted.
func Blend ¶
Blend returns the new order of the first n candidates (indexes into the fused order) and each one's blended score in [0, 1]. The final score is weight × min-max-normalised relevance + (1 − weight) × fused-rank prior (1 for the top fused hit, falling linearly). Candidates the reranker did not score get relevance 0. Ties keep the fused order. A weight outside (0, 1] uses DefaultWeight.
func BuildRequest ¶
BuildRequest returns the JSON body for a rerank call over the first Head(len(cands)) candidates.
func Document ¶
Document renders a candidate as the text the reranker judges: title, host and snippet, bounded in length.
func Rerank ¶
func Rerank(ctx context.Context, client *http.Client, apiKey, model, query string, cands []Candidate) ([]int, []float64, error)
Rerank calls the rerank endpoint for the self-hosted Go hub and returns the blended order of the first Head(len(cands)) candidates. On any error the caller keeps the fused order.
Types ¶
type Candidate ¶
type Candidate struct {
Title string `json:"title"`
URL string `json:"url"`
Snippet string `json:"snippet"`
}
Candidate is one fused search hit offered to the reranker, in fused (best-first) order.