Class Generator
java.lang.Object
smile.onnx.genai.Generator
- All Implemented Interfaces:
AutoCloseable, Iterable<Integer>
Autoregressive token generator. Canonical loop:
generator.appendTokens(promptTokens);
try (var stream = tokenizer.createStream()) {
while (!generator.isDone()) {
generator.generateNextToken();
String chunk = stream.decode(generator.getLastToken(0));
}
}
Also implements IterableInteger> so callers can write
for (int token : generator).
-
Method Summary
Modifier and TypeMethodDescriptionvoidappendSequences(Sequences sequences) Appends encoded sequences as the prompt.voidappendTokens(int[] tokens) Appends prompt token ids.voidclose()voidGenerates the next token (computes logits and samples).Returns a copy of a named model input as aTensor.intgetLastToken(long index) Returns the last token in the sequence atindex.Returns a copy of a named model output as aTensor.int[]getSequence(long index) Returns a copy of the full token sequence atindex.booleanisDone()Returns whether generation has finished.iterator()static Generatorof(Model model, GeneratorParams params) Creates a generator from a model and search parameters.voidrewindTo(long newLength) Rewinds generation state to the given length.voidsetActiveAdapter(Adapters adapters, String adapterName) Activates a previously loaded LoRA adapter for this generator.voidsetInputs(NamedTensors namedTensors) Sets multimodal / multi-input tensors (e.g.voidsetModelInput(String name, Tensor tensor) Sets a single named model input tensor.longReturns the current token count in the generator state.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Method Details
-
of
Creates a generator from a model and search parameters.- Parameters:
model- the model (must outlive this generator).params- search parameters.- Returns:
- a new generator owned by the caller.
-
isDone
public boolean isDone()Returns whether generation has finished.- Returns:
truewhen no more tokens will be produced.
-
appendTokens
public void appendTokens(int[] tokens) Appends prompt token ids.- Parameters:
tokens- prompt tokens.
-
appendSequences
Appends encoded sequences as the prompt.- Parameters:
sequences- token sequences (typically fromTokenizer.encode(String)).
-
setModelInput
-
setInputs
Sets multimodal / multi-input tensors (e.g. fromMultiModalProcessor).- Parameters:
namedTensors- named inputs.
-
generateNextToken
public void generateNextToken()Generates the next token (computes logits and samples). -
tokenCount
public long tokenCount()Returns the current token count in the generator state.- Returns:
- token count.
-
rewindTo
public void rewindTo(long newLength) Rewinds generation state to the given length.- Parameters:
newLength- desired length in tokens after rewind.
-
getSequence
public int[] getSequence(long index) Returns a copy of the full token sequence atindex.- Parameters:
index- sequence index (usually0).- Returns:
- token ids including the prompt.
-
getLastToken
public int getLastToken(long index) Returns the last token in the sequence atindex.- Parameters:
index- sequence index (usually0).- Returns:
- last token id.
-
setActiveAdapter
Activates a previously loaded LoRA adapter for this generator.- Parameters:
adapters- adapter container.adapterName- adapter name passed toAdapters.load(String, String).
-
getInput
-
getOutput
-
iterator
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-