UTL_TO_GENERATE_TEXT

Use the DBMS_VECTOR_CHAIN.UTL_TO_GENERATE_TEXT chainable utility function to generate a text response for a given prompt or an image, by accessing third-party text generation models.

Purpose

To communicate with Large Language Models (LLMs) through natural language conversations. You can generate a textual answer, description, or summary for prompts and images, given as input to LLM-powered chat interfaces.

  • Prompt to Text:

    A prompt can be an input text string, such as a question that you ask an LLM. For example, "What is Oracle Text?". A prompt can also be a command, such as "Summarize the following ...", "Draft an email asking for ...", or "Rewrite the following ...", and can include results from a search. The LLM responds with a textual answer or description based on the specified task in the prompt.

    For this operation, this API makes a REST call to either a remote third-party provider (Cohere, Google AI, Hugging Face, Generative AI, OpenAI, or Vertex AI) or a local third-party provider (Ollama).

  • Image to Text:

    You can also prompt with a media file, such as an image, to extract text from pictures or photos. You supply a text question as the prompt (such as "What is this image about?" or "How many birds are there in this painting?") along with the image. The LLM responds with a textual analysis or description of the contents of the image.

    For this operation, this API makes a REST call to either a remote third-party provider (Google AI, Hugging Face, OpenAI, or Vertex AI) or a local third-party provider (Ollama).

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

This function accepts the input as CLOB containing text data (for textual prompts) or as BLOB containing media data (for media files such as images). It then processes this information to generate a new CLOB containing the generated text.

  • Prompt to Text:

    DBMS_VECTOR_CHAIN.UTL_TO_GENERATE_TEXT (
        	DATA          IN CLOB,
        	PARAMS        IN JSON default NULL
    ) return CLOB;
  • Image to Text:

    DBMS_VECTOR_CHAIN.UTL_TO_GENERATE_TEXT(
              TEXT_DATA      IN CLOB,
              MEDIA_DATA     IN BLOB,
              MEDIA_TYPE     IN VARCHAR2 default 'image/jpeg',
              PARAMS         IN JSON default NULL
    ) return CLOB;

DATA and TEXT_DATA

Specify the textual prompt as CLOB for the DATA or TEXT_DATA clause.

Note:

Hugging Face uses an image captioning model that does not require a prompt, when giving an image as input. If you input a prompt along with an image, then the prompt will be ignored.

MEDIA_DATA

Specify the BLOB file, such as an image or a visual PDF file.

MEDIA_TYPE

Specify the image format for the given image or visual PDF file (BLOB file) in one of the supported image data MIME types. For example:

  • For PNG: image/png

  • For JPEG: image/jpeg

  • For PDF: application/pdf

Note:

For a complete list of the supported image formats, refer to your third-party provider's documentation.

PARAMS

Specify the following input parameters in JSON format, depending on the service provider that you want to access for text generation:

{
  "provider"        : "<AI service provider>",
  "credential_name" : "<credential name>",  
  "url"             : "<REST endpoint URL for text generation service>",
  "model"           : "<text generation model name>",
  "transfer_timeout": <maximum wait time for the request to complete>,
  "<additional REST provider parameter>": "<REST provider parameter value>"
}

Table 12-28 UTL_TO_GENERATE_TEXT Parameter Details

Parameter Description

provider

Supported REST provider that you want to access to generate text.

Specify one of the following values:

For CLOB input:

  • cohere

  • googleai

  • huggingface

  • ocigenai

  • openai

  • vertexai

For BLOB input:

  • googleai

  • huggingface

  • 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_CHAIN.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 text generation model in the form:

schema.model_name

If the model name is not schema-qualified, then the schema of the procedure invoker is used.

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.

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/chat",
  "model"          : "generate-text-model"
}
Google AI example:
{
  "provider"        : "googleai",
  "credential_name" : "GOOGLEAI_CRED",
  "url"             : "https://googleapis.example.com/models/",
  "model"           : "generate-text-model"
}
Hugging Face example:
{
  "provider"        : "huggingface",
  "credential_name" : "HF_CRED",
  "url"             : "https://api.huggingface.example.com/models/",
  "model"           : "generate-text-model"
}
Generative AI example:
{
  "provider"        : "ocigenai", 
  "credential_name" : "GENAI_CRED",
  "url"             : "https://generativeai.oci.example.com/generateText",
  "model"           : "generate-text-model",
  "inferenceRequest": {
    "maxTokens"     : 300,
    "temperature"   : 1
  }
}
or more current chat models may implement:
{
  "provider"        : "ocigenai",
  "credential_name" : "OCI_CRED",
  "url"             : "https://inference.generativeai.oci.example.com/actions/chat",
  "model"           : "generate-text-model",
  "chatRequest"     : {
    "maxTokens"     : 256
  }
}
Ollama example:
{
  "provider"       : "ollama", 
  "host"           : "local", 
  "url"            : "http://localhost:11434/api/generate", 
  "model"          : "llama3"
}
OpenAI example:
{
  "provider"        : "openai",
  "credential_name" : "OPENAI_CRED",
  "url"             : "https://api.openai.example.com",
  "model"           : "generate-text-model",
  "max_tokens"      : 60,
  "temperature"     : 1.0
}
Vertex AI example:
{
  "provider"         : "vertexai",
  "credential_name"  : "VERTEXAI_CRED",
  "url"              : "https://googleapis.example.com/models/",
  "model"            : "generate-text-model",
  "generation_config": {
    "temperature"    : 0.9,
    "topP"           : 1,
    "candidateCount" : 1,
    "maxOutputTokens": 256
  }
}

