> For the complete documentation index, see [llms.txt](https://developer.collibra.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.collibra.com/tutorials/collibra-ai-model-python-integration.md).

# Collibra AI model Python integration

A specific, immutable implementation of the model logic and parameters that serves as the audited "raw" model version before it is activated. You can integrate AI/ML models in Collibra or manually create AI Model assets and link them to the relevant AI use cases.

To help you transfer the metadata related to your AI models to Collibra, adapt and use the following code in your integration applications. We use Python and the Collibra REST Import API for this example.

## Prerequisites

* You have Python version 3.
* You have installed the [**requests** module](https://pypi.org/project/requests/).
* You have [created](https://productresources.collibra.com/docs/collibra/latest/Default.htm#cshid=DOC0655) a programmatic user for API requests that has at least the **AI Business User** global role.
* You have [obtained the ID](https://productresources.collibra.com/docs/collibra/latest/Default.htm#cshid=DOC1092) of the **AI Governance** domain.

  <div data-gb-custom-block data-tag="hint" data-style="success" class="hint hint-success"><p>The ID of the <strong>AI Governance</strong> domain should be <code>00000000-0000-0000-0000-000000006023</code>.</p></div>

## Code sample

```python
import json
import requests

# Update the following variables with your own values
DOMAIN_UUID = "YOUR_DOMAIN_UUID"
USERNAME = "YOUR_USERNAME"
PASSWORD = "YOUR_PASSWORD"

# COLLIBRA / API variables
COLLIBRA_API_URL = "YOUR_COLLIBRA_ENVIRONMENT_URL"
MODEL_ASSET_TYPE = "00000000-0000-0000-0000-000000031402"
MODEL_ACCURACY_ATTRIBUTE_ID = "00000000-0000-0000-0000-000000000328"
MODEL_PRECISION_ATTRIBUTE_ID = "00000000-0000-0000-0000-000000000329"
MODEL_TYPE_ATTRIBUTE_ID = "00000000-0000-0000-0000-000000000334"

class CollibraAIGov:
  def __init__(self):
    # Unique identifier for the domain where all AI Model assets are stored
    self.domain = DOMAIN_UUID
    # Base URL for the Collibra API
    self.COLLIBRA_API_URL = COLLIBRA_API_URL
    # Retrieve password from secrets manager
    self.PASSWORD = PASSWORD
    # Username for API authentication
    self.USERNAME = USERNAME

  def model_update_creation(
    self,
    model_name,
    model_precision="0",
    model_accuracy="0",
    model_type="Generative AI",
  ):
    """
    Create a model and add all relevant information in the AI Governance Module.

    Parameters:
    - model_name: Name of the model to be created.
    - model_precision: Precision metric of the model (optional).
    - model_accuracy: Accuracy metric of the model (optional).
    - model_type: Type of the model (optional). Allowed values are "Generative AI", "Classification", "Regression", "Computer Vision", "Reinforcement Learning" or "Image Classification"
    """

    def builder(model_name):
      """
      Constructs the query payload for model creation.

      Parameters:
      - model_name: Name of the model to be created.

      Returns:
      - A dictionary representing the query payload.
      """
      query = {
        "attributes": {
          MODEL_ACCURACY_ATTRIBUTE_ID: [{ "value": model_accuracy }],
          MODEL_PRECISION_ATTRIBUTE_ID: [{ "value": model_precision }],
          MODEL_TYPE_ATTRIBUTE_ID: [{ "value": model_type }],
        },
        "displayName": model_name,
        "identifier": {
          "domain": {
            "id": self.domain,
          },
          "name": model_name,
        },
        "relations": {},
        "resourceType": "Asset",
        "type": {
          "id": MODEL_ASSET_TYPE,
        },
      }
      return query

    # Serialize the query payload to JSON
    query_payload = json.dumps([builder(model_name)])

    # Construct the file object to be sent in the request
    file = {"file": ("data.json", query_payload)}

    # Make a POST request to the Collibra import API
	# Increase the timeout if the request takes longer
    response = requests.post(
      f"{self.COLLIBRA_API_URL}/rest/2.0/import/json-job",
      files=file,
      auth=(self.USERNAME, self.PASSWORD),
      timeout=30
    )

    # Print the response for debugging purposes
    # Consider handling the response more gracefully in a real application
    print(response)

# Example usage
# collibra = CollibraAIGov()
# collibra.model_update_creation("Example 1 Model")
# collibra.model_update_creation("Example 2 Model", "0.95", "0.90", "Generative AI")
```

Metadata from your ML models are ingested as assets in Collibra. All AI Model assets, and the assets of child asset types, appear in the **Assets** drop-down list.

If you need to add more information that is important for your model registry, contact your Collibra administrator to expand the number of characteristics that you can add to the model asset type.

## Additional resources

* [Create your own Collibra REST API client](https://developer.collibra.com/tutorials/create-your-own-rest-api-client/): learn how to use the OpenApi Generator to create a Python REST API client.
* Read the [AI Governance documentation](https://productresources.collibra.com/docs/collibra/latest/Content/AIGovernance/co_about-ai-governance.htm).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developer.collibra.com/tutorials/collibra-ai-model-python-integration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
