reranker

package
v1.2.11 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package reranker implements RAG's optional second-pass reranking step: an HTTP client speaking the Cohere-compatible rerank API shape ({query, documents, top_n} -> {results: [{index, relevance_score}]}), which LangSearch's hosted API, vLLM's built-in reranker endpoint, and Cohere itself all speak. HuggingFace Text Embeddings Inference (TEI) uses a slightly different shape ({query, texts} -> a bare array of {index, score}) - HTTPReranker sends both request field names and parses both response shapes, so the same client works against either without needing to know in advance which server is on the other end.

This means a fully free, self-hosted alternative to LangSearch's hosted API is just a BaseURL away: point RAG_RERANK_URL at a local TEI or vLLM instance serving BAAI/bge-reranker-v2-m3 (Apache 2.0, runs on CPU) and no API key or external network call is needed at all.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v1.1.0

type Config struct {
	// BaseURL is the full rerank endpoint URL, e.g.
	// "https://api.langsearch.com/v1/rerank" or "http://localhost:8080/rerank"
	// for a self-hosted TEI/vLLM instance. Empty means unconfigured -
	// IsConfigured() reports false and Rerank refuses to run.
	BaseURL string

	// APIKey is sent as "Authorization: Bearer <key>" when non-empty.
	// Self-hosted servers (TEI, a local vLLM instance) typically don't need
	// one; LangSearch and Cohere do.
	APIKey string

	// Model is sent as the "model" request field when non-empty. Ignored by
	// single-model servers like TEI, required by LangSearch/Cohere/vLLM.
	Model string

	Timeout time.Duration
}

Config configures an HTTPReranker.

func FromEnv added in v1.1.0

func FromEnv() Config

FromEnv reads reranker configuration from environment variables:

RAG_RERANK_URL      — full rerank endpoint URL (self-hosted TEI/vLLM, or any Cohere-compatible service)
RAG_RERANK_MODEL    — model name (ignored by single-model servers such as TEI)
RAG_RERANK_API_KEY  — optional; omit for a self-hosted server that doesn't require auth

When RAG_RERANK_URL isn't set, falls back to LangSearch's hosted endpoint if LANGSEARCH_API_KEY is set - the previous zero-config default, kept for backward compatibility. Returns a zero Config (IsConfigured() == false) when neither is configured.

type HTTPReranker added in v1.1.0

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

HTTPReranker reranks documents via a Cohere-compatible (or TEI-compatible) HTTP rerank endpoint.

func New added in v1.1.0

func New(cfg Config) *HTTPReranker

New creates an HTTPReranker from an explicit Config.

func NewFromEnv added in v1.1.0

func NewFromEnv() *HTTPReranker

NewFromEnv creates an HTTPReranker from environment variables (see FromEnv).

func (*HTTPReranker) IsConfigured added in v1.1.0

func (r *HTTPReranker) IsConfigured() bool

IsConfigured reports whether a rerank endpoint is set.

func (*HTTPReranker) Rerank added in v1.1.0

func (r *HTTPReranker) Rerank(ctx context.Context, query string, docs []string, topN int) ([]int, []float32, error)

Rerank sends docs to the configured endpoint and returns their indices sorted by descending relevance, along with the score for each position. topN caps the returned list; pass 0 to return all.

type LangSearchReranker

type LangSearchReranker = HTTPReranker

LangSearchReranker is HTTPReranker preconfigured for LangSearch's hosted rerank API (langsearch.com - no credit card required for the free tier). Kept as a distinct name for backward compatibility; for a fully free, self-hosted alternative requiring no API key, use New/NewFromEnv with a BaseURL pointing at a local TEI or vLLM instance instead - see reranker.go's package doc.

func NewLangSearchReranker

func NewLangSearchReranker() *LangSearchReranker

NewLangSearchReranker reads LANGSEARCH_API_KEY from the environment.

func NewLangSearchRerankerWithKey

func NewLangSearchRerankerWithKey(apiKey string) *LangSearchReranker

NewLangSearchRerankerWithKey builds a LangSearch-configured reranker. An empty apiKey produces an unconfigured reranker (IsConfigured() == false) rather than one pointed at LangSearch with no credentials, since LangSearch's API requires a key - matches the pre-generalization behavior.

Jump to

Keyboard shortcuts

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