entity

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const ExtractionPrompt = `` /* 454-byte string literal not displayed */

ExtractionPrompt is the system prompt for entity extraction. With structured output, we don't need to specify the JSON format.

Variables

View Source
var (
	ErrEntityNotFound    = errors.New("entity not found")
	ErrEmptyName         = errors.New("entity name cannot be empty")
	ErrInvalidType       = errors.New("invalid entity type")
	ErrSelfMerge         = errors.New("cannot merge entity with itself")
	ErrEmbeddingRequired = errors.New("embedding provider required for search")
	ErrEmptyQuery        = errors.New("search query cannot be empty")
	ErrEmptySourceID     = errors.New("source ID is required")
)

Common errors returned by the entity engine.

ValidEntityTypes defines valid entity types.

Functions

func ToEntityType

func ToEntityType(s string) types.EntityType

ToEntityType converts a string to types.EntityType.

Types

type CreateOpts

type CreateOpts struct {
	Aliases    []string          // Alternative names
	Summary    string            // Initial summary
	Attributes map[string]string // Structured facts
	Metadata   map[string]string // Additional metadata
}

CreateOpts contains options for creating an entity.

type CreateResult

type CreateResult struct {
	Entity  *types.Entity `json:"entity"`
	Created bool          `json:"created"` // false if entity already existed
}

CreateResult contains the result of entity creation.

type Engine

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

Engine implements the entity memory logic layer. It orchestrates storage, embedding, and extraction operations.

func NewEngine

func NewEngine(store storage.Backend, emb embedding.Provider, cfg *config.EntityConfig) (*Engine, error)

NewEngine creates a new entity engine.

func (*Engine) AddAlias

func (e *Engine) AddAlias(ctx context.Context, namespace, entityID, alias string) error

AddAlias registers a new alias for an entity.

func (*Engine) AddRelationship

func (e *Engine) AddRelationship(ctx context.Context, namespace, sourceID, targetID, relationType string, opts *RelationshipOpts) (*types.EntityRelationship, error)

AddRelationship creates or updates a relationship between entities.

func (*Engine) CompleteExtraction

func (e *Engine) CompleteExtraction(ctx context.Context, itemID int64, status string) error

CompleteExtraction marks an extraction queue item as completed or failed.

func (*Engine) Create

func (e *Engine) Create(ctx context.Context, namespace, name string, entityType types.EntityType, opts *CreateOpts) (*CreateResult, error)

Create creates a new entity or returns existing one with same name.

func (*Engine) Delete

func (e *Engine) Delete(ctx context.Context, namespace, entityID string) error

Delete removes an entity and all associated data.

func (*Engine) DequeueExtraction

func (e *Engine) DequeueExtraction(ctx context.Context, batchSize int) ([]*types.ExtractionQueueItem, error)

DequeueExtraction retrieves items from the extraction queue for processing.

func (*Engine) EnqueueExtraction

func (e *Engine) EnqueueExtraction(ctx context.Context, namespace, sourceType, sourceID, content string) error

EnqueueExtraction adds content to the extraction queue.

func (*Engine) ExtractionQueueStats

func (e *Engine) ExtractionQueueStats(ctx context.Context) (*storage.ExtractionQueueStats, error)

ExtractionQueueStats returns statistics about the extraction queue.

func (*Engine) Get

func (e *Engine) Get(ctx context.Context, namespace, entityID string) (*types.Entity, error)

Get retrieves an entity by ID.

func (*Engine) GetByName

func (e *Engine) GetByName(ctx context.Context, namespace, name string) (*types.Entity, error)

GetByName retrieves an entity by its canonical name.

func (*Engine) GetMentions

func (e *Engine) GetMentions(ctx context.Context, entityID string, limit int) ([]*types.EntityMention, error)

GetMentions retrieves recent mentions of an entity.

func (*Engine) GetRelationships

func (e *Engine) GetRelationships(ctx context.Context, namespace, entityID string, opts *GetRelationshipsOpts) ([]*types.EntityRelationship, error)

GetRelationships retrieves relationships for an entity.

func (*Engine) List

func (e *Engine) List(ctx context.Context, namespace string, opts *ListOpts) (*ListResult, error)

List returns entities matching the criteria.

func (*Engine) Merge