Table 12-29 Additional REST Provider Parameter Details

Parameter Description

max_tokens

Maximum number of tokens in the output text.

temperature

Degree of randomness used when generating the output text, in the range of 0.0-5.0.

To generate the same output for a prompt, use 0. To generate a random new text for that prompt, increase the temperature.

Note: Start with the temperature set to 0. If you do not require random results, a recommended temperature value is between 0 and 1. A higher value is not recommended because a high temperature may produce creative text, which might also include hallucinations.

topP

Probability of tokens in the output, in the range of 0.0–1.0.

A lower value provides less random responses and a higher value provides more random responses.

candidateCount

Number of response variations to return, in the range of 1-4.

maxOutputTokens

Maximum number of tokens to generate for each response.

Examples

  • Prompt to Text:

    The following statements generate text by making a REST call to Generative AI. Here, the prompt is "What is Oracle Text?".

    -- select example
    
    var params clob;
    exec :params := '
    {
      "provider": "ocigenai",
      "credential_name": "OCI_CRED",
      "url": "https://generativeai.oci.example.com/generateText",
      "model": "generate.modelname"
    }';
    
    select dbms_vector_chain.utl_to_generate_text(
     'What is Oracle Text?',
     json(:params)) from dual;
    
    -- PL/SQL example
    
    declare
      input clob;
      params clob;
      output clob;
    begin
      input := 'What is Oracle Text?';
    
      params := '
    {
      "provider": "ocigenai",
      "credential_name": "OCI_CRED",
      "url": "https://generativeai.oci.example.com/generateText",
      "model": "generate.modelname"
    }';
    
      output := dbms_vector_chain.utl_to_generate_text(input, json(params));
      dbms_output.put_line(output);
      if output is not null then
        dbms_lob.freetemporary(output);
      end if;
    exception
      when OTHERS THEN
        DBMS_OUTPUT.PUT_LINE (SQLERRM);
        DBMS_OUTPUT.PUT_LINE (SQLCODE);
    end;
    /

    End-to-end examples:

    To run end-to-end example scenarios, see Generate Text Response.

  • Image to Text:

    The following statements generate text by making a REST call to OpenAI. Here, the input is an image (sample_image.jpeg) along with the prompt "Describe this image?".

    -- select example
    
    var input clob;
    var media_data blob;
    var media_type clob;
    var params clob;
    
    begin
      :input := 'Describe this image';
      :media_data := load_blob_from_file('DEMO_DIR', 'sample_image.jpeg');
      :media_type := 'image/jpeg';
      :params := '
    {
      "provider": "openai",
      "credential_name": "OPENAI_CRED",
      "url": "https://api.openai.example.com/chat/completions",
      "model": "generate.modelname",
      "max_tokens": 256
    }';
    end;
    /
    
    select dbms_vector_chain.utl_to_generate_text(:input, :media_data, :media_type, json(:params));
    
    -- PL/SQL example
    
    declare
      input clob;
      media_data blob;
      media_type varchar2(32);
      params clob;
      output clob;
    
    begin
      input := 'Describe this image';
      media_data := load_blob_from_file('DEMO_DIR', 'image_file');
      media_type := 'image/jpeg';
      params := '
    {
      "provider": "openai",
      "credential_name": "OPENAI_CRED",
      "url": "https://api.openai.example.com/chat/completions",
      "model": "generate.modelname",
      "max_tokens": 256
    }';
    
      output := dbms_vector_chain.utl_to_generate_text(
        input, media_data, media_type, json(params));
      dbms_output.put_line(output);
    
      if output is not null then
        dbms_lob.freetemporary(output);
      end if;
      if media_data is not null then
        dbms_lob.freetemporary(media_data);
      end if;
    exception
      when OTHERS THEN
        DBMS_OUTPUT.PUT_LINE (SQLERRM);
        DBMS_OUTPUT.PUT_LINE (SQLCODE);
    end;
    /

    End-to-end examples:

    To run end-to-end example scenarios, see Describe Image Content.