search

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildPrompt

func BuildPrompt(question string, results []Result) (systemPrompt, userPrompt string)

BuildPrompt constructs a RAG prompt from search results.

Types

type AskResult

type AskResult struct {
	Answer  string
	Sources []Result
}

AskResult holds the generated answer and its source chunks.

type Asker

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

Asker retrieves relevant chunks and generates an LLM answer with citations.

func NewAsker

func NewAsker(hybrid *Hybrid, bm25 *BM25, generator providers.GenerationProvider, database *db.DB, topK int) *Asker

func (*Asker) Ask

func (a *Asker) Ask(ctx context.Context, question string, collection string) (*AskResult, error)

Ask retrieves context and generates an answer.

type BM25

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

BM25 runs a full-text search using SQLite FTS5's built-in BM25 ranking.

func NewBM25

func NewBM25(database *db.DB) *BM25

func (*BM25) Search

func (b *BM25) Search(ctx context.Context, opts SearchOpts) ([]Result, error)

Search returns up to topK results ranked by BM25.

type Hybrid

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

Hybrid orchestrates BM25 + vector search + RRF fusion.

func NewHybrid

func NewHybrid(bm25 *BM25, vector *VectorSearch, embedding providers.EmbeddingProvider, cfg config.SearchConfig) *Hybrid

func (*Hybrid) Search

func (h *Hybrid) Search(ctx context.Context, opts SearchOpts) ([]Result, error)

Search runs BM25 and (optionally) vector search, then fuses with RRF.

type LLMCache

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

LLMCache caches LLM responses keyed by SHA-256(model + prompt).

func NewLLMCache

func NewLLMCache(database *db.DB) *LLMCache

func (*LLMCache) Get

func (c *LLMCache) Get(ctx context.Context, model, prompt string) (string, bool)

Get returns a cached response, or ("", false) if not found.

func (*LLMCache) Set

func (c *LLMCache) Set(ctx context.Context, model, prompt, response string) error

Set stores a response in the cache.

type Result

type Result struct {
	DocID       int64         `json:"doc_id"`
	ChunkID     int64         `json:"chunk_id"`
	Collection  string        `json:"collection"`
	Path        string        `json:"path"`
	Title       string        `json:"title,omitempty"`
	HeadingPath string        `json:"heading_path,omitempty"`
	Snippet     string        `json:"snippet"`
	Score       float64       `json:"score"`
	Explain     *ScoreExplain `json:"explain,omitempty"`
}

Result is a single search hit.

func ReciprocalRankFusion

func ReciprocalRankFusion(bm25 []Result, vec []Result, k int) []Result

ReciprocalRankFusion merges BM25 and vector result lists using RRF. k is the rank constant (default 60 per the paper). Returns results sorted by descending RRF score.

type ScoreExplain

type ScoreExplain struct {
	BM25Score   float64 `json:"bm25_score,omitempty"`
	BM25Rank    int     `json:"bm25_rank,omitempty"`
	VectorDist  float64 `json:"vector_distance,omitempty"`
	VectorRank  int     `json:"vector_rank,omitempty"`
	RRFScore    float64 `json:"rrf_score,omitempty"`
	RerankScore float64 `json:"rerank_score,omitempty"`
}

ScoreExplain breaks down how a score was computed.

type SearchOpts

type SearchOpts struct {
	Query      string
	Collection string // empty = all collections
	TopK       int
	Mode       string // lexical | hybrid | deep
	Explain    bool
}

SearchOpts configures a search operation.

type VectorSearch

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

VectorSearch performs KNN search using pure Go cosine similarity. Embeddings are loaded from the DB and compared in memory. For large corpora, a dedicated vector index (sqlite-vec, etc.) is preferred.

func NewVectorSearch

func NewVectorSearch(database *db.DB) *VectorSearch

func (*VectorSearch) Search

func (v *VectorSearch) Search(ctx context.Context, queryEmbedding []float32, topK int, collection string) ([]Result, error)

Search returns up to topK results nearest to the query embedding.

Jump to

Keyboard shortcuts

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