Documentation
¶
Overview ¶
Package gorag provides a high-level API for building Retrieval-Augmented Generation (RAG) applications.
This package offers pre-configured RAG implementations with support for multiple retrieval strategies including Native, Advanced, Agentic, and Graph-based RAG patterns. It leverages dependency injection for flexible component customization and supports various vector stores, document stores, and LLM providers.
Quick Start:
rag, err := gorag.DefaultNativeRAG(
gorag.WithWorkDir("./data"),
gorag.WithTopK(5),
)
if err != nil {
log.Fatal(err)
}
defer rag.Close()
// Index documents
err = rag.IndexFile(ctx, "path/to/document.pdf")
// Search
result, err := rag.Search(ctx, "your query", 5)
Index ¶
- func CheckEnvironment(ctx context.Context) (bool, []string)
- func PrepareEnvironment(ctx context.Context, ...) error
- type RAG
- func DefaultAdvancedRAG(opts ...RAGOption) (RAG, error)
- func DefaultAgenticRAG(opts ...RAGOption) (RAG, error)
- func DefaultCRAG(opts ...RAGOption) (RAG, error)
- func DefaultGraphRAG(opts ...RAGOption) (RAG, error)
- func DefaultNativeRAG(opts ...RAGOption) (RAG, error)
- func DefaultSelfRAG(opts ...RAGOption) (RAG, error)
- type RAGConfig
- type RAGOption
- func WithAPIKeyEnv(name string) RAGOption
- func WithBaseURL(url string) RAGOption
- func WithContainer(ctr *di.Container) RAGOption
- func WithDepth(d int) RAGOption
- func WithDimension(dim int) RAGOption
- func WithEmbedder(e embedding.Provider) RAGOption
- func WithLLM(l gochat.Client) RAGOption
- func WithLimit(l int) RAGOption
- func WithMaxRetries(r int) RAGOption
- func WithMetrics(m core.Metrics) RAGOption
- func WithModelName(name string) RAGOption
- func WithModelPath(path string) RAGOption
- func WithName(name string) RAGOption
- func WithParsers(p ...core.Parser) RAGOption
- func WithSemanticCache(enable bool, cacheType ...string) RAGOption
- func WithThreshold(t float32) RAGOption
- func WithTopK(k int) RAGOption
- func WithTracer(t observability.Tracer) RAGOption
- func WithWebSearcher(s core.WebSearcher) RAGOption
- func WithWorkDir(path string) RAGOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckEnvironment ¶ added in v1.1.4
CheckEnvironment verifies if the GoRAG environment is ready for operation.
Types ¶
type RAG ¶
type RAG interface {
// IndexFile processes a single file and adds it to the knowledge base.
IndexFile(ctx context.Context, filePath string) error
// IndexDirectory processes all files in a directory and adds them to the knowledge base.
// If recursive is true, it will also process subdirectories.
IndexDirectory(ctx context.Context, dirPath string, recursive bool) error
// Search performs a retrieval query and returns the top K results.
Search(ctx context.Context, query string, topK int) (*core.RetrievalResult, error)
// Container returns the underlying dependency injection container for advanced usage.
Container() *di.Container
// Close releases all resources held by the RAG instance.
Close() error
}
RAG application interface defines the main entry point for RAG operations.
func DefaultAdvancedRAG ¶
DefaultAdvancedRAG creates a high-performance RAG instance optimized for production use. It supports advanced features like query rewriting, fusion, and reranking.
func DefaultAgenticRAG ¶
DefaultAgenticRAG creates an agentic RAG instance with smart routing capabilities. It automatically selects the best retrieval strategy based on query intent classification.
func DefaultCRAG ¶ added in v1.1.4
DefaultCRAG creates a corrective RAG instance with external search fallback. It automatically triggers web search when internal knowledge is insufficient or incorrect.
func DefaultGraphRAG ¶
DefaultGraphRAG creates a knowledge graph-enhanced RAG instance. It leverages entity relationships and structured knowledge for complex queries.
func DefaultNativeRAG ¶
DefaultNativeRAG creates a lightweight, local-first RAG instance. It uses default TokenChunker, local SQLite and GoVector stores, suitable for quick prototyping.
Example:
rag, err := gorag.DefaultNativeRAG(
gorag.WithWorkDir("./data"),
gorag.WithTopK(5),
)
func DefaultSelfRAG ¶ added in v1.1.4
DefaultSelfRAG creates a self-correcting RAG instance with reflection capabilities. It evaluates retrieval relevance and generation quality for high-precision answers.
type RAGConfig ¶
type RAGConfig struct {
// 0. Identity
// Name specifies a unique name for the RAG instance, used for resource isolation.
Name string
// 1. Persistence
// WorkDir specifies the working directory for storing persistent data (vector DB, document DB, etc.).
WorkDir string
// VectorDBType specifies the type of vector store to use (e.g., "govector", "milvus", "pinecone").
VectorDBType string // "govector", "milvus"
// Dimension specifies the dimension of embedding vectors.
Dimension int
// 2. Model "Grounding" (Variable Name Driven)
// EmbedderType specifies the embedding model provider (e.g., "openai", "ollama", "local-onnx").
EmbedderType string // "openai", "ollama", "local-onnx"
// LLMType specifies the large language model provider (e.g., "openai", "claude", "ollama").
LLMType string // "openai", "claude", "ollama"
// ModelPath specifies the local model file path (typically under .test/models/...).
ModelPath string // Local model file path (.test/models/...)
// ModelName specifies the model name identifier (e.g., "qwen-turbo", "bge-small-zh-v1.5").
ModelName string // e.g., "qwen-turbo", "bge-small-zh-v1.5"
// APIKeyEnv specifies the environment variable name for the API key (e.g., "DASHSCOPE_API_KEY").
APIKeyEnv string // Name of the env var, e.g., "DASHSCOPE_API_KEY"
// BaseURL specifies the base URL for API-compatible model providers.
BaseURL string // e.g., "https://dashscope.aliyuncs.com/compatible-mode/"
// 3. Retrieval Tuning
// TopK specifies the default number of top results to retrieve during search.
TopK int
// 4. Semantic Cache
// EnableSemanticCache enables semantic caching for improved performance.
EnableSemanticCache bool
// SemanticCacheType specifies the cache backend type: "memory" or "bolt".
SemanticCacheType string
// 5. Advanced Tuning (Self-RAG & CRAG)
// SelfRAGThreshold specifies the quality threshold for Self-RAG reflection.
SelfRAGThreshold float32
// MaxRetries specifies the maximum number of refinement attempts in Self-RAG.
MaxRetries int
// 6. Graph RAG Tuning
// GraphDepth specifies the search depth in the knowledge graph.
GraphDepth int
// GraphLimit specifies the maximum number of related nodes to retrieve per entity.
GraphLimit int
// contains filtered or unexported fields
}
RAGConfig is the single source of truth for all RAG modes. It holds configuration parameters for RAG system initialization.
type RAGOption ¶
type RAGOption func(*RAGConfig)
RAGOption is a function type for configuring RAG instances using the functional options pattern.
func WithAPIKeyEnv ¶
WithAPIKeyEnv sets the environment variable name for API key.
func WithBaseURL ¶
WithBaseURL sets the base URL for API-compatible providers.
func WithContainer ¶
WithContainer injects a custom dependency injection container.
func WithDimension ¶
WithDimension sets the embedding vector dimension.
func WithEmbedder ¶
Expert injection WithEmbedder injects a custom embedding provider.
func WithMaxRetries ¶ added in v1.1.4
WithMaxRetries sets the maximum refinement attempts for Self-RAG.
func WithMetrics ¶ added in v1.1.4
WithMetrics injects a custom metrics collector.
func WithModelName ¶
WithModelName sets the model name identifier.
func WithModelPath ¶
WithModelPath sets the local model file path.
func WithName ¶ added in v1.1.4
WithName sets a unique name for the RAG instance for resource isolation.
func WithParsers ¶
WithParsers injects custom document parsers.
func WithSemanticCache ¶ added in v1.1.4
WithSemanticCache enables semantic caching with specified backend type. cacheType can be "memory" (default, in-memory) or "bolt" (persistent).
func WithThreshold ¶ added in v1.1.4
WithThreshold sets the quality threshold for Self-RAG reflection.
func WithTracer ¶ added in v1.1.4
func WithTracer(t observability.Tracer) RAGOption
WithTracer injects a custom distributed tracer.
func WithWebSearcher ¶ added in v1.1.4
func WithWebSearcher(s core.WebSearcher) RAGOption
WithWebSearcher sets the external search engine for CRAG.
func WithWorkDir ¶
WithWorkDir sets the working directory for persistent storage.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gorag
command
|
|
|
pkg
|
|
|
core
Package core defines the fundamental entities, interfaces, and types for the goRAG framework.
|
Package core defines the fundamental entities, interfaces, and types for the goRAG framework. |
|
di
Package di provides a lightweight dependency injection container for managing component lifecycle.
|
Package di provides a lightweight dependency injection container for managing component lifecycle. |
|
indexer
Package indexer provides high-level indexers for building RAG pipelines.
|
Package indexer provides high-level indexers for building RAG pipelines. |
|
indexing
Package indexing provides the core indexing pipeline for offline data preparation.
|
Package indexing provides the core indexing pipeline for offline data preparation. |
|
retrieval/answer
Package answer provides answer generation utilities for RAG systems.
|
Package answer provides answer generation utilities for RAG systems. |
|
retrieval/enhancement
Package enhancement provides query and document enhancement utilities for RAG systems.
|
Package enhancement provides query and document enhancement utilities for RAG systems. |
|
retrieval/graph
Package graph provides graph-related utilities for RAG systems.
|
Package graph provides graph-related utilities for RAG systems. |
|
steps/crag
Package crag provides evaluation steps for RAG retrieval quality assessment.
|
Package crag provides evaluation steps for RAG retrieval quality assessment. |
|
steps/decompose
Package decompose provides query decomposition steps for RAG retrieval pipelines.
|
Package decompose provides query decomposition steps for RAG retrieval pipelines. |
|
steps/filter
Package filter provides query preprocessing steps for RAG pipelines.
|
Package filter provides query preprocessing steps for RAG pipelines. |
|
steps/fuse
Package fuse provides result fusion steps for RAG retrieval pipelines.
|
Package fuse provides result fusion steps for RAG retrieval pipelines. |
|
steps/generate
Package generate provides answer generation steps for RAG pipelines.
|
Package generate provides answer generation steps for RAG pipelines. |
|
steps/image
Package image provides image retrieval steps for multimodal RAG pipelines.
|
Package image provides image retrieval steps for multimodal RAG pipelines. |
|
steps/indexing
Package indexing provides document indexing pipeline steps for RAG data preparation.
|
Package indexing provides document indexing pipeline steps for RAG data preparation. |
|
steps/rerank
Package rerank provides reranking steps for RAG retrieval pipelines.
|
Package rerank provides reranking steps for RAG retrieval pipelines. |
|
steps/rewrite
Package rewrite provides query rewriting steps for RAG retrieval pipelines.
|
Package rewrite provides query rewriting steps for RAG retrieval pipelines. |
|
steps/sparse
Package sparse provides sparse retrieval steps using BM25 algorithm.
|
Package sparse provides sparse retrieval steps using BM25 algorithm. |
|
steps/stepback
Package stepback provides query abstraction steps for RAG pipelines.
|
Package stepback provides query abstraction steps for RAG pipelines. |