Building Your First Generative AI Agent with Oracle APEX - Getting Started
Introduction
Generative AI is transforming how we build applications, and Oracle APEX provides an excellent low-code platform to quickly develop AI-powered solutions. In this first part of our series, I'll walk you through the complete process of setting up your environment and creating your first AI agent using Oracle APEX and Oracle Cloud Infrastructure (OCI) Generative AI Service.
By the end of this tutorial, you'll have a working AI chatbot that can answer questions and engage in conversations.
Prerequisites
Before we begin, ensure you have:
- An Oracle Cloud Infrastructure (OCI) account with appropriate permissions
- Access to Oracle APEX workspace (version 23.1 or later recommended)
- Basic understanding of APEX application development
- Familiarity with REST API concepts
Step 1: Setting Up Oracle Cloud Infrastructure
1.1 Create an OCI Compartment
First, let's organize our resources by creating a dedicated compartment:
- Log in to your OCI Console (https://cloud.oracle.com)
- Navigate to Identity & Security → Compartments
- Click Create Compartment
- Fill in the details:
- Name:
GenAI-APEX-Demo - Description: Compartment for Generative AI APEX applications
- Parent Compartment: Select your root or preferred compartment
- Name:
- Click Create Compartment
1.2 Enable OCI Generative AI Service
- From the OCI Console, open the navigation menu
- Go to Analytics & AI → Generative AI
- Ensure your region supports Generative AI (currently available in US Midwest - Chicago, Germany - Frankfurt, and UK South - London)
- If needed, switch to a supported region using the region selector at the top right
1.3 Create API Keys for Authentication
- Click on your profile icon (top right) → User Settings
- Scroll down to Resources → API Keys
- Click Add API Key
- Select Generate API Key Pair
- Click Download Private Key and save it securely (you'll need this later)
- Click Download Public Key (optional, for backup)
- Click Add
- Copy the configuration file preview - you'll need:
- User OCID
- Tenancy OCID
- Fingerprint
- Region
Important: Keep your private key secure and never commit it to version control!
1.4 Create a Policy for Generative AI Access
- Navigate to Identity & Security → Policies
- Select your
GenAI-APEX-Democompartment - Click Create Policy
- Enter the following:
- Name:
GenAI-APEX-Policy - Description: Policy to allow Generative AI service usage
- Compartment: Select your compartment
- Name:
- Toggle to Show manual editor and add:
Step 2: Configuring OCI Generative AI Service
2.1 Test the Generative AI Playground
Before integrating with APEX, let's verify the service is working:
- Navigate to Analytics & AI → Generative AI
- Click on Playgrounds → Chat
- Try sending a test message like "What is Oracle APEX?"
- If you receive a response, the service is working correctly
2.2 Identify Your Model OCID
- In the Generative AI console, go to Models
- Find the model you want to use (e.g.,
cohere.command-r-plusormeta.llama-3.1-70b-instruct) - Copy the OCID of the model - you'll need this for API calls
Example model endpoint:
https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/20231130/actions/chat
Step 3: Setting Up Oracle APEX Application
3.1 Create a New APEX Application
- Log in to your APEX workspace
- Click App Builder → Create
- Select New Application
- Configure:
- Name:
AI Agent Demo - Appearance: Choose your preferred theme (Universal Theme 42 recommended)
- Name:
- Add a blank page:
- Page Name: AI Chatbot
- Page Type: Blank Page
- Click Create Application
3.2 Store API Credentials Securely
Never hardcode API keys! Use APEX credentials store:
- Go to Shared Components → Web Credentials
- Click Create
- Configure:
- Name:
OCI_GenAI_Credentials - Authentication Type: OCI Native Authentication
- OCI User ID: Paste your User OCID from Step 1.3
- OCI Private Key: Paste the contents of your downloaded private key file
- OCI Tenancy ID: Paste your Tenancy OCID
- OCI Public Key Fingerprint: Paste your fingerprint
- Name:
- Click Create
3.3 Create Application Items for Configuration
- Go to Shared Components → Application Items
- Create the following items:
Item 1:
- Name:
AI_MODEL_OCID - Initial Value: Your model OCID from Step 2.2
Item 2:
- Name:
AI_ENDPOINT - Initial Value:
https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/20231130/actions/chat
Item 3:
- Name:
AI_COMPARTMENT_ID - Initial Value: Your compartment OCID
Step 4: Building the REST Integration
4.1 Create REST Data Source
- Go to Shared Components → REST Data Sources
- Click Create
- Choose From Scratch
- Configure the REST Data Source:
General Settings:
- Name:
OCI_GenAI_Chat - URL Endpoint:
https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/20231130/actions/chat - Authentication Type: OCI Native Authentication
- Credentials: Select
OCI_GenAI_Credentials
- Click Next
4.2 Configure the POST Operation
- On the Operations step, click Add Operation
- Configure:
- URL Pattern: (leave empty for POST to base URL)
- HTTP Method: POST
- Request Body Type: JSON
- Add a sample request body:
- Click Discover to automatically detect response structure
- Click Create REST Data Source
Step 5: Creating the Chat Interface
5.1 Design the Page Layout
- Navigate to your AI Chatbot page
- Create a new Region:
- Type: Static Content
- Title: AI Assistant
- Template: Standard
5.2 Add Chat Input Elements
Add the following page items:
Item 1: User Message Input
- Name:
P1_USER_MESSAGE - Type: Textarea
- Label: Your Message
- Placeholder: Type your message here...
- Settings:
- Width: 100%
- Rows: 3
Item 2: Chat History Display
- Name:
P1_CHAT_HISTORY - Type: Display Only
- Label: Conversation
- Format: HTML Expression
- Settings:
- Add custom CSS classes:
chat-container
- Add custom CSS classes:
Item 3: Send Button
- Create a Button:
- Name:
SEND_MESSAGE - Label: Send
- Position: Below P1_USER_MESSAGE
- Action: Submit Page
Step 6: Implementing the AI Logic
6.1 Create a PL/SQL Process
- Create a new Process on your page
- Configure:
- Name:
Call_GenAI_Service - Type: Execute Code
- When Button Pressed: SEND_MESSAGE
- Name:
- Add the following PL/SQL code:
6.2 Add Custom CSS
- Navigate to Page Designer → Page Properties
- Under CSS → Inline, add:
Step 7: Testing Your AI Agent
7.1 Run the Application
- Click Save and Run Page
- Type a test message: "What is Oracle APEX?"
- Click Send
- You should see:
- Your message displayed in blue
- The AI's response displayed in green
7.2 Troubleshooting Common Issues
Issue 1: Authentication Error
- Verify your API keys are correctly entered in Web Credentials
- Check that the private key includes the BEGIN/END markers
- Ensure your user has proper permissions in OCI
Issue 2: Model Not Found
- Verify the model OCID is correct
- Check that you're using a supported region
- Ensure the model is available in your compartment
Issue 3: No Response
- Check the APEX Debug mode to see the actual API response
- Verify your compartment ID is correct
- Check OCI service limits and quotas
Step 8: Next Steps and Enhancements
Congratulations! You've built your first AI agent with Oracle APEX. Here are some immediate improvements you can make:
- Add Session Management: Store conversation history in a database table
- Implement Streaming: Show responses as they're generated
- Add Error Handling: Display user-friendly error messages
- Style Improvements: Enhance the UI with better formatting
What's Coming in Blog 2
In the next part of this series, we'll dive deeper into creating sophisticated conversational interfaces including:
- Building dynamic chat bubbles with timestamps
- Implementing conversation memory and context
- Adding typing indicators and smooth animations
- Supporting multi-turn conversations with context retention
Conclusion
You've successfully set up OCI Generative AI with Oracle APEX and created a working AI chatbot. This foundation will serve you well as we explore more advanced features in the upcoming blogs.
The combination of Oracle APEX's rapid development capabilities and OCI's powerful Generative AI services enables you to build sophisticated AI applications without extensive coding.
Resources:
Comments
Post a Comment