GeneratorBase<T>

Base Classes · AiDotNet.RetrievalAugmentedGeneration.Generators

Base class for generator implementations providing common functionality and validation.

Implements: IGenerator<T>

Known derived types: ChatClientGenerator<T>

For Beginners

This is the foundation for all text generators in RAG systems.

It handles common tasks so you don’t have to repeat them:

  • Checking that inputs aren’t null or empty
  • Building prompts that combine the query and retrieved documents
  • Extracting citations from generated text
  • Creating properly formatted answers

When you create a new generator (like OpenAIGenerator or OnnxGenerator):

  1. Inherit from this class
  2. Set MaxContextTokens and MaxGenerationTokens in the constructor
  3. Implement GenerateCore with your specific generation logic
  4. Everything else (validation, prompt formatting, citations) is handled automatically

How It Works

This base class provides standard validation, prompt construction, and citation handling for generator implementations. It defines the template for implementing custom generation logic.

Properties

Property Summary
int MaxContextTokens { get; set; } Gets the maximum number of tokens this generator can process in a single request.
int MaxGenerationTokens { get; set; } Gets the maximum number of tokens this generator can generate in a response.

Methods

Method Summary
GroundedAnswer<T> GenerateGrounded(string query, IEnumerable<Document<T>> context) Generates a grounded answer using provided context documents.
Task<GroundedAnswer<T>> GenerateGroundedAsync(string query, IEnumerable<Document<T>> context, CancellationToken cancellationToken) Asynchronously generates a grounded answer using provided context documents, honoring cancellation.
Task<string> GenerateAsync(string prompt, CancellationToken cancellationToken) Asynchronously generates a text response based on a prompt, honoring cancellation.
string Generate(string prompt) Generates a text response based on a prompt with validation.