Vector Databases – Part 6 – SQL function/trigger using OpenAI
In my previous post, I gave examples of using Cohere to create vector embeddings using SQL and of using a Trigger to populate a Vector column. This post extends those concepts, and in this post, we will use OpenAI.
Warning: At the time of writing this post there is a bug in Oracle 23.5 and 23.6 that limits the OpenAI key to a maximum of 130 characters. The newer project-based API keys can generate keys which are greater than 130 characters. You might get lucky with getting a key of appropriate length or you might have to generate several. An alternative to to create a Legacy (or User Key). But there is no guarantee how long these will be available.
Assuming you have an OpenAI API key of 130 characters or less you can follow the remaining steps. This is now a know bug for the Oracle Database (23.5, 23.6) and it should be fixed in the not-too-distant future. Hopefully!
In my previous post I’ve already added to the ACL (Access Control List) the ability to run against any host. The command to do that was easy, perhaps too easy, as it will allow the ‘vectorai’ schema to access any website etc. I really should have limited it to the address of Cohere and in this post to OpenAI. Additionally, I should have limited to specific port numbers. That’s a bit of security risk and in your development, test and production environment you should have these restrictions.
In the ‘vectorai’ schema we need to create a new credential to store the OpenAI key. I’ve called this credential CRED_OPENAI
DECLARE
jo json_object_t;
BEGIN
jo := json_object_t();
jo.put('access_token', '...');
dbms_vector.create_credential(
credential_name => 'CRED_OPENAI',
params => json(jo.to_string));
END;
Next, we can test calling the embedding model from OpenAI. The embedding model used in this example is called ‘text-embedding-3-small’
declare
input clob;
params clob;
output clob;
v VECTOR;
begin
-- input := 'hello';
input := 'Aromas include tropical fruit, broom, brimstone and dried herb. The palate isnt overly expressive, offering unripened apple, citrus and dried sage alongside brisk acidity.';
params := '
{
"provider": "OpenAI",
"credential_name": "CRED_OPEAI",
"url": "https://api.openai.com/v1/embeddings",
"model": "text-embedding-3-small"
}';
v := dbms_vector.utl_to_embedding(input, json(params));
output := to_clob(v);
dbms_output.put_line('VECTOR');
dbms_output.put_line('--------------------');
dbms_output.put_line(dbms_lob.substr(output,1000)||'...');
exception
when OTHERS THEN
DBMS_OUTPUT.PUT_LINE (SQLERRM);
DBMS_OUTPUT.PUT_LINE (SQLCODE);
end;
And in a similar manner to the Cohere example we can create a trigger to popular a Vector column.
Warning: You should not mix the use of different embedding models when creating vectors. A vector column should only have vectors created by the same embedding models, and not from two different models.
create or replace trigger vec_test_trig2
before insert or update on vec_test
for each row
declare
params clob;
v vector;
begin
params := '{
"provider": "OpenAI",
"credential_name": "CRED_OPEAI",
"url": "https://api.openai.com/v1/embeddings",
"model": "text-embedding-3-small"
}';
v := dbms_vector.utl_to_embedding(:new.col2, json(params));
:new.col3 := v;
end;
This entry was posted in 23ai, Vector Database, Vector Embeddings and tagged Vector Database, Vector Embedding.