Pipelines models reference
==========================

This reference documentation for EDB Postgres AI - AI Accelerator
Pipelines models includes information on the functions and views
available in the aidb extension for working with :ref:`Pipelines models reference <Pipelines models reference>`  .

Tables
------

aidb.model_providers
^^^^^^^^^^^^^^^^^^^^

The ``aidb.model_providers`` table stores information about the model
providers that are available.

.. csv-table::
  :header: Column,Type,Description
  :widths: 12,15,25
  :align: left
  :class: longtable

  `server_name`,name,Name for the model server
  `server_description`,text,Description of the model server
  `server_options`,text\[\],Options for the model server

aidb.models
^^^^^^^^^^^

Returns a list of all models in the registry and their configured
options, including predefined models and user-created models.

.. csv-table::
  :header: Column,Type,Description
  :widths: 12,15,25
  :align: left
  :class: longtable

  `name`,text,User-defined name for the model
  `provider`,text,Name of the model provider
  `options`,jsonb,Optional configuration for the model provider

Example
^^^^^^^

.. code:: sql

   SELECT * FROM aidb.models;
   __OUTPUT__
    name  |  provider  |    options
   - ------+------------+---------------
    bert  | bert_local | {"config={}"}
    clip  | clip_local | {"config={}"}
    t5    | t5_local   | {"config={}"}

Functions
---------

aidb.create_model
^^^^^^^^^^^^^^^^^

Creates a new model in the system by saving its name, provider, and
optional configuration.

Parameters
^^^^^^^^^^

.. csv-table::
  :header: Parameter,Type,Default,Description
  :widths: 15,12,12,20
  :align: left
  :class: longtable

  `name`,text,"",User-defined name for the model.
  `provider`,text,"",Name of the model provider (as found in  :ref:`aidb.model_providers<aidb.model_providers>` ).
  `config`,jsonb,'{}'::jsonb,"Optional configuration for the model provider. May include model-specific parameters such as `model`, `url`, and TLS options (e.g., `tls_config`)."
  `credentials`,jsonb,'{}'::jsonb,Optional credentials for the model provider.
  `replace_credentials`,boolean,false,"If true, replaces the credentials for the model provider. If false, the credentials aren't overwritten."

.. _example-1:

Example
^^^^^^^

.. code:: sql

   SELECT aidb.create_model(
                  name => my_t5::text,
                  provider => t5_local::character varying,
                  config => {"param1": "value1", "param2": "value2"}::jsonb,
                  credentials => {"token": "abcd"}::jsonb
              );

or equivalently, using default values:

.. code:: sql

   SELECT aidb.create_model(my_t5, t5_local);

or if updating the credentials of a model’s provider, which has already
been created.

.. code:: sql

   SELECT aidb.create_model(
                  name => my_t5::text,
                  provider => t5_local::character varying,
                  config => {"param1": "value1", "param2": "value2"}::jsonb,
                  credentials => {"token": "abcd"}::jsonb,
                  replace_credentials => true
              );

TLS Configuration (optional)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To securely connect to HTTPS-based model endpoints, the ``config``
object can include a ``tls_config`` field:

.. code:: json

   "tls_config": {
     "insecure_skip_verify": true,  // (optional) disables certificate validation
     "ca_path": "/etc/aidb/myCA.pem"  // (optional) path to a trusted CA certificate
   }

aidb.get_model
^^^^^^^^^^^^^^

Returns the configuration for a model in the registry.

.. _parameters-1:

Parameters
^^^^^^^^^^

.. csv-table::
  :header: Parameter,Type,Default,Description
  :widths: 15,12,12,20
  :align: left
  :class: longtable

  `model_name`,text,"",Name of the model

Returns
^^^^^^^

.. csv-table::
  :header: Column,Type,Description
  :widths: 12,15,25
  :align: left
  :class: longtable

  `name`,text,User-defined name for the model
  `provider`,text,Name of the model provider
  `options`,jsonb,Optional configuration for the model provider

.. _example-2:

Example
^^^^^^^

.. code:: sql

   SELECT * FROM aidb.get_model(t5);
   __OUTPUT__
    name | provider |    options
   - -----+----------+---------------
    t5   | t5_local | {"config={}"}
   (1 row)

aidb.delete_model
^^^^^^^^^^^^^^^^^

Deletes a model from the registry.

.. _parameters-2:

Parameters
^^^^^^^^^^

.. csv-table::
  :header: Parameter,Type,Default,Description
  :widths: 15,12,12,20
  :align: left
  :class: longtable

  `model_name`,text,"",Name of the model

.. _example-3:

Example
^^^^^^^

.. code:: sql

   SELECT aidb.delete_model(t5);
   __OUTPUT__
        delete_model
   - --------------------------------
    (t5,t5_local,"{""config={}""}")
   (1 row)

.. _returns-1:

Returns
^^^^^^^

.. csv-table::
  :header: Column,Type,Description
  :widths: 12,15,25
  :align: left
  :class: longtable

  `delete_model`,jsonb,"The name, provider, and options of the deleted model"

aidb.list_hcp_models
^^^^^^^^^^^^^^^^^^^^

Gets models running on the Hybrid Manager.

.. _returns-2:

Returns
^^^^^^^

.. csv-table::
  :header: Column,Type,Description
  :widths: 12,15,25
  :align: left
  :class: longtable

  `name`,text,The name of the model instance running on the HCP
  `url`,text,The API URL of the model running on the HCP
  `model`,text,The name the model running on the HCP

.. _example-4:

