Documentation
¶
Overview ¶
Package toolsearch is the ranked, cross-server tool index behind the search_tools gateway tool (issue #305). It scores every cached tool against a natural-language query with Okapi BM25 over the tool's server name, tool name, description, title, parameter names and parameter descriptions, optionally fused (Reciprocal Rank Fusion) with the cosine similarity of embeddings when an Embedder is configured.
The index is fed per server (SetServerTools) from the proxy's tool cache and answers queries lock-free from an immutable snapshot, so a refresh of one server never blocks a search.
Index ¶
- Constants
- func DefaultSynonyms() map[string]string
- func Stem(w string) string
- func Tokenize(text string) []string
- type Config
- type Embedder
- type Hit
- type HybridConfig
- type Index
- func (ix *Index) EmbedPending(ctx context.Context) (int, error)
- func (ix *Index) EnableHybrid(ctx context.Context, emb Embedder)
- func (ix *Index) HybridEnabled() bool
- func (ix *Index) Len() int
- func (ix *Index) RemoveServer(server string)
- func (ix *Index) Search(ctx context.Context, q Query) []Hit
- func (ix *Index) Servers() []string
- func (ix *Index) SetServerTools(server string, tools []Tool)
- type Options
- type Query
- type Tool
Constants ¶
const ( DefaultK1 = 1.2 DefaultB = 0.75 )
Okapi BM25 parameters (issue #305).
const DefaultK = 5
DefaultK is the number of hits returned when a query asks for none.
const MaxK = 20
MaxK is the largest number of hits a query can ask for.
const RRFK = 60
RRFK is the Reciprocal Rank Fusion constant: a document ranked r (1 based) in a list contributes 1/(RRFK+r) to its fused score.
Variables ¶
This section is empty.
Functions ¶
func DefaultSynonyms ¶
DefaultSynonyms is the small built-in synonym table.
Types ¶
type Config ¶
type Config struct {
// Synonyms adds query expansions ("k8s": "kubernetes cluster") to the
// defaults (DefaultSynonyms); an entry with the same key replaces the
// default one. Each key must be a single word.
Synonyms map[string]string `yaml:"synonyms,omitempty"`
// DisableDefaultSynonyms drops the built-in synonym table.
DisableDefaultSynonyms bool `yaml:"disable_default_synonyms,omitempty"`
// Hybrid turns on BM25 + embedding ranking. Off by default.
Hybrid *HybridConfig `yaml:"hybrid,omitempty"`
}
Config is the `tool_search:` block of the proxy config. A nil *Config means BM25 with the default synonyms and no hybrid mode.
func (*Config) HybridEnabled ¶
HybridEnabled reports whether hybrid mode is configured on.
type Embedder ¶
Embedder turns text into a vector. pkg/cache/embedder's Ollama and OpenAI clients are adapted to it by the caller.
type HybridConfig ¶
type HybridConfig struct {
Enabled bool `yaml:"enabled"`
// Embedder selects the embedding provider (ollama or openai), with
// the same keys as elsewhere in the config.
Embedder embedder.Config `yaml:"embedder"`
}
HybridConfig is `tool_search.hybrid`.
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
Index is a BM25 (optionally hybrid) index over the tools of every server. All methods are safe for concurrent use.
func (*Index) EmbedPending ¶
EmbedPending embeds every indexed tool without a cached vector and drops the vectors of tools no longer indexed. It stops at the first error. It returns the number of tools embedded. Without hybrid mode it does nothing.
func (*Index) EnableHybrid ¶
EnableHybrid turns on hybrid ranking: tool documents are embedded in the background (one goroutine, stopped when ctx ends), their vectors cached by tool-definition hash, and every search fuses the BM25 ranking with the cosine ranking by Reciprocal Rank Fusion. Tools not embedded yet only take part through BM25. Only the first call has an effect.
func (*Index) HybridEnabled ¶
HybridEnabled reports whether EnableHybrid was called.
func (*Index) RemoveServer ¶
RemoveServer drops every tool of server from the index.
func (*Index) Search ¶
Search returns the best hits for q, best first. Ties are broken by server then tool name, so the order is deterministic. With hybrid mode enabled (EnableHybrid), the BM25 ranking is fused with the embedding ranking; if the query cannot be embedded, it falls back to BM25 only.
func (*Index) SetServerTools ¶
SetServerTools replaces the indexed tools of one server. Only that server's tools are re-tokenized; the corpus statistics are then rebuilt into a new snapshot that queries pick up atomically.
type Options ¶
type Options struct {
// Synonyms maps a query word to the words it expands to (e.g.
// "pr" -> "pull request"). Nil means DefaultSynonyms; use an empty,
// non-nil map for none.
Synonyms map[string]string
// Logger receives the hybrid mode's background errors. Nil means
// slog.Default().
Logger *slog.Logger
}
Options configure an Index. The zero value is BM25 with the default synonyms.
type Query ¶
type Query struct {
Text string
// K is the number of hits wanted: 0 means DefaultK, and it is capped
// at MaxK.
K int
// Server, when set, restricts the results to that server's tools.
Server string
// Exclude, when set, drops the tools it reports before the top K are
// taken (tool pinning hides tools awaiting approval, #310).
Exclude func(server, name string) bool
}
Query is one search request.