gognee

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrTypeNetwork    = "network"
	ErrTypeTimeout    = "timeout"
	ErrTypeLLM        = "llm"
	ErrTypeDatabase   = "database"
	ErrTypeValidation = "validation"
	ErrTypeUnknown    = "unknown"
)

Error type constants for classification

View Source
const (
	SearchTypeVector = search.SearchTypeVector
	SearchTypeGraph  = search.SearchTypeGraph
	SearchTypeHybrid = search.SearchTypeHybrid
)

SearchType constants re-exported from search package

Variables

View Source
var RetentionPolicies = map[string]RetentionPolicyDef{
	"permanent": {HalfLifeDays: 0, Prunable: false},
	"decision":  {HalfLifeDays: 365, Prunable: true},
	"standard":  {HalfLifeDays: 90, Prunable: true},
	"ephemeral": {HalfLifeDays: 7, Prunable: true},
	"session":   {HalfLifeDays: 1, Prunable: true},
}

RetentionPolicies defines all available retention policies (M6: Plan 021)

Functions

func ClassifyError added in v1.1.1

func ClassifyError(err error) string

ClassifyError inspects an error and returns its type classification. This enables grouping errors by category in metrics and traces.

Types

type AddOptions

type AddOptions struct {
	Source string
}

AddOptions configures the Add() method

type AddedDocument

type AddedDocument struct {
	Text    string
	Source  string
	AddedAt time.Time
}

AddedDocument represents a document added to the buffer for processing

type CognifyOptions

type CognifyOptions struct {
	// SkipProcessed enables incremental mode, skipping previously processed documents.
	// Default: true (incremental by default). Use pointer to distinguish unset from explicit false.
	// When true, documents are identified by content hash (SHA-256).
	// Documents with matching hash are skipped unless Force is true.
	SkipProcessed *bool

	// Force reprocesses all documents regardless of cached state.
	// Overrides SkipProcessed when true.
	// Use after changing chunker settings or to rebuild the knowledge graph.
	Force bool

	// TraceEnabled enables detailed timing instrumentation for performance analysis.
	// Default: false (off by default to minimize overhead).
	// When enabled, timing spans are collected and returned in CognifyResult.Trace.
	TraceEnabled bool
}

CognifyOptions configures the Cognify() method

type CognifyResult

type CognifyResult struct {
	DocumentsProcessed int // Documents actually processed (chunked + extracted)
	DocumentsSkipped   int // Documents skipped due to incremental caching
	ChunksProcessed    int
	ChunksFailed       int
	NodesCreated       int
	EdgesCreated       int
	EdgesSkipped       int             // Count of edges skipped due to entity lookup failure or ambiguity
	Errors             []error         // Includes details of skipped edges ("skipped edge" in message)
	Trace              *OperationTrace // Timing data (populated when CognifyOptions.TraceEnabled is true)
}

CognifyResult reports the outcome of a Cognify() operation

type Config

type Config struct {
	// OpenAI API key for embeddings and LLM
	OpenAIKey string

	// Embedding model (default: "text-embedding-3-small")
	EmbeddingModel string

	// LLM model for entity extraction (default: "gpt-4o-mini")
	LLMModel string

	// Chunk size in tokens (default: 512)
	ChunkSize int

	// Chunk overlap in tokens (default: 50)
	ChunkOverlap int

	// DBPath is the path to the SQLite database file.
	// If empty or ":memory:", an in-memory database is used.
	DBPath string

	// DecayEnabled enables time-based memory decay scoring (default: false)
	DecayEnabled bool

	// DecayHalfLifeDays is the number of days after which a node's score is halved (default: 30)
	DecayHalfLifeDays int

	// DecayBasis determines decay calculation: "access" (last access time) or "creation" (creation time)
	// Default: "access"
	DecayBasis string

	// AccessFrequencyEnabled enables frequency-based decay modification (default: true when DecayEnabled)
	// When enabled, frequently accessed memories resist time-based decay
	AccessFrequencyEnabled bool

	// ReferenceAccessCount is the access count at which heat_multiplier = 1.0 (default: 10)
	// Memories with this many accesses get full heat protection from decay
	ReferenceAccessCount int
}

Config holds configuration for the Gognee system

type Edge

type Edge = store.Edge

Edge is re-exported from store package

type Gognee

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

Gognee is the main entry point for the memory system

func New

func New(cfg Config) (*Gognee, error)

New creates a new Gognee instance using OpenAI clients

func NewWithClients added in v1.1.1

func NewWithClients(cfg Config, embClient embeddings.EmbeddingClient, llmClient llm.LLMClient) (*Gognee, error)

NewWithClients creates a new Gognee instance with custom embedding and LLM clients. This allows using alternative providers like Ollama for local inference.

func (*Gognee) Add

func (g *Gognee) Add(ctx context.Context, text string, opts AddOptions) error