Example
^^^^^^^

.. code:: sql

   SELECT * FROM  aidb.get_hcp_models();
                    name                  |                                       url                                        |               model                
   - --------------------------------------+----------------------------------------------------------------------------------+------------------------------------
    llama-3-1-8b-instruct-1xgpu-g6        | http://llama-3-1-8b-instruct-1xgpu-g6-predictor.default.svc.cluster.local        | meta/llama-3.1-8b-instruct
    llama-3-2-nv-embedqa-1b-v2            | http://llama-3-2-nv-embedqa-1b-v2-predictor.default.svc.cluster.local            | nvidia/llama-3.2-nv-embedqa-1b-v2
    meta-nim-llama3-70b-instruct-8xgpu-g5 | http://meta-nim-llama3-70b-instruct-8xgpu-g5-predictor.default.svc.cluster.local | meta/llama3-70b-instruct
   (3 rows)

aidb.create_hcp_model
^^^^^^^^^^^^^^^^^^^^^

Creates a new model in the system by referencing a running instance in
the HCP

.. _parameters-3:

Parameters
^^^^^^^^^^

.. csv-table::
  :header: Parameter,Type,Default,Description
  :widths: 15,12,12,20
  :align: left
  :class: longtable

  `name`,text,"",User-defined name of the model
  `hcp_model_name`,text,"",Name of the model instance running on HCP

aidb.sync_hcp_models
^^^^^^^^^^^^^^^^^^^^

Creates models in the HCP and sets ``is_hcp_model=true`` ; deletes
models with that setting if not found in the HCP.

.. _returns-3:

Returns
^^^^^^^

.. csv-table::
  :header: Column,Type,Description
  :widths: 12,15,25
  :align: left
  :class: longtable

  `status`,text,"Synchronization status; either `deleted`, `unchanged`, `created`, or `skipped`."
  `model`,text,The name the synchronized HCP model.

aidb.encode_text
^^^^^^^^^^^^^^^^

Encodes text using a model, generating a vector representation of a
given text input.

.. _parameters-4:

Parameters
^^^^^^^^^^

.. csv-table::
  :header: Parameter,Type,Default,Description
  :widths: 15,12,12,20
  :align: left
  :class: longtable

  `model_name`,text,"",Name of the model to encode with
  `text`,text,"",Text to encode

aidb.encode_text_batch
^^^^^^^^^^^^^^^^^^^^^^

Encodes a batch of text using a model, generating a vector
representation of a given text input.

.. _parameters-5:

Parameters
^^^^^^^^^^

.. csv-table::
  :header: Parameter,Type,Default,Description
  :widths: 15,12,12,20
  :align: left
  :class: longtable

  `model_name`,text,"",Name of the model to encode with
  `text`,text\[\],"",Array of text to encode

aidb.decode_text
^^^^^^^^^^^^^^^^

Decodes text using a model, generating a vector representation of a
given text input.

.. _parameters-6:

Parameters
^^^^^^^^^^

.. csv-table::
  :header: Parameter,Type,Default,Description
  :widths: 15,12,12,20
  :align: left
  :class: longtable

  `model_name`,text,"",Name of the model to decode with
  `text`,text,"",Text to decode

.. _returns-4:

Returns
^^^^^^^

.. csv-table::
  :header: Column,Type,Description
  :widths: 12,15,25
  :align: left
  :class: longtable

  `decode_text`,text,The decoded text

aidb.decode_text_batch
^^^^^^^^^^^^^^^^^^^^^^

Decodes a batch of text using a model, generating a representation of a
given text input.

.. _parameters-7:

Parameters
^^^^^^^^^^

.. csv-table::
  :header: Parameter,Type,Default,Description
  :widths: 15,12,12,20
  :align: left
  :class: longtable

  `model_name`,text,"",Name of the model to decode with
  `text`,text\[\],"",Array of text to decode

.. _returns-5:

Returns
^^^^^^^

.. csv-table::
  :header: Column,Type,Description
  :widths: 12,15,25
  :align: left
  :class: longtable

  `decode_text`,text,The decoded text

aidb.encode_image
^^^^^^^^^^^^^^^^^

Encodes an image using a model, generating a vector representation of a
given image input.

.. _parameters-8:

Parameters
^^^^^^^^^^

.. csv-table::
  :header: Parameter,Type,Default,Description
  :widths: 15,12,12,20
  :align: left
  :class: longtable

  `model_name`,text,"",Name of the model to encode with
  `image`,bytea,"",Image to encode

.. _returns-6:

Returns
^^^^^^^

.. csv-table::
  :header: Column,Type,Description
  :widths: 12,15,25
  :align: left
  :class: longtable

  `encode_image`,bytea,The encoded image

aidb.rerank_text
^^^^^^^^^^^^^^^^

Reranks text using a model, generating a vector representation of a
given text input.

.. _parameters-9:

Parameters
^^^^^^^^^^

.. csv-table::
  :header: Parameter,Type,Default,Description
  :widths: 15,12,12,20
  :align: left
  :class: longtable

  `model_name`,text,"",Name of the model to rerank with
  `query`,text,"",Query based on which the input will be ranked
  `input`,text\[\],\[\],Inputs to be ranked

.. _returns-7:

Returns
^^^^^^^

.. csv-table::
  :header: Column,Type,Description
  :widths: 12,15,25
  :align: left
  :class: longtable

  `text`,text,"The text from ""input"""
  `logit_score`,double precision,Score/rank of this text
  `id`,int,index that the text had in the input array
