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
namestringThe name of this compressor.
tokenCounterFunc<string, int>Optional custom token counter function.
Properties
Name
Gets the name of this compressor implementation.
public string Name { get; }
Property Value
Methods
Compress(string, CompressionOptions?)
Compresses a prompt to reduce its token count.
public string Compress(string prompt, CompressionOptions? options = null)
Parameters
promptstringThe prompt string to compress.
optionsCompressionOptionsOptions 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
promptstringThe prompt string to compress.
optionsCompressionOptionsOptions controlling compression behavior.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
CompressCore(string, CompressionOptions)
Core compression logic to be implemented by derived classes.
protected abstract string CompressCore(string prompt, CompressionOptions options)
Parameters
promptstringThe validated prompt to compress.
optionsCompressionOptionsThe 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
promptstringThe prompt string to compress.
optionsCompressionOptionsOptions 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
textstringThe 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
promptstringThe 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
promptstringThe 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
promptstringcodeBlocksDictionary<string, string>
Returns
ReplaceVariablesWithPlaceholders(string, Dictionary<string, string>)
Replaces variables with placeholders to protect them during compression.
protected string ReplaceVariablesWithPlaceholders(string prompt, Dictionary<string, string> variables)
Parameters
promptstringThe prompt to process.
variablesDictionary<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
promptstringcodeBlocksDictionary<string, string>
Returns
RestoreVariables(string, Dictionary<string, string>)
Restores variables from placeholders after compression.
protected string RestoreVariables(string prompt, Dictionary<string, string> variables)
Parameters
promptstringThe compressed prompt with placeholders.
variablesDictionary<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
promptstringThe prompt to validate.