func (e *Engine) Merge(ctx context.Context, namespace, sourceID, targetID string) (*MergeResult, error)

Merge combines two entities, moving all data from source to target.

func (*Engine) Query

func (e *Engine) Query(ctx context.Context, namespace, nameOrAlias string, mentionLimit int) (*types.EntityQueryResponse, error)

Query retrieves an entity with its relationships and recent mentions.

func (*Engine) RecordMention

func (e *Engine) RecordMention(ctx context.Context, namespace, entityID, sourceType, sourceID string, opts *MentionOpts) error

RecordMention records a mention of an entity.

func (*Engine) Resolve

func (e *Engine) Resolve(ctx context.Context, namespace, nameOrAlias string) (*types.Entity, error)

Resolve finds an entity by name or alias.

func (*Engine) Search

func (e *Engine) Search(ctx context.Context, namespace, query string, opts *SearchOpts) (*SearchResult, error)

Search performs semantic search across entity summaries.

func (*Engine) Update

func (e *Engine) Update(ctx context.Context, namespace, entityID string, opts UpdateOpts) (*types.Entity, error)

Update modifies an existing entity.

type EntityExtractor added in v0.2.0

type EntityExtractor interface {
	Extract(ctx context.Context, text string) (*ExtractionResult, error)
}

EntityExtractor defines the interface for entity extraction.

type ExtractedEntity

type ExtractedEntity struct {
	Name       string            `json:"name"`
	Type       string            `json:"type"`
	Aliases    []string          `json:"aliases,omitempty"`
	Attributes map[string]string `json:"attributes,omitempty"`
	Confidence float64           `json:"confidence"`
}

ExtractedEntity represents an entity extracted from text by the LLM.

type ExtractedRelationship

type ExtractedRelationship struct {
	SourceName   string  `json:"source_name"`
	TargetName   string  `json:"target_name"`
	RelationType string  `json:"relation_type"`
	Description  string  `json:"description,omitempty"`
	Confidence   float64 `json:"confidence"`
}

ExtractedRelationship represents a relationship between extracted entities.

type ExtractionEnqueuerAdapter

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

ExtractionEnqueuerAdapter adapts the storage backend to the ExtractionEnqueuer interface expected by conversation and knowledge engines.

func NewExtractionEnqueuerAdapter

func NewExtractionEnqueuerAdapter(store storage.Backend) *ExtractionEnqueuerAdapter

NewExtractionEnqueuerAdapter creates a new adapter that wraps a storage backend.

func (*ExtractionEnqueuerAdapter) EnqueueForExtraction

func (a *ExtractionEnqueuerAdapter) EnqueueForExtraction(ctx context.Context, namespace, sourceType, sourceID, content string) error

EnqueueForExtraction queues content for entity extraction. Implements the interface expected by conversation.ExtractionEnqueuer and knowledge.ExtractionEnqueuer.

type ExtractionResult

type ExtractionResult struct {
	Entities      []ExtractedEntity       `json:"entities"`
	Relationships []ExtractedRelationship `json:"relationships,omitempty"`
	SourceText    string                  `json:"-"` // Original text (not serialized)
}

ExtractionResult contains the result of entity extraction.

type Extractor

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

Extractor extracts entities from text using an LLM via the iris SDK.

func NewExtractor

func NewExtractor(cfg *config.Config) (*Extractor, error)

NewExtractor creates a new entity extractor using the iris SDK.

func (*Extractor) Extract

func (e *Extractor) Extract(ctx context.Context, text string) (*ExtractionResult, error)

Extract extracts entities from the given text. Uses structured output (JSON Schema) when supported by the provider for reliable parsing.

type GetRelationshipsOpts

type GetRelationshipsOpts struct {
	RelationType *string                     // Filter by type
	Direction    types.RelationshipDirection // Direction filter
}

GetRelationshipsOpts contains options for retrieving relationships.

type ListOpts

type ListOpts struct {
	EntityType *types.EntityType  // Filter by type
	SortBy     types.EntitySortBy // Sort order
	Cursor     string             // Pagination cursor
	Limit      int                // Max results (0 = default)
}

ListOpts contains options for listing entities.

type ListResult

type ListResult struct {
	Entities   []*types.Entity `json:"entities"`
	NextCursor string          `json:"next_cursor,omitempty"`
	Count      int             `json:"count"`
}

