Documentation
¶
Overview ¶
Package query provides query processing components for the RAG system. It includes query decomposition, rewriting, expansion, and classification capabilities to improve retrieval quality and handle complex user queries.
Index ¶
- func NewEntityExtractor(llm chat.Client, opts ...EntityExtractorOption) *entityExtractor
- func NewIntentRouter(llm chat.Client, opts ...IntentRouterOption) *intentRouter
- func NewKeywordExtractor(opts ...KeywordExtractorOption) *keywordExtractor
- func NewVectorExtractor(graphStore core.GraphStore, embedder embedding.Provider, ...) *vectorExtractor
- type Decomposer
- type DecomposerOption
- type EntityExtractorOption
- type HyDE
- type IntentRouterOption
- func WithDefaultIntent(intent core.IntentType) IntentRouterOption
- func WithIntentPromptTemplate(tmpl string) IntentRouterOption
- func WithIntentRouterCollector(collector observability.Collector) IntentRouterOption
- func WithIntentRouterLogger(logger logging.Logger) IntentRouterOption
- func WithMinConfidence(v float32) IntentRouterOption
- type KeywordExtractorOption
- func WithKeywordExtractorCollector(collector observability.Collector) KeywordExtractorOption
- func WithKeywordExtractorFilter(filter func(word string) bool) KeywordExtractorOption
- func WithKeywordExtractorLogger(logger logging.Logger) KeywordExtractorOption
- func WithKeywordExtractorMaxEntities(max int) KeywordExtractorOption
- func WithKeywordExtractorMinWordLen(minLen int) KeywordExtractorOption
- func WithKeywordExtractorStopWords(stopWords []string) KeywordExtractorOption
- type Rewriter
- type StepBack
- type VectorExtractorOption
- func WithVectorExtractorCollector(collector observability.Collector) VectorExtractorOption
- func WithVectorExtractorLogger(logger logging.Logger) VectorExtractorOption
- func WithVectorExtractorMinLen(minLen int) VectorExtractorOption
- func WithVectorExtractorThreshold(threshold float64) VectorExtractorOption
- func WithVectorExtractorTopK(topK int) VectorExtractorOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewEntityExtractor ¶
func NewEntityExtractor(llm chat.Client, opts ...EntityExtractorOption) *entityExtractor
NewEntityExtractor creates a new entity extractor.
func NewIntentRouter ¶
func NewIntentRouter(llm chat.Client, opts ...IntentRouterOption) *intentRouter
NewIntentRouter creates a new intent router.
func NewKeywordExtractor ¶ added in v1.1.6
func NewKeywordExtractor(opts ...KeywordExtractorOption) *keywordExtractor
NewKeywordExtractor creates a new keyword-based entity extractor. It identifies potential entities using heuristics like capitalization, quoted strings, and non-stopwords, without requiring an LLM.
func NewVectorExtractor ¶ added in v1.1.6
func NewVectorExtractor( graphStore core.GraphStore, embedder embedding.Provider, opts ...VectorExtractorOption, ) *vectorExtractor
NewVectorExtractor creates a new vector-based entity extractor. It extracts candidate entities using heuristics and validates them against the graph store. If an embedder is provided, it can optionally re-rank entities by semantic similarity.
Types ¶
type Decomposer ¶
type Decomposer struct {
// contains filtered or unexported fields
}
Decomposer implements core.QueryDecomposer to break down complex queries. It uses an LLM to analyze queries and generate simpler sub-queries that can be processed independently, enabling multi-hop retrieval for complex questions.
Query decomposition is useful for:
- Multi-hop questions: "What is the revenue of the company that acquired GitHub?"
- Comparative questions: "Compare the features of Product A and Product B"
- Temporal questions: "How has the market changed since 2020?"
Example:
llm := openai.NewClient(apiKey)
decomposer := query.NewDecomposer(llm,
query.WithMaxSubQueries(3),
query.WithDecomposerLogger(logger),
)
result, err := decomposer.Decompose(ctx, query)
// result.SubQueries contains ["What company acquired GitHub?", "What is the revenue of that company?"]
func NewDecomposer ¶
func NewDecomposer(llm chat.Client, opts ...DecomposerOption) *Decomposer
NewDecomposer creates a new query decomposer with the given LLM client. The decomposer uses default settings unless modified by options.
Parameters:
- llm: LLM client for generating decompositions
- opts: Optional configuration functions
Returns:
- *Decomposer: Configured decomposer instance
Example:
decomposer := query.NewDecomposer(llm,
query.WithMaxSubQueries(3),
query.WithDecomposerLogger(logger),
)
func (*Decomposer) Decompose ¶
func (d *Decomposer) Decompose(ctx context.Context, query *core.Query) (*core.DecompositionResult, error)
Decompose breaks down a complex query into simpler sub-queries. It uses the configured LLM to analyze the query and generate sub-queries that can be answered independently.
Parameters:
- ctx: Context for cancellation and timeout
- query: The query to decompose
Returns:
- *core.DecompositionResult: Contains sub-queries, reasoning, and complexity flag
- error: Any error that occurred during decomposition
The result includes:
- SubQueries: List of simpler questions
- Reasoning: Explanation of the decomposition strategy
- IsComplex: Whether the original query was deemed complex
If decomposition fails or the query is simple, returns the original query as a single sub-query.
type DecomposerOption ¶
type DecomposerOption func(*Decomposer)
DecomposerOption configures a Decomposer instance.
func WithDecomposerCollector ¶
func WithDecomposerCollector(collector observability.Collector) DecomposerOption
WithDecomposerCollector sets an observability collector for metrics.
Parameters:
- collector: Metrics collector (if nil, no-op collector is used)
Returns:
- DecomposerOption: Configuration function
func WithDecomposerLogger ¶
func WithDecomposerLogger(logger logging.Logger) DecomposerOption
WithDecomposerLogger sets a structured logger for the decomposer.
Parameters:
- logger: Logger implementation (if nil, no-op logger is used)
Returns:
- DecomposerOption: Configuration function
func WithDecompositionPromptTemplate ¶
func WithDecompositionPromptTemplate(tmpl string) DecomposerOption
WithDecompositionPromptTemplate sets a custom prompt template for decomposition. The template should contain one %s placeholder for the query text.
Parameters:
- tmpl: Custom prompt template (must contain %s placeholder)
Returns:
- DecomposerOption: Configuration function
Example:
decomposer := query.NewDecomposer(llm,
query.WithDecompositionPromptTemplate("Custom prompt: %s"),
)
func WithMaxSubQueries ¶
func WithMaxSubQueries(max int) DecomposerOption
WithMaxSubQueries sets the maximum number of sub-queries to generate. Default is 5. If decomposition produces more, they are truncated.
Parameters:
- max: Maximum number of sub-queries (must be > 0)
Returns:
- DecomposerOption: Configuration function
type EntityExtractorOption ¶
type EntityExtractorOption func(*entityExtractor)
EntityExtractorOption configures an entityExtractor instance.
func WithEntityExtractionPromptTemplate ¶
func WithEntityExtractionPromptTemplate(tmpl string) EntityExtractorOption
WithEntityExtractionPromptTemplate overrides the default extraction prompt.
func WithEntityExtractorCollector ¶
func WithEntityExtractorCollector(collector observability.Collector) EntityExtractorOption
WithEntityExtractorCollector sets an observability collector.
func WithEntityExtractorLogger ¶
func WithEntityExtractorLogger(logger logging.Logger) EntityExtractorOption
WithEntityExtractorLogger sets a structured logger.
type HyDE ¶
type HyDE struct {
// contains filtered or unexported fields
}
HyDE generates hypothetical answers to improve search results.
type IntentRouterOption ¶
type IntentRouterOption func(*intentRouter)
IntentRouterOption configures an intentRouter instance.
func WithDefaultIntent ¶
func WithDefaultIntent(intent core.IntentType) IntentRouterOption
WithDefaultIntent sets the fallback intent when LLM confidence is low.
func WithIntentPromptTemplate ¶
func WithIntentPromptTemplate(tmpl string) IntentRouterOption
WithIntentPromptTemplate overrides the default intent classification prompt.
func WithIntentRouterCollector ¶
func WithIntentRouterCollector(collector observability.Collector) IntentRouterOption
WithIntentRouterCollector sets an observability collector.
func WithIntentRouterLogger ¶
func WithIntentRouterLogger(logger logging.Logger) IntentRouterOption
WithIntentRouterLogger sets a structured logger.
func WithMinConfidence ¶
func WithMinConfidence(v float32) IntentRouterOption
WithMinConfidence sets the minimum confidence threshold.
type KeywordExtractorOption ¶ added in v1.1.6
type KeywordExtractorOption func(*keywordExtractor)
KeywordExtractorOption configures a keywordExtractor instance.
func WithKeywordExtractorCollector ¶ added in v1.1.6
func WithKeywordExtractorCollector(collector observability.Collector) KeywordExtractorOption
WithKeywordExtractorCollector sets an observability collector.
func WithKeywordExtractorFilter ¶ added in v1.1.6
func WithKeywordExtractorFilter(filter func(word string) bool) KeywordExtractorOption
WithKeywordExtractorFilter sets a custom filter function for entity candidates.
func WithKeywordExtractorLogger ¶ added in v1.1.6
func WithKeywordExtractorLogger(logger logging.Logger) KeywordExtractorOption
WithKeywordExtractorLogger sets a structured logger.
func WithKeywordExtractorMaxEntities ¶ added in v1.1.6
func WithKeywordExtractorMaxEntities(max int) KeywordExtractorOption
WithKeywordExtractorMaxEntities sets maximum entities to return. Default is 10.
func WithKeywordExtractorMinWordLen ¶ added in v1.1.6
func WithKeywordExtractorMinWordLen(minLen int) KeywordExtractorOption
WithKeywordExtractorMinWordLen sets minimum word length to consider. Default is 2.
func WithKeywordExtractorStopWords ¶ added in v1.1.6
func WithKeywordExtractorStopWords(stopWords []string) KeywordExtractorOption
WithKeywordExtractorStopWords sets custom stop words to exclude.
type Rewriter ¶
type Rewriter struct {
// contains filtered or unexported fields
}
Rewriter uses an LLM to rewrite and improve user queries. Query rewriting enhances search quality by:
- Removing conversational filler words
- Resolving ambiguous pronouns and references
- Making queries more specific and searchable
- Expanding abbreviated terms
This is particularly useful for:
- Conversational queries: "What about their revenue?" → "What is [Company X]'s revenue?"
- Vague queries: "the thing" → specific entity name
- Informal language: "how do I fix it" → "How do I fix [specific error]"
Example:
llm := openai.NewClient(apiKey) rewriter := query.NewRewriter(llm) rewritten, err := rewriter.Rewrite(ctx, originalQuery) // rewritten.Text contains the improved query
func NewRewriter ¶
NewRewriter creates a new query rewriter with the given LLM client.
Parameters:
- llm: LLM client for query rewriting
Returns:
- *Rewriter: Configured rewriter instance
Example:
rewriter := query.NewRewriter(llm) rewritten, err := rewriter.Rewrite(ctx, query)
func (*Rewriter) Rewrite ¶
Rewrite rewrites the user's query to improve search quality. It uses an LLM to transform the query into a clearer, more specific form that is better suited for vector database search.
Parameters:
- ctx: Context for cancellation and timeout
- query: The original query to rewrite
Returns:
- *core.Query: New query object with rewritten text
- error: Any error that occurred during rewriting
The rewritten query:
- Has a new unique ID
- Contains improved, clearer text
- Is better suited for semantic search
If rewriting fails, returns the original query text in a new query object.
type StepBack ¶
type StepBack struct {
// contains filtered or unexported fields
}
StepBack abstracts queries into higher-level background questions.
func NewStepBack ¶
NewStepBack creates a new StepBack generator.
type VectorExtractorOption ¶ added in v1.1.6
type VectorExtractorOption func(*vectorExtractor)
VectorExtractorOption configures a vectorExtractor instance.
func WithVectorExtractorCollector ¶ added in v1.1.6
func WithVectorExtractorCollector(collector observability.Collector) VectorExtractorOption
WithVectorExtractorCollector sets an observability collector.
func WithVectorExtractorLogger ¶ added in v1.1.6
func WithVectorExtractorLogger(logger logging.Logger) VectorExtractorOption
WithVectorExtractorLogger sets a structured logger.
func WithVectorExtractorMinLen ¶ added in v1.1.6
func WithVectorExtractorMinLen(minLen int) VectorExtractorOption
WithVectorExtractorMinLen sets minimum entity length. Default is 2.
func WithVectorExtractorThreshold ¶ added in v1.1.6
func WithVectorExtractorThreshold(threshold float64) VectorExtractorOption
WithVectorExtractorThreshold sets the similarity threshold for entity matching. Default is 0.7. Lower values return more entities but with lower precision.
func WithVectorExtractorTopK ¶ added in v1.1.6
func WithVectorExtractorTopK(topK int) VectorExtractorOption
WithVectorExtractorTopK sets the maximum number of entities to return. Default is 5.