query

package
v0.5.13 Latest Latest
Warning

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

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

Documentation

Overview

Package query 提供 RAG 系统的查询增强功能

Query Enhancement 用于提升检索质量:

  • QueryExpander: 查询扩展,添加相关术语
  • QueryRewriter: 查询重写,改进查询表达
  • MultiQueryGenerator: 多查询生成,从不同角度检索
  • HyDEGenerator: 假设文档生成,基于假设答案检索

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExpanderOption

type ExpanderOption func(*QueryExpander)

ExpanderOption QueryExpander 选项

func WithExpanderMaxExpansions

func WithExpanderMaxExpansions(max int) ExpanderOption

WithExpanderMaxExpansions 设置最大扩展数量

func WithExpanderStrategy

func WithExpanderStrategy(strategy string) ExpanderOption

WithExpanderStrategy 设置扩展策略

type HyDEGenerator

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

HyDEGenerator 假设文档嵌入生成器

HyDE (Hypothetical Document Embeddings) 是一种创新的检索策略: 1. 根据查询生成一个假设的答案文档 2. 使用假设文档的嵌入进行检索,而不是查询的嵌入 3. 因为文档-文档相似度通常高于查询-文档相似度,可以提高检索质量

func NewHyDEGenerator

func NewHyDEGenerator(llm LLMProvider, opts ...HyDEOption) *HyDEGenerator

NewHyDEGenerator 创建 HyDE 生成器

func (*HyDEGenerator) Generate

func (g *HyDEGenerator) Generate(ctx context.Context, query string) (string, error)

Generate 生成假设文档

func (*HyDEGenerator) GenerateMultiple

func (g *HyDEGenerator) GenerateMultiple(ctx context.Context, query string, count int) ([]string, error)

GenerateMultiple 生成多个假设文档

通过生成多个假设文档并检索,可以进一步提高覆盖率

type HyDEOption

type HyDEOption func(*HyDEGenerator)

HyDEOption HyDEGenerator 选项

func WithHyDEDocLength

func WithHyDEDocLength(length int) HyDEOption

WithHyDEDocLength 设置生成文档长度

func WithHyDETemperature

func WithHyDETemperature(temp float32) HyDEOption

WithHyDETemperature 设置 LLM 温度

type LLMProvider

type LLMProvider interface {
	Complete(ctx context.Context, prompt string) (string, error)
}

LLMProvider 是 LLM 提供者接口(简化版)

type MultiQueryGenerator

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

MultiQueryGenerator 多查询生成器

从不同角度生成多个查询,提高检索覆盖率。 这对于复杂问题特别有用,可以从多个侧面检索相关信息。

func NewMultiQueryGenerator

func NewMultiQueryGenerator(llm LLMProvider, opts ...MultiQueryOption) *MultiQueryGenerator

NewMultiQueryGenerator 创建多查询生成器

func (*MultiQueryGenerator) Generate

func (g *MultiQueryGenerator) Generate(ctx context.Context, query string) ([]string, error)

Generate 生成多个查询

type MultiQueryOption

type MultiQueryOption func(*MultiQueryGenerator)

MultiQueryOption MultiQueryGenerator 选项

func WithDiversityBoost

func WithDiversityBoost(boost bool) MultiQueryOption

WithDiversityBoost 设置是否增强多样性

func WithIncludeSelf

func WithIncludeSelf(include bool) MultiQueryOption

WithIncludeSelf 设置是否包含原始查询

func WithNumQueries

func WithNumQueries(num int) MultiQueryOption

WithNumQueries 设置生成查询数量

type ProcessResult

type ProcessResult struct {
	Original        string   // 原始查询
	Rewritten       string   // 重写后的查询
	Expanded        []string // 扩展查询列表
	MultiQueries    []string // 多角度查询列表
	HypotheticalDoc string   // 假设文档
}

ProcessResult 查询处理结果

func (*ProcessResult) GetAllQueries

func (p *ProcessResult) GetAllQueries() []string

GetAllQueries 获取所有生成的查询

返回所有查询的去重列表,用于多路检索

type ProcessorOption

type ProcessorOption func(*QueryProcessor)

ProcessorOption QueryProcessor 选项

func WithEnableAll

func WithEnableAll(enable bool) ProcessorOption

WithEnableAll 设置是否启用所有增强

func WithExpander

func WithExpander(expander *QueryExpander) ProcessorOption

WithExpander 设置查询扩展器

func WithHyDEGen

func WithHyDEGen(hydeGen *HyDEGenerator) ProcessorOption

WithHyDEGen 设置 HyDE 生成器

func WithMultiGen

func WithMultiGen(multiGen *MultiQueryGenerator) ProcessorOption

WithMultiGen 设置多查询生成器

func WithRewriter

func WithRewriter(rewriter *QueryRewriter) ProcessorOption

WithRewriter 设置查询重写器

type QueryExpander

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

QueryExpander 查询扩展器

通过添加同义词、相关术语等方式扩展查询,提高召回率。 可以使用 LLM 或词典方式进行扩展。

func NewQueryExpander

func NewQueryExpander(llm LLMProvider, opts ...ExpanderOption) *QueryExpander

NewQueryExpander 创建查询扩展器

func (*QueryExpander) Expand

func (e *QueryExpander) Expand(ctx context.Context, query string) ([]string, error)

Expand 扩展查询

输入原始查询,返回扩展后的查询列表(包含原始查询)

type QueryProcessor

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

QueryProcessor 查询处理器

组合多种查询增强技术,提供统一的查询处理接口

func NewQueryProcessor

func NewQueryProcessor(opts ...ProcessorOption) *QueryProcessor

NewQueryProcessor 创建查询处理器

func (*QueryProcessor) Process

func (p *QueryProcessor) Process(ctx context.Context, query string) (*ProcessResult, error)

Process 处理查询

根据配置应用各种查询增强技术

type QueryRewriter

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

QueryRewriter 查询重写器

使用 LLM 重写查询,使其更清晰、更具体、更易检索。

func NewQueryRewriter

func NewQueryRewriter(llm LLMProvider, opts ...RewriterOption) *QueryRewriter

NewQueryRewriter 创建查询重写器

func (*QueryRewriter) Rewrite

func (r *QueryRewriter) Rewrite(ctx context.Context, query string) (string, error)

Rewrite 重写查询

type RewriterOption

type RewriterOption func(*QueryRewriter)

RewriterOption QueryRewriter 选项

func WithRewriterMode

func WithRewriterMode(mode string) RewriterOption

WithRewriterMode 设置重写模式

Jump to

Keyboard shortcuts

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