Class GeneratorBehaviour<TInput, TOutput, TFinalOutput, TChunk, TEvent, TParams, TRequest>
- Namespace
- Glitch9.AIDevKit.Generators
Base class for AI components that generate output based on input. Handles configuration, queuing, streaming, and final output events.
public abstract class GeneratorBehaviour<TInput, TOutput, TFinalOutput, TChunk, TEvent, TParams, TRequest> : AIBehaviour, IErrorHandler, IUsageHandler, IUniTaskQueueEventListener where TFinalOutput : IGeneratedOutput where TEvent : IGenerativeEvent<TChunk, TFinalOutput> where TParams : IGenerativeParameters where TRequest : IGenerativeRequest
Type Parameters
TInputThe type of input used for generation (e.g. text prompt, audio, image).
TOutputThe type of output exposed to Unity via onOutput (usually a wrapper or view model created from
TFinalOutput).TFinalOutputThe raw final output type produced by the model/provider (e.g.
GeneratedText,GeneratedImage,ModerationResult).TChunkThe type of each streamed chunk in streaming scenarios (e.g. text delta, audio buffer, partial image, etc.).
TEventThe stream event type that wraps
TChunkandTFinalOutput, used by the streaming pipeline. Must implement IGenerativeEvent<TChunk, TOutput>.TParamsThe type of generation parameters/configuration (model, options, stream flag, etc.).
TRequestThe provider-specific request type sent to the backend API.
- Inheritance
-
objectGeneratorBehaviour<TInput, TOutput, TFinalOutput, TChunk, TEvent, TParams, TRequest>
- Implements
-
IUniTaskQueueEventListener
- Derived
- Inherited Members
Fields
onOutput
public UnityEvent<TOutput> onOutput
Field Value
- UnityEvent<TOutput>
onStatusChanged
public UnityEvent<ItemStatus> onStatusChanged
Field Value
- UnityEvent<ItemStatus>
onStream
public DeltaEvent<TChunk> onStream
Field Value
- DeltaEvent<TChunk>
Properties
Api
Selected AI model's provider API.
public override Api Api { get; }
Property Value
CurrentRequest
public TRequest CurrentRequest { get; protected set; }
Property Value
- TRequest
Model
public virtual Model Model { get; }
Property Value
ModelType
public abstract ModelType ModelType { get; }
Property Value
Parameters
public TParams Parameters { get; protected set; }
Property Value
- TParams
SaveFolderPath
public string SaveFolderPath { get; set; }
Property Value
- string
SaveOutputs
public bool SaveOutputs { get; set; }
Property Value
- bool
Stream
public bool Stream { get; set; }
Property Value
- bool
output
[Obsolete("Use onOutput instead.")]
public UnityEvent<TOutput> output { get; }
Property Value
- UnityEvent<TOutput>
Methods
Awake()
protected override void Awake()
Configure(TParams)
Configures the generator with the specified parameters. It will overwrite the existing parameters including the serialized parameters. Overwritten parameters will not be saved(serialized).
public void Configure(TParams parameters)
Parameters
parametersTParams
CreateOutput(TFinalOutput)
protected abstract TOutput CreateOutput(TFinalOutput finalOutput)
Parameters
finalOutputTFinalOutput
Returns
- TOutput
CreateRequest(TInput)
protected abstract TRequest CreateRequest(TInput prompt)
Parameters
promptTInput
Returns
- TRequest
CreateStreamAsync(TInput)
public UniTask<IRESTStream<TEvent>> CreateStreamAsync(TInput prompt)
Parameters
promptTInput
Returns
- UniTask<IRESTStream<TEvent>>
CreateStreamAsyncInternal(TRequest)
protected abstract UniTask<IRESTStream<TEvent>> CreateStreamAsyncInternal(TRequest request)
Parameters
requestTRequest
Returns
- UniTask<IRESTStream<TEvent>>
FinalizeStream(TChunk, UsageMetadata)
protected virtual void FinalizeStream(TChunk finalStream, UsageMetadata usage = null)
Parameters
finalStreamTChunkusageUsageMetadata
Generate(TInput)
Generates output based on the provided input prompt. This method enqueues the generation task and triggers the onSendRequest event. It should be called to start the generation process.
public void Generate(TInput prompt)
Parameters
promptTInput
GenerateAsync(TInput)
Asynchronously generates output based on the provided input prompt. This method should be overridden in derived classes to implement the actual generation logic. It returns a UniTask that resolves to the generated output. The onReceiveGeneratedContent event is triggered with the generated output once the task completes.
public UniTask<TFinalOutput> GenerateAsync(TInput prompt)
Parameters
promptTInput
Returns
- UniTask<TFinalOutput>
GenerateAsyncInternal(TRequest)
protected abstract UniTask<TFinalOutput> GenerateAsyncInternal(TRequest request)
Parameters
requestTRequest
Returns
- UniTask<TFinalOutput>
InitializeAsync()
Initialize the component asynchronously. This method must be called before using the component.
public override UniTask InitializeAsync()
Returns
- UniTask
OnChunkReceived(TChunk)
protected virtual void OnChunkReceived(TChunk chunk)
Parameters
chunkTChunk
OnStatusChanged(ItemStatus)
public void OnStatusChanged(ItemStatus status)
Parameters
statusItemStatus