ListResult contains the result of listing entities.

type MentionOpts

type MentionOpts struct {
	Context string // Surrounding text
	Snippet string // Exact mention text
}

MentionOpts contains options for recording a mention.

type MergeResult

type MergeResult struct {
	KeptEntity          *types.Entity `json:"kept_entity"`
	MergedMentions      int           `json:"merged_mentions"`
	MergedRelationships int           `json:"merged_relationships"`
}

MergeResult contains the result of entity merging.

type QueueProcessor

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

QueueProcessor processes the entity extraction queue asynchronously.

func NewQueueProcessor

func NewQueueProcessor(
	store storage.Backend,
	extractor EntityExtractor,
	resolver *Resolver,
	engine *Engine,
	cfg *config.EntityConfig,
) *QueueProcessor

NewQueueProcessor creates a new extraction queue processor.

func (*QueueProcessor) IsRunning

func (q *QueueProcessor) IsRunning() bool

IsRunning returns whether the processor is running.

func (*QueueProcessor) ProcessSingle

func (q *QueueProcessor) ProcessSingle(ctx context.Context, namespace, content, sourceType, sourceID string) (*ExtractionResult, error)

ProcessSingle processes a single text for entity extraction immediately. This is useful for testing or for on-demand extraction.

func (*QueueProcessor) Start

func (q *QueueProcessor) Start(ctx context.Context)

Start begins processing the extraction queue in the background.

func (*QueueProcessor) Stop

func (q *QueueProcessor) Stop()

Stop signals the processor to stop.

type RelationshipOpts

type RelationshipOpts struct {
	Description string  // Free-text description
	Confidence  float64 // 0.0-1.0 extraction confidence
}

RelationshipOpts contains options for creating a relationship.

type ResolvedEntity

type ResolvedEntity struct {
	Entity     *types.Entity // The existing entity (nil if no match)
	Confidence float64       // Resolution confidence (0-1)
	MatchType  string        // "exact", "alias", "fuzzy", or "new"
}

ResolvedEntity represents the result of resolving a name.

type Resolver

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

Resolver handles name resolution for entity deduplication. It implements blocking and fuzzy matching to efficiently resolve extracted entity names to existing entities.

func NewResolver

func NewResolver(store storage.Backend, fuzzyThreshold float64) *Resolver

NewResolver creates a new name resolver.

func (*Resolver) Resolve

func (r *Resolver) Resolve(ctx context.Context, namespace, name string) (*ResolvedEntity, error)

Resolve attempts to match an extracted entity name to an existing entity. It uses a multi-phase approach: 1. Exact name match 2. Alias match 3. Blocked candidates with fuzzy matching

func (*Resolver) ResolveExtracted

func (r *Resolver) ResolveExtracted(ctx context.Context, namespace string, extracted *ExtractedEntity) (*ResolvedEntity, error)

ResolveExtracted resolves an extracted entity and returns the best match or creates info for a new entity.

type SearchMode

type SearchMode string

SearchMode defines the search strategy.

const (
	// SearchModeVector uses pure vector similarity search (default).
	SearchModeVector SearchMode = "vector"
	// SearchModeHybrid combines vector and text search with RRF.
	SearchModeHybrid SearchMode = "hybrid"
	// SearchModeText uses pure full-text search (BM25).
	SearchModeText SearchMode = "text"
)

type SearchOpts

type SearchOpts struct {
	EntityType *types.EntityType // Filter by type
	TopK       int               // Number of results (0 = default 10)
	MinScore   float64           // Minimum similarity score (0-1)
	SearchMode SearchMode        // Search mode: "vector" (default), "hybrid", or "text"
	Alpha      float64           // Hybrid search weight: 0=pure text, 1=pure vector, 0.5=equal (default: 0.5)
}

SearchOpts contains options for entity search.

type SearchResult

type SearchResult struct {
	Results    []*types.EntityResult `json:"results"`
	Query      string                `json:"query"`
	TotalFound int                   `json:"total_found"`
}

SearchResult contains search results.

type UpdateOpts

type UpdateOpts struct {
	Summary    *string           // New summary (nil = don't update)
	Attributes map[string]string // Merge with existing attributes
	Metadata   map[string]string // Merge with existing metadata
}

UpdateOpts contains options for updating an entity.

Jump to

Keyboard shortcuts

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