You are working on a project that involves parsing YAML files for model configurations. You have come across a specific requirement where you need to add a new field to the YAML file called `disable_first_quantization`. This field should be added at the top level of the `model` section.

Write a function `add_disable_first_quantization` that takes a YAML string as input and returns a modified YAML string with the `disable_first_quantization` field added to the `model` section.

Function Signature: `def add_disable_first_quantization(yaml_string: str) -> str:`

**Input**
- `yaml_string` (1 <= `len(yaml_string)` <= 10^6): A valid YAML string representing a model configuration.

**Output**
- Returns a modified YAML string with the `disable_first_quantization` field added to the `model` section. The returned string should be properly indented and formatted.

**Note**
- The input YAML string is guaranteed to be valid and properly indented.
- The `model` section may contain other fields, but the new `disable_first_quantization` field should be added above them.
- The `model` section may or may not contain nested sections.
user
Sure, I can help you with that. Here's a Python function you can use:

```python
import yaml

def add_disable_first_quantization(yaml_string):
    data = yaml.safe_load(yaml_string)
    if "model" in data:
        model_data = data.get("model", {})
        # Check if "disable_first_quantization" already exists in the "model" section
        if "disable_first_quantization" not in model_data:
            model_data["disable_first_quantization"] = False
        else:
            # If it already exists, don't add it again
            return yaml_string
        # Otherwise, add it at the top level of the "model" section
        data["model"] = model_data
    else:
        # If "model" section does not exist, add it at the top level
        data["model"] = {"disable_first_quantization": False}
    return yaml.dump(data, default_flow_style=False)
```

This function uses the `safe_load` function from the `yaml` module to parse the input YAML string. It then checks if the "model" section exists and whether the "disable_first_quantization" field already exists in the "model" section. If not, it adds it. If it already exists, it doesn't add it again. The function then converts the updated Python data structure back into a formatted YAML string.

Please note that I've added a check to ensure that "disable_first_quantization" is always set to False. This is to make sure the YAML stays valid, as the "disable_first_quantization" field is a boolean and must be either True or False.

Also, in case the "model" section doesn't exist, this function adds it at the top level of the YAML. This is done to preserve the structure of the YAML.

Finally, this function adds indents by `default_flow_style`, as `yaml.dump(...)` automatically adds indentation. You may choose to use `yaml.dump(...)` without the `default_flow_style` param to keep the original indentation, but you'll lose that convenience when you edit things like.
assistant
You are working on a project that involves parsing YAML files for model configurations. You have come across a specific requirement where you need to add a new field to the YAML file called `disable_first_quantization`. This field should be added at the top level of the `model` section.

Write a function `add_disable_first_quantization` that takes a YAML string as input and returns a modified YAML string with the `disable_first_quantization` field added to the `model` section.

Function Signature: `def add_disable_first_quantization(yaml_string: str) -> str:`

**Input**
- `yaml_string` (1 <= `len(yaml_string)` <= 10^6): A valid YAML string representing a model configuration.

**Output**
- Returns a modified YAML string with the `disable_first_quantization` field added to the `model` section. The returned string should be properly indented and formatted.

**Note**
- The input YAML string is guaranteed to be valid and properly indented.
- The `model` section may contain other fields, but the new `disable_first_quantization` field should be added above them.
- The `model` section may or may not contain nested sections.
user
Sure, here is a Python function using the PyYAML library to modify the YAML string:

```python
import yaml
from ruamel.yaml import YAML

def add_disable_first_quantization(yaml_string: str) -> str:
    yaml = YAML()
    data = yaml.load(yaml_string)

    if 'model' in data:
        data['model']['disable_first_quantization'] = False
    else:
        raise KeyError("The YAML does not contain a 'model' section.")

    return yaml.dump(data, default_flow_style=False)
```

This function loads the YAML string into a Python dictionary, adds the `disable_first_quantization` field to the `model` section, and then dumps the dictionary back into a YAML string. The `default_flow_style=False` argument ensures that the output YAML string is properly indented and formatted.

Please note that you need to install the `ruamel.yaml` library if it's not already installed. You can install it using pip:

```bash
pip install ruamel.yaml
```

Also, the function assumes that the `disable_first_quantization` field should be set to `False`. If it should be set to a different value, you can modify the function accordingly.
assistant