# AI Models

<a id="opengate_data.ai_models.ai_models"></a>

## opengate\_data.ai\_models.ai\_models

AIModelsBuilder

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder"></a>

### AIModelsBuilder Objects

```python
class AIModelsBuilder()
```

AI Model Builder

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.with_organization_name"></a>

---
#### with\_organization\_name

```python
def with_organization_name(organization_name: str) -> "AIModelsBuilder"
```

Specify the organization name.

**Arguments**:

- `organization_name` _str_ - The name of the organization.
  

**Returns**:

- `AIModelsBuilder` - Returns self for chaining.
  

**Example**:

  ~~~python
  builder.with_organization_name('organization_name')
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.with_identifier"></a>

---
#### with\_identifier

```python
def with_identifier(identifier: str) -> "AIModelsBuilder"
```

Specify the identifier for the pipeline.

**Arguments**:

- `identifier` _str_ - The identifier for the pipeline.
  

**Returns**:

- `AIModelsBuilder` - Returns self for chaining.
  

**Example**:

  ~~~python
  builder.with_identifier('identifier')
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.with_env"></a>

---
#### with\_env

```python
def with_env(data_env: str) -> "AIModelsBuilder"
```

Specify the environment variable.

**Arguments**:

- `data_env` _str_ - The environment variable.
  

**Returns**:

- `AIModelsBuilder` - Returns self for chaining.
  

**Example**:

  ~~~python
  builder.with_env('MODEL_ID')
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.with_find_by_name"></a>

---
#### with\_find\_by\_name

```python
def with_find_by_name(find_name: str) -> "AIModelsBuilder"
```

Specify the name to find.

**Arguments**:

- `find_name` _str_ - The name of the model.
  

**Returns**:

- `AIModelsBuilder` - Returns self for chaining.
  

**Example**:

  ~~~python
  builder.with_find_by_name('model_name.onnx')
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.with_config_file"></a>

---
#### with\_config\_file

```python
def with_config_file(config_file: str, section: str,
                     config_key: str) -> "AIModelsBuilder"
```

Sets up the configuration file (.ini).

This method allows specifying a configuration file, a section within that file, and a key to retrieve a specific value from the section.

**Arguments**:

- `config_file` _str_ - The path to the.ini configuration file.
- `section` _str_ - The section name within the.ini file where the desired configuration is located.
- `config_key` _str_ - The key within the specified section whose value will be retrieved.
  

**Raises**:

- `TypeError` - If the provided config_file is not a string.
- `TypeError` - If the provided section is not a string.
- `TypeError` - If the provided config_key is not a string.
  

**Returns**:

- `AIModelsBuilder` - Returns itself to allow for method chaining.
  

**Example**:

  ~~~python
  [id]
  model_id = afe07216-14ec-4134-97ae-c483b11d965a
  builder.with_config_file('model_config.ini', 'id', 'model_id')
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.add_file"></a>

---
#### add\_file

```python
def add_file(file: str) -> "AIModelsBuilder"
```

Attaches the model file to upload, in PMML or ONNX format.

**Arguments**:

- `file` _str_ - The path to the file to be added.
  

**Returns**:

- `AIModelsBuilder` - Returns itself to allow for method chaining.
  

**Raises**:

- `TypeError` - If the provided file path is not a string.
  

**Example**:

  ~~~python
  builder.add_file('file.onnx')
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.with_prediction"></a>

---
#### with\_prediction

```python
def with_prediction(data_prediction: dict) -> "AIModelsBuilder"
```

Prediction with a model

**Arguments**:

- `data_prediction` _dict_ - Prediction
  

**Raises**:

- `TypeError` - If the prediction is not a dict.
  

**Returns**:

- `AIModelsBuilder` - Returns itself to allow for method chaining.
  

**Example**:

  ~~~python
  prediction = {
- `"X"` - [
  {
- `"input_8"` - [
  [
  -0.5391107438074961,
  -0.15950019784171535,
  ]
  ]
  }
  ]
  }
  builder.with_prediction(prediction)
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.with_output_file_path"></a>

---
#### with\_output\_file\_path

```python
def with_output_file_path(output_file_path: str) -> "AIModelsBuilder"
```

Sets the output file path for the model.

This method allows you to specify the path where the output file will be saved.
It is particularly useful for operations that involve downloading or saving files.

**Arguments**:

- `output_file_path` _str_ - The path where the output file will be saved.
  

**Returns**:

- `AIModelsBuilder` - The instance of the AIModelsBuilder class.
  

