graph

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RelatedTo   = "related_to"   // semantic relationship between entities
	CausedBy    = "caused_by"    // causal relationship (effect → cause)
	ResolvedBy  = "resolved_by"  // resolution relationship (error → fix)
	Follows     = "follows"      // temporal ordering (observation → observation)
	SimilarTo   = "similar_to"   // similarity relationship (learning ↔ learning)
	Contains    = "contains"     // containment (session → observation)
	InSession   = "in_session"   // session membership
	ReflectsOn  = "reflects_on"  // reflection targets (reflection → observation)
	LearnedFrom = "learned_from" // provenance (learning → session)
)

Predicate constants define relationship types in the knowledge graph. These are untyped string constants to allow direct use in Triple struct literals where the Predicate field is string. Use Predicate(x).Valid() for validation.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoltStore

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

BoltStore is a BoltDB-backed triple store with SPO, POS, and OSP indexes.

func NewBoltStore

func NewBoltStore(path string) (*BoltStore, error)

NewBoltStore opens (or creates) a BoltDB database at path and initialises the three index buckets.

func (*BoltStore) AddTriple

func (s *BoltStore) AddTriple(_ context.Context, t Triple) error

AddTriple adds a single triple to all three indexes.

func (*BoltStore) AddTriples

func (s *BoltStore) AddTriples(_ context.Context, triples []Triple) error

AddTriples adds multiple triples in a single atomic transaction.

func (*BoltStore) AllTriples added in v0.4.0

func (s *BoltStore) AllTriples(_ context.Context) ([]Triple, error)

AllTriples returns every triple stored in the SPO index.

func (*BoltStore) ClearAll

func (s *BoltStore) ClearAll(_ context.Context) error

ClearAll removes all triples from all index buckets.

func (*BoltStore) Close

func (s *BoltStore) Close() error

Close closes the underlying BoltDB database.

func (*BoltStore) Count

func (s *BoltStore) Count(_ context.Context) (int, error)

Count returns the total number of triples by counting keys in the SPO bucket.

func (*BoltStore) PredicateStats

func (s *BoltStore) PredicateStats(_ context.Context) (map[string]int, error)

PredicateStats returns the number of triples grouped by predicate.

func (*BoltStore) QueryByObject

func (s *BoltStore) QueryByObject(_ context.Context, object string) ([]Triple, error)

QueryByObject returns all triples whose object matches.

func (*BoltStore) QueryBySubject

func (s *BoltStore) QueryBySubject(_ context.Context, subject string) ([]Triple, error)

QueryBySubject returns all triples whose subject matches.

func (*BoltStore) QueryBySubjectPredicate

func (s *BoltStore) QueryBySubjectPredicate(_ context.Context, subject, predicate string) ([]Triple, error)

QueryBySubjectPredicate returns triples matching both subject and predicate.

func (*BoltStore) RemoveTriple

func (s *BoltStore) RemoveTriple(_ context.Context, t Triple) error

RemoveTriple removes a triple from all three indexes.

func (*BoltStore) Traverse

func (s *BoltStore) Traverse(_ context.Context, startNode string, maxDepth int, predicates []string) ([]Triple, error)

Traverse performs a breadth-first traversal from startNode up to maxDepth hops. If predicates is non-empty, only edges with matching predicate types are followed.

type Extractor

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

Extractor uses an LLM to extract entities and relationships from text.

func NewExtractor

func NewExtractor(generator TextGenerator, logger *zap.SugaredLogger) *Extractor

NewExtractor creates a new LLM-based entity/relationship extractor.

func (*Extractor) Extract

func (e *Extractor) Extract(ctx context.Context, content, sourceID string) ([]Triple, error)

Extract extracts triples from the given text content. The sourceID is used as context for provenance tracking.

type GraphBuffer

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

GraphBuffer collects graph update requests and processes them in batches on a background goroutine. It follows the same lifecycle pattern as embedding.EmbeddingBuffer: Start -> Enqueue -> Stop.

Note: GraphRequest items are expanded into individual Triples for batch processing, so the BatchBuffer operates on Triple slices internally.

func NewGraphBuffer

func NewGraphBuffer(store Store, logger *zap.SugaredLogger) *GraphBuffer

NewGraphBuffer creates a new asynchronous graph update buffer.

func (*GraphBuffer) DroppedCount

func (b *GraphBuffer) DroppedCount() int64

DroppedCount returns the total number of dropped graph requests.

func (*GraphBuffer) Enqueue

func (b *GraphBuffer) Enqueue(req GraphRequest)

Enqueue submits a graph update request. Non-blocking; drops if the queue is full.

func (*GraphBuffer) Start

