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.