**Example**:

  ~~~python
  builder.with_output_file_path("rute/prueba.onnx")
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.create"></a>

---
#### create

```python
def create() -> "AIModelsBuilder"
```

Initiates the creation process of a new model.

This method prepares the AIModelsBuilder instance to create a new model by setting up the necessary parameters such as the organization name and the file to be associated with the model.

**Returns**:

- `AIModelsBuilder` - The instance of the AIModelsBuilder class itself, allowing for method chaining.
  

**Example**:

  ~~~python
  builder.with_organization_name("MyOrganization").add_file("example.onnx").create()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.find_one"></a>

---
#### find\_one

```python
def find_one() -> "AIModelsBuilder"
```

Retrieve a single model.

This method sets up the AIModelsBuilder instance to retrieve a specific model associated with the specified organization and identifier.

**Returns**:

- `AIModelsBuilder` - The instance of the AIModelsBuilder class itself, allowing for method chaining.
  

**Example**:

  ~~~python
  builder.with_organization_name("organization_name").with_identifier("model_identifier").find_one()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.find_all"></a>

---
#### find\_all

```python
def find_all() -> "AIModelsBuilder"
```

Retrieve all models.

This method sets up the AIModelsBuilder instance to retrieve all models associated with the specified organization.

**Returns**:

- `AIModelsBuilder` - The instance of the AIModelsBuilder class itself, allowing for method chaining.
  

**Example**:

  ~~~python
  builder.with_organization_name("MyOrganization").find_all()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.update"></a>

---
#### update

```python
def update() -> "AIModelsBuilder"
```

Update an existing model.

This method sets up the AIModelsBuilder instance to update a specific model associated with the specified organization and identifier.

**Returns**:

- `AIModelsBuilder` - The instance of the AIModelsBuilder class itself, allowing for method chaining.
  

**Example**:

  ~~~python
  builder.with_organization_name("MyOrganization").with_identifier("model_identifier").add_file("updated_model.onnx").update()
  builder.with_organization_name("MyOrganization").with_find_by_name("model_name.onnx").add_file("updated_model.onnx").update()
  builder.with_organization_name("MyOrganization").with_config_file('model_config.ini', 'id', 'model').add_file("updated_model.onnx").update()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.delete"></a>

---
#### delete

```python
def delete() -> "AIModelsBuilder"
```

Delete an existing model within the organization.

This method sets up the AIModelsBuilder instance to delete a specific model associated with the specified organization and identifier.

**Returns**:

- `AIModelsBuilder` - The instance of the AIModelsBuilder class itself, allowing for method chaining.
  

**Example**:

  ~~~python
  builder.with_organization_name("MyOrganization").with_identifier("model_identifier").delete()
  builder.with_organization_name("MyOrganization").with_find_by_name("model_name.onnx").delete()
  builder.with_organization_name("MyOrganization").with_config_file('model_config.ini', 'id', 'model').delete()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.validate"></a>

---
#### validate

```python
def validate() -> "AIModelsBuilder"
```

Validate the model configuration.

This method sets up the AIModelsBuilder instance to validate the configuration of a model associated with the specified organization.

**Returns**:

- `AIModelsBuilder` - The instance of the AIModelsBuilder class itself, allowing for method chaining.
  

**Example**:

  ~~~python
  builder.with_organization_name("MyOrganization").add_file("model.onnx").validate()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.download"></a>

---
#### download

```python
def download() -> "AIModelsBuilder"
```

Download the model file.

This method sets up the AIModelsBuilder instance to download the file of a specific model associated with the specified organization and identifier.

**Returns**:

- `AIModelsBuilder` - The instance of the AIModelsBuilder class itself, allowing for method chaining.
  

**Example**:

  ~~~python
  builder.with_organization_name("MyOrganization").with_identifier("model_identifier").with_output_file_path("model.onnx").download()
  builder.with_organization_name("MyOrganization").with_find_by_name("model_name.onnx").with_output_file_path("model.onnx").download()
  builder.with_organization_name("MyOrganization").with_config_file('model_config.ini', 'id', 'model').with_output_file_path("model.onnx").download()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.prediction"></a>

---
#### prediction

```python
def prediction() -> "AIModelsBuilder"
```

Make a prediction using the model.

This method sets up the AIModelsBuilder instance to make a prediction using a specific model associated with the specified organization and identifier.

**Returns**:

- `AIModelsBuilder` - The instance of the AIModelsBuilder class itself, allowing for method chaining.
  

