Below is a detailed blog post tailored for Oracle APEX developers, explaining Retrieval-Augmented Generation (RAG) in APEX 24.2. It includes a step-by-step guide and a sample use case with complete code. This is designed to be practical, engaging, and immediately useful for APEX developers looking to leverage AI in their applications.
Mastering Retrieval-Augmented Generation (RAG) in Oracle APEX 24.2: A Complete Guide for Developers
Oracle APEX 24.2 brings a wave of AI-driven enhancements, and one of the standout features is Retrieval-Augmented Generation (RAG). If you’re an APEX developer eager to make your applications smarter by integrating real-time, context-aware AI responses grounded in your own data, this blog is for you. We’ll dive into what RAG is, how it works in APEX 24.2, and provide a step-by-step guide with a sample use case—complete with code—to get you started. By the end, you’ll have a working RAG-powered feature in your APEX app. Let’s get to it!
What is RAG, and Why Should APEX Developers Care?
Retrieval-Augmented Generation (RAG) is a technique that supercharges AI by combining two steps: 1. Retrieval: Fetching relevant data from an external source (like your database). 2. Generation: Using an AI model (e.g., via Oracle’s AI services) to craft a response based on that data.
Unlike traditional AI that relies solely on pre-trained knowledge (which can be outdated or generic), RAG ensures responses are fresh, specific, and tied to your application’s data. In APEX 24.2, this is made declarative and seamless through AI Configurations and RAG Sources, allowing you to enhance the APEX AI Assistant or power custom AI-driven features.
For APEX developers, this means: – Contextual Intelligence: Deliver answers based on your app’s live data. – Low-Code Power: Set it up without writing complex AI code. – Real-World Impact: Solve problems like customer queries, data summaries, or dynamic content generation.
RAG in APEX 24.2: How It Works
In APEX 24.2, RAG is baked into the platform via: – AI Configurations: A new Shared Component where you define system prompts, welcome messages, and RAG Sources. – RAG Sources: Dynamic data feeds (SQL queries, PL/SQL functions, or static text) that the AI uses to augment its responses. – Integration Points: Use RAG with the APEX AI Assistant or the “Generate Text with AI” Dynamic Action.
The process is straightforward: 1. A user asks a question (e.g., through a text field or AI Assistant). 2. APEX retrieves relevant data from RAG Sources based on the prompt. 3. The AI combines the retrieved data with the user’s input to generate a response.

Step-by-Step Guide: Building a RAG-Powered Feature
Let’s walk through setting up RAG in APEX 24.2, step by step. Our goal? Create a simple customer support app where users can ask about their orders, and the AI responds with accurate, data-driven answers.

Step 1: Set Up Your APEX Environment
- Ensure you’re using Oracle APEX 24.2 with an Oracle Database (ideally 23ai for vector search, but 19c+ works for basic RAG).

- Configure an AI Service (e.g., Oracle OCI Generative AI or a third-party provider like OpenAI) in your workspace under Shared Components > AI Services.

Step 2: Create a Sample Table
We’ll simulate a customer orders database. Run this SQL in SQL Workshop:
CREATE TABLE customer_orders (
order_id NUMBER PRIMARY KEY,
customer_id NUMBER,
order_status VARCHAR2(20),
order_date DATE,
details VARCHAR2(4000)
);

INSERT INTO customer_orders VALUES (1, 101, ‘Shipped’, SYSDATE – 2, ‘Order of 5 laptops, shipped via FedEx.’);
INSERT INTO customer_orders VALUES (2, 102, ‘Pending’, SYSDATE – 1, ‘Order of 3 monitors, awaiting stock.’);
INSERT INTO customer_orders VALUES (3, 101, ‘Delivered’, SYSDATE – 5, ‘Order of 1 printer, delivered to office.’);

Step 3: Define an AI Configuration
- Go to Shared Components > AI Configurations.
- Click Create.
- Set:
- Name: OrderSupportAI
- System Prompt: You are a customer support assistant. Use the provided order data to answer user questions accurately.
- Welcome Message: Ask me anything about your orders!
- Save it for now—we’ll add RAG Sources next.

Step 4: Add a RAG Source
- In the OrderSupportAI configuration, go to RAG Sources.
- Click Create RAG Source.