Add buffers text for processing via Cognify()

func (*Gognee) AddMemory

func (g *Gognee) AddMemory(ctx context.Context, input MemoryInput) (*MemoryResult, error)

AddMemory creates a new first-class memory with full CRUD support. Uses two-phase model: persist memory record → cognify → link provenance.

func (*Gognee) BufferedCount

func (g *Gognee) BufferedCount() int

BufferedCount returns the number of documents currently in the buffer

func (*Gognee) Close

func (g *Gognee) Close() error

Close releases all resources

func (*Gognee) Cognify

func (g *Gognee) Cognify(ctx context.Context, opts CognifyOptions) (*CognifyResult, error)

Cognify processes all buffered documents through the extraction pipeline

func (*Gognee) CountMemories added in v1.0.2

func (g *Gognee) CountMemories(ctx context.Context) (int64, error)

CountMemories returns the total number of memories in the store.

func (*Gognee) DeleteMemory

func (g *Gognee) DeleteMemory(ctx context.Context, id string) error

DeleteMemory removes a memory and runs garbage collection on orphaned artifacts.

func (*Gognee) GarbageCollect

func (g *Gognee) GarbageCollect(ctx context.Context) (nodesDeleted, edgesDeleted int, err error)

GarbageCollect manually triggers garbage collection. Returns counts of deleted nodes and edges.

func (*Gognee) GetChunker

func (g *Gognee) GetChunker() *chunker.Chunker

GetChunker returns the configured chunker

func (*Gognee) GetEmbeddings

func (g *Gognee) GetEmbeddings() embeddings.EmbeddingClient

GetEmbeddings returns the configured embeddings client

func (*Gognee) GetGraphStore

func (g *Gognee) GetGraphStore() store.GraphStore

GetGraphStore returns the configured graph store

func (*Gognee) GetLLM

func (g *Gognee) GetLLM() llm.LLMClient

GetLLM returns the configured LLM client

func (*Gognee) GetMemory

func (g *Gognee) GetMemory(ctx context.Context, id string) (*store.MemoryRecord, error)

GetMemory retrieves a memory by ID.

func (*Gognee) GetVectorStore

func (g *Gognee) GetVectorStore() store.VectorStore

GetVectorStore returns the configured vector store

func (*Gognee) ListMemories

func (g *Gognee) ListMemories(ctx context.Context, opts store.ListMemoriesOptions) ([]store.MemorySummary, error)

ListMemories returns paginated memory summaries.

func (*Gognee) PinMemory added in v1.5.0

func (g *Gognee) PinMemory(ctx context.Context, id string, reason string) error

PinMemory marks a memory as pinned, exempting it from decay and prune (M9: Plan 021).

func (*Gognee) Prune

func (g *Gognee) Prune(ctx context.Context, opts PruneOptions) (*PruneResult, error)

Prune removes old or low-scoring nodes from the knowledge graph. Edges connected to pruned nodes are also deleted (cascade). Use DryRun to preview what would be pruned without actually deleting.

func (*Gognee) Search

func (g *Gognee) Search(ctx context.Context, query string, opts search.SearchOptions) (*SearchResponse, error)

Search queries the knowledge graph

func (*Gognee) Stats

func (g *Gognee) Stats() (Stats, error)

Stats returns basic telemetry

func (*Gognee) UnpinMemory added in v1.5.0

func (g *Gognee) UnpinMemory(ctx context.Context, id string) error

UnpinMemory removes pinning from a memory, allowing normal decay/prune (M9: Plan 021).

func (*Gognee) UpdateMemory

func (g *Gognee) UpdateMemory(ctx context.Context, id string, updates store.MemoryUpdate) (*MemoryResult, error)

UpdateMemory applies partial updates to a memory and re-cognifies if content changed.

func (*Gognee) WithLogger added in v1.6.0

func (g *Gognee) WithLogger(logger *slog.Logger) *Gognee

WithLogger sets the structured logger for this Gognee instance (Plan 023 M2). When nil, logging is disabled (zero overhead). Propagates logger to DecayingSearcher if present.

func (*Gognee) WithMetricsCollector added in v1.1.1

func (g *Gognee) WithMetricsCollector(collector metrics.Collector) *Gognee

WithMetricsCollector sets the metrics collector for this Gognee instance

func (*Gognee) WithTraceExporter added in v1.1.1

func (g *Gognee) WithTraceExporter(exporter tracepkg.Exporter) *Gognee

WithTraceExporter sets the trace exporter for this Gognee instance (Plan 016 M4)

type MemoryInput

type MemoryInput struct {
	Topic     string
	Context   string
	Decisions []string
	Rationale []string
	Metadata  map[string]interface{}
	Source    string
	// TraceEnabled enables timing instrumentation (Plan 015)
	TraceEnabled bool
	// Supersedes lists memory IDs that this new memory replaces (M4: Plan 021)
	Supersedes []string
	// SupersessionReason explains why this memory supersedes the old ones (M4: Plan 021)
	SupersessionReason string
	// RetentionPolicy sets the retention policy for this memory (M6: Plan 021)
	// Valid values: permanent, decision, standard, ephemeral, session (default: standard)
	RetentionPolicy string
}

