pattern

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ExtractionStrategyLLM uses LLM for entity extraction
	ExtractionStrategyLLM = graphretriever.ExtractionStrategyLLM
	// ExtractionStrategyVector uses vector similarity matching
	ExtractionStrategyVector = graphretriever.ExtractionStrategyVector
	// ExtractionStrategyKeyword uses keyword heuristics
	ExtractionStrategyKeyword = graphretriever.ExtractionStrategyKeyword
	// ExtractionStrategyAuto automatically selects the best available strategy
	ExtractionStrategyAuto = graphretriever.ExtractionStrategyAuto
)

ExtractionStrategy constants for convenience

Variables

This section is empty.

Functions

func WithBERT

func WithBERT(modelName string) bertOpt

WithBERT creates a Sentence-BERT embedding provider with auto-download. If the model doesn't exist locally, it will be downloaded automatically. Applicable to: NativeRAG, GraphRAG

Available models:

  • "all-MiniLM-L6-v2" - English Sentence-BERT small model (~45MB)
  • "all-mpnet-base-v2" - English MPNet base model (~218MB)
  • "bert-base-uncased" - English BERT base model (~200MB)

func WithBGE

func WithBGE(modelName string) bgeOpt

WithBGE creates a BGE embedding provider with auto-download. If the model doesn't exist locally, it will be downloaded automatically. Applicable to: NativeRAG, GraphRAG

Available models:

  • "bge-small-zh-v1.5" - Chinese BGE small model (~48MB)
  • "bge-base-zh-v1.5" - Chinese BGE base model (~100MB)

func WithBoltDoc

func WithBoltDoc(path string) boltDocOpt

WithBoltDoc configures BoltDB as the document store. Applicable to: NativeRAG, GraphRAG

func WithCLIP

func WithCLIP(modelName string) clipOpt

WithCLIP creates a CLIP multimodal embedding provider with auto-download. If the model doesn't exist locally, it will be downloaded automatically. Applicable to: NativeRAG, GraphRAG

Available models:

  • "clip-vit-base-patch32" - OpenAI CLIP base model (~300MB)

func WithCharacterChunker

func WithCharacterChunker(size, overlap int) charChunkerOpt

WithCharacterChunker configures a character-based text chunker. Applicable to: NativeRAG, GraphRAG

func WithConsoleLogger

func WithConsoleLogger() consoleLoggerOpt

WithConsoleLogger enables console logging.

func WithEmbedding

func WithEmbedding(provider embedding.Provider) embedderOpt

WithEmbedding sets a custom embedding provider. Applicable to: NativeRAG, GraphRAG

func WithGoVector

func WithGoVector(collection string, path string, dimension int) goVectorOpt

WithGoVector configures local in-memory/file GoVector storage. Applicable to: NativeRAG, GraphRAG

func WithLLM

func WithLLM(client chat.Client) llmOpt

WithLLM sets the LLM client for retrieval enhancement strategies. Required for: QueryRewrite, StepBack, HyDE, Fusion strategies in NativeRAG Required for: GraphRAG with LLM-based entity extraction

func WithMilvus

func WithMilvus(collection string, addr string, dimension int) milvusOpt

WithMilvus configures remote Milvus vector storage. Applicable to: NativeRAG, GraphRAG

func WithName

func WithName(name string) nameOpt

WithName sets the name of the RAG pattern instance. Applicable to: NativeRAG, GraphRAG

func WithOpenAI

func WithOpenAI(apiKey string, model string) openAIOpt

WithOpenAI sets the OpenAI embedding model. Applicable to: NativeRAG, GraphRAG

func WithSQLiteDoc

func WithSQLiteDoc(path string) sqliteDocOpt

WithSQLiteDoc configures SQLite as the document store. Applicable to: NativeRAG, GraphRAG

func WithTokenChunker

func WithTokenChunker(size, overlap int, model string) tokenChunkerOpt

WithTokenChunker configures a token-based chunker. Applicable to: NativeRAG, GraphRAG

func WithTopK

func WithTopK(k int) topKOpt

WithTopK sets the number of top semantic results to retrieve. Applicable to: NativeRAG, GraphRAG

Types

type GraphOption

type GraphOption interface {
	// contains filtered or unexported methods
}

GraphOption configures a GraphRAG instance.

func WithExtractionStrategy

func WithExtractionStrategy(strategy graphretriever.ExtractionStrategy) GraphOption

WithExtractionStrategy sets the entity extraction strategy for GraphRAG.

