Documentation
¶
Overview ¶
Package indexing implements a LangChain-style indexing module that tracks which documents have already been indexed, so unchanged content is not re-written to a vector store on repeat runs. It mirrors langchain's langchain.indexes module.
Index ¶
- func ContentHash(doc Document) string
- type DocHash
- type Document
- type InMemoryRecordManager
- func (m *InMemoryRecordManager) Delete(_ context.Context, namespace string, ids []string) error
- func (m *InMemoryRecordManager) Exists(_ context.Context, namespace string, id string) (bool, error)
- func (m *InMemoryRecordManager) GetHash(_ context.Context, namespace string, id string) (string, error)
- func (m *InMemoryRecordManager) Keys(_ context.Context, namespace string) ([]string, error)
- func (m *InMemoryRecordManager) Update(_ context.Context, namespace string, docs []DocHash) error
- type InMemoryVectorStore
- type IndexResult
- type Indexer
- type RecordManager
- type VectorStore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContentHash ¶
ContentHash computes a stable hash of a document's content and metadata. The metadata keys are sorted so the hash is order-independent.
Types ¶
type DocHash ¶
type DocHash struct {
// ID of the document.
ID string
// ContentHash is the hash of the document content and metadata.
ContentHash string
}
DocHash ties a document id to its content hash.
type Document ¶
type Document struct {
// ID is an optional stable identifier for the document.
ID string
// PageContent is the text of the document.
PageContent string
// Metadata holds arbitrary metadata used in the content hash.
Metadata map[string]any
}
Document is a single document to be indexed.
type InMemoryRecordManager ¶
type InMemoryRecordManager struct {
// contains filtered or unexported fields
}
InMemoryRecordManager is a thread-safe RecordManager backed by a map.
func NewInMemoryRecordManager ¶
func NewInMemoryRecordManager() *InMemoryRecordManager
NewInMemoryRecordManager builds an empty in-memory record manager.
func (*InMemoryRecordManager) Exists ¶
func (m *InMemoryRecordManager) Exists(_ context.Context, namespace string, id string) (bool, error)
Exists implements RecordManager.
func (*InMemoryRecordManager) GetHash ¶
func (m *InMemoryRecordManager) GetHash(_ context.Context, namespace string, id string) (string, error)
GetHash implements RecordManager.
type InMemoryVectorStore ¶
type InMemoryVectorStore struct {
// contains filtered or unexported fields
}
InMemoryVectorStore is a simple VectorStore for tests.
func NewInMemoryVectorStore ¶
func NewInMemoryVectorStore() *InMemoryVectorStore
NewInMemoryVectorStore builds an empty in-memory vector store.
func (*InMemoryVectorStore) AddDocuments ¶
AddDocuments implements VectorStore.
func (*InMemoryVectorStore) Count ¶
func (s *InMemoryVectorStore) Count() int
Count returns the number of stored documents.
type IndexResult ¶
type IndexResult struct {
// Added documents newly indexed.
Added []Document
// Updated documents whose content changed.
Updated []Document
// Skipped documents already up to date.
Skipped []Document
// Deleted documents removed from the store.
Deleted []string
}
IndexResult reports the outcome of an indexing run.
type Indexer ¶
type Indexer struct {
// RecordManager tracks indexed documents.
RecordManager RecordManager
// Store is the destination vector store.
Store VectorStore
// Namespace separates different indexes.
Namespace string
// CleanupMode controls handling of documents no longer present. "incremental"
// deletes store entries whose ids are no longer in the source.
CleanupMode string
// WithID assigns ids to documents. If nil, the content hash is used as id.
WithID func(doc Document) string
}
Indexer coordinates document indexing against a RecordManager and a VectorStore.
type RecordManager ¶
type RecordManager interface {
// Update records the given document hashes as indexed.
Update(ctx context.Context, namespace string, docs []DocHash) error
// Exists reports whether a document id with the given content hash is
// already indexed.
Exists(ctx context.Context, namespace string, id string) (bool, error)
// GetHash returns the stored content hash for an id, or "" if absent.
GetHash(ctx context.Context, namespace string, id string) (string, error)
// Delete removes document ids from the index.
Delete(ctx context.Context, namespace string, ids []string) error
// Keys returns all indexed ids in the namespace.
Keys(ctx context.Context, namespace string) ([]string, error)
}
RecordManager persists the set of indexed document IDs and their content hashes for a given namespace.
type VectorStore ¶
type VectorStore interface {
// AddDocuments stores documents and returns their ids.
AddDocuments(ctx context.Context, docs []Document) ([]string, error)
// Delete removes documents by id.
Delete(ctx context.Context, ids []string) error
}
VectorStore is the minimal vector store interface the indexer writes to.