The Java API is delivered by the ai.onnxruntime.genai Java package. Package publication is pending. To build the package from source, see the [build from source guide](../howto/build-from-source.md).
The `SimpleGenAI` class provides a simple usage example of the GenAI API. It works with a model that generates text based on a prompt, processing a single prompt at a time.
Usage:
Create an instance of the class with the path to the model. The path should also contain the GenAI configuration files.
```java
SimpleGenAI genAI = new SimpleGenAI(folderPath);
```
Call createGeneratorParams with the prompt text.
Set any other search options via the GeneratorParams object as needed using `setSearchOption`.
The listener is used as a callback mechanism so that tokens can be used as they are generated. Create a class that implements the `Consumer<String>` interface and provide an instance of that class as the `listener` argument.
### Constructor
```java
public SimpleGenAI(String modelPath) throws GenAIException
```
#### Throws
`GenAIException`- on failure.
### Generate Method
Generate text based on the prompt and settings in GeneratorParams.
NOTE: This only handles a single sequence of input (i.e. a single prompt which equates to batch size of 1).
```java
public String generate(GeneratorParams generatorParams, Consumer<String> listener) throws GenAIException
```
#### Parameters
-`generatorParams`: the prompt and settings to run the model with.
-`listener`: optional callback for tokens to be provided as they are generated.
NOTE: Token generation will be blocked until the listener's `accept` method returns.
#### Throws
`GenAIException`- on failure.
#### Returns
The generated text.
#### Example
```java
SimpleGenAI generator = new SimpleGenAI(modelPath);
GeneratorParams params = generator.createGeneratorParams("What's 6 times 7?");
String result = generator.generate(params, listener);
logger.info("Result: " + result);
```
### createGenerateParams Method
Create the generator parameters and add the prompt text. The user can set other search options via the GeneratorParams object prior to running `generate`.
```java
public GeneratorParams createGeneratorParams(String prompt) throws GenAIException
```
#### Parameters
-`prompt`: the prompt text to encode.
#### Throws
`GenAIException`- on failure.
#### Returns
The generator parameters.
## Exception Class
An exception which contains the error message and code produced by the native layer.
### Constructor
```java
public GenAIException(String message)
```
#### Example
```java
catch (GenAIException e) {
throw new GenAIException("Token generation loop failed.", e);
}
```
## Model class
### Constructor
```java
Model(String modelPath)
```
### Create Tokenizer Method
Creates a Tokenizer instance for this model. The model contains the configuration information that determines the tokenizer to use.
```java
public Tokenizer createTokenizer() throws GenAIException
```
#### Throws
`GenAIException`- if the call to the GenAI native API fails
#### Returns
The tokenizer instance.
### Generate Method
```java
public Sequences generate(GeneratorParams generatorParams) throws GenAIException
```
#### Parameters
-`generatorParams`: the generator parameters.
#### Throws
`GenAIException`- if the call to the GenAI native API fails.
Tensor tensor = new Tensor(data, shape, Tensor.ElementType.float32);
```
## GeneratorParams class
The `GeneratorParams` class represents the parameters used for generating sequences with a model. Set the prompt using setInput, and any other search options using setSearchOption.
### Create a Generator Params object
```java
GeneratorParams params = new GeneratorParams(model);
```
### setSearchOption Method
```java
public void setSearchOption(String optionName, double value) throws GenAIException
```
#### Throws
`GenAIException`
#### Example
Set search option to limit the model generation length.