Table of Contents

Class GeneratorBehaviour<TInput, TOutput, TFinalOutput, TChunk, TEvent, TParams, TRequest>

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

TInput

The type of input used for generation (e.g. text prompt, audio, image).

TOutput

The type of output exposed to Unity via onOutput (usually a wrapper or view model created from TFinalOutput).

TFinalOutput

The raw final output type produced by the model/provider (e.g. GeneratedText, GeneratedImage, ModerationResult).

TChunk

The type of each streamed chunk in streaming scenarios (e.g. text delta, audio buffer, partial image, etc.).

TEvent

The stream event type that wraps TChunk and TFinalOutput, used by the streaming pipeline. Must implement IGenerativeEvent<TChunk, TOutput>.

TParams

The type of generation parameters/configuration (model, options, stream flag, etc.).

TRequest

The provider-specific request type sent to the backend API.

Inheritance
object
GeneratorBehaviour<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

Api

CurrentRequest

public TRequest CurrentRequest { get; protected set; }

Property Value

TRequest

Model

public virtual Model Model { get; }

Property Value

Model

ModelType

public abstract ModelType ModelType { get; }

Property Value

ModelType

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

parameters TParams

CreateOutput(TFinalOutput)

protected abstract TOutput CreateOutput(TFinalOutput finalOutput)

Parameters

finalOutput TFinalOutput

Returns

TOutput

CreateRequest(TInput)

protected abstract TRequest CreateRequest(TInput prompt)

Parameters

prompt TInput

Returns

TRequest

CreateStreamAsync(TInput)

public UniTask<IRESTStream<TEvent>> CreateStreamAsync(TInput prompt)

Parameters

prompt TInput

Returns

UniTask<IRESTStream<TEvent>>

CreateStreamAsyncInternal(TRequest)

protected abstract UniTask<IRESTStream<TEvent>> CreateStreamAsyncInternal(TRequest request)

Parameters

request TRequest

Returns

UniTask<IRESTStream<TEvent>>

FinalizeStream(TChunk, UsageMetadata)

protected virtual void FinalizeStream(TChunk finalStream, UsageMetadata usage = null)

Parameters

finalStream TChunk
usage UsageMetadata

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

prompt TInput

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

prompt TInput

Returns

UniTask<TFinalOutput>

GenerateAsyncInternal(TRequest)

protected abstract UniTask<TFinalOutput> GenerateAsyncInternal(TRequest request)

Parameters

request TRequest

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

chunk TChunk

OnStatusChanged(ItemStatus)

public void OnStatusChanged(ItemStatus status)

Parameters

status ItemStatus