Sample templates for registering model endpoints

This document shows the providers that model endpoint management supports. Select the provider, filter the model endpoint you want to create, and then enter the model ID and other parameters to generate a create model SQL query.

Provider Model type Auth with Secret Manager Auth with custom header Sample SQL query
custom text_embedding Yes No
CALL google_ml.create_sm_secret(
secret_id => 'UNIQUE_CUSTOM_SECRET_ID',
secret_path => 'projects/PROJECT_ID/secrets/SECRET_NAME/versions/VERSION_NUMBER');

CREATE OR REPLACE FUNCTION custom_text_embedding_input_transform(model_id VARCHAR(100), input_text TEXT)
RETURNS JSON
LANGUAGE plpgsql
AS $$
#variable_conflict use_variable
DECLARE
  transformed_input JSON;
BEGIN
  -- Lookup model details, if required, using model_id through convenience functions
  -- Transform input_text into JSON format required by the model.
  SELECT json_build_object('input_text_key', json_build_array(input_text))::JSON INTO transformed_input;
  RETURN transformed_input;
END;
$$;

CREATE OR REPLACE FUNCTION custom_text_embedding_output_transform(
  model_id VARCHAR(100), response_json JSON)
RETURNS REAL[]
LANGUAGE plpgsql
AS $$
DECLARE
  transformed_output REAL[];
BEGIN
  -- Lookup model details, if required, using model_id through convenience functions.
  -- Transform response JSON from the model into the desired output
  SELECT ARRAY(SELECT json_array_elements_text(response_json->'some_field')) INTO transformed_output;
  RETURN transformed_output;
END;
$$;

CALL google_ml.create_model(
model_id => 'UNIQUE_EMB_MODEL_ID_1',
model_request_url => 'CUSTOM_ENDPOINT_URL',
model_provider => 'custom',
model_type => 'text_embedding',
model_auth_type => 'secret_manager',
model_auth_id => 'UNIQUE_CUSTOM_SECRET_ID',
model_in_transform_fn => 'custom_text_embedding_input_transform',
model_out_transform_fn => 'custom_text_embedding_output_transform');
custom text_embedding Yes No
CALL google_ml.create_sm_secret(
secret_id => 'UNIQUE_CUSTOM_SECRET_ID',
secret_path => 'projects/PROJECT_ID/secrets/SECRET_NAME/versions/VERSION_NUMBER');

CREATE OR REPLACE FUNCTION custom_sample_header_gen_fn(model_id VARCHAR(100), input_text TEXT)
-- Sample custom header generation function, update as required
RETURNS JSON
LANGUAGE plpgsql
AS $$
#variable_conflict use_variable
BEGIN
      RETURN json_build_object('CUSTOM_HEADER_KEY1', 'CUSTOM_HEADER_VALUE1', 'CUSTOM_HEADER_KEY2', 'CUSTOM_HEADER_VALUE2')::JSON;
END;
$$;

CREATE OR REPLACE FUNCTION custom_text_embedding_input_transform(model_id VARCHAR(100), input_text TEXT)
RETURNS JSON
LANGUAGE plpgsql
AS $$
#variable_conflict use_variable
DECLARE
  transformed_input JSON;
BEGIN
  -- Lookup model details, if required, using model_id through convenience functions
  -- Transform input_text into JSON format required by the model.
  SELECT json_build_object('input_text_key', json_build_array(input_text))::JSON INTO transformed_input;
  RETURN transformed_input;
END;
$$;

CREATE OR REPLACE FUNCTION custom_text_embedding_output_transform(
  model_id VARCHAR(100), response_json JSON)
RETURNS REAL[]
LANGUAGE plpgsql
AS $$
DECLARE
  transformed_output REAL[];
BEGIN
  -- Lookup model details, if required, using model_id through convenience functions.
  -- Transform response JSON from the model into the desired output
  SELECT ARRAY(SELECT json_array_elements_text(response_json->'some_field')) INTO transformed_output;
  RETURN transformed_output;
END;
$$;

CALL google_ml.create_model(
model_id => 'UNIQUE_EMB_MODEL_ID_2',
model_request_url => 'CUSTOM_ENDPOINT_URL',
model_provider => 'custom',
model_type => 'text_embedding',
model_auth_type => 'secret_manager',
model_auth_id => 'UNIQUE_CUSTOM_SECRET_ID',
generate_headers_fn => 'custom_sample_header_gen_fn',
model_in_transform_fn => 'custom_text_embedding_input_transform',
model_out_transform_fn => 'custom_text_embedding_output_transform');
custom text_embedding No No
CREATE OR REPLACE FUNCTION custom_text_embedding_input_transform(model_id VARCHAR(100), input_text TEXT)
RETURNS JSON
LANGUAGE plpgsql
AS $$
#variable_conflict use_variable
DECLARE
  transformed_input JSON;
BEGIN
  -- Lookup model details, if required, using model_id through convenience functions
  -- Transform input_text into JSON format required by the model.
  SELECT json_build_object('input_text_key', json_build_array(input_text))::JSON INTO transformed_input;
  RETURN transformed_input;
END;
$$;

CREATE OR REPLACE FUNCTION custom_text_embedding_output_transform(
  model_id VARCHAR(100), response_json JSON)
RETURNS REAL[]
LANGUAGE plpgsql
AS $$
DECLARE
  transformed_output REAL[];
BEGIN
  -- Lookup model details, if required, using model_id through convenience functions.
  -- Transform response JSON from the model into the desired output
  SELECT ARRAY(SELECT json_array_elements_text(response_json->'some_field')) INTO transformed_output;
  RETURN transformed_output;
