rerank

package
v0.14.0 Latest Latest
Warning

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

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

Documentation

Overview

Package rerank defines the provider-neutral protocol for ranking documents against one query. Results address the immutable input batch by index so the protocol does not duplicate document ownership or depend on a retrieval type.

Example
package main

import (
	"fmt"

	"github.com/Tangerg/scope/core/rerank"
)

func main() {
	request, err := rerank.NewRequest("capital of France", []string{
		"Berlin is the capital of Germany.",
		"Paris is the capital of France.",
	})
	if err != nil {
		panic(err)
	}
	request.Options.Model = "provider-reranker"

	fmt.Println(request.Query, len(request.Documents))
}
Output:
capital of France 2

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidOptions  = errors.New("rerank: invalid options")
	ErrInvalidRequest  = errors.New("rerank: invalid request")
	ErrInvalidResponse = errors.New("rerank: invalid response")
)

Functions

This section is empty.

Types

type Model

type Model interface {
	// Call ranks an immutable document batch for one query. It must not retain or
	// mutate request and transfers ownership of the response to the caller.
	Call(ctx context.Context, request *Request) (*Response, error)
}

Model is the complete provider-neutral reranking SPI. Implementations validate requests before I/O, reject explicit options they cannot represent, preserve context error identity, and return responses that pass ValidateFor.

type ModelFunc

type ModelFunc func(context.Context, *Request) (*Response, error)

ModelFunc lets an ordinary function satisfy Model without declaring a named type, which is what keeps middleware and test doubles from each inventing their own adapter.

func (ModelFunc) Call

func (m ModelFunc) Call(ctx context.Context, request *Request) (*Response, error)

type Options

type Options struct {
	Model      string              `json:"model"`
	TopK       int                 `json:"top_k,omitempty"`
	Extensions metadata.Extensions `json:"extensions,omitzero"`
}

Options holds per-request reranking configuration. TopK zero means every document; provider-specific controls remain in Extensions.

func (Options) Clone

func (o Options) Clone() Options

func (Options) MarshalJSON

func (o Options) MarshalJSON() ([]byte, error)

func (Options) Resolve

func (o Options) Resolve(override Options) (Options, error)

func (Options) ResultLimit

func (o Options) ResultLimit(documentCount int) int

func (*Options) UnmarshalJSON

func (o *Options) UnmarshalJSON(data []byte) error

func (Options) Validate

func (o Options) Validate() error

type Request

type Request struct {
	Query     string   `json:"query"`
	Documents []string `json:"documents"`
	Options   Options  `json:"options,omitzero"`
}

Request is one reranking call. Result indices address Documents in this immutable order.

func NewRequest

func NewRequest(query string, documents []string) (*Request, error)

NewRequest binds the query and its candidate set together because a rerank result is addressed by position into that exact slice. Accepting them separately would let a caller score one list and index into another.

func (Request) MarshalJSON

func (r Request) MarshalJSON() ([]byte, error)

func (*Request) UnmarshalJSON

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

func (*Request) Validate

func (r *Request) Validate() error

type Response

type Response struct {
	Results  []*Result         `json:"results"`
	Metadata *ResponseMetadata `json:"metadata,omitempty"`
}

Response is a relevance-descending subset of the input document indices.

func NewResponse

func NewResponse(results []*Result, responseMetadata *ResponseMetadata) (*Response, error)

NewResponse validates a complete provider result at the protocol boundary.

func (*Response) First

func (r *Response) First() *Result

func (Response) MarshalJSON

func (r Response) MarshalJSON() ([]byte, error)

func (*Response) UnmarshalJSON

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

func (*Response) Validate

func (r *Response) Validate() error

func (*Response) ValidateFor

func (r *Response) ValidateFor(request *Request) error

type ResponseMetadata

type ResponseMetadata struct {
	Model string       `json:"model"`
	Usage *Usage       `json:"usage,omitempty"`
	Extra metadata.Map `json:"extra,omitzero"`
}

ResponseMetadata records the served model, portable usage, and open provider response metadata.

func (ResponseMetadata) MarshalJSON

func (r ResponseMetadata) MarshalJSON() ([]byte, error)

func (*ResponseMetadata) UnmarshalJSON

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

type Result

type Result struct {
	Index int   `json:"index"`
	Score Score `json:"score"`
}

Result relates one input document index to its normalized relevance.

func NewResult

func NewResult(index int, score Score) (*Result, error)

NewResult addresses a document by its index in the request rather than carrying the document itself. Repeating the text would let a response disagree with the request it answers, and would pay to move the corpus back across the wire.

func (Result) MarshalJSON

func (r Result) MarshalJSON() ([]byte, error)

func (*Result) UnmarshalJSON

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

func (*Result) Validate

func (r *Result) Validate() error

type Score

type Score float64

Score is a normalized relevance value in [0, 1].

func (Score) Float64

func (s Score) Float64() float64

func (Score) MarshalJSON

func (s Score) MarshalJSON() ([]byte, error)

func (*Score) UnmarshalJSON

func (s *Score) UnmarshalJSON(data []byte) error

func (Score) Validate

func (s Score) Validate() error

type Usage

type Usage struct {
	InputTokens int64 `json:"input_tokens"`
}

Usage records input tokens when the provider reports them.

func (Usage) MarshalJSON

func (u Usage) MarshalJSON() ([]byte, error)

func (*Usage) UnmarshalJSON

func (u *Usage) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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