๐Ÿš€ Building a Generative AI Chatbot with RAG in Oracle APEX (Step-by-Step Guide)

๐Ÿš€ Building a Generative AI Chatbot with RAG in Oracle APEX (Step-by-Step Guide)

Artificial Intelligence is transforming how users interact with applications. With Generative AI + RAG (Retrieval-Augmented Generation), you can build smart chatbots that not only generate responses but also fetch accurate data from your database.

In this blog, Iโ€™ll walk you through how to build a ChatGPT-like AI Assistant inside Oracle APEX using Cohere + RAG Sources.


๐Ÿ” What We Are Building

A chatbot inside Oracle APEX that can answer questions like:

  • โ€œWhat is Scottโ€™s department?โ€
  • โ€œShow me Wardโ€™s salary and departmentโ€
  • โ€œGive me Sales department locationโ€

๐Ÿ‘‰ All answers are dynamically fetched from database tables using RAG.


๐Ÿง  What is RAG (Retrieval-Augmented Generation)?

RAG enhances AI responses by combining:

  • Retrieval โ†’ Fetch relevant data from database
  • Generation โ†’ Use AI (LLM) to generate a human-like response

This ensures:

โœ” Accurate answers

โœ” Context-aware responses

โœ” Reduced hallucination


โš™๏ธ Step 1: Create Generative AI Configuration

Navigate to:

Shared Components โ†’ Generative AI Configurations โ†’ Create

Configuration Details:

  • Name: Cohere
  • Static ID: cohere
  • Service: Cohere

Key Sections:

โœ” System Prompt

(Define how AI should behave โ€“ optional but recommended)

โœ” Welcome Message

(Initial message shown in chatbot)


๐Ÿ“š Step 2: Create RAG Source

Navigate to:

Shared Components โ†’ RAG Sources โ†’ Create

Configuration:

  • Type: SQL Query
  • Name: RAG Sources

Sample SQL Query:

SELECT content_text
FROM v_rag_knowledge_base
WHERE (
    :APEX$AI_LAST_USER_PROMPT IS NULL
    OR EXISTS (
        SELECT 1
        FROM table(apex_string.split(UPPER(:APEX$AI_LAST_USER_PROMPT), ' '))
        WHERE LENGTH(column_value) > 2
        AND UPPER(content_text) LIKE '%' || column_value || '%'
    )
)
FETCH FIRST 20 ROWS ONLY;

๐Ÿ’ก What this does:

  • Breaks user prompt into keywords
  • Matches with stored knowledge
  • Returns relevant data for AI context

๐Ÿค– Step 3: Add AI Chat Assistant (UI)

Go to your Page (Global Page recommended)

Create a Dynamic Action:

  • Event: Button Click (e.g., โ€œAsk AIโ€)
  • Action: Show AI Assistant

Configuration:

  • Display As: Dialog
  • Generative AI Config: Cohere
  • RAG Source: Your created source

๐Ÿ’ฌ Step 4: Enable Chat UI

Your chatbot UI will automatically:

โœ” Show user queries

โœ” Display AI responses

โœ” Maintain conversation flow

Example from implementation:

  • Scottโ€™s department is Research
  • Wardโ€™s salary is $1250 and he works in Sales
  • Sales department is located in Chicago

๐Ÿ”— Step 5: Connect with Your Tables

Ensure your RAG source view (v_rag_knowledge_base) contains:

  • Employee data
  • Department data
  • Any business knowledge

๐Ÿ‘‰ You can create a view combining multiple tables:

  CREATE OR REPLACE FORCE EDITIONABLE VIEW "V_RAG_KNOWLEDGE_BASE" ("SOURCE_TABLE", "PRIMARY_KEY", "CONTENT_TEXT") AS 
  SELECT 'DEPT' as source_table, 
       deptno as primary_key, 
       'Department Number: ' || deptno || ' Name: ' || dname || ' Location: ' || loc as content_text
FROM dept
UNION ALL
-- 2. Employee Data (CRITICAL: Added Salary here)
SELECT 'EMP', 
       e.empno, 
       'Employee: ' || e.ename || ' (ID: ' || e.empno || ') Job: ' || e.job || 
       ' Salary: ' || e.sal || ' works in Dept: ' || d.dname as content_text
FROM emp e
LEFT JOIN dept d ON e.deptno = d.deptno
UNION ALL
-- 3. Projects Data
SELECT 'PROJECTS', 
       project_id, 
       'Project ID: ' || project_id || ' Name: ' || project_name || '. Description: ' || project_description
FROM projects;

๐Ÿš€ Key Benefits

โœ” Natural language querying of database

โœ” No need for complex reports

โœ” AI-powered user experience

โœ” Works across multiple tables

โœ” Easy to extend with more data


๐Ÿ”ฅ Pro Tips

  • Use clean, descriptive content_text for better AI responses
  • Avoid too much raw dataโ€”focus on meaningful sentences
  • Add filters in RAG query for performance
  • Customize System Prompt for better control

๐ŸŽฏ Final Thoughts

With Oracle APEX + Generative AI + RAG, you can transform traditional applications into intelligent, conversational systems.

No more complex UI navigationโ€”just ask questions and get answers instantly.


๐Ÿ’ก Whatโ€™s Next?

  • Add role-based AI responses
  • Integrate with APIs
  • Enable document-based RAG
  • Use vector search for advanced matching

๐Ÿš€ Conclusion

Building AI-powered apps is no longer complex.

With Oracle APEX, you can quickly create a smart chatbot that understands your data and speaks your business language.

๐Ÿ‘‰ Start building today and bring AI into your applications!

Love coding? Me too! Letโ€™s keep in touch โ€“ subscribe to my website for regular chats on Oracle APEX, PL/SQL,SQL JavaScript, and CSS.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *