AI inference functions
======================

AIDB provides a set of SQL functions for performing AI inference
directly in queries. These functions let you encode data into vector
representations, generate text with language models, and control
inference behavior — without setting up a pipeline.

aidb.encode_text()
------------------

Encodes a single text string into a vector using a registered embedding
model.

.. code:: sql

   SELECT aidb.encode_text(
       input   => What is pgvector?,
       options => {"model": "my_embedder"}
   );

   __OUTPUT__
                            encode_text
   - -------------------------------------------------------------
    [0.021,-0.043,0.117, ...]
   (1 row)

For the full parameter reference, see :ref:`Models reference <Models reference>`  .

--------------

aidb.encode_text_batch()
------------------------

Encodes multiple text strings into vectors in a single batched request.
More efficient than calling ``encode_text()`` in a loop when processing
many inputs.

.. code:: sql

   SELECT * FROM aidb.encode_text_batch(
       my_model_name,
       ARRAY[What is pgvector?, How does RAG work?, Explain embeddings.]
   );

   __OUTPUT__
     encode_text_batch
   - ------------------------
    [0.021,-0.043,0.117,...]
    [0.008, 0.091,-0.034,...]
    [0.055,-0.012, 0.076,...]
   (3 rows)

For the full parameter reference, see :ref:`Models reference <Models reference>`  .

--------------

aidb.encode_image()
-------------------

Encodes a single image into a vector using a registered multimodal
embedding model (for example, a CLIP model).

.. code:: sql

   SELECT aidb.encode_image(
       my_model_name,
       pg_read_binary_file(/path/to/image.png)::BYTEA
   );

   __OUTPUT__
                            encode_image
   - -------------------------------------------------------------
    [-0.012, 0.084, 0.031, ...]
   (1 row)

For the full parameter reference, see :ref:`Models reference <Models reference>`  .

--------------

aidb.decode_text()
------------------

Generates text from a language model (LLM) given a prompt. This is the
core function for LLM inference in AIDB.

.. code:: sql

   SELECT aidb.decode_text(
       model_name => my_model_name,
       input   => Explain what a knowledge base is in two sentences.,
       inference_config => NULL
   );

   __OUTPUT__
                                           decode_text
   - -------------------------------------------------------------------------------------------
    A knowledge base is a structured repository of information used to answer queries or ...
   (1 row)

To control generation behavior, pass an ``inference_config`` object in
options:

.. code:: sql

   SELECT aidb.decode_text(
                  model_name => my_model_name,
                  input   => Summarize the following text: ...,
                  inference_config => aidb.inference_config(
                                  system_prompt => You are a concise technical writer.,
                                  temperature   => 0.2,
                                  max_tokens    => 256
                                              )
                             
          );

For the full parameter reference, see :ref:`Models reference <Models reference>`  .

--------------

aidb.decode_text_batch()
------------------------

Generates text responses for multiple prompts in a single batched
request.

.. code:: sql

   SELECT * FROM aidb.decode_text_batch(
       model_name => my_model_name,
       input  => ARRAY[
           What is a vector database?,
           What is retrieval-augmented generation?
       ]
   );

   __OUTPUT__
    id |                          value
   - ---+----------------------------------------------------------
     0 | A vector database stores high-dimensional vectors ...
     1 | Retrieval-augmented generation (RAG) combines a ...
   (2 rows)

For the full parameter reference, see :ref:`Models reference <Models reference>`  .

--------------

aidb.inference_config()
-----------------------

Builds an inference configuration object to control language model
generation behavior. Pass the result as the ``"inference_config"`` key
inside the ``options`` JSONB of ``decode_text()`` ,
``decode_text_batch()`` , or ``summarize_text()`` .

.. code:: sql

   SELECT aidb.decode_text(
       input   => Write a haiku about PostgreSQL.,
       options => jsonb_build_object(
           model,            my_llm,
           inference_config, aidb.inference_config(
               temperature => 0.9,
               max_tokens  => 64,
               seed        => 42
           )
       )
   );

For the full parameter reference, see :ref:`Models reference <Models reference>`  .

!!!Note To use these functions as steps inside a pipeline, see
:ref:`Pipeline steps <Pipeline steps>`  .
