reranking

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package reranking provides cross-encoder reranking for search results.

Package reranking provides cross-encoder reranking for search results.

Package reranking provides cross-encoder reranking for search results. Uses MS-MARCO MiniLM L6 v2 cross-encoder model for relevance scoring.

Package reranking provides cross-encoder reranking for search results.

Index

Constants

View Source
const (
	// ModelName is the human-readable name for the cross-encoder model
	ModelName = "ms-marco-MiniLM-L6-v2"
	// ModelVersion is the short version string for identification
	ModelVersion = "msmarco-v2"
	// MaxSequenceLength is the maximum combined query+document token length
	MaxSequenceLength = 512
)
View Source
const (
	// DefaultCandidateLimit is the default number of candidates to rerank.
	DefaultCandidateLimit = 100
	// DefaultResultLimit is the default number of results to return after reranking.
	DefaultResultLimit = 10
)

Variables

View Source
var NewONNXReranker func(alpha float64) (Reranker, error)

NewONNXReranker creates an ONNX-based reranker. On Windows, this always returns nil (ONNX runtime unavailable). On other platforms, it creates the cross-encoder service.

Functions

This section is empty.

Types

type APIConfig

type APIConfig struct {
	BaseURL string
	APIKey  string
	Model   string
	Alpha   float64
	Timeout time.Duration
}

APIConfig holds configuration for the API-based reranker.

func DefaultAPIConfig

func DefaultAPIConfig() APIConfig

DefaultAPIConfig returns sensible defaults for the API reranker.

type APIService

type APIService struct {
	// contains filtered or unexported fields
}

APIService provides cross-encoder reranking via an external API (Cohere-compatible).

func NewAPIService

func NewAPIService(cfg APIConfig) (*APIService, error)

NewAPIService creates a new API-based reranker.

func (*APIService) Close

func (s *APIService) Close() error

Close releases resources. API service has no persistent resources to release.

func (*APIService) Rerank

func (s *APIService) Rerank(query string, candidates []Candidate, limit int) ([]RerankResult, error)

Rerank reranks candidates using combined bi-encoder + cross-encoder scores.

func (*APIService) RerankByScore

func (s *APIService) RerankByScore(query string, candidates []Candidate, limit int) ([]RerankResult, error)

RerankByScore reranks candidates sorted by pure cross-encoder score only.

func (*APIService) Score

func (s *APIService) Score(query, document string) (rawScore, normalizedScore float64, err error)

Score scores a single query-document pair. API rerankers return a single normalized score, so rawScore == normalizedScore.

type Candidate

type Candidate struct {
	Metadata   map[string]any
	RerankInfo map[string]float64
	ID         string
	Content    string
	Score      float64
}

Candidate represents a search result candidate for reranking.

type Config

type Config struct {
	// Alpha is the weight for combining scores (0.0-1.0)
	// Higher values favor cross-encoder scores, lower values favor bi-encoder scores
	Alpha float64
}

Config holds configuration for the reranking service.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults for reranking.

type RerankResult

type RerankResult struct {
	Metadata        map[string]any
	ID              string
	Content         string
	OriginalScore   float64
	RerankScore     float64
	CombinedScore   float64
	OriginalRank    int
	RerankRank      int
	RankImprovement int
}

RerankResult represents a reranked search result.

type Reranker

type Reranker interface {
	// Rerank reranks candidates using combined bi-encoder + cross-encoder scores.
	// Returns up to limit results sorted by combined score.
	Rerank(query string, candidates []Candidate, limit int) ([]RerankResult, error)

	// RerankByScore reranks candidates sorted by pure cross-encoder score only.
	RerankByScore(query string, candidates []Candidate, limit int) ([]RerankResult, error)

	// Score scores a single query-document pair.
	// Returns raw cross-encoder logit and normalized (0-1) score.
	Score(query, document string) (rawScore, normalizedScore float64, err error)

	// Close releases model resources.
	Close() error
}

Reranker defines the interface for cross-encoder reranking implementations. Implementations include the ONNX-based local Service and the API-based APIService.

type Service

type Service struct {

	// Weight for combining scores: combined = alpha*rerank + (1-alpha)*original
	// Default 0.7 favors cross-encoder score
	Alpha float64
	// contains filtered or unexported fields
}

Service provides cross-encoder reranking functionality.

func NewService

func NewService(cfg Config) (*Service, error)

NewService creates a new cross-encoder reranking service. Note: ONNX runtime must be initialized before calling this (via embedding.NewService).

func (*Service) Close

func (s *Service) Close() error

Close releases model resources.

func (*Service) Rerank

func (s *Service) Rerank(query string, candidates []Candidate, limit int) ([]RerankResult, error)

Rerank reranks candidates using the cross-encoder model. Takes a query and list of candidates, returns reranked results.

func (*Service) RerankByScore

func (s *Service) RerankByScore(query string, candidates []Candidate, limit int) ([]RerankResult, error)

RerankByScore reranks candidates and returns sorted by pure cross-encoder score. Useful when you want to completely replace bi-encoder ranking.

func (*Service) Score

func (s *Service) Score(query, document string) (rawScore, normalizedScore float64, err error)

Score scores a single query-document pair. Returns the raw cross-encoder logit and normalized score.

Jump to

Keyboard shortcuts

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