UTL_TO_EMBEDDING and UTL_TO_EMBEDDINGS
Use the DBMS_VECTOR.UTL_TO_EMBEDDING
and DBMS_VECTOR.UTL_TO_EMBEDDINGS
chainable utility functions to generate one or more vector embeddings from textual documents and images.
Purpose
To automatically generate one or more vector embeddings from textual documents and images.
-
Text to Vector:
You can perform a text-to-embedding transformation by accessing either Oracle Database or a third-party service provider:
-
Oracle Database as the service provider (default setting):
This API calls an ONNX format embedding model that you load into the database.
-
Third-party embedding model:
This API makes a REST API call to your chosen remote service provider (Cohere, Generative AI, Google AI, Hugging Face, OpenAI, or Vertex AI) or local service provider (Ollama).
-
-
Image to Vector:
You can also perform an image-to-embedding transformation. This API makes a REST call to your chosen image embedding model or multimodal embedding model by Vertex AI. Note that currently Vertex AI is the only supported service provider for this operation.
WARNING:
Certain features of the database may allow you to access services offered separately by third-parties, for example, through the use of JSON specifications that facilitate your access to REST APIs.
Your use of these features is solely at your own risk, and you are solely responsible for complying with any terms and conditions related to use of any such third-party services. Notwithstanding any other terms and conditions related to the third-party services, your use of such database features constitutes your acceptance of that risk and express exclusion of Oracle's responsibility or liability for any damages resulting from such access.
Syntax
-
Text to Vector:
DBMS_VECTOR.UTL_TO_EMBEDDING ( DATA IN CLOB, PARAMS IN JSON default NULL ) return VECTOR;
DBMS_VECTOR.UTL_TO_EMBEDDINGS ( DATA IN VECTOR_ARRAY_T, PARAMS IN JSON default NULL ) return VECTOR_ARRAY_T;
-
Image to Vector:
DBMS_VECTOR.UTL_TO_EMBEDDING ( DATA IN BLOB, MODALITY IN VARCHAR2, PARAMS IN JSON default NULL ) return VECTOR;
DATA
-
Text to Vector:
UTL_TO_EMBEDDING
accepts the input asCLOB
containing textual data (text strings or small documents). It then converts the text to a single embedding (VECTOR
).UTL_TO_EMBEDDINGS
converts an array of chunks (VECTOR_ARRAY_T
) to an array of embeddings (VECTOR_ARRAY_T
).Note:
Although data is aCLOB
or aVECTOR_ARRAY_T
ofCLOB
, the maximum input is 4000 characters. If you have input that is greater, you can useUTL_TO_CHUNKS
to split the data into smaller chunks before passing in. -
Image to Vector:
UTL_TO_EMBEDDING
accepts the input asBLOB
containing media data for media files such as images. It then converts the image input to a single embedding (VECTOR
).
A generated embedding output includes:
{
"embed_id" : NUMBER,
"embed_data" : "VARCHAR2(4000)",
"embed_vector": "CLOB"
}
-
embed_id
displays the ID number of each embedding. -
embed_data
displays the input text that is transformed into embeddings. -
embed_vector
displays the generated vector representations.
MODALITY
For BLOB
inputs, specify the type of content to vectorize. The only supported value is image
.
PARAMS
Specify input parameters in JSON format, depending on the service provider that you want to use.
{
"provider" : "database",
"model" : "<in-database ONNX embedding model filename>"
}
Table 12-12 Database Provider Parameter Details
Parameter | Description |
---|---|
|
Specify |
|
User-specified name under which the imported ONNX embedding model is stored in Oracle Database. If you do not have an embedding model in ONNX format, then perform the steps listed in Convert Pretrained Models to ONNX Format. |
If using a third-party provider:
Set the following parameters along with additional embedding parameters specific to your provider:
-
For
UTL_TO_EMBEDDING
:{ "provider" : "<AI service provider>", "credential_name" : "<credential name>", "url" : "<REST endpoint URL for embedding service>", "model" : "<REST provider embedding model name>", "transfer_timeout": <maximum wait time for the request to complete>, "<additional REST provider parameter>": "<REST provider parameter value>" }
-
For
UTL_TO_EMBEDDINGS
:{ "provider" : "<AI service provider>", "credential_name" : "<credential name>", "url" : "<REST endpoint URL for embedding service>", "model" : "<REST provider embedding model name>", "transfer_timeout": <maximum wait time for the request to complete>, "batch_size" : "<number of vectors to request at a time>", "<additional REST provider parameter>": "<REST provider parameter value>" }
Table 12-13 Third-Party Provider Parameter Details
Parameter | Description |
---|---|
|
Third-party service provider that you want to access for this operation. A REST call is made to the specified provider to access its embedding model. For image input, specify For text input, specify one of the following values:
|
|
Name of the credential in the form:
A credential name holds authentication credentials to enable access to your provider for making REST API calls. You need to first set up your credential by calling the |
|
URL of the third-party provider endpoint for each REST call, as listed in Supported Third-Party Provider Operations and Endpoints. |
|
Name of the third-party embedding model in the form:
If you do not specify a schema, then the schema of the procedure invoker is used. Note:
|
|
Maximum time to wait for the request to complete. The default value is |
|
Maximum number of vectors to request at a time. For example, for a batch size of For REST calls, it is more efficient to send a batch of inputs at a time rather than requesting a single input per call. Increasing the batch size can provide better performance, whereas reducing the batch size may reduce memory and data usage, especially if your provider has a rate limit. The default or maximum allowed value depends on the third-party provider settings. |
Additional third-party provider parameters:
Optionally, specify additional provider-specific parameters.
Table 12-14 Additional REST Provider Parameter Details
Parameter | Description |
---|---|
|
Type of input to vectorize. |
Let us look at some example configurations for all third-party providers:
Important:
-
The following examples are for illustration purposes. For accurate and up-to-date information on the parameters to use, refer to your third-party provider's documentation.
-
For a list of all supported REST endpoint URLs, see Supported Third-Party Provider Operations and Endpoints.
-
The generated embedding results may be different between requests for the same input and configuration, depending on your embedding model or floating point precision. However, this does not affect your queries (and provides semantically correct results) because the vector distance will be similar.
{
"provider" : "cohere",
"credential_name": "COHERE_CRED",
"url" : "https://api.cohere.example.com/embed",
"model" : "embed-english-light-v2.0",
"input_type" : "search_query"
}
{
"provider" : "ocigenai",
"credential_name": "OCI_CRED",
"url" : "https://generativeai.oci.example.com/embedText",
"model" : "cohere.embed-english-v3.0",
"batch_size" : 10
}
{
"provider" : "googleai",
"credential_name": "GOOGLEAI_CRED",
"url" : "https://googleapis.example.com/models/",
"model" : "embedding-001"
}
{
"provider" : "huggingface",
"credential_name": "HF_CRED",
"url" : "https://api.huggingface.example.com/",
"model" : "sentence-transformers/all-MiniLM-L6-v2"
}
{
"provider" : "ollama",
"host" : "local",
"url" : "http://localhost:11434/api/embeddings",
"model" : "phi3:mini"
}
{
"provider" : "openai",
"credential_name": "OPENAI_CRED",
"url" : "https://api.openai.example.com/embeddings",
"model" : "text-embedding-3-small"
}
{
"provider" : "vertexai",
"credential_name": "VERTEXAI_CRED",
"url" : "https://googleapis.example.com/models/",
"model" : "textembedding-gecko:predict"
}
Examples
You can use UTL_TO_EMBEDDING
in a SELECT
clause and UTL_TO_EMBEDDINGS
in a FROM
clause, as follows:
UTL_TO_EMBEDDING:
-
Text to vector using Generative AI:
The following examples use
UTL_TO_EMBEDDING
to generate an embedding withHello world
as the input.Here, the cohere.embed-english-v3.0 model is used by accessing Generative AI as the provider. You can replace the
model
value with any other supported model that you want to use with Generative AI, as listed in Supported Third-Party Provider Operations and Endpoints.-- declare embedding parameters var params clob; begin :params := ' { "provider": "ocigenai", "credential_name": "OCI_CRED", "url": "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/20231130/actions/embedText", "model": "cohere.embed-english-v3.0", "batch_size": 10 }'; end; / -- get text embedding: PL/SQL example declare input clob; v vector; begin input := 'Hello world'; v := dbms_vector.utl_to_embedding(input, json(params)); dbms_output.put_line(vector_serialize(v)); exception when OTHERS THEN DBMS_OUTPUT.PUT_LINE (SQLERRM); DBMS_OUTPUT.PUT_LINE (SQLCODE); end; / -- get text embedding: select example select dbms_vector.utl_to_embedding('Hello world', json(:params)) from dual;
-
Image to vector using Vertex AI:
The following examples use
UTL_TO_EMBEDDING
to generate an embedding by accessing the Vertex AI's multimodal embedding model.Here, the input is
parrots.jpg
,VEC_DUMP
is a local directory that stores theparrots.jpg
file, and the modality is specified asimage
.-- declare embedding parameters var params clob; begin :params := ' { "provider": "vertexai", "credential_name": "VERTEXAI_CRED", "url": "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/publishers/google/models/", "model": "multimodalembedding:predict" }'; end; / -- get image embedding: PL/SQL example declare v vector; output clob; begin v := dbms_vector.utl_to_embedding( to_blob(bfilename('VEC_DUMP', 'parrots.jpg')), 'image', json(:params)); output := vector_serialize(v); dbms_output.put_line('vector data=' || dbms_lob.substr(output, 100) || '...'); end; / -- get image embedding: select example select dbms_vector.utl_to_embedding( to_blob(bfilename('VEC_DUMP', 'parrots.jpg')), 'image', json(:params));
-
Text to vector using in-database embedding model:
The following example uses
UTL_TO_EMBEDDING
to generate a vector embedding by calling an ONNX format embedding model (doc_model
) loaded into Oracle Database.Here, the provider is
database
, and the input ishello
.var params clob; exec :params := '{"provider":"database", "model":"doc_model"}'; select dbms_vector.utl_to_embedding('hello', json(:params)) from dual;
For complete example, see Convert Text String to Embedding Within Oracle Database.
-
End-to-end examples:
To run various end-to-end example scenarios using
UTL_TO_EMBEDDING
, see Generate Embedding.
UTL_TO_EMBEDDINGS:
-
Text to vector using in-database embedding model:
The following example uses
UTL_TO_EMBEDDINGS
to generate an array of embeddings by calling an ONNX format embedding model (doc_model
) loaded into Oracle Database.Here, the provider is
database
, and the input is a PDF document stored in thedocumentation_tab
table. As you can see, you first useUTL_TO_CHUNKS
to split the data into smaller chunks before passing in toUTL_TO_EMBEDDINGS
.CREATE TABLE doc_chunks as (select dt.id doc_id, et.embed_id, et.embed_data, to_vector(et.embed_vector) embed_vector from documentation_tab dt, dbms_vector.utl_to_embeddings( dbms_vector.utl_to_chunks(dbms_vector.utl_to_text(dt.data), json('{"normalize":"all"}')), json('{"provider":"database", "model":"doc_model"}')) t, JSON_TABLE(t.column_value, '$[*]' COLUMNS (embed_id NUMBER PATH '$.embed_id', embed_data VARCHAR2(4000) PATH '$.embed_data', embed_vector CLOB PATH '$.embed_vector')) et );
For complete example, see SQL Quick Start Using a Vector Embedding Model Uploaded into the Database.
-
End-to-end examples:
To run various end-to-end example scenarios using
UTL_TO_EMBEDDINGS
, see Perform Chunking With Embedding.
Parent topic: DBMS_VECTOR