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.
SELECT aidb.encode_text(model_name => my_embedder, input => What is pgvector?);
__OUTPUT__
encode_text
- -------------------------------------------------------------
[0.021,-0.043,0.117, ...]
(1 row)
For the full parameter reference, see 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.
SELECT * FROM aidb.encode_text_batch(
model_name => my_model_name,
input => 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 Models reference .
aidb.encode_image()#
Encodes a single image into a vector using a registered multimodal embedding model (for example, a CLIP model).
SELECT aidb.encode_image(
model_name => my_model_name,
input => 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 Models reference .
aidb.generate_text()#
Generates text from a language model (LLM) given a prompt. This is the core function for LLM inference in AIDB.
SELECT aidb.generate_text(
model_name => my_model_name,
input => Explain what a knowledge base is in two sentences.
);
__OUTPUT__
generate_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
built by aidb.inference_config() . Cast its JSONB result to json
so the call matches the aidb.generate_text(TEXT, TEXT, json)
signature:
SELECT aidb.generate_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
)::json
);
For the full parameter reference, see Models reference .
aidb.generate_text_batch()#
Generates text responses for multiple prompts in a single batched request.
SELECT aidb.generate_text_batch(
model_name => my_model_name,
input => ARRAY[
What is a vector database?,
What is retrieval-augmented generation?
]
);
__OUTPUT__
generate_text_batch
- ---------------------------------------------------------
A vector database stores high-dimensional vectors ...
Retrieval-augmented generation (RAG) combines a ...
(2 rows)
For the full parameter reference, see Models reference .
aidb.inference_config()#
Builds an inference configuration object to control language model
generation behavior. Pass the result as the inference_config
argument of aidb.generate_text() or aidb.generate_text_batch() ,
or as part of aidb.summarize_text_config() inside a pipeline step.
SELECT aidb.generate_text(
model_name => my_llm,
input => Write a haiku about PostgreSQL.,
inference_config => aidb.inference_config(
temperature => 0.9,
max_tokens => 64,
seed => 42
)::json
);
For the full parameter reference, see Models reference .
!!!note To use these functions as steps inside a pipeline, see Pipeline steps .