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 Choice
- 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.
type Choice ¶ added in v2.8.41
type Choice struct {
// Model is the model to call; empty when reranking is off.
Model string `json:"model"`
// Explicit reports that the caller asked for reranking (so a missing key or bad model is an unhonored option).
Explicit bool `json:"explicit"`
}
Choice is the rerank decision for one search request.
func Choose ¶ added in v2.8.41
func Choose(option json.RawMessage, operatorDefault string) Choice
Choose resolves a request's `rerank` option against the operator default (SEARCH_RERANK_MODEL). The option is raw JSON because callers send it loosely: MCP clients that do not know the field pass `false` as the string "false". Absent or null uses the operator default; false, "false", "off", "no", "none" and "0" turn it off; true, "true", "on", "yes" and "1" use the operator default or DefaultModel; any other string names a model (validated later by BuildRequest, so an unknown name is reported, not silently swapped).