Available strategies:

  • "llm": Use LLM for entity extraction (most accurate, requires LLM)
  • "vector": Use vector similarity matching (requires embedder)
  • "keyword": Use keyword heuristics (no dependencies, fastest)
  • "auto": Automatically select best available strategy (default)

Applicable exclusively to: GraphRAG

func WithNeoGraph

func WithNeoGraph(uri, username, password, dbName string) GraphOption

WithNeoGraph configures Neo4j graph database for GraphRAG. Applicable exclusively to: GraphRAG

type GraphRAGPattern

type GraphRAGPattern interface {
	RAGPattern

	// GraphRepository returns the graph data access interface with automatic index synchronization.
	// Use GraphRepository for Node/Edge CRUD operations that require storage-index consistency.
	GraphRepository() core.GraphRepository

	// AddNode adds a single node to the knowledge graph.
	AddNode(ctx context.Context, node *core.Node) error
	// AddNodes adds multiple nodes to the knowledge graph.
	AddNodes(ctx context.Context, nodes []*core.Node) error
	// GetNode retrieves a node by its ID.
	GetNode(ctx context.Context, nodeID string) (*core.Node, error)
	// DeleteNode removes a node and its associated edges from the graph.
	DeleteNode(ctx context.Context, nodeID string) error

	// AddEdge adds a single edge (relationship) to the knowledge graph.
	AddEdge(ctx context.Context, edge *core.Edge) error
	// AddEdges adds multiple edges to the knowledge graph.
	AddEdges(ctx context.Context, edges []*core.Edge) error
	// DeleteEdge removes an edge from the graph.
	DeleteEdge(ctx context.Context, edgeID string) error

	// QueryGraph executes a Cypher/GQL query against the knowledge graph.
	QueryGraph(ctx context.Context, query string, params map[string]any) ([]map[string]any, error)
	// GetNeighbors retrieves neighboring nodes and edges starting from a node.
	GetNeighbors(ctx context.Context, nodeID string, depth int, limit int) ([]*core.Node, []*core.Edge, error)
}

GraphRAGPattern extends RAGPattern with graph-specific operations.

func GraphRAG

func GraphRAG(name string, opts ...GraphOption) (GraphRAGPattern, error)

GraphRAG creates a Knowledge-Graph enabled RAG pattern. Zero configuration: just provide a name and embedding model.

Example:

rag, err := pattern.GraphRAG("myapp", pattern.WithBGE("bge-small-zh-v1.5"))

Automatically configured:

  • BoltDB document store: ~/.gorag/{name}/docs.bolt
  • GoVector vector store: ~/.gorag/{name}/vectors.db
  • In-memory knowledge graph (GoGraph)
  • Default embedder: bge-small-zh-v1.5 (if not specified)

For Neo4j integration, use WithNeoGraph option.

type NativeOption

type NativeOption interface {
	// contains filtered or unexported methods
}

NativeOption configures a NativeRAG instance.

func WithCache

func WithCache() NativeOption

WithCache enables semantic caching. Caches query results to avoid redundant LLM calls and vector searches. Applicable to: NativeRAG

func WithContextPrune

func WithContextPrune() NativeOption

WithContextPrune enables context pruning. Removes irrelevant chunks to improve generation quality. Applicable to: NativeRAG

func WithFusion

func WithFusion(count int) NativeOption

WithFusion enables multi-query fusion retrieval. Decomposes complex queries into sub-queries and fuses results using RRF. Requires: WithLLM() to be set count: number of sub-queries to generate (default: 5) Applicable to: NativeRAG

func WithHyDE

func WithHyDE() NativeOption

WithHyDE enables hypothetical document embedding. Generates hypothetical documents to improve retrieval for ambiguous queries. Requires: WithLLM() to be set Applicable to: NativeRAG

func WithParentDoc

func WithParentDoc() NativeOption

WithParentDoc enables parent document expansion. Retrieves the parent document for each chunk, providing broader context. Applicable to: NativeRAG

func WithQueryRewrite

func WithQueryRewrite() NativeOption

WithQueryRewrite enables query rewriting for ambiguous queries. Uses LLM to clarify and rephrase queries before retrieval. Requires: WithLLM() to be set Applicable to: NativeRAG

func WithRerank

func WithRerank() NativeOption

WithRerank enables result reranking. Re-scores and reorders retrieved results for better relevance. Applicable to: NativeRAG

func WithSentenceWindow

func WithSentenceWindow() NativeOption

WithSentenceWindow enables sentence window expansion. Expands each chunk with surrounding sentences for better context. Applicable to: NativeRAG

func WithStepBack

func WithStepBack() NativeOption

