The Talent500 Blog

Developing Data-Driven AI Applications: Direct AI Service Integration from Oracle Database

As artificial intelligence (AI) services gain prominence across various applications, the underlying platforms and architectures become increasingly vital. While there is no universal solution, this article outlines an optimized approach to building data-driven AI application architectures.AI applications often require multiple network calls, including those to retrieve and store various types of content—such as text, audio, images, and video. This persistent information is typically analyzed further, necessitating additional calls to AI services or other systems.

The Oracle Database facilitates direct communication with both internal and external services, enhancing application performance. By executing calls from the database itself, developers can achieve several advantages:

  • Reduced Network Latency: Fewer network calls lead to decreased latency.
  • Increased Reliability: Fewer calls also enhance the reliability of the application.
  • Transactional Operations: The database supports ACID transactions on AI data, eliminating the need for duplicate processing logic and conserving resources.
  • Optimized Processing: Locality of data—whether stored in the database or an object store—enables efficient processing. The Oracle Database acts as a robust interface for object storage, offering various options for data synchronization and operation.
  • Enhanced Security: A unified authentication mechanism leverages the robust security infrastructure of Oracle’s database and cloud services.
  • Simplified Configuration: Centralized call management reduces overall configuration complexity. The database can be exposed as a REST endpoint with minimal setup.

Advantages of Oracle Database Machine Learning

In addition to its AI service offerings, the Oracle Database has integrated machine learning capabilities for years. The Oracle Machine Learning (OML) framework streamlines the ML lifecycle with scalable support for SQL, R, Python, REST, AutoML, and no-code tools. It features over 30 in-database algorithms that enhance data synchronization and security by processing data directly within the database.The Oracle Autonomous Database includes advanced features such as:

  • Natural Language Querying: Enables users to query data using natural language and automatically generates SQL tailored to specific databases.
  • AI Vector Search: Introduces a new vector data type, vector indexes, and SQL operators that allow the storage of semantic content from documents and images as vectors. This capability supports fast similarity queries while maintaining privacy by avoiding exposure of sensitive data in large language models (LLMs).

Comparison of AI Application Architectures

The architecture for AI applications can vary significantly based on requirements. Below is a basic comparison of two approaches:

Feature Traditional Approach Oracle Database Approach
Network Calls Multiple external calls Direct calls from the database
Latency Higher due to network overhead Lower due to reduced calls
Transaction Management Complex handling Simplified with ACID compliance
Data Processing External processing Localized processing within the DB
Security Varies by service Centralized through Oracle security

Code Implementation

Oracle’s database supports various programming languages such as Java, JavaScript, and PL/SQL for executing application logic. PL/SQL examples can be run from multiple interfaces including OCI console, SQLcl command line tool, SQLDeveloper, or VS Code with an Oracle plugin.To make API calls to AI services, developers can use standard REST calls via the UTL_HTTP package or utilize OCI SDKs for services hosted in Oracle Cloud Infrastructure (OCI). A recommended approach is using the DBMS_CLOUD.send_request package for dynamic service calls.

Credential Creation Example

Creating reusable credentials for cloud service calls is straightforward:

sql
BEGIN
dbms_cloud.create_credential (
credential_name => 'OCI_KEY_CRED',
user_ocid => 'ocid1.user.oc1..[youruserocid]',
tenancy_ocid => 'ocid1.tenancy.oc1..[yourtenancyocid]',
private_key => '[yourprivatekey]',
fingerprint => '[7f:yourfingerprint]'
);
END;

Table Structure for Storing AI Results

An example table structure designed to save AI results might look like this:

sql
CREATE TABLE aivision_results (
id RAW(16) NOT NULL,
date_loaded TIMESTAMP WITH TIME ZONE,
label VARCHAR2(20),
textfromai VARCHAR2(32767),
jsondata CLOB CONSTRAINT ensure_aivision_results_json CHECK (jsondata IS JSON)
);

Function Example for Calling AI Services

A simple function that exemplifies this architecture might be structured as follows:

sql
CREATE OR REPLACE FUNCTION VISIONAI_TEXTDETECTION (
p_endpoint VARCHAR2,
p_compartment_ocid VARCHAR2,
p_namespaceName VARCHAR2,
p_bucketName VARCHAR2,
p_objectName VARCHAR2,
p_featureType VARCHAR2,
p_label VARCHAR2
) RETURN VARCHAR2 IS
resp DBMS_CLOUD_TYPES.resp;
json_response CLOB;
v_textfromai VARCHAR2(32767);
BEGIN
resp := DBMS_CLOUD.send_request(
credential_name => 'OCI_KEY_CRED',
uri => p_endpoint || '/20220125/actions/analyzeImage',
method => 'POST',
body => UTL_RAW.cast_to_raw(
JSON_OBJECT(
'features' VALUE JSON_ARRAY(
JSON_OBJECT('featureType' VALUE p_featureType)
),
'image' VALUE JSON_OBJECT(
'source' VALUE 'OBJECT_STORAGE',
'namespaceName' VALUE p_namespaceName,
'bucketName' VALUE p_bucketName,
'objectName' VALUE p_objectName
),
'compartmentId' VALUE p_compartment_ocid
)
)
);
json_response := DBMS_CLOUD.get_response_text(resp);

SELECT LISTAGG(text, ‘, ‘) WITHIN GROUP (ORDER BY ROWNUM)
INTO v_textfromai
FROM JSON_TABLE(json_response, ‘$.imageText.words[*]’
COLUMNS (
text VARCHAR2(100) PATH ‘$.text’
)
);

INSERT INTO aivision_results (id, date_loaded, label, textfromai, jsondata)
VALUES (SYS_GUID(), SYSTIMESTAMP, p_label, v_textfromai, json_response);

RETURN v_textfromai;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END VISIONAI_TEXTDETECTION;

Exposing Functions as REST Endpoints

To expose functions programmatically as REST endpoints:

sql
BEGIN
ORDS.ENABLE_OBJECT(
P_ENABLED => TRUE,
P_SCHEMA => 'AIUSER',
P_OBJECT => 'VISIONAI_OBJECTDETECTION',
P_OBJECT_TYPE => 'FUNCTION',
P_OBJECT_ALIAS => 'VISIONAI_OBJECTDETECTION',
P_AUTO_REST_AUTH => FALSE
);
COMMIT;
END;

Conclusion

This overview presents an architectural pattern for developing data-driven AI applications through direct integration with Oracle Database services. By leveraging these capabilities, developers can enhance efficiency and security while streamlining their application processes.

For any questions or feedback regarding this approach or implementation details, please feel free to reach out.
Read more such articles from our Newsletter here.
0