Table of Contents

Class PromptCompressorBase

Namespace
AiDotNet.PromptEngineering.Compression
Assembly
AiDotNet.dll

Provides a base implementation for prompt compressors with common functionality.

public abstract class PromptCompressorBase : IPromptCompressor
Inheritance
PromptCompressorBase
Implements
Derived
Inherited Members

Remarks

This abstract class implements the IPromptCompressor interface and provides common functionality for prompt compression strategies. It handles token counting, validation, and delegates to derived classes for the core compression logic.

For Beginners: This is the foundation that all prompt compressors build upon.

Think of it like a template for making prompts shorter:

  • It handles common tasks (counting tokens, checking inputs)
  • Specific compression methods fill in how they compress
  • This ensures all compressors work consistently

Derived classes just need to implement CompressCore to define their compression logic.

Constructors

PromptCompressorBase(string, Func<string, int>?)

Initializes a new instance of the PromptCompressorBase class.

protected PromptCompressorBase(string name, Func<string, int>? tokenCounter = null)

Parameters

name string

The name of this compressor.

tokenCounter Func<string, int>

Optional custom token counter function.

Properties

Name

Gets the name of this compressor implementation.

public string Name { get; }

Property Value

string

Methods

Compress(string, CompressionOptions?)

Compresses a prompt to reduce its token count.

public string Compress(string prompt, CompressionOptions? options = null)

Parameters

prompt string

The prompt string to compress.

options CompressionOptions

Options controlling compression behavior.

Returns

string

The compressed prompt string.

CompressAsync(string, CompressionOptions?, CancellationToken)

Compresses a prompt asynchronously.

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

Parameters

prompt string

The prompt string to compress.

options CompressionOptions

Options controlling compression behavior.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<string>

A task that resolves to the compressed prompt string.

CompressCore(string, CompressionOptions)

Core compression logic to be implemented by derived classes.

protected abstract string CompressCore(string prompt, CompressionOptions options)

Parameters

prompt string

The validated prompt to compress.

options CompressionOptions

The compression options.

Returns

string

The compressed prompt.

Remarks

For Implementers: This is where you implement your specific compression algorithm.

You don't need to:

  • Validate the prompt (already done)
  • Count tokens (use CountTokens helper if needed)
  • Handle null inputs (already validated)

Just focus on: Making the prompt shorter while preserving meaning.

CompressWithMetrics(string, CompressionOptions?)

Compresses a prompt and returns detailed metrics about the compression.

public CompressionResult CompressWithMetrics(string prompt, CompressionOptions? options = null)

Parameters

prompt string

The prompt string to compress.

options CompressionOptions

Options controlling compression behavior.

Returns

CompressionResult

A CompressionResult containing the compressed prompt and metrics.

CountTokens(string)

Counts tokens in the given text.

protected int CountTokens(string text)

Parameters

text string

The text to count tokens in.

Returns

int

The estimated token count.

Remarks

Uses custom token counter if provided, otherwise uses a simple word-based approximation (1 token ≈ 0.75 words for English).

ExtractCodeBlocks(string)

Extracts and preserves code blocks from a prompt.

protected Dictionary<string, string> ExtractCodeBlocks(string prompt)

Parameters

prompt string

The prompt to extract code blocks from.

Returns

Dictionary<string, string>

A dictionary mapping placeholder tokens to original code blocks.

ExtractVariables(string)

Extracts and preserves variables from a prompt (like {variable_name}).

protected Dictionary<string, string> ExtractVariables(string prompt)

Parameters

prompt string

The prompt to extract variables from.

Returns

Dictionary<string, string>

A dictionary mapping placeholder tokens to original variables.

ReplaceCodeBlocksWithPlaceholders(string, Dictionary<string, string>)

Replaces code blocks with placeholders to protect them during compression.

protected string ReplaceCodeBlocksWithPlaceholders(string prompt, Dictionary<string, string> codeBlocks)

Parameters

prompt string
codeBlocks Dictionary<string, string>

Returns

string

ReplaceVariablesWithPlaceholders(string, Dictionary<string, string>)

Replaces variables with placeholders to protect them during compression.

protected string ReplaceVariablesWithPlaceholders(string prompt, Dictionary<string, string> variables)

Parameters

prompt string

The prompt to process.

variables Dictionary<string, string>

The variable mappings.

Returns

string

The prompt with variables replaced by placeholders.

RestoreCodeBlocks(string, Dictionary<string, string>)

Restores code blocks from placeholders after compression.

protected string RestoreCodeBlocks(string prompt, Dictionary<string, string> codeBlocks)

Parameters

prompt string
codeBlocks Dictionary<string, string>

Returns

string

RestoreVariables(string, Dictionary<string, string>)

Restores variables from placeholders after compression.

protected string RestoreVariables(string prompt, Dictionary<string, string> variables)

Parameters

prompt string

The compressed prompt with placeholders.

variables Dictionary<string, string>

The variable mappings.

Returns

string

The prompt with variables restored.

ValidatePrompt(string)

Validates the prompt input.

protected virtual void ValidatePrompt(string prompt)

Parameters

prompt string

The prompt to validate.