Table of Contents

Class CompositeCompressor

Namespace
AiDotNet.PromptEngineering.Compression
Assembly
AiDotNet.dll

Compressor that chains multiple compressors together in sequence.

public class CompositeCompressor : PromptCompressorBase, IPromptCompressor
Inheritance
CompositeCompressor
Implements
Inherited Members

Remarks

This compressor applies multiple compression strategies in sequence, with each compressor working on the output of the previous one. This allows combining the strengths of different compression approaches.

For Beginners: Combines multiple compressors for better results.

Example:

// Create a pipeline of compressors
var compressor = new CompositeCompressor(
    new RedundancyRemovalCompressor(),    // First: remove redundant phrases
    new SentenceCompressor(),             // Then: simplify sentences
    new StopWordRemovalCompressor()       // Finally: remove stop words
);

// Apply all compressors in sequence
var result = compressor.Compress(longPrompt);
// Result: Prompt has been compressed by all three methods

Order matters:

  • Put rule-based compressors first
  • Put aggressive compressors last
  • Each compressor should work well with the output of the previous one

Constructors

CompositeCompressor(params IPromptCompressor[])

Initializes a new instance of the CompositeCompressor class.

public CompositeCompressor(params IPromptCompressor[] compressors)

Parameters

compressors IPromptCompressor[]

The compressors to apply in sequence.

CompositeCompressor(IEnumerable<IPromptCompressor>, Func<string, int>?)

Initializes a new instance of the CompositeCompressor class.

public CompositeCompressor(IEnumerable<IPromptCompressor> compressors, Func<string, int>? tokenCounter = null)

Parameters

compressors IEnumerable<IPromptCompressor>

The compressors to apply in sequence.

tokenCounter Func<string, int>

Optional custom token counter function.

Properties

Compressors

Gets the list of compressors in this composite.

public IReadOnlyList<IPromptCompressor> Compressors { get; }

Property Value

IReadOnlyList<IPromptCompressor>

Methods

CompressAsync(string, CompressionOptions?, CancellationToken)

Compresses the prompt asynchronously by applying all compressors in sequence.

public override Task<string> CompressAsync(string prompt, CompressionOptions? options = null, CancellationToken cancellationToken = default)

Parameters

prompt string
options CompressionOptions
cancellationToken CancellationToken

Returns

Task<string>

CompressCore(string, CompressionOptions)

Compresses the prompt by applying all compressors in sequence.

protected override string CompressCore(string prompt, CompressionOptions options)

Parameters

prompt string
options CompressionOptions

Returns

string

CreateAggressivePipeline()

Creates a composite compressor with an aggressive pipeline for maximum compression.

public static CompositeCompressor CreateAggressivePipeline()

Returns

CompositeCompressor

A composite compressor with all available compression techniques.

CreateStandardPipeline()

Creates a composite compressor with a standard pipeline for general use.

public static CompositeCompressor CreateStandardPipeline()

Returns

CompositeCompressor

A composite compressor with redundancy, sentence, and light stop word removal.