Table of Contents

Class MetadataCollection

Namespace
LMKit.Data
Assembly
LM-Kit.NET.dll

Represents a collection of metadata entries, each consisting of a key and a value. This class provides methods to manage metadata efficiently, allowing addition, removal, and key-based queries.

public sealed class MetadataCollection : IList<Metadata>, ICollection<Metadata>, IEnumerable<Metadata>, IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged
Inheritance
MetadataCollection
Implements
Inherited Members

Examples

Example: Create and populate metadata

using LMKit.Data;
using System;

var metadata = new MetadataCollection();
metadata.Add("Author", "John Doe");
metadata.Add("Title", "Sample Document");
metadata.Add("Category", "Technical");
metadata.Add("Year", "2024");

// Access values
if (metadata.TryGetValue("Author", out string author))
{
    Console.WriteLine($"Author: {author}");
}

// Check if key exists
bool hasCategory = metadata.ContainsKey("Category");
Console.WriteLine($"Has category: {hasCategory}");

// Iterate over all metadata
foreach (var item in metadata)
{
    Console.WriteLine($"{item.Key}: {item.Value}");
}

Example: Use metadata with DataSource for RAG

using LMKit.Model;
using LMKit.Data;
using LMKit.Retrieval;
using System;

LM model = LM.LoadFromModelID("llama-3.2-1b");

// Create metadata for a document
var docMetadata = new MetadataCollection();
docMetadata.Add("source", "company-handbook.pdf");
docMetadata.Add("department", "HR");
docMetadata.Add("lastUpdated", "2024-01-15");

// Create data source with metadata
var dataSource = DataSource.CreateInMemoryDataSource(
    "hr-documents",
    model,
    docMetadata);

// Import document content
dataSource.ImportText("Employee handbook content...", "handbook-section1");

Console.WriteLine($"DataSource '{dataSource.Identifier}' created with {dataSource.PartitionCount} partitions");

Example: Metadata with data binding

using LMKit.Data;
using System;
using System.Collections.Specialized;

var metadata = new MetadataCollection();

// Subscribe to collection changes (useful for UI binding)
metadata.CollectionChanged += (sender, e) =>
{
    if (e.Action == NotifyCollectionChangedAction.Add)
    {
        Console.WriteLine($"Added: {e.NewItems[0]}");
    }
};

metadata.Add("Status", "Active");  // Triggers CollectionChanged event

Remarks

The collection enforces unique keys; attempting to add a duplicate key will result in an exception.

This class implements INotifyCollectionChanged and INotifyPropertyChanged to support data binding scenarios.

Common Use Cases

  • Attaching metadata to documents in RAG systems
  • Storing document properties (author, date, category)
  • Filtering and querying data sources by metadata

Constructors

MetadataCollection()

Initializes a new instance of the MetadataCollection class that is empty.

MetadataCollection(MetadataCollection)

Initializes a new instance of the MetadataCollection class that contains the metadata entries copied from the specified MetadataCollection.

Properties

Count

Gets the number of metadata entries currently in the collection.

this[int]

Gets or sets the metadata entry at the specified zero-based index.

Methods

Add(Metadata)

Adds a metadata entry to the collection.

Add(MetadataCollection)

Adds all metadata entries from another MetadataCollection to this collection.

Add(KeyValuePair<string, string>)

Adds a metadata entry to the collection using a key-value pair.

Add(string, string)

Adds a metadata entry to the collection by specifying a key and a value.

AddOrReplace(Metadata)

Adds a metadata entry to the collection, or replaces an existing entry with the same key.

AddOrReplace(MetadataCollection)

Adds or replaces metadata entries from the specified collection.

Clear()

Removes all metadata entries from the collection.

Clone()

Creates a deep copy of the current MetadataCollection instance.

Contains(Metadata)

Determines whether the metadata collection contains the specified metadata entry.

Contains(MetadataCollection)

Determines whether the current collection contains all keys from the specified metadata collection.

Contains(MetadataCollection, bool)

Determines whether the current collection contains all keys from the specified metadata collection, optionally comparing values as well.

ContainsKey(string)

Determines whether a metadata entry with the specified key exists in the collection.

ElementAt(int)

Retrieves the metadata entry at the specified zero-based index.

Get(string)

Retrieves the metadata entry with the specified key.

GetEnumerator()

Returns an enumerator that iterates through the metadata entries in the collection.

IndexOf(Metadata)

Returns the zero-based index of the first occurrence of the specified metadata entry in the collection.

Insert(int, Metadata)

Inserts a metadata entry into the collection at the specified index.

Remove(Metadata)

Removes the specified metadata entry from the collection.

Remove(string)

Removes the first metadata entry with the specified key from the collection.

RemoveAt(int)

Removes the metadata entry at the specified index.

TryGet(string, out Metadata)

Attempts to retrieve the metadata entry with the specified key.

TryGetValue(string, out string)

Attempts to retrieve the value associated with the specified key.

Events

CollectionChanged

Occurs when the collection changes, such as when items are added, removed, replaced, or the entire list is refreshed.

PropertyChanged

Occurs when a property value changes.