πŸš€ 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!

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 *