- Set:
- Name: OrderDataType: SQL QuerySQL Query:SELECT
‘Order #’ || order_id || ‘ (‘ || order_date || ‘): ‘ ||
‘Status: ‘ || order_status || ‘, ‘ ||
‘Details: ‘ || details AS order_info
FROM customer_orders
WHERE customer_id = NVL(:CURRENT_USER_ID, 101)
AND (
1=1 — Return all orders if no specific keywords
OR UPPER(order_status) LIKE ‘%’ || UPPER(:APEX$AI_LAST_USER_PROMPT) || ‘%’
OR UPPER(details) LIKE ‘%’ || UPPER(:APEX$AI_LAST_USER_PROMPT) || ‘%’
)
- Condition: Leave blank (runs for all prompts), or add a server-side condition like User Prompt Contains with keywords like “order”.
- Name: OrderDataType: SQL QuerySQL Query:SELECT

- Explanation:
- :CURRENT_USER_ID ties results to the logged-in user.
- :APEX$AI_LAST_USER_PROMPT dynamically filters based on the user’s question.
- Save the RAG Source.
Step 5: Create a Page with AI Interaction
- Create a new Blank Page (e.g., Page 10).

- Add a Text Field:
- Name: P10_QUESTION
- Label: Ask About Your Order

- Add a Button:
- Name: SUBMIT_QUESTIONLabel: Get AnswerAction: Submit Page
- Position: Below P10_QUESTION

- Add a Display Only item:
- Name: P10_RESPONSE
- Label: AI Response

Step 6: Add the “Generate Text with AI” Dynamic Action
- Right-click the SUBMIT button, select Create Dynamic Action.
- Set:
- Name: Generate AI Response
- Event: Click
- Selection Type: Button
- Button: SUBMIT_QUESTION

- For True Action:
- Action: Generate Text with AI
- AI Configuration: OrderSupportAI
- Input Value Item: P10_QUESTION
- User Response Item: P10_RESPONSE

- Save the Dynamic Action.
Step 7: Test Your RAG Feature
- Log in as a user with customer_id = 101 (set this via :CURRENT_USER_ID in your app’s authentication).

- Go to Page 10.
- Enter: What’s the status of my laptop order?
- Click Submit.
- Expected Output in P10_RESPONSE: Something like, Your order of 5 laptops (Order ID: 1) is currently Shipped via FedEx.


Sample Use Case: Customer Order Inquiry
Let’s flesh out the use case with all required code and a realistic scenario.
Scenario: A customer wants to know about their recent orders. Instead of manually querying the database, they ask the AI in natural language, and RAG pulls the data to respond.
Complete Code Recap: – Table Setup: See Step 2. – AI Configuration: – System Prompt: You are a customer support assistant. Use the provided order data to answer user questions accurately. – RAG Source SQL: sql SELECT order_id, order_status, details FROM customer_orders WHERE customer_id = :CURRENT_USER_ID AND UPPER(details) LIKE ‘%’ || UPPER(:APEX$AI_LAST_USER_PROMPT) || ‘%’ – Page Components: – P10_QUESTION: Text Field. – SUBMIT: Button. – P10_RESPONSE: Display Only. – Dynamic Action: – Action: Generate Text with AI – Prompt: &P10_QUESTION. – Target: P10_RESPONSE
Enhancing the Use Case: – Add a PL/SQL Function RAG Source for more complex logic: sql FUNCTION get_order_summary RETURN CLOB IS l_summary CLOB; BEGIN SELECT LISTAGG(‘Order ‘ || order_id || ‘: ‘ || order_status || ‘ – ‘ || details, CHR(10)) INTO l_summary FROM customer_orders WHERE customer_id = :CURRENT_USER_ID; RETURN l_summary; END; – Update the RAG Source Type to Function Returning CLOB and use this function. – Test with: Summarize my recent orders. Result: A neat list of all orders for the user.
Tips for APEX Developers
- Dynamic Filtering: Use :APEX$AI_LAST_USER_PROMPT creatively to match user intent (e.g., parse for keywords like “shipped” or “pending”).
- Security: Apply row-level security in your SQL to ensure users only see their data.
- Performance: Limit RAG Source results (e.g., FETCH FIRST 5 ROWS ONLY) to keep AI responses focused.
- Debugging: Check the AI Assistant logs in Shared Components > AI Logs if responses aren’t as expected.
Why This Matters
With RAG in APEX 24.2, you’re not just building apps—you’re crafting intelligent experiences. Whether it’s customer support, data insights, or dynamic content, RAG lets you harness AI without leaving the low-code comfort of APEX. This guide gives you the foundation; now, experiment with your own data and use cases!
Got questions or want to share your RAG-powered APEX app? Drop a comment below—I’d love to hear from you. Happy coding, APEX devs!
This blog provides a practical, hands-on introduction to RAG in APEX 24.2, complete with a usable example. It’s tailored to engage developers by focusing on real-world application and minimizing fluff, while ensuring all steps and code are clear and actionable. Let me know if you’d like to tweak anything further!