END;
$$;

CALL google_ml.create_model(
model_id => 'UNIQUE_EMB_MODEL_ID_3',
model_request_url => 'CUSTOM_ENDPOINT_URL',
model_provider => 'custom',
model_type => 'text_embedding',
model_in_transform_fn => 'custom_text_embedding_input_transform',
model_out_transform_fn => 'custom_text_embedding_output_transform');
custom text_embedding No Yes
CREATE OR REPLACE FUNCTION custom_sample_custom_auth_header_gen_fn(model_id VARCHAR(100), input_text TEXT)
-- Sample auth header and custom header generation function, update as required
RETURNS JSON
LANGUAGE plpgsql
AS $$
#variable_conflict use_variable
BEGIN
      RETURN json_build_object('AUTH_HEADER_NAME', 'CUSTOM_API_KEY', 'CUSTOM_HEADER_KEY1', 'CUSTOM_HEADER_VALUE1', 'CUSTOM_HEADER_KEY2', 'CUSTOM_HEADER_VALUE2')::JSON;
END;
$$;

CREATE OR REPLACE FUNCTION custom_text_embedding_input_transform(model_id VARCHAR(100), input_text TEXT)
RETURNS JSON
LANGUAGE plpgsql
AS $$
#variable_conflict use_variable
DECLARE
  transformed_input JSON;
BEGIN
  -- Lookup model details, if required, using model_id through convenience functions
  -- Transform input_text into JSON format required by the model.
  SELECT json_build_object('input_text_key', json_build_array(input_text))::JSON INTO transformed_input;
  RETURN transformed_input;
END;
$$;

CREATE OR REPLACE FUNCTION custom_text_embedding_output_transform(
  model_id VARCHAR(100), response_json JSON)
RETURNS REAL[]
LANGUAGE plpgsql
AS $$
DECLARE
  transformed_output REAL[];
BEGIN
  -- Lookup model details, if required, using model_id through convenience functions.
  -- Transform response JSON from the model into the desired output
  SELECT ARRAY(SELECT json_array_elements_text(response_json->'some_field')) INTO transformed_output;
  RETURN transformed_output;
END;
$$;

CALL google_ml.create_model(
model_id => 'UNIQUE_EMB_MODEL_ID_4',
model_request_url => 'CUSTOM_ENDPOINT_URL',
model_provider => 'custom',
model_type => 'text_embedding',
generate_headers_fn => 'custom_sample_custom_auth_header_gen_fn',
model_in_transform_fn => 'custom_text_embedding_input_transform',
model_out_transform_fn => 'custom_text_embedding_output_transform');
custom generic Yes No
CALL google_ml.create_sm_secret(
secret_id => 'UNIQUE_CUSTOM_SECRET_ID',
secret_path => 'projects/PROJECT_ID/secrets/SECRET_NAME/versions/VERSION_NUMBER');

CALL google_ml.create_model(
model_id => 'UNIQUE_GEN_MODEL_ID_1',
model_request_url => 'USER_PROVIDED_URL',
model_provider => 'custom',
model_type => 'generic',
model_auth_type => 'secret_manager',
model_auth_id => 'UNIQUE_CUSTOM_SECRET_ID');
custom generic Yes No
CALL google_ml.create_sm_secret(
secret_id => 'UNIQUE_CUSTOM_SECRET_ID',
secret_path => 'projects/PROJECT_ID/secrets/SECRET_NAME/versions/VERSION_NUMBER');

CREATE OR REPLACE FUNCTION custom_sample_header_gen_fn(model_id VARCHAR(100), input_text TEXT)
-- Sample custom header generation function, update as required
RETURNS JSON
LANGUAGE plpgsql
AS $$
#variable_conflict use_variable
BEGIN
      RETURN json_build_object('CUSTOM_HEADER_KEY1', 'CUSTOM_HEADER_VALUE1', 'CUSTOM_HEADER_KEY2', 'CUSTOM_HEADER_VALUE2')::JSON;
END;
$$;

CALL google_ml.create_model(
model_id => 'UNIQUE_GEN_MODEL_ID_2',
model_request_url => 'USER_PROVIDED_URL',
model_provider => 'custom',
model_type => 'generic',
model_auth_type => 'secret_manager',
model_auth_id => 'UNIQUE_CUSTOM_SECRET_ID',
generate_headers_fn => 'custom_sample_header_gen_fn');
custom generic No No
CALL google_ml.create_model(
model_id => 'UNIQUE_GEN_MODEL_ID_3',
model_request_url => 'USER_PROVIDED_URL',
model_provider => 'custom',
model_type => 'generic');
custom generic No Yes
CREATE OR REPLACE FUNCTION custom_sample_custom_auth_header_gen_fn(model_id VARCHAR(100), input_text TEXT)
-- Sample auth header and custom header generation function, update as required
RETURNS JSON
LANGUAGE plpgsql
AS $$
#variable_conflict use_variable
BEGIN
      RETURN json_build_object('AUTH_HEADER_NAME1', 'CUSTOM_API_KEY', 'CUSTOM_HEADER_KEY1', 'CUSTOM_HEADER_VALUE1', 'CUSTOM_HEADER_KEY2', 'CUSTOM_HEADER_VALUE2')::JSON;
END;
$$;

CALL google_ml.create_model(
model_id => 'UNIQUE_GEN_MODEL_ID_4',
model_request_url => 'USER_PROVIDED_URL',
model_provider => 'custom',
model_type => 'generic',
generate_headers_fn => 'custom_sample_custom_auth_header_gen_fn');