WithStepBack enables step-back questioning for complex queries. Generates abstracted high-level questions to retrieve broader context. Requires: WithLLM() to be set Applicable to: NativeRAG

type RAGPattern

type RAGPattern interface {
	// Indexer returns the underlying indexer for document processing.
	Indexer() indexer.Indexer
	// Retriever returns the underlying retriever for search operations.
	Retriever() core.Retriever
	// Repository returns the entity data access interface with automatic index synchronization.
	// Use Repository to store any entity type in different collections with auto-vectorization.
	Repository() core.Repository

	// IndexFile is a convenience method for indexing a single file.
	IndexFile(ctx context.Context, filePath string) error
	// IndexDirectory is a convenience method for indexing an entire directory.
	IndexDirectory(ctx context.Context, dirPath string, recursive bool) error
	// Retrieve performs parallel retrieval for multiple queries.
	Retrieve(ctx context.Context, queries []string, topK int) ([]*core.RetrievalResult, error)

	// IndexText indexes plain text content directly (no file parsing required).
	IndexText(ctx context.Context, text string, metadata ...map[string]any) error
	// IndexTexts indexes multiple plain text contents in batch.
	IndexTexts(ctx context.Context, texts []string, metadata ...map[string]any) error
	// Delete removes indexed content by ID (cascades to chunks and vectors).
	Delete(ctx context.Context, id string) error
}

RAGPattern defines a high-level application interface that combines indexing and retrieval. It simplifies the usage of complex RAG pipelines for common application scenarios.

RAGPattern exposes three independent interfaces, each with distinct responsibilities:

Indexer()    - Index construction and management
Retriever()  - Semantic search and retrieval
Repository() - Entity data access with index synchronization (CRUD + index sync)

This separation ensures:

  • Clear separation of concerns
  • Data consistency between storage and indexes
  • Flexibility for developers to choose the appropriate interface

func NativeRAG

func NativeRAG(name string, opts ...NativeOption) (RAGPattern, error)

NativeRAG creates a configurable Vector-based RAG pattern. NativeRAG supports various enhancement strategies through options:

**Query Enhancement (Pre-retrieval):**

  • WithQueryRewrite() - Clarify ambiguous queries
  • WithStepBack() - Generate high-level questions
  • WithHyDE() - Generate hypothetical documents

**Retrieval Enhancement:**

  • WithFusion(n) - Multi-query fusion with RRF

Example - Simple RAG:

rag, err := pattern.NativeRAG("myapp", pattern.WithBGE("bge-small-zh-v1.5"))

Example - With Query Enhancement:

rag, err := pattern.NativeRAG("myapp",
    pattern.WithBGE("bge-small-zh-v1.5"),
    pattern.WithLLM(myLLMClient),
    pattern.WithQueryRewrite(),
)

Example - Full Advanced RAG:

rag, err := pattern.NativeRAG("myapp",
    pattern.WithBGE("bge-small-zh-v1.5"),
    pattern.WithLLM(myLLMClient),
    pattern.WithQueryRewrite(),  // Query enhancement
    pattern.WithFusion(5),        // Retrieval enhancement
)

The following components are automatically configured:

  • BoltDB document store: ~/.gorag/{name}/docs.bolt
  • GoVector vector store: ~/.gorag/{name}/vectors.db
  • Default chunker: character-based, 512 chars with 50 overlap
  • Default embedder: bge-small-zh-v1.5 (if not specified)

type SyncEdge

type SyncEdge struct {
	*core.Edge
	// DocumentID links this edge to a source document for cascade delete.
	// If empty, edge.Properties["document_id"] is used.
	DocumentID string
}

SyncEdge wraps an Edge with synchronization options.

type SyncMode

type SyncMode int

SyncMode controls which stores are synchronized during graph operations.

const (
	// SyncGraphOnly only writes to GraphStore (default for backward compatibility)
	SyncGraphOnly SyncMode = iota
	// SyncGraphWithVector writes to GraphStore and generates vector embedding for nodes
	SyncGraphWithVector
	// SyncFull writes to GraphStore, VectorStore, and DocStore (for complete traceability)
	SyncFull
)

type SyncNode

type SyncNode struct {
	*core.Node
	// Content is the text content to embed for semantic search.
	// If empty, node.Properties["content"] or node.Properties["description"] is used.
	Content string
	// SyncMode controls which stores to sync. Default is SyncGraphOnly.
	SyncMode SyncMode
	// DocumentID links this node to a source document for cascade delete.
	// If empty, node.Properties["document_id"] is used.
	DocumentID string
}

SyncNode wraps a Node with synchronization options.

Jump to

Keyboard shortcuts

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