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.