**Example**:

  ~~~python
  prediction_data = {
- `"X"` - [
  {
- `"input_8"` - [
  [
  -0.5391107438074961,
  -0.15950019784171535,
  ]
  ]
  }
  ]
  }
  builder.with_organization_name("MyOrganization").with_identifier("model_identifier").with_prediction(prediction_data).prediction()
  builder.with_organization_name("MyOrganization").with_find_by_name("model_name.onnx").with_prediction(prediction_data).prediction()
  builder.with_organization_name("MyOrganization").with_config_file('model_config.ini', 'id', 'model').with_prediction(prediction_data).prediction()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.save"></a>

---
#### save

```python
def save() -> "AIModelsBuilder"
```

Save the model configuration.

This method sets up the AIModelsBuilder instance to save the configuration of a model associated with the specified organization.

**Returns**:

- `AIModelsBuilder` - The instance of the AIModelsBuilder class itself, allowing for method chaining.
  

**Example**:

  ~~~python
  builder.with_organization_name("MyOrganization").with_env("env_var").save()
  builder.with_organization_name("MyOrganization").with_config_file('model_config.ini', 'id', 'model').save()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.set_config_file_identifier"></a>

---
#### set\_config\_file\_identifier

```python
def set_config_file_identifier() -> "AIModelsBuilder"
```

Selects the operation that writes the model identifier into a
configuration file.

It stores the identifier given to `with_identifier()` under the section
and key named with `with_config_file()`, so a later run can pick the
model up from the file instead of carrying its identifier in the code.
The key must already exist in the file.

**Returns**:

- `AIModelsBuilder` - Returns itself to allow for method chaining.
  

**Example**:

  ~~~python
  builder.with_identifier("model_identifier").with_config_file(
  'model_config.ini', 'id', 'model_id'
  ).set_config_file_identifier().build().execute()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.set_env_identifier"></a>

---
#### set\_env\_identifier

```python
def set_env_identifier() -> "AIModelsBuilder"
```

Selects the operation that writes the model identifier into the `.env`
file.

It stores the identifier given to `with_identifier()` under the variable
named with `with_env()`, so a later run can pick the model up from the
environment. The variable must already exist in the `.env` file.

**Returns**:

- `AIModelsBuilder` - Returns itself to allow for method chaining.
  

**Raises**:

- `ValueError` - If the variable is not in the `.env` file.
  

**Example**:

  ~~~python
  builder.with_identifier("model_identifier").with_env(
  "MODEL_ID"
  ).set_env_identifier().build().execute()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.build"></a>

---
#### build

```python
def build() -> "AIModelsBuilder"
```

Finalizes the construction of the IoT collection configuration.

This method prepares the builder to execute the collection by ensuring all necessary configurations are set and validates the overall integrity of the build. It should be called before executing the collection to ensure that the configuration is complete and valid.

The build process involves checking that mandatory fields such as the device identifier are set. It also ensures that method calls that are incompatible with each other (like `build` and `build_execute`) are not both used.

**Returns**:

- `AIModelsBuilder` - Returns itself to allow for method chaining, enabling further actions like `execute`.
  

**Raises**:

- `ValueError` - If required configurations are missing or if incompatible methods are used together.
  

**Example**:

  ~~~python
  builder.build()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.build_execute"></a>

---
#### build\_execute

```python
def build_execute()
```

Executes the data sets search immediately after building the configuration.

This method is a shortcut that combines building and executing in a single step.

**Returns**:

- `dict` - A dictionary containing the execution response which includes the status code and potentially other metadata about the execution.
  

**Raises**:

- `ValueError` - If `build` has already been called on this builder instance.
  

**Example**:

  ~~~python
  builder.build_execute()
  ~~~

<a id="opengate_data.ai_models.ai_models.AIModelsBuilder.execute"></a>

---
#### execute

```python
def execute() -> Response
```

Execute the configured operation and return the response.

This method executes the operation that has been configured using the builder pattern. It ensures that the `build` method has been called and that it is the last method invoked before `execute`. Depending on the configured method (e.g., create, find, update, delete), it calls the appropriate internal execution method.

**Returns**:

- `requests.Response` - The response object from the executed request.
  

**Raises**:

- `Exception` - If the `build` method has not been called or if it is not the last method invoked before `execute`.
- `ValueError` - If the configured method is unsupported.
  

**Example**:

  ~~~python
  builder.execute()
  ~~~

<a id="opengate_data.ai_models"></a>

## opengate\_data.ai\_models

