Previously, we explored the OpenAI API’s completion models, which enabled text generation and language-based automation. Now, we are shifting our focus to OpenAI’s new Assistant API, a more structured approach that enhances interactions by integrating memory, function calling, and multi-step reasoning. This new paradigm allows for more sophisticated and context-aware AI-driven assistants that can work seamlessly within Oracle APEX applications. Oracle APEX, a low-code development platform, benefits greatly from AI-powered assistants by providing users with automated responses, data analysis, and workflow optimizations. By leveraging OpenAI Assistants, developers can build intelligent chatbots, automate decision-making processes, and improve application functionality with minimal effort.
To achieve this seamless integration, we utilize Oracle APEX’s REST Data Source feature, which enables direct communication with external APIs. This ensures a smooth exchange of information between Oracle APEX applications and OpenAI’s Assistant API, allowing dynamic creation, retrieval, and management of assistant instances.

REST Data Source configuration for connecting to OpenAI’s API, showcasing setup details for seamless integration and optimized performance.
The GIA_OPENAI_ASSISTANT
REST Data Source can dynamically create and retrieve assistant instances, ensuring that AI-powered assistants are assigned and maintained efficiently. As shown in the image, this resource will be used to manage and create the AI assistant. The next step is to determine how to load this information into the system through the appropriate Oracle APEX page load procedures.

Web application interface showcasing page layout design and process configuration for executing PL/SQL code to handle AI assistant interactions.
Retrieving and Storing OpenAI Assistant Identifiers in Oracle APEX To efficiently manage OpenAI Assistants within Oracle APEX, we use a PL/SQL procedure that retrieves an existing assistant identifier for a user and, if not found, generates a new instance using the OpenAI Assistant API. This process ensures that each user has an associated assistant ID stored in the database for future interactions.
The procedure follows these steps:
-
Check for an Existing Assistant ID: The system first queries the
GESTALTIA_USERS_ASSISTANTS
table to check if an assistant ID already exists for the given user. -
Create a New Assistant if Necessary: If no record is found, a REST API call is made to
GIA_OPENAI_ASSISTANT
, which generates a new assistant instance using the OpenAI Assistant API. -
Store the Assistant ID: The newly created assistant ID is extracted from the API response and stored in the
GESTALTIA_USERS_ASSISTANTS
table. -
Handle Updates: If an assistant ID already exists, it is updated in the database to maintain consistency.
-
Error Handling: The procedure includes robust error handling to manage failed API calls and unexpected issues.
Each assistant has a set of threads associated with it, which naturally correspond to different interactions with the AI over time. These threads represent distinct conversational contexts, allowing the assistant to maintain continuity and relevance in its responses. To ensure a coherent user experience, it is essential to manage the loading process in a way that enables each thread to be correctly linked to its respective interaction. This requires careful coordination between the data retrieved from the REST Data Source and the logic implemented in Oracle APEX. In the following section, we will explore how to structure this process efficiently, leveraging APEX page load procedures and dynamic actions to seamlessly integrate thread management within the broader framework of AI assistant deployment.

PL/SQL process setup in Oracle APEX for retrieving conversation threads and ensuring seamless AI assistant communication.
To manage the association between users and their AI interaction threads, we have created a PL/SQL procedure named PR_GET_THREAD
. This procedure is responsible for retrieving the most recent thread associated with a given user and assistant. If no thread exists, it dynamically creates one via a REST API call to the GIA_OPENAI_THREAD
REST Data Source. This ensures that each interaction with the AI assistant maintains contextual continuity, a key element in delivering a coherent and personalized user experience.
Key Steps in PR_GET_THREAD
:
-
Initialization and Variable Declaration
The procedure starts by declaring several local variables, including a parameter set for the REST call, the thread ID, the HTTP status code, and a CLOB to hold the JSON response. -
User ID Assignment
TheUSER_ID
value is assigned to the APEX page item:P1_USER_ID
, ensuring that the session context remains consistent throughout the procedure. -
Retrieving the Latest Thread
A query attempts to fetch the most recent thread ID for the given user and assistant from theGESTALTIA_USERS_THREADS
table. If no thread is found, the control gracefully falls into theNO_DATA_FOUND
exception handler, settingl_thread_id
toNULL
. -
REST API Call for Thread Creation
If no existing thread is retrieved, the procedure triggers a POST request to theGIA_OPENAI_THREAD
REST Data Source. Upon a successful API response (HTTP 200 OK
), the JSON response is parsed, and the new thread ID is extracted. -
Thread Assignment and Persistence
The retrieved thread ID is assigned to APEX page variables:P1_THREAD_ID
and:THREAD_ID
. Then, aMERGE
operation ensures that the thread ID is either inserted or updated in theGESTALTIA_USERS_THREADS
table, preserving data consistency and avoiding duplicates. -
Error Handling
The procedure includes robust error handling mechanisms, both for unsuccessful API responses and unexpected exceptions. Errors are logged and surfaced throughapex_error.add_error
, ensuring that issues are traceable during execution.
Now that we have defined our AI assistant and established a conversation thread to manage interactions, the next step is to efficiently structure the storage and display of messages. To achieve this, we will use an Oracle APEX collection, which allows for fast message retrieval, maintains a well-organized conversation structure, and optimizes resource usage. Rather than relying on constant database queries, this approach provides a dynamic, temporary storage mechanism that enables real-time message access with minimal performance overhead. To implement this strategy, we have developed the PR_LOAD_MESSAGES
procedure, responsible for initializing the conversation environment, retrieving predefined system messages, and managing the message collection in a structured and efficient manner.
With this, we now have a solid foundation in place: the assistant is set up, conversation threads are properly managed, and messages are efficiently stored and retrieved. This structure not only ensures smooth interaction but also optimizes performance by reducing unnecessary database queries.
With this, we now have a solid foundation in place: the assistant is set up, conversation threads are properly managed, and messages are efficiently stored and retrieved. This structure not only ensures smooth interaction but also optimizes performance by reducing unnecessary database queries.
Now, we move on to the next critical step: sending messages to the assigned thread and facilitating a seamless interaction with the AI. This involves designing an efficient mechanism to transmit user inputs, process responses, and maintain the conversation context across interactions. By carefully managing how messages flow between the user and the AI, we can enhance responsiveness, improve scalability, and create a more intuitive experience. Our next task will be to refine this communication process, ensuring that each message is properly delivered, processed, and stored, paving the way for a robust and dynamic exchange with the assistant.
In the next post (Part 2) of this series, we will cover all these aspects in detail, exploring how to send messages to the AI, handle responses efficiently, and maintain a structured, high-performance interaction model.
For more information, please visit our company website. Stay updated and feel free to reach out for support or contributions!
Leave A Comment