func (b *GraphBuffer) Start(wg *sync.WaitGroup)

Start launches the background goroutine. The WaitGroup is incremented so callers can wait for graceful shutdown.

func (*GraphBuffer) Stop

func (b *GraphBuffer) Stop()

Stop signals the background goroutine to drain and exit.

type GraphNode

type GraphNode struct {
	ID        string
	Predicate string // the edge that led here
	FromNode  string // the source node this was discovered from
	Depth     int
}

GraphNode represents a node discovered through graph expansion.

type GraphRAGResult

type GraphRAGResult struct {
	// VectorResults are the original vector-search results.
	VectorResults []VectorResult
	// GraphResults are additional results discovered via graph traversal.
	GraphResults []GraphNode
}

GraphRAGResult extends VectorResult with graph-expanded context.

type GraphRAGService

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

GraphRAGService provides hybrid retrieval combining vector search with graph expansion.

func NewGraphRAGService

func NewGraphRAGService(
	vectorRAG VectorRetriever,
	graph Store,
	maxDepth int,
	maxExpand int,
	logger *zap.SugaredLogger,
) *GraphRAGService

NewGraphRAGService creates a new hybrid graph RAG service.

func (*GraphRAGService) AssembleSection

func (s *GraphRAGService) AssembleSection(result *GraphRAGResult) string

AssembleSection builds a formatted string section for context injection.

func (*GraphRAGService) Retrieve

Retrieve performs 2-phase hybrid retrieval: Phase 1: Vector search (sqlite-vec cosine similarity) Phase 2: Graph expansion from Phase 1 results (depth 1-2 hops)

type GraphRequest

type GraphRequest struct {
	Triples []Triple
}

GraphRequest represents a request to add triples to the graph.

type Predicate

type Predicate string

Predicate represents a relationship type in the knowledge graph.

func (Predicate) Valid

func (p Predicate) Valid() bool

Valid reports whether p is a known predicate type.

func (Predicate) Values

func (p Predicate) Values() []Predicate

Values returns all known predicate types.

type Store

type Store interface {
	// AddTriple adds a single triple to the graph.
	AddTriple(ctx context.Context, t Triple) error

	// AddTriples adds multiple triples atomically.
	AddTriples(ctx context.Context, triples []Triple) error

	// RemoveTriple removes a triple from the graph.
	RemoveTriple(ctx context.Context, t Triple) error

	// QueryBySubject returns all triples with the given subject.
	QueryBySubject(ctx context.Context, subject string) ([]Triple, error)

	// QueryByObject returns all triples with the given object.
	QueryByObject(ctx context.Context, object string) ([]Triple, error)

	// QueryBySubjectPredicate returns triples matching subject and predicate.
	QueryBySubjectPredicate(ctx context.Context, subject, predicate string) ([]Triple, error)

	// Traverse performs a breadth-first traversal from a start node.
	// predicates filters which edge types to follow (empty = all).
	Traverse(ctx context.Context, startNode string, maxDepth int, predicates []string) ([]Triple, error)

	// Count returns the total number of triples in the store.
	Count(ctx context.Context) (int, error)

	// PredicateStats returns the number of triples for each predicate type.
	PredicateStats(ctx context.Context) (map[string]int, error)

	// AllTriples returns every triple in the store.
	AllTriples(ctx context.Context) ([]Triple, error)

	// ClearAll removes all triples from the store.
	ClearAll(ctx context.Context) error

	// Close closes the underlying store.
	Close() error
}

Store provides graph CRUD and traversal operations.

type TextGenerator

type TextGenerator interface {
	GenerateText(ctx context.Context, systemPrompt, userPrompt string) (string, error)
}

TextGenerator generates text from an LLM for entity extraction.

type Triple

type Triple struct {
	Subject   string
	Predicate string
	Object    string
	Metadata  map[string]string
}

Triple represents a Subject-Predicate-Object relationship in the graph.

type VectorResult

type VectorResult struct {
	Collection string
	SourceID   string
	Content    string
	Distance   float32
}

VectorResult mirrors embedding.RAGResult to avoid an import cycle.

type VectorRetrieveOptions

type VectorRetrieveOptions struct {
	Collections []string
	Limit       int
	SessionKey  string
	MaxDistance float32
}

VectorRetrieveOptions mirrors embedding.RetrieveOptions to avoid an import cycle.

type VectorRetriever

type VectorRetriever interface {
	Retrieve(ctx context.Context, query string, opts VectorRetrieveOptions) ([]VectorResult, error)
}

VectorRetriever retrieves results from a vector store. Implemented by embedding.RAGService (satisfies via adapter).

Jump to

Keyboard shortcuts

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