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 vector embeddings from a set of textual documents and images.

  • Text to Vector:

    You can perform a text-to-embedding transformation by accessing:

    • Oracle Database as the service provider: Calls an ONNX format embedding model that you load into the database (default setting)

    • Third-party embedding model: Makes a REST API call to either a remote service provider (Cohere, Google AI, Hugging Face, Generative AI, OpenAI, or Vertex AI) or a local service provider (Ollama)

  • Image to Vector:

    You can also perform an image-to-embedding transformation. For this operation, this API makes a REST call to your chosen image embedding model or multimodal embedding model by Vertex AI (which is the only supported third-party provider).

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

UTL_TO_EMBEDDING accepts the input as CLOB containing textual data (text strings or small documents) or as BLOB containing media data (for media files such as images). It then converts the text or image input to a single embedding (VECTOR).

UTL_TO_EMBEDDINGS converts an array of chunks (VECTOR_ARRAY_T) to an array of embeddings (VECTOR_ARRAY_T).

A generated embedding output includes:

{
    "embed_id"    :  NUMBER,
    "embed_data"  : "VARCHAR2(4000)", 
    "embed_vector": "CLOB"
}
Where,
  • 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.

If using Oracle Database as the provider:
{
  "provider" : "database", 
  "model"    : "<in-database ONNX embedding model filename>" 
}

Table 12-14 Database Provider Parameter Details

Parameter Description

provider

Specify database (default setting) to use Oracle Database as the provider. With this setting, you must load an ONNX format embedding model into the database.

model

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-15 Third-Party Provider Parameter Details

Parameter Description

provider

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 vertexai.

For text input, specify one of the following values:

  • cohere

  • googleai

  • huggingface

  • ocigenai

  • openai

  • vertexai

credential_name

Name of the credential in the form:

schema.credential_name

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 DBMS_VECTOR.CREATE_CREDENTIAL helper function to create and store a credential, and then refer to the credential name here. See CREATE_CREDENTIAL.

url

URL of the API endpoint for each REST call.

Note: For a list of all supported REST endpoints, see Supported Third-Party Provider Operations and Endpoints.

model

Name of the third-party embedding model in the form:

schema.model_name

If you do not specify a schema, then the schema of the procedure invoker is used.

Note:
  • For accurate results, ensure that the chosen text embedding model matches the vocabulary file used for chunking. If you are not using a vocabulary file, then ensure that the input length is defined within the token limits of your model.

  • To get image embeddings, you can use any image embedding model or multimodal embedding model supported by Vertex AI. Multimodal embedding is a technique that vectorizes data from different modalities such as text and images.

    When using a multimodal embedding model to generate embeddings, ensure that you use the same model to vectorize both types of content (text and images). By doing so, the resulting embeddings are compatible and situated in the same vector space, which allows for effective comparison between the two modalities during similarity searches.

transfer_timeout

Maximum time to wait for the request to complete.

The default value is 60 seconds. You can increase this value for busy web servers.

batch size

Maximum number of vectors to request at a time.

For example, for a batch size of 50, if 100 chunks are passed, then this API sends two requests with an array of 50 strings each. If 30 chunks are passed (which is lesser than the defined batch size), then the API sends those in a single request.

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 REST provider parameters:

Optionally, specify additional provider-specific parameters.

Important:

The following examples are for illustration purposes. For accurate and up-to-date information on additional parameters to use, refer to your third-party provider's documentation.
Cohere example:
{
  "provider"       : "cohere",
  "credential_name": "COHERE_CRED",
  "url"            : "https://api.cohere.example.com/embed",
  "model"          : "embed-model",
  "input_type"     : "search_query"
}
Google AI example:
{
  "provider"       : "googleai",
  "credential_name": "GOOGLEAI_CRED",
  "url"            : "https://googleapis.example.com/models/",
  "model"          : "embed-model"
}
Hugging Face example:
{
  "provider"       : "huggingface",
  "credential_name": "HF_CRED",
  "url"            : "https://api.huggingface.example.com/",
  "model"          : "embed-model"
}
Generative AI example:
{
  "provider"       : "ocigenai",
  "credential_name": "OCI_CRED",
  "url"            : "https://generativeai.oci.example.com/embedText",
  "model"          : "embed-model",
  "batch_size"     : 10
}
Ollama example:
{
  "provider"       : "ollama", 
  "host"           : "local", 
  "url"            : "http://localhost:11434/api/embeddings", 
  "model"          : "llama3"
}
OpenAI example:
{
  "provider"       : "openai",
  "credential_name": "OPENAI_CRED",
  "url"            : "https://api.openai.example.com/embeddings",
  "model"          : "embed-model"
}
Vertex AI example:
{
  "provider"       : "vertexai",
  "credential_name": "VERTEXAI_CRED",
  "url"            : "https://googleapis.example.com/models/",
  "model"          : "embed-model"
}

Table 12-16 Additional REST Provider Parameter Details

Parameter Description

input_type

Type of input to vectorize.

Note:

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.

Examples

You can use UTL_TO_EMBEDDING in a SELECT clause and UTL_TO_EMBEDDINGS in a FROM clause, as follows:

  • The following examples use UTL_TO_EMBEDDING to generate an embedding with Hello world as input, by accessing the Generative AI embedding model:
    -- declare embedding parameters
    
    var params clob;
    
    begin
     :params := '
    { 
      "provider": "ocigenai",
      "credential_name": "OCI_CRED", 
      "url": "https://generativeai.oci.example.com/embedText",
      "model": "embed.modelname",
      "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;

    The following examples use UTL_TO_EMBEDDING to generate an embedding with parrots.jpg as input and modality as image, by accessing the Vertex AI's multimodal embedding model.

    -- 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));

    End-to-end examples:

    To run end-to-end example scenarios using UTL_TO_EMBEDDING, see Generate Embedding.

  • The following example uses UTL_TO_EMBEDDINGS to generate embeddings with a PDF document (stored in the documentation_tab table) as input, by calling an ONNX format model loaded into the database:
    SELECT et.* from documentation_tab dt,dbms_vector.utl_to_embeddings(
      dbms_vector.utl_to_chunks(dbms_vector.utl_to_text(dt.data)),
      json(:embed_params)) et;

    End-to-end examples:

    To run end-to-end example scenarios using UTL_TO_EMBEDDINGS, see Perform Chunking With Embedding.