rerank

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0, MIT Imports: 25 Imported by: 0

Documentation

Overview

Package rerank adapts rerank protocols to application batching and score semantics.

Index

Constants

View Source
const (
	LKEAPRerankEndpoint     = tencentlkeap.LKEAPRerankEndpoint
	LKEAPDefaultRegion      = tencentlkeap.LKEAPDefaultRegion
	LKEAPDefaultRerankModel = tencentlkeap.LKEAPDefaultRerankModel
)

Tencent action defaults retained for existing callers.

Variables

View Source
var ErrImagesUnsupported = errors.New("rerank model does not accept images")

ErrImagesUnsupported is returned when a reranker is asked to score images its model or endpoint cannot take.

Functions

func MaxPassageRunes

func MaxPassageRunes(r Reranker, query string) int

MaxPassageRunes reports r's passage limit for query, or 0 when r documents none.

Types

type DocumentInfo

type DocumentInfo struct {
	Text string `json:"text"`
}

func (*DocumentInfo) UnmarshalJSON

func (d *DocumentInfo) UnmarshalJSON(data []byte) error

UnmarshalJSON handles both string and object formats for DocumentInfo

type Image

type Image = api.EmbedImage

Image is one image document: its bytes and MIME type.

type ImageReranker

type ImageReranker interface {
	// AcceptsImages reports whether both the model and its endpoint take
	// image documents.
	AcceptsImages() bool
	// ImageLimits are the vendor's documented per-image limits. The caller
	// prepares an image to fit; one outside them is refused.
	ImageLimits() imageprep.Limits
	// RerankImages scores images against the query on the same scale as
	// Rerank scores text, best first; indices refer to images.
	RerankImages(ctx context.Context, query string, images []Image) ([]RankResult, error)
}

ImageReranker is the image side of a Reranker whose model scores an image against a text query by what it shows. Most rerankers have none; use AsImageReranker rather than a type assertion, since every decorator implements it whether or not the model underneath does.

func AsImageReranker

func AsImageReranker(r Reranker) (ImageReranker, bool)

AsImageReranker returns r's image side when its model scores images.

type PassageLimiter

type PassageLimiter interface {
	// MaxPassageRunes returns the longest document, in runes, one request
	// can carry beside query. 0 means no documented limit.
	MaxPassageRunes(query string) int
}

PassageLimiter is implemented by rerankers whose vendor documents how large a document may be. The rerank stage builds its passages itself (title, chunk body, captions, OCR text, generated questions), so it can fit them to the limit instead of letting one oversized candidate fail the whole request, which the protocol layer rightly refuses to truncate on its own.

type RankResult

type RankResult struct {
	Index          int          `json:"index"`
	Document       DocumentInfo `json:"document"`
	RelevanceScore float64      `json:"relevance_score"`
}

func (*RankResult) UnmarshalJSON

func (r *RankResult) UnmarshalJSON(data []byte) error

Handles the RelevanceScore field by checking if RelevanceScore exists first, otherwise falls back to Score field

type Reranker

type Reranker interface {
	// Rerank reranks documents based on relevance to the query
	Rerank(ctx context.Context, query string, documents []string) ([]RankResult, error)

	// GetModelName returns the model name
	GetModelName() string

	// GetModelID returns the model ID
	GetModelID() string
}

Reranker defines the interface for document reranking

func NewReranker

func NewReranker(config *RerankerConfig) (Reranker, error)

NewReranker creates a reranker based on the configuration

type RerankerConfig

type RerankerConfig struct {
	APIKey      string
	BaseURL     string
	ModelName   string
	Source      types.ModelSource
	ModelID     string
	Provider    string                   // Provider identifier: openai, aliyun, zhipu, siliconflow, jina, generic
	Spec        *types.ModelSpecOverride `json:"spec,omitempty"`
	ExtraConfig map[string]string
	// CustomHeaders 允许在调用远程 API 时附加自定义 HTTP 请求头(类似 OpenAI Python SDK 的 extra_headers)。
	CustomHeaders map[string]string
	AppID         string
	AppSecret     string // 加密值,工厂函数调用方传入,使用前已解密
}

func ConfigFromModel

func ConfigFromModel(m *types.Model, appID, appSecret string) *RerankerConfig

ConfigFromModel 根据 types.Model 构造 RerankerConfig。 生产路径(从 DB 拉起)和测试连接路径(临时表单)共享这份映射。 appID / appSecret 是已解密的 WeKnoraCloud 凭证,调用方负责传入。

Jump to

Keyboard shortcuts

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