rerank

package
v2.8.40 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 13 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 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

View Source
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
)
View Source
const DefaultModel = "cohere/rerank-5-fast"

DefaultModel is used when reranking is enabled without naming a model.

View Source
const Endpoint = "https://openrouter.ai/api/v1/rerank"

Endpoint is OpenRouter's rerank API.

Variables

This section is empty.

Functions

func AllowedModel

func AllowedModel(model string) bool

AllowedModel reports whether model is a supported rerank model ID.

func AllowedModels

func AllowedModels() []string

AllowedModels returns the supported model IDs, sorted.

func Blend

func Blend(n int, scores []Score, weight float64) (order []int, blended []float64)

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

func BuildRequest(model, query string, cands []Candidate) ([]byte, error)

BuildRequest returns the JSON body for a rerank call over the first Head(len(cands)) candidates.

func Document

func Document(c Candidate) string

Document renders a candidate as the text the reranker judges: title, host and snippet, bounded in length.

func Head(n int) int

Head returns how many leading candidates are sent (at most MaxCandidates).

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 Score

type Score struct {
	Index     int     `json:"index"`
	Relevance float64 `json:"relevance_score"`
}

Score is one reranker judgement: Index into the candidates sent and its relevance (higher is better).

func ParseResponse

func ParseResponse(body []byte, n int) ([]Score, error)

ParseResponse reads a rerank reply for n candidates. It rejects out-of-range or duplicate indexes and non-finite scores, so a malformed reply can never reorder results.

Jump to

Keyboard shortcuts

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