Documentation
¶
Overview ¶
RAG (Retrieval-Augmented Generation) Package
The rag package provides comprehensive RAG (Retrieval-Augmented Generation) capabilities for the LangGraph Go framework. It integrates various RAG approaches including traditional vector-based retrieval and advanced GraphRAG techniques.
Features ¶
- Vector-based RAG: Traditional retrieval using vector similarity
- GraphRAG: Knowledge graph-based retrieval for enhanced context understanding
- Multiple Embedding Models: Support for OpenAI, local models, and more
- Flexible Document Processing: Various document loaders and splitters
- Hybrid Search: Combine vector and graph-based retrieval
- Integration Ready: Seamless integration with LangGraph agents
Quick Start ¶
Basic vector RAG:
import (
"context"
"github.com/smallnest/langgraphgo/rag/engine"
"github.com/tmc/langchaingo/embeddings/openai"
"github.com/tmc/langchaingo/vectorstores/pgvector"
)
func main() {
llm := initLLM()
embedder, _ := openai.NewEmbedder()
store, _ := pgvector.New(ctx, pgvector.WithEmbedder(embedder))
ragEngine, _ := engine.NewVectorRAGEngine(llm, embedder, store, 5)
result, err := ragEngine.Query(ctx, "What is quantum computing?")
}
GraphRAG integration:
import (
"context"
"github.com/smallnest/langgraphgo/rag/engine"
)
func main() {
graphRAG, _ := engine.NewGraphRAGEngine(engine.GraphRAGConfig{
DatabaseURL: "redis://localhost:6379",
ModelProvider: "openai",
EmbeddingModel: "text-embedding-3-small",
}, llm, embedder, kg)
// Extract and store knowledge graph
err := graphRAG.AddDocuments(ctx, documents)
// Query using graph-enhanced retrieval
response, err := graphRAG.Query(ctx, "Who directed the Matrix?")
}
Architecture ¶
The rag package consists of several key components:
Core Components ¶
rag/engine.go Main RAG engine interfaces and base implementations
type Engine interface {
Query(ctx context.Context, query string) (*QueryResult, error)
AddDocuments(ctx context.Context, docs []Document) error
SimilaritySearch(ctx context.Context, query string, k int) ([]Document, error)
}
rag/engine/vector.go Traditional vector-based RAG implementation
vectorEngine, _ := engine.NewVectorRAGEngine(llm, embedder, vectorStore, k)
rag/engine/graph.go GraphRAG implementation with knowledge graph extraction
graphEngine, _ := engine.NewGraphRAGEngine(config, llm, embedder, kg)
Document Processing ¶
rag/types.go Core document and entity types
type Document struct {
ID string
Content string
Metadata map[string]any
}
rag/loader/ Various document loaders (text, static, etc.)
loader := loader.NewTextLoader("document.txt")
docs, err := loader.Load(ctx)
rag/splitter/ Text splitting strategies
splitter := splitter.NewRecursiveCharacterTextSplitter( splitter.WithChunkSize(1000), splitter.WithChunkOverlap(200), )
Retrieval Strategies ¶
rag/retriever/ Various retrieval implementations
vectorRetriever := retriever.NewVectorRetriever(vectorStore, embedder, 5)
graphRetriever := retriever.NewGraphRetriever(knowledgeGraph, 5)
hybridRetriever := retriever.NewHybridRetriever([]Retriever{r1, r2}, weights, config)
Integration with LangGraph ¶
The rag package integrates seamlessly with LangGraph agents:
// Create a RAG pipeline
pipeline := rag.NewRAGPipeline(config)
runnable, _ := pipeline.Compile()
result, _ := runnable.Invoke(ctx, rag.RAGState{Query: "..."})
Configuration ¶
The rag package supports various configuration options:
type Config struct {
VectorRAG *VectorRAGConfig `json:"vector_rag,omitempty"`
GraphRAG *GraphRAGConfig `json:"graph_rag,omitempty"`
}
Supported Data Sources ¶
- Local files (TXT, MD, etc.)
- Static documents
- Web pages and websites (via adapters)
Supported Vector Stores ¶
- pgvector (via adapter)
- Redis (via adapter)
- chromem-go (native Go implementation)
- Chroma v2 API (native Go implementation)
- Mock/In-memory store for testing
GraphRAG Features ¶
- Automatic entity extraction
- Relationship detection
- Multi-hop reasoning
- Context-aware retrieval
Index ¶
- func NewRetrievalNode(engine Engine, inputKey, outputKey string) func(context.Context, any) (any, error)
- func WeightedAggregator(weights []float64) func([]*QueryResult) *QueryResult
- type BaseEngine
- func (e *BaseEngine) AddDocuments(ctx context.Context, docs []Document) error
- func (e *BaseEngine) DeleteDocument(ctx context.Context, docID string) error
- func (e *BaseEngine) GetMetrics() *Metrics
- func (e *BaseEngine) Query(ctx context.Context, query string) (*QueryResult, error)
- func (e *BaseEngine) QueryWithConfig(ctx context.Context, query string, config *RetrievalConfig) (*QueryResult, error)
- func (e *BaseEngine) ResetMetrics()
- func (e *BaseEngine) SimilaritySearch(ctx context.Context, query string, k int) ([]Document, error)
- func (e *BaseEngine) SimilaritySearchWithScores(ctx context.Context, query string, k int) ([]DocumentSearchResult, error)
- func (e *BaseEngine) UpdateDocument(ctx context.Context, doc Document) error
- type Community
- type CompositeEngine
- func (c *CompositeEngine) AddDocuments(ctx context.Context, docs []Document) error
- func (c *CompositeEngine) DeleteDocument(ctx context.Context, docID string) error
- func (c *CompositeEngine) Query(ctx context.Context, query string) (*QueryResult, error)
- func (c *CompositeEngine) QueryWithConfig(ctx context.Context, query string, config *RetrievalConfig) (*QueryResult, error)
- func (c *CompositeEngine) SimilaritySearch(ctx context.Context, query string, k int) ([]Document, error)
- func (c *CompositeEngine) SimilaritySearchWithScores(ctx context.Context, query string, k int) ([]DocumentSearchResult, error)
- func (c *CompositeEngine) UpdateDocument(ctx context.Context, doc Document) error
- type Config
- type Document
- type DocumentLoader
- type DocumentSearchResult
- type Embedder
- type Engine
- type Entity
- type GlobalRetrievalConfig
- type GraphQuery
- type GraphQueryResult
- type GraphRAGConfig
- type HybridRetrievalConfig
- type KnowledgeGraph
- type LLMInterface
- type LangChainDocumentLoader
- func (l *LangChainDocumentLoader) Load(ctx context.Context) ([]Document, error)
- func (l *LangChainDocumentLoader) LoadAndSplit(ctx context.Context, splitter textsplitter.TextSplitter) ([]Document, error)
- func (l *LangChainDocumentLoader) LoadWithMetadata(ctx context.Context, metadata map[string]any) ([]Document, error)
- type LangChainEmbedder
- type LangChainRetriever
- func (r *LangChainRetriever) Retrieve(ctx context.Context, query string) ([]Document, error)
- func (r *LangChainRetriever) RetrieveWithConfig(ctx context.Context, query string, config *RetrievalConfig) ([]DocumentSearchResult, error)
- func (r *LangChainRetriever) RetrieveWithK(ctx context.Context, query string, k int) ([]Document, error)
- type LangChainTextSplitter
- type LangChainVectorStore
- func (l *LangChainVectorStore) Add(ctx context.Context, docs []Document) error
- func (l *LangChainVectorStore) Delete(ctx context.Context, ids []string) error
- func (l *LangChainVectorStore) GetStats(ctx context.Context) (*VectorStoreStats, error)
- func (l *LangChainVectorStore) Search(ctx context.Context, query []float32, k int) ([]DocumentSearchResult, error)
- func (l *LangChainVectorStore) SearchWithFilter(ctx context.Context, query []float32, k int, filter map[string]any) ([]DocumentSearchResult, error)
- func (l *LangChainVectorStore) Update(ctx context.Context, docs []Document) error
- type LightRAGConfig
- type LocalRetrievalConfig
- type Metrics
- type PipelineConfig
- type QueryResult
- type RAGConfig
- type RAGDocument
- type RAGDocumentLoader
- type RAGPipeline
- type RAGState
- type RAGTextSplitter
- type Relationship
- type Reranker
- type RetrievalConfig
- type Retriever
- type RetrieverTool
- type TextSplitter
- type VectorRAGConfig
- type VectorStore
- type VectorStoreStats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewRetrievalNode ¶ added in v0.7.1
func NewRetrievalNode(engine Engine, inputKey, outputKey string) func(context.Context, any) (any, error)
NewRetrievalNode creates a LangGraph node function that retrieves documents using the RAG engine. It expects the input state to be a map[string]any.
Parameters:
- engine: The RAG engine to use for retrieval.
- inputKey: The key in the state map where the query string is stored.
- outputKey: The key in the returned map where the retrieved context (string) will be stored.
Usage:
graph.AddNode("retrieve", rag.NewRetrievalNode(myEngine, "question", "context"))
func WeightedAggregator ¶
func WeightedAggregator(weights []float64) func([]*QueryResult) *QueryResult
WeightedAggregator provides weighted result aggregation logic
Types ¶
type BaseEngine ¶
type BaseEngine struct {
// contains filtered or unexported fields
}
BaseEngine provides common functionality for RAG engines
func NewBaseEngine ¶
func NewBaseEngine(retriever Retriever, embedder Embedder, config *Config) *BaseEngine
NewBaseEngine creates a new base RAG engine
func (*BaseEngine) AddDocuments ¶
func (e *BaseEngine) AddDocuments(ctx context.Context, docs []Document) error
AddDocuments adds documents to the base engine
func (*BaseEngine) DeleteDocument ¶
func (e *BaseEngine) DeleteDocument(ctx context.Context, docID string) error
DeleteDocument removes a document from the base engine
func (*BaseEngine) GetMetrics ¶
func (e *BaseEngine) GetMetrics() *Metrics
GetMetrics returns the current metrics
func (*BaseEngine) Query ¶
func (e *BaseEngine) Query(ctx context.Context, query string) (*QueryResult, error)
Query performs a RAG query using the base engine
func (*BaseEngine) QueryWithConfig ¶
func (e *BaseEngine) QueryWithConfig(ctx context.Context, query string, config *RetrievalConfig) (*QueryResult, error)
QueryWithConfig performs a RAG query with custom configuration
func (*BaseEngine) ResetMetrics ¶
func (e *BaseEngine) ResetMetrics()
ResetMetrics resets all metrics
func (*BaseEngine) SimilaritySearch ¶
SimilaritySearch performs similarity search without generation
func (*BaseEngine) SimilaritySearchWithScores ¶
func (e *BaseEngine) SimilaritySearchWithScores(ctx context.Context, query string, k int) ([]DocumentSearchResult, error)
SimilaritySearchWithScores performs similarity search with scores
func (*BaseEngine) UpdateDocument ¶
func (e *BaseEngine) UpdateDocument(ctx context.Context, doc Document) error
UpdateDocument updates an existing document in the base engine
type Community ¶ added in v0.8.2
type Community struct {
ID string `json:"id"`
Level int `json:"level"`
Title string `json:"title"`
Summary string `json:"summary"`
Entities []string `json:"entities"`
ParentID string `json:"parent_id,omitempty"`
Children []string `json:"children,omitempty"`
Properties map[string]any `json:"properties,omitempty"`
Score float64 `json:"score,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
Community represents a community of entities in the knowledge graph
type CompositeEngine ¶
type CompositeEngine struct {
// contains filtered or unexported fields
}
CompositeEngine combines multiple RAG engines
func NewCompositeEngine ¶
func NewCompositeEngine(engines []Engine, aggregator func([]*QueryResult) *QueryResult) *CompositeEngine
NewCompositeEngine creates a new composite RAG engine
func (*CompositeEngine) AddDocuments ¶
func (c *CompositeEngine) AddDocuments(ctx context.Context, docs []Document) error
AddDocuments adds documents to all composite engines
func (*CompositeEngine) DeleteDocument ¶
func (c *CompositeEngine) DeleteDocument(ctx context.Context, docID string) error
DeleteDocument removes a document from all composite engines
func (*CompositeEngine) Query ¶
func (c *CompositeEngine) Query(ctx context.Context, query string) (*QueryResult, error)
Query performs a query using all composite engines and aggregates results
func (*CompositeEngine) QueryWithConfig ¶
func (c *CompositeEngine) QueryWithConfig(ctx context.Context, query string, config *RetrievalConfig) (*QueryResult, error)
QueryWithConfig performs a query with custom configuration
func (*CompositeEngine) SimilaritySearch ¶
func (c *CompositeEngine) SimilaritySearch(ctx context.Context, query string, k int) ([]Document, error)
SimilaritySearch performs similarity search using the first available engine
func (*CompositeEngine) SimilaritySearchWithScores ¶
func (c *CompositeEngine) SimilaritySearchWithScores(ctx context.Context, query string, k int) ([]DocumentSearchResult, error)
SimilaritySearchWithScores performs similarity search with scores using the first available engine
func (*CompositeEngine) UpdateDocument ¶
func (c *CompositeEngine) UpdateDocument(ctx context.Context, doc Document) error
UpdateDocument updates a document in all composite engines
type Config ¶
type Config struct {
VectorRAG *VectorRAGConfig `json:"vector_rag,omitempty"`
GraphRAG *GraphRAGConfig `json:"graph_rag,omitempty"`
LightRAG *LightRAGConfig `json:"lightrag,omitempty"`
}
Config is a generic RAG configuration
type Document ¶
type Document struct {
ID string `json:"id"`
Content string `json:"content"`
Metadata map[string]any `json:"metadata"`
Embedding []float32 `json:"embedding,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
Document represents a document or document chunk in the RAG system
type DocumentLoader ¶
DocumentLoader interface for loading documents
type DocumentSearchResult ¶
type DocumentSearchResult struct {
Document Document `json:"document"`
Score float64 `json:"score"`
Metadata map[string]any `json:"metadata,omitempty"`
}
DocumentSearchResult represents a document search result with relevance score
type Embedder ¶
type Embedder interface {
EmbedDocument(ctx context.Context, text string) ([]float32, error)
EmbedDocuments(ctx context.Context, texts []string) ([][]float32, error)
GetDimension() int
}
Embedder interface for text embeddings
type Engine ¶
type Engine interface {
Query(ctx context.Context, query string) (*QueryResult, error)
QueryWithConfig(ctx context.Context, query string, config *RetrievalConfig) (*QueryResult, error)
AddDocuments(ctx context.Context, docs []Document) error
DeleteDocument(ctx context.Context, docID string) error
UpdateDocument(ctx context.Context, doc Document) error
SimilaritySearch(ctx context.Context, query string, k int) ([]Document, error)
SimilaritySearchWithScores(ctx context.Context, query string, k int) ([]DocumentSearchResult, error)
}
Engine interface for RAG engines
type Entity ¶
type Entity struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
Properties map[string]any `json:"properties"`
Embedding []float32 `json:"embedding,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
Entity represents a knowledge graph entity
type GlobalRetrievalConfig ¶ added in v0.8.2
type GlobalRetrievalConfig struct {
// Maximum number of communities to retrieve
MaxCommunities int `json:"max_communities"`
// Include community hierarchy
IncludeHierarchy bool `json:"include_hierarchy"`
// Weight for community relevance
CommunityWeight float64 `json:"community_weight"`
// Maximum hierarchy depth
MaxHierarchyDepth int `json:"max_hierarchy_depth"`
}
GlobalRetrievalConfig configures global mode retrieval Global mode retrieves information from community-level summaries
type GraphQuery ¶
type GraphQuery struct {
EntityTypes []string `json:"entity_types,omitempty"`
Relationships []string `json:"relationships,omitempty"`
Filters map[string]any `json:"filters,omitempty"`
Limit int `json:"limit,omitempty"`
MaxDepth int `json:"max_depth,omitempty"`
StartEntity string `json:"start_entity,omitempty"`
EntityType string `json:"entity_type,omitempty"`
}
GraphQuery represents a query to the knowledge graph
type GraphQueryResult ¶
type GraphQueryResult struct {
Entities []*Entity `json:"entities"`
Relationships []*Relationship `json:"relationships"`
Paths [][]*Entity `json:"paths,omitempty"`
Score float64 `json:"score"`
Scores []float64 `json:"scores,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
GraphQueryResult represents the result of a graph query
type GraphRAGConfig ¶
type GraphRAGConfig struct {
DatabaseURL string `json:"database_url"`
ModelProvider string `json:"model_provider"`
EmbeddingModel string `json:"embedding_model"`
ChatModel string `json:"chat_model"`
EntityTypes []string `json:"entity_types"`
Relationships map[string][]string `json:"relationships"`
MaxDepth int `json:"max_depth"`
EnableReasoning bool `json:"enable_reasoning"`
ExtractionPrompt string `json:"extraction_prompt"`
}
GraphRAGConfig represents configuration for graph-based RAG
type HybridRetrievalConfig ¶ added in v0.8.2
type HybridRetrievalConfig struct {
// Weight for local retrieval results (0-1)
LocalWeight float64 `json:"local_weight"`
// Weight for global retrieval results (0-1)
GlobalWeight float64 `json:"global_weight"`
// Fusion method: "rrf" (reciprocal rank fusion) or "weighted"
FusionMethod string `json:"fusion_method"`
// RRF parameter for rank fusion
RFFK int `json:"rrf_k"`
}
HybridRetrievalConfig configures hybrid mode retrieval Hybrid mode combines local and global retrieval results
type KnowledgeGraph ¶
type KnowledgeGraph interface {
AddEntity(ctx context.Context, entity *Entity) error
AddRelationship(ctx context.Context, relationship *Relationship) error
Query(ctx context.Context, query *GraphQuery) (*GraphQueryResult, error)
GetRelatedEntities(ctx context.Context, entityID string, maxDepth int) ([]*Entity, error)
GetEntity(ctx context.Context, entityID string) (*Entity, error)
}
KnowledgeGraph interface for graph-based retrieval
type LLMInterface ¶
type LLMInterface interface {
Generate(ctx context.Context, prompt string) (string, error)
GenerateWithConfig(ctx context.Context, prompt string, config map[string]any) (string, error)
GenerateWithSystem(ctx context.Context, system, prompt string) (string, error)
}
LLMInterface defines the interface for language models
type LangChainDocumentLoader ¶
type LangChainDocumentLoader struct {
// contains filtered or unexported fields
}
LangChainDocumentLoader adapts langchaingo's documentloaders.Loader to our DocumentLoader interface
func NewLangChainDocumentLoader ¶
func NewLangChainDocumentLoader(loader documentloaders.Loader) *LangChainDocumentLoader
NewLangChainDocumentLoader creates a new adapter for langchaingo document loaders
func (*LangChainDocumentLoader) Load ¶
func (l *LangChainDocumentLoader) Load(ctx context.Context) ([]Document, error)
Load loads documents using the underlying langchaingo loader
func (*LangChainDocumentLoader) LoadAndSplit ¶
func (l *LangChainDocumentLoader) LoadAndSplit(ctx context.Context, splitter textsplitter.TextSplitter) ([]Document, error)
LoadAndSplit loads and splits documents using langchaingo's text splitter
func (*LangChainDocumentLoader) LoadWithMetadata ¶
func (l *LangChainDocumentLoader) LoadWithMetadata(ctx context.Context, metadata map[string]any) ([]Document, error)
LoadWithMetadata loads documents with additional metadata using the underlying langchaingo loader
type LangChainEmbedder ¶
type LangChainEmbedder struct {
// contains filtered or unexported fields
}
LangChainEmbedder adapts langchaingo's embeddings.Embedder to our Embedder interface
func NewLangChainEmbedder ¶
func NewLangChainEmbedder(embedder embeddings.Embedder) *LangChainEmbedder
NewLangChainEmbedder creates a new adapter for langchaingo embedders
func (*LangChainEmbedder) EmbedDocument ¶
EmbedDocument embeds a single document using the underlying langchaingo embedder
func (*LangChainEmbedder) EmbedDocuments ¶
func (l *LangChainEmbedder) EmbedDocuments(ctx context.Context, texts []string) ([][]float32, error)
EmbedDocuments embeds multiple documents using the underlying langchaingo embedder
func (*LangChainEmbedder) GetDimension ¶
func (l *LangChainEmbedder) GetDimension() int
GetDimension returns the embedding dimension
type LangChainRetriever ¶
type LangChainRetriever struct {
// contains filtered or unexported fields
}
LangChainRetriever adapts langchaingo's vectorstores.VectorStore to our Retriever interface
func NewLangChainRetriever ¶
func NewLangChainRetriever(store vectorstores.VectorStore, topK int) *LangChainRetriever
NewLangChainRetriever creates a new adapter for langchaingo vector stores as a retriever
func (*LangChainRetriever) RetrieveWithConfig ¶
func (r *LangChainRetriever) RetrieveWithConfig(ctx context.Context, query string, config *RetrievalConfig) ([]DocumentSearchResult, error)
RetrieveWithConfig retrieves documents with custom configuration
func (*LangChainRetriever) RetrieveWithK ¶
func (r *LangChainRetriever) RetrieveWithK(ctx context.Context, query string, k int) ([]Document, error)
RetrieveWithK retrieves exactly k documents
type LangChainTextSplitter ¶
type LangChainTextSplitter struct {
// contains filtered or unexported fields
}
LangChainTextSplitter adapts langchaingo's textsplitter.TextSplitter to our TextSplitter interface
func NewLangChainTextSplitter ¶
func NewLangChainTextSplitter(splitter textsplitter.TextSplitter) *LangChainTextSplitter
NewLangChainTextSplitter creates a new adapter for langchaingo text splitters
func (*LangChainTextSplitter) JoinText ¶
func (l *LangChainTextSplitter) JoinText(chunks []string) string
JoinText joins text chunks back together
func (*LangChainTextSplitter) SplitDocuments ¶
func (l *LangChainTextSplitter) SplitDocuments(docs []Document) []Document
SplitDocuments splits documents using simple paragraph splitting
func (*LangChainTextSplitter) SplitText ¶
func (l *LangChainTextSplitter) SplitText(text string) []string
SplitText splits text using simple paragraph splitting
type LangChainVectorStore ¶
type LangChainVectorStore struct {
// contains filtered or unexported fields
}
LangChainVectorStore adapts langchaingo's vectorstores.VectorStore to our VectorStore interface
func NewLangChainVectorStore ¶
func NewLangChainVectorStore(store vectorstores.VectorStore) *LangChainVectorStore
NewLangChainVectorStore creates a new adapter for langchaingo vector stores
func (*LangChainVectorStore) Add ¶
func (l *LangChainVectorStore) Add(ctx context.Context, docs []Document) error
Add adds documents to the vector store
func (*LangChainVectorStore) Delete ¶
func (l *LangChainVectorStore) Delete(ctx context.Context, ids []string) error
Delete removes documents by IDs
func (*LangChainVectorStore) GetStats ¶
func (l *LangChainVectorStore) GetStats(ctx context.Context) (*VectorStoreStats, error)
GetStats returns vector store statistics
func (*LangChainVectorStore) Search ¶
func (l *LangChainVectorStore) Search(ctx context.Context, query []float32, k int) ([]DocumentSearchResult, error)
Search performs similarity search
func (*LangChainVectorStore) SearchWithFilter ¶
func (l *LangChainVectorStore) SearchWithFilter(ctx context.Context, query []float32, k int, filter map[string]any) ([]DocumentSearchResult, error)
SearchWithFilter performs similarity search with filters
type LightRAGConfig ¶ added in v0.8.2
type LightRAGConfig struct {
// Retrieval mode: "local", "global", "hybrid", or "naive"
Mode string `json:"mode"`
// Local retrieval configuration
LocalConfig LocalRetrievalConfig `json:"local_config"`
// Global retrieval configuration
GlobalConfig GlobalRetrievalConfig `json:"global_config"`
// Hybrid retrieval configuration
HybridConfig HybridRetrievalConfig `json:"hybrid_config"`
// Chunk size for text splitting
ChunkSize int `json:"chunk_size"`
// Chunk overlap for text splitting
ChunkOverlap int `json:"chunk_overlap"`
// Threshold for entity extraction
EntityExtractionThreshold float64 `json:"entity_extraction_threshold"`
// Maximum number of entities to extract per chunk
MaxEntitiesPerChunk int `json:"max_entities_per_chunk"`
// Enable community detection for global retrieval
EnableCommunityDetection bool `json:"enable_community_detection"`
// Community detection algorithm: "louvain", "leiden", or "label_propagation"
CommunityDetectionAlgorithm string `json:"community_detection_algorithm"`
// Number of communities to return in global retrieval
MaxCommunities int `json:"max_communities"`
// Temperature for LLM-based operations
Temperature float64 `json:"temperature"`
// Custom prompt templates
PromptTemplates map[string]string `json:"prompt_templates,omitempty"`
}
LightRAGConfig represents configuration for LightRAG LightRAG combines low-level semantic chunks with high-level graph structures
type LocalRetrievalConfig ¶ added in v0.8.2
type LocalRetrievalConfig struct {
// Maximum number of hops in the knowledge graph
MaxHops int `json:"max_hops"`
// Number of entities to retrieve
TopK int `json:"top_k"`
// Include entity descriptions
IncludeDescriptions bool `json:"include_descriptions"`
// Weight for entity relevance
EntityWeight float64 `json:"entity_weight"`
}
LocalRetrievalConfig configures local mode retrieval Local mode retrieves relevant entities and their relationships within a localized context
type Metrics ¶
type Metrics struct {
TotalQueries int64 `json:"total_queries"`
TotalDocuments int64 `json:"total_documents"`
AverageLatency time.Duration `json:"average_latency"`
MinLatency time.Duration `json:"min_latency"`
MaxLatency time.Duration `json:"max_latency"`
LastQueryTime time.Time `json:"last_query_time"`
CacheHits int64 `json:"cache_hits"`
CacheMisses int64 `json:"cache_misses"`
IndexingLatency time.Duration `json:"indexing_latency"`
}
Metrics contains performance metrics for RAG engines
type PipelineConfig ¶
type PipelineConfig struct {
// Retrieval configuration
TopK int // Number of documents to retrieve
ScoreThreshold float64 // Minimum relevance score
UseReranking bool // Whether to use reranking
UseFallback bool // Whether to use fallback search
// Generation configuration
SystemPrompt string
IncludeCitations bool
MaxTokens int
Temperature float64
// Components
Loader RAGDocumentLoader
Splitter RAGTextSplitter
Embedder Embedder
VectorStore VectorStore
Retriever Retriever
Reranker Reranker
LLM llms.Model
}
PipelineConfig configures a RAG pipeline
func DefaultPipelineConfig ¶
func DefaultPipelineConfig() *PipelineConfig
DefaultPipelineConfig returns a default RAG configuration
type QueryResult ¶
type QueryResult struct {
Query string `json:"query"`
Answer string `json:"answer"`
Sources []Document `json:"sources"`
Context string `json:"context"`
Confidence float64 `json:"confidence"`
ResponseTime time.Duration `json:"response_time"`
Metadata map[string]any `json:"metadata"`
}
QueryResult represents the result of a RAG query
func DefaultAggregator ¶
func DefaultAggregator(results []*QueryResult) *QueryResult
DefaultAggregator provides default result aggregation logic
type RAGConfig ¶
type RAGConfig struct {
Config `json:"config"`
EnableCache bool `json:"enable_cache"`
CacheSize int `json:"cache_size"`
EnableMetrics bool `json:"enable_metrics"`
Debug bool `json:"debug"`
Timeout time.Duration `json:"timeout"`
}
RAGConfig represents the main RAG configuration
type RAGDocument ¶
type RAGDocument struct {
Content string `json:"content"`
Metadata map[string]any `json:"metadata"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
RAGDocument represents a document with content and metadata (for pipeline compatibility)
func DocumentFromRAGDocument ¶
func DocumentFromRAGDocument(doc Document) RAGDocument
DocumentFromRAGDocument converts Document to RAGDocument
func (RAGDocument) Document ¶
func (d RAGDocument) Document() Document
ConvertToDocument converts RAGDocument to Document
type RAGDocumentLoader ¶
type RAGDocumentLoader interface {
Load(ctx context.Context) ([]RAGDocument, error)
}
RAGDocumentLoader represents a document loader for RAG pipelines
type RAGPipeline ¶
type RAGPipeline struct {
// contains filtered or unexported fields
}
RAGPipeline represents a complete RAG pipeline
func NewRAGPipeline ¶
func NewRAGPipeline(config *PipelineConfig) *RAGPipeline
NewRAGPipeline creates a new RAG pipeline with the given configuration
func (*RAGPipeline) BuildAdvancedRAG ¶
func (p *RAGPipeline) BuildAdvancedRAG() error
BuildAdvancedRAG builds an advanced RAG pipeline: Retrieve -> Rerank -> Generate
func (*RAGPipeline) BuildBasicRAG ¶
func (p *RAGPipeline) BuildBasicRAG() error
BuildBasicRAG builds a basic RAG pipeline: Retrieve -> Generate
func (*RAGPipeline) BuildConditionalRAG ¶
func (p *RAGPipeline) BuildConditionalRAG() error
BuildConditionalRAG builds a RAG pipeline with conditional routing based on relevance
func (*RAGPipeline) Compile ¶
func (p *RAGPipeline) Compile() (*graph.StateRunnable[map[string]any], error)
Compile compiles the RAG pipeline into a runnable graph
func (*RAGPipeline) GetGraph ¶
func (p *RAGPipeline) GetGraph() *graph.StateGraph[map[string]any]
GetGraph returns the underlying graph for visualization
type RAGState ¶
type RAGState struct {
Query string
Documents []RAGDocument
RetrievedDocuments []RAGDocument
RankedDocuments []DocumentSearchResult
Context string
Answer string
Citations []string
Metadata map[string]any
}
RAGState represents the state flowing through a RAG pipeline
type RAGTextSplitter ¶
type RAGTextSplitter interface {
SplitDocuments(documents []RAGDocument) ([]RAGDocument, error)
}
RAGTextSplitter represents a text splitter for RAG pipelines
type Relationship ¶
type Relationship struct {
ID string `json:"id"`
Source string `json:"source"`
Target string `json:"target"`
Type string `json:"type"`
Properties map[string]any `json:"properties"`
Weight float64 `json:"weight,omitempty"`
Confidence float64 `json:"confidence,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
Relationship represents a relationship between entities
type Reranker ¶
type Reranker interface {
Rerank(ctx context.Context, query string, documents []DocumentSearchResult) ([]DocumentSearchResult, error)
}
Reranker interface for reranking search results
type RetrievalConfig ¶
type RetrievalConfig struct {
K int `json:"k"`
ScoreThreshold float64 `json:"score_threshold"`
SearchType string `json:"search_type"`
Filter map[string]any `json:"filter,omitempty"`
IncludeScores bool `json:"include_scores"`
}
RetrievalConfig contains configuration for retrieval operations
type Retriever ¶
type Retriever interface {
Retrieve(ctx context.Context, query string) ([]Document, error)
RetrieveWithK(ctx context.Context, query string, k int) ([]Document, error)
RetrieveWithConfig(ctx context.Context, query string, config *RetrievalConfig) ([]DocumentSearchResult, error)
}
Retriever interface for document retrieval
type RetrieverTool ¶ added in v0.7.1
RetrieverTool wraps a RAG Engine as a LangChain Tool, allowing agents to query the knowledge base.
func NewRetrieverTool ¶ added in v0.7.1
func NewRetrieverTool(engine Engine, name, description string) *RetrieverTool
NewRetrieverTool creates a new RetrieverTool. If name or description are empty, defaults will be used.
func (*RetrieverTool) Description ¶ added in v0.7.1
func (t *RetrieverTool) Description() string
Description returns the description of the tool.
func (*RetrieverTool) Name ¶ added in v0.7.1
func (t *RetrieverTool) Name() string
Name returns the name of the tool.
type TextSplitter ¶
type TextSplitter interface {
SplitText(text string) []string
SplitDocuments(documents []Document) []Document
JoinText(chunks []string) string
}
TextSplitter interface for splitting text into chunks
type VectorRAGConfig ¶
type VectorRAGConfig struct {
EmbeddingModel string `json:"embedding_model"`
VectorStoreType string `json:"vector_store_type"`
VectorStoreConfig map[string]any `json:"vector_store_config"`
ChunkSize int `json:"chunk_size"`
ChunkOverlap int `json:"chunk_overlap"`
EnableReranking bool `json:"enable_reranking"`
RetrieverConfig RetrievalConfig `json:"retriever_config"`
}
VectorRAGConfig represents configuration for vector-based RAG
type VectorStore ¶
type VectorStore interface {
Add(ctx context.Context, documents []Document) error
Search(ctx context.Context, query []float32, k int) ([]DocumentSearchResult, error)
SearchWithFilter(ctx context.Context, query []float32, k int, filter map[string]any) ([]DocumentSearchResult, error)
Delete(ctx context.Context, ids []string) error
Update(ctx context.Context, documents []Document) error
GetStats(ctx context.Context) (*VectorStoreStats, error)
}
VectorStore interface for vector storage and retrieval