In this optional lab, you will learn how to implement Vector Search using an enhanced RAG source for the Learn More button in the Highschools App. You will run the app, ask questions about the school and observe how efficiently the information is filtered with reduced content length thereby saving costs without compromising on the accuracy of responses.
Note: The screenshots in this workshop are taken using Dark Mode in APEX 24.2
Estimated Time: 15 minutes
Watch the video below for a quick walk-through of the lab.
Prerequisites
To run this lab, you should configure an APEX workspace on an Oracle Database 23ai instance.
Objectives
In this lab, you will:
Implement Vector Search using RAG source
Run the app and observe the difference
Task 1: Grant necessary privileges to the Parsing Schema
Run the following SQL commands to insert data into the HIGH_SCHOOL_INFO table.
BEGIN-- Fetching data using FOR loopFOR rec IN (
SELECT
ID,
'Overview of the school : '|| OVERVIEW_PARAGRAPH AS OVERVIEW_PARAGRAPH,
'The following Language Courses are taught here : '|| LANGUAGE_CLASSES AS LANGUAGE_CLASSES,
'The following Advanced Placement Courses are taught : '|| ADVANCEDPLACEMENT_COURSES AS ADVANCED_PLACEMENT_COURSES,
'The following is the Diversity in Admission Policy for this school: '|| DIADETAILS AS DIVERSITY_IN_ADMISSION_POLICY,
'The below extra curricular activities are available : '|| EXTRACURRICULAR_ACTIVITIES AS EXTRACURRICULAR_ACTIVITIES,
'The below are Public Schools Athletic League (PSAL) sports for boys: '|| PSAL_SPORTS_BOYS AS PSAL_SPORTS_BOYS,
'The below are Public Schools Athletic League (PSAL) sports for girls : '|| PSAL_SPORTS_GIRLS AS PSAL_SPORTS_GIRLS,
'Other facilities in this school : '|| ADDTL_INFO1 AS ADDITIONAL_INFO1,
'The following academic opportunities are available : '|| ACADEMIC_OPPORTUNITIES AS ACADEMIC_OPPORTUNITIES,
'Attendance rate : '|| ATTENDANCE_RATE AS ATTENDANCE_RATE,
'Graduation rate : '|| GRADUATION_RATE AS GRADUATION_RATE
FROM HIGHSCHOOLS
)
LOOP
-- Inserting into HIGH_SCHOOL_INFO tableINSERTINTO HIGH_SCHOOL_INFO (SCHOOL_ID, CHUNK_C)
VALUES (rec.ID, rec.OVERVIEW_PARAGRAPH);
INSERTINTO HIGH_SCHOOL_INFO (SCHOOL_ID, CHUNK_C)
VALUES (rec.ID, rec.LANGUAGE_CLASSES);
INSERTINTO HIGH_SCHOOL_INFO (SCHOOL_ID, CHUNK_C)
VALUES (rec.ID, rec.ADVANCED_PLACEMENT_COURSES);
INSERTINTO HIGH_SCHOOL_INFO (SCHOOL_ID, CHUNK_C)
VALUES (rec.ID, rec.DIVERSITY_IN_ADMISSION_POLICY);
INSERTINTO HIGH_SCHOOL_INFO (SCHOOL_ID, CHUNK_C)
VALUES (rec.ID, rec.EXTRACURRICULAR_ACTIVITIES);
INSERTINTO HIGH_SCHOOL_INFO (SCHOOL_ID, CHUNK_C)
VALUES (rec.ID, rec.PSAL_SPORTS_BOYS);
INSERTINTO HIGH_SCHOOL_INFO (SCHOOL_ID, CHUNK_C)
VALUES (rec.ID, rec.PSAL_SPORTS_GIRLS);
INSERTINTO HIGH_SCHOOL_INFO (SCHOOL_ID, CHUNK_C)
VALUES (rec.ID, rec.ADDITIONAL_INFO1);
INSERTINTO HIGH_SCHOOL_INFO (SCHOOL_ID, CHUNK_C)
VALUES (rec.ID, rec.ACADEMIC_OPPORTUNITIES);
INSERTINTO HIGH_SCHOOL_INFO (SCHOOL_ID, CHUNK_C)
VALUES (rec.ID, rec.ATTENDANCE_RATE);
INSERTINTO HIGH_SCHOOL_INFO (SCHOOL_ID, CHUNK_C)
VALUES (rec.ID, rec.GRADUATION_RATE);
END LOOP;
END;
Run the following SQL command to convert the data into vector chunks. Note that this may take a while.
update HIGH_SCHOOL_INFO set chunk_v = apex_ai.get_vector_embeddings(p_value => chunk_c, p_service_static_id =>'db_onnx_model');
Task 5: Create a Search Configuration
Search Configuration contains information about a searchable data source.
From your application homepage, navigate to Shared Components.
Under Navigation and Search, select Search Configurations.
Click Create.
In the Create Search Configuration dialog, enter/select the following:
Name: Highschool Info
Search Type: Oracle Vector Search
Click Next.
Enter/select the following:
Vector Provider: DB ONNX Model
Table/View Name: HIGH_SCHOOL_INFO
Click Next.
Map the table columns to our search configuration. Enter/select the following:
Primary Key Column: ID (Number)
Vector Column: CHUNK_V (Vector)
Title Column: CHUNK_C (Clob)
Click Create Search Configuration.
Ensure that the Static ID is highschool_info in the search configuration.
Under the Source group, for Where Clause, enter the following:
SCHOOL_ID = :P2_SCHOOL_ID
Scroll down and for Maximum Rows to Return, enter 1. Click Apply Changes.
Task 6: Update RAG Source to use AI Vector Search
In this task, we update the RAG source to use the vector we created. The Learn More AI assisted chat will now be able to provide answers based on the vectorized data.
Navigate to Shared Components.
Under Generative AI, select AI Configurations.
Select Learn More AI.
Under RAG Sources, select School Context to edit.
Replace the SQL query with the following SQL command and click Apply Changes:
select title
from table( apex_search.search(
p_search_static_ids => apex_t_varchar2( 'highschool_info'),
p_search_expression => :APEX$AI_LAST_USER_PROMPT ) );
Task 7: Run the Application
From the Generative AI Configuration page, click Run App to see vector search in action.
Click Learn More icon on a School Card of your choice.
Select a Quick Action like 'What is the graduation rate?'.
The chat assistant provides a suitable response. Click Edit App from the developer toolbar.
Navigate to SQL Workshop > SQL commands.
Run the following SQL command to see how the CONTENT_LENGTH column shows significant decrease in the length:
You now know how to implement Vector Search in your APEX app. You understood how vectors can be used in AI configurations to optimize RAG implementations by reducing the content sent to large language models, thereby saving costs without compromising on the accuracy of responses.