Skip to main content

APEX 24.2 RAG Search with apex_ai - Search your Database Records

Published 3 min readMarkdown
APEX 24.2 RAG Search with apex_ai - Search your Database Records

In this blog,

  1. We are going Vectorize our EMP table by creating embeddings.

  2. We’re going to perform a Vector Search for ‘Who is the President?’

  3. When the record comes back we’ll use Generative AI with the same question to give a natural language response.

I want to present this blog in as few steps are possible. If you want to search PDFs, click here

I like to think of embeddings like the Oracle Date Datatype. I imagine the Date Datatype as some mysterious value that I cannot ever see. I can only ever see a TO_CHAR’ed version of it.. ultimately Oracle probably store it as a non-text value. Same with embeddings; its just some internal format which when I TO_CHAR it, it looks like an array of numbers.

Prerequisites

  • A 23ai Oracle Database - get one here

  • An Generative AI Service - get one here

  • A Vector Provider i.e ONNX (I dont have a guide yet) or OCI (Click Here)

  • Install The EMP/DEPT APEX Sample data set

Database Configuration

  1. Add a VECTOR column

     alter table EMP add
     ("EMBEDDINGS" VECTOR);
    
  1. Update all Rows with the Embedding of a JSON Object representing the row.

     BEGIN
     FOR x in ( SELECT empno, JSON_OBJECT(*) AS emp_dept_json FROM EMP_DEPT_V )
     LOOP
       UPDATE EMP 
          SET EMBEDDINGS =  apex_ai.get_vector_embeddings(
             p_value             => x.emp_dept_json,
             p_service_static_id => 'OCI_VECTOR' )
         WHERE EMPNO = x.EMPNO;
     END LOOP;
     END;
    
  2. Test it out by performing a Vector Search for the President

     SELECT JSON_OBJECT( v.* ) x
       FROM EMP e, EMP_DEPT_V v 
       WHERE e.empno = v.empno
       ORDER BY vector_distance(embeddings, apex_ai.get_vector_embeddings(
             p_value             => 'Who is the President?',
             p_service_static_id => 'OCI_VECTOR' ))
       FETCH FIRST 1 ROW ONLY;
    

  1. Ask AI to make sense of all this: Paste this as a computation or something

  2.  DECLARE
       -- Constants
       c_prompt CONSTANT VARCHAR2(4000) := 'Who is the President?';
       c_system_prompt CONSTANT VARCHAR2(4000) := 
         'I will pass you the question followed by a JSON representation of the record. 
          Your answer must be less than 255 Characters. 
          Your response must only relate to the JSON I send you.';
       c_service_static_id_vector CONSTANT VARCHAR2(100) := 'OCI_VECTOR';
       c_service_static_id_ai CONSTANT VARCHAR2(100) := 'oci';
    
       -- Variable
       l_response CLOB;
     BEGIN
       -- Query to fetch JSON object of the closest match
       SELECT JSON_OBJECT(v.*) x
         INTO l_response
         FROM EMP e, EMP_DEPT_V v 
         WHERE e.empno = v.empno
         ORDER BY vector_distance(
                   embeddings, 
                   apex_ai.get_vector_embeddings(
                     p_value             => c_prompt,
                     p_service_static_id => c_service_static_id_vector))
         FETCH FIRST 1 ROW ONLY;
    
       -- Generate AI response using APEX_AI package
       l_response := apex_ai.generate(
         p_prompt            => 'Who is the President and when was he hired ' || l_response,
         p_system_prompt     => c_system_prompt,
         p_service_static_id => c_service_static_id_ai);
    
       RETURN l_response;
     END;
    

Troubleshooting

If you see this… you cannot call apex_ai.generate outside an APEX Session.

ENJOY!

What's the picture? Its a view of The Inspire at Hornbeam Park. Take a look inside. Visit Yorkshire!

About the author

  • With around 20 years on the job, Matt is one of the most experienced software developers at Pretius. He likes meeting new people, traveling to conferences, and working on different projects.

    He’s also a big sports fan (regularly watches Leeds United, Formula 1, and boxing), and not just as a spectator – he often starts his days on a mountain bike, to tune his mind.

No comments yet. Be the first to comment.