Skip to content

Generate Embedding

The generate_embeddings function has this signature:

function generate_embeddings (
p_input in json_array_t
, p_provider in provider_type
, p_model in model_type
) return json_array_t;

p_input receives an array of strings. generate_embeddings embeds them in one call and returns an array of embeddings.

ParameterTypeRequiredDescription
p_inputjson_array_tYesArray of texts to embed
p_providerprovider_typeYesAI provider to use. See uc_ai spec for constants.
p_modelmodel_typeYesSpecific AI model to use (varies by provider). See uc_ai spec for constants.

The return is an array of arrays. As you can pass multiple input texts you can get back multiple vectors. The first vector represents the first input text and so on.

There is also an overload with an extra p_config parameter that derives its configuration from a JSON object instead of the package globals — the same config-driven mechanism as generate_text. The globals are neither read nor mutated, so other libraries can request embeddings self-contained and re-entrantly. Custom HTTP headers (uc_ai.g_extra_headers or the g_extra_headers config key) apply to embedding requests too:

function generate_embeddings (
p_input in json_array_t
, p_provider in provider_type
, p_model in model_type
, p_config in json_object_t
) return json_array_t;
l_result := uc_ai.generate_embeddings(
p_input => json_array_t('["hello world", "oracle database"]'),
p_provider => uc_ai.c_provider_openai,
p_model => uc_ai_openai.c_model_text_embedding_3_small,
p_config => json_object_t('{"openai": {"g_apex_web_credential": "MY_OPENAI_CRED"}}')
);
declare
l_result json_array_t;
l_array_clob clob;
begin
uc_ai.g_base_url := 'host.containers.internal:11434/api';
uc_ai.g_enable_tools := false; -- disable tools for this test
uc_ai.g_enable_reasoning := false; -- disable reasoning for this test
l_result := uc_ai.generate_embeddings(
p_input => json_array_t('["APEX Office Print lets you create and manage print jobs directly from your APEX applications.", "UC AI allows you to easily use AI from PL/SQL"]'),
p_provider => uc_ai.c_provider_ollama,
p_model => 'granite-embedding:30m'
);
-- example result:
-- [
-- [0.340329043034, 0.2334029034, ...]
-- [0.954233454545, 0.5343434023, ...]
-- ]
-- l_result.get_size → 2
-- l_result.get(0) → [0.340329043034, 0.2334029034, ...]
end;

You can convert the result to an Oracle Vector datatype on 23ai or 26ai with the following command (docs):

declare
l_result json_array_t := json_array_t('[0.340329043034, 0.2334029034]');
l_my_vector vector;
begin
-- convert JSON array to Oracle native vector type:
l_my_vector := vector(l_result.to_clob);
-- log Oracle native vector type
sys.dbms_output.put_line(from_vector(l_my_vector));
end;

On older databases you can send the JSON array vector to any third party vector database or process it manually.