> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/microsoft/onnxruntime-genai/llms.txt
> Use this file to discover all available pages before exploring further.

# GeneratorParams

> Configure text generation parameters for ONNX Runtime GenAI

The `GeneratorParams` class controls the behavior of text generation, including search strategies, sampling parameters, and output constraints.

## Constructor

### GeneratorParams(Model model)

Creates a new generator parameters instance.

<ParamField path="model" type="Model" required>
  The model to create parameters for
</ParamField>

```csharp GeneratorParams.cs:15-18 theme={null}
using Microsoft.ML.OnnxRuntimeGenAI;

using Model model = new Model("/path/to/model");
using GeneratorParams generatorParams = new GeneratorParams(model);
```

## Search Option Methods

### SetSearchOption(string searchOption, double value)

Sets a numeric search option (e.g., temperature, top\_p, max\_length).

<ParamField path="searchOption" type="string" required>
  The name of the search option to set
</ParamField>

<ParamField path="value" type="double" required>
  The numeric value for the option
</ParamField>

```csharp GeneratorParams.cs:22-25 theme={null}
generatorParams.SetSearchOption("max_length", 1024);
generatorParams.SetSearchOption("temperature", 0.7);
generatorParams.SetSearchOption("top_p", 0.9);
generatorParams.SetSearchOption("top_k", 50);
```

### SetSearchOption(string searchOption, bool value)

Sets a boolean search option (e.g., do\_sample).

<ParamField path="searchOption" type="string" required>
  The name of the search option to set
</ParamField>

<ParamField path="value" type="bool" required>
  The boolean value for the option
</ParamField>

```csharp GeneratorParams.cs:27-30 theme={null}
generatorParams.SetSearchOption("do_sample", true);
```

### GetSearchNumber(string searchOption)

Retrieves the value of a numeric search option.

<ParamField path="searchOption" type="string" required>
  The name of the search option to retrieve
</ParamField>

**Returns:** `double` - The current value of the option

```csharp GeneratorParams.cs:37-41 theme={null}
double temperature = generatorParams.GetSearchNumber("temperature");
Console.WriteLine($"Temperature: {temperature}");
```

### GetSearchBool(string searchOption)

Retrieves the value of a boolean search option.

<ParamField path="searchOption" type="string" required>
  The name of the search option to retrieve
</ParamField>

**Returns:** `bool` - The current value of the option

```csharp GeneratorParams.cs:43-47 theme={null}
bool doSample = generatorParams.GetSearchBool("do_sample");
Console.WriteLine($"Do sample: {doSample}");
```

## Guidance Methods

### SetGuidance(string type, string data, bool enableFFTokens = false)

Sets structured output guidance (JSON schema or grammar) to constrain the model's output.

<ParamField path="type" type="string" required>
  The type of guidance: "json\_schema" or "lark\_grammar"
</ParamField>

<ParamField path="data" type="string" required>
  The guidance specification (JSON schema or LARK grammar string)
</ParamField>

<ParamField path="enableFFTokens" type="bool" default="false">
  Whether to enable fast-forward tokens
</ParamField>

```csharp GeneratorParams.cs:32-35 theme={null}
string jsonSchema = @"
{
  ""type"": ""object"",
  ""properties"": {
    ""name"": { ""type"": ""string"" },
    ""age"": { ""type"": ""number"" }
  },
  ""required"": [""name"", ""age""]
}
";

generatorParams.SetGuidance("json_schema", jsonSchema);
```

## Common Search Options

Here are the most commonly used search options:

### Generation Length

<ParamField path="min_length" type="int">
  Minimum number of tokens to generate (including prompt)
</ParamField>

<ParamField path="max_length" type="int">
  Maximum number of tokens to generate (including prompt)
</ParamField>

```csharp theme={null}
generatorParams.SetSearchOption("min_length", 10);
generatorParams.SetSearchOption("max_length", 1024);
```

### Sampling Parameters

<ParamField path="do_sample" type="bool">
  Enable random sampling. When `false`, uses greedy or beam search
</ParamField>

<ParamField path="temperature" type="double">
  Controls randomness. Higher values (e.g., 1.0) make output more random, lower values (e.g., 0.1) make it more deterministic
</ParamField>

<ParamField path="top_p" type="double">
  Nucleus sampling: only sample from tokens with cumulative probability mass of `top_p`
</ParamField>

<ParamField path="top_k" type="int">
  Only sample from the top K most likely tokens
</ParamField>

```csharp theme={null}
generatorParams.SetSearchOption("do_sample", true);
generatorParams.SetSearchOption("temperature", 0.7);
generatorParams.SetSearchOption("top_p", 0.9);
generatorParams.SetSearchOption("top_k", 50);
```

### Penalty Parameters

<ParamField path="repetition_penalty" type="double">
  Penalty for repeating tokens. Values > 1.0 discourage repetition
</ParamField>

```csharp theme={null}
generatorParams.SetSearchOption("repetition_penalty", 1.1);
```

### Beam Search Parameters

<ParamField path="num_beams" type="int">
  Number of beams for beam search. 1 means greedy search
</ParamField>

<ParamField path="num_return_sequences" type="int">
  Number of sequences to return (must be ≤ num\_beams)
</ParamField>

```csharp theme={null}
generatorParams.SetSearchOption("num_beams", 4);
generatorParams.SetSearchOption("num_return_sequences", 1);
```

### Batch Parameters

<ParamField path="batch_size" type="int">
  Batch size for processing multiple prompts
</ParamField>

```csharp theme={null}
generatorParams.SetSearchOption("batch_size", 1);
```

## Complete Examples

### Basic Generation Configuration

```csharp theme={null}
using Microsoft.ML.OnnxRuntimeGenAI;

using Model model = new Model("/path/to/model");
using GeneratorParams generatorParams = new GeneratorParams(model);

// Set basic parameters
generatorParams.SetSearchOption("max_length", 1024);
generatorParams.SetSearchOption("temperature", 0.7);
generatorParams.SetSearchOption("top_p", 0.9);
generatorParams.SetSearchOption("do_sample", true);

// Use with generator
using Generator generator = new Generator(model, generatorParams);
```

### Creative vs Deterministic Generation

```csharp theme={null}
// Creative generation (higher temperature, sampling enabled)
using GeneratorParams creativeParams = new GeneratorParams(model);
creativeParams.SetSearchOption("do_sample", true);
creativeParams.SetSearchOption("temperature", 1.0);
creativeParams.SetSearchOption("top_p", 0.95);
creativeParams.SetSearchOption("top_k", 50);

// Deterministic generation (low temperature or greedy)
using GeneratorParams deterministicParams = new GeneratorParams(model);
deterministicParams.SetSearchOption("do_sample", false); // Use greedy search
// Or use very low temperature:
// deterministicParams.SetSearchOption("do_sample", true);
// deterministicParams.SetSearchOption("temperature", 0.1);
```

### Beam Search Configuration

```csharp theme={null}
using GeneratorParams beamParams = new GeneratorParams(model);
beamParams.SetSearchOption("num_beams", 4);
beamParams.SetSearchOption("num_return_sequences", 1);
beamParams.SetSearchOption("max_length", 512);
```

### Structured Output with JSON Schema

```csharp examples/csharp/ModelChat/Program.cs:166-170 theme={null}
using Microsoft.ML.OnnxRuntimeGenAI;

using Model model = new Model("/path/to/model");
using GeneratorParams generatorParams = new GeneratorParams(model);

// Define JSON schema for structured output
string jsonSchema = @"
{
  ""type"": ""object"",
  ""properties"": {
    ""name"": { 
      ""type"": ""string",
      ""description"": ""Person's full name""
    },
    ""age"": { 
      ""type"": ""number",
      ""description"": ""Person's age in years""
    },
    ""email"": { 
      ""type"": ""string",
      ""description"": ""Email address""
    }
  },
  ""required"": [""name"", ""age""]
}
";

generatorParams.SetGuidance("json_schema", jsonSchema);

// The model will now generate valid JSON matching this schema
using Generator generator = new Generator(model, generatorParams);
```

### Complete Example from Source

```csharp examples/csharp/Common/Common.cs:134-159 theme={null}
using Microsoft.ML.OnnxRuntimeGenAI;

public static void SetSearchOptions(
    GeneratorParams generatorParams,
    GeneratorParamsArgs args,
    bool verbose)
{
    var options = new List<string>();
    
    // Set do_sample if provided
    if (args.do_sample.HasValue)
    {
        bool val = args.do_sample.Value;
        options.Add($"do_sample: {val}");
        generatorParams.SetSearchOption("do_sample", val);
    }
    
    // Set numeric options
    if (args.min_length.HasValue)
    {
        double val = Convert.ToDouble(args.min_length.Value);
        options.Add($"min_length: {val}");
        generatorParams.SetSearchOption("min_length", val);
    }
    
    if (args.max_length.HasValue)
    {
        double val = Convert.ToDouble(args.max_length.Value);
        options.Add($"max_length: {val}");
        generatorParams.SetSearchOption("max_length", val);
    }
    
    if (args.temperature.HasValue)
    {
        double val = args.temperature.Value;
        options.Add($"temperature: {val}");
        generatorParams.SetSearchOption("temperature", val);
    }
    
    if (args.top_p.HasValue)
    {
        double val = args.top_p.Value;
        options.Add($"top_p: {val}");
        generatorParams.SetSearchOption("top_p", val);
    }
    
    if (args.top_k.HasValue)
    {
        double val = Convert.ToDouble(args.top_k.Value);
        options.Add($"top_k: {val}");
        generatorParams.SetSearchOption("top_k", val);
    }
    
    if (args.repetition_penalty.HasValue)
    {
        double val = args.repetition_penalty.Value;
        options.Add($"repetition_penalty: {val}");
        generatorParams.SetSearchOption("repetition_penalty", val);
    }
    
    if (verbose)
    {
        Console.WriteLine("GeneratorParams: {" + string.Join(", ", options) + "}");
    }
}
```

## See Also

* [Generator](/api/csharp/generator) - Use parameters with generators
* [Model](/api/csharp/model) - Load models
* [Tokenizer](/api/csharp/tokenizer) - Encode and decode text