MemoryInput represents the input for creating a memory.

type MemoryResult

type MemoryResult struct {
	MemoryID     string
	NodesCreated int
	EdgesCreated int
	NodesDeleted int
	EdgesDeleted int
	Errors       []error
	// Trace contains timing data when TraceEnabled is true (Plan 015)
	Trace *OperationTrace
	// MemoriesSuperseded is the count of memories marked as Superseded (M4: Plan 021)
	MemoriesSuperseded int
}

MemoryResult reports the outcome of memory operations.

type Node

type Node = store.Node

Node is re-exported from store package

type OperationTrace added in v1.1.0

type OperationTrace struct {
	// Spans contains timing data for each stage of the operation
	Spans []Span `json:"spans"`

	// TotalDurationMs is the total elapsed time for the operation in milliseconds
	TotalDurationMs int64 `json:"totalDurationMs"`
}

OperationTrace captures timing and performance data for a Cognify or Search operation. This structure is stable and versioned to support downstream consumers.

type PruneOptions

type PruneOptions struct {
	// MaxAgeDays prunes nodes older than this many days (based on decay basis).
	// If zero, this criterion is not used.
	MaxAgeDays int

	// MinDecayScore prunes nodes with decay score below this threshold.
	// If zero, this criterion is not used.
	// Score is calculated using current decay settings.
	MinDecayScore float64

	// DryRun reports what would be pruned without actually deleting.
	DryRun bool

	// PruneSuperseded enables pruning of Superseded memories (M5: Plan 021, default: true)
	PruneSuperseded bool

	// SupersededAgeDays only prunes Superseded memories older than this (M5: Plan 021, default: 30)
	SupersededAgeDays int
}

PruneOptions configures the Prune() method

type PruneResult

type PruneResult struct {
	NodesEvaluated int      // Total number of nodes considered
	NodesPruned    int      // Number of nodes deleted
	EdgesPruned    int      // Number of edges deleted (via cascade)
	NodeIDs        []string // IDs of pruned nodes (for verification)
	// SupersededMemoriesPruned is the count of Superseded memories pruned (M5: Plan 021)
	SupersededMemoriesPruned int
	// MemoriesEvaluated is the total number of memories considered for pruning (M5: Plan 021)
	MemoriesEvaluated int
}

PruneResult reports the outcome of a Prune() operation

type RetentionPolicyDef added in v1.5.0

type RetentionPolicyDef struct {
	HalfLifeDays int  // Decay half-life in days (0 = no decay for permanent)
	Prunable     bool // Whether memories with this policy can be pruned
}

RetentionPolicyDef defines the parameters for a retention policy (M6: Plan 021)

type SearchOptions

type SearchOptions = search.SearchOptions

SearchOptions is re-exported from search package

type SearchResponse added in v1.1.0

type SearchResponse struct {
	Results []search.SearchResult // The search results
	Trace   *OperationTrace       // Timing data (populated when SearchOptions.TraceEnabled is true)
}

SearchResponse wraps search results with optional timing trace

type SearchResult

type SearchResult = search.SearchResult

SearchResult is re-exported from search package

type SearchType

type SearchType = search.SearchType

SearchType is re-exported from search package

type Span added in v1.1.0

type Span struct {
	// Name identifies the operation stage (see Span documentation for stable names)
	Name string `json:"name"`

	// DurationMs is the elapsed time for this span in milliseconds
	DurationMs int64 `json:"durationMs"`

	// OK indicates whether the span completed successfully
	OK bool `json:"ok"`

	// Error contains error message if OK is false (optional)
	Error string `json:"error,omitempty"`

	// ErrorType classifies the error for metrics (added in Plan 016 M3)
	ErrorType string `json:"errorType,omitempty"`

	// Counters provides additional metrics for the span (optional)
	// Example keys: "chunkCount", "nodeUpserts", "edgeUpserts", "resultsReturned"
	Counters map[string]int64 `json:"counters,omitempty"`
}

Span represents a single timed stage within an operation. Stage names are stable and documented:

  • "chunk": Text chunking
  • "embed": Embedding generation
  • "extract": Entity/relationship extraction
  • "write-graph": Graph database writes
  • "write-vector": Vector store writes
  • "search-vector": Vector similarity search
  • "search-expand": Graph traversal/expansion

type Stats

type Stats struct {
	NodeCount     int64
	EdgeCount     int64
	MemoryCount   int64
	BufferedDocs  int
	LastCognified time.Time
}

Stats reports basic telemetry about the knowledge graph

Jump to

Keyboard shortcuts

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