DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Exploring the New Boolean Data Type in Oracle 23c AI
  • Select ChatGPT From SQL? You Bet!
  • Apache Ranger and AWS EMR Automated Installation and Integration Series (5): Windows AD + Open-Source Ranger
  • Apache Ranger and AWS EMR Automated Installation and Integration Series (4): OpenLDAP + Open-Source Ranger

Trending

  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • Medallion Architecture: Why You Need It and How To Implement It With ClickHouse
  1. DZone
  2. Data Engineering
  3. Databases
  4. How To Call Cohere and Hugging Face AI From Within an Oracle Database Using JavaScript

How To Call Cohere and Hugging Face AI From Within an Oracle Database Using JavaScript

Optimize your AI architecture entirely free using JavaScript and open source and access the same data using SQL, JSON, or REST.

By 
Paul Parkinson user avatar
Paul Parkinson
·
Jul. 05, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
8.3K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I will show you how to quickly create an entirely free app using a JavaScript program that runs within the free Oracle database and calls Hugging Face AI, the results of which are stored in the database and can then be accessed using SQL, JSON, REST, or any other language. All of the source code is available here.
Hugging Face, JS, and Oracle logos

A common flow of applications in general and AI applications specifically involves an app calling an AI service and then storing the information in the database where it may be retrieved and processed (e.g., using OML) further and/or analyzed and queried later.

By issuing the command from the database itself, the data is stored immediately and is thus also more reliable as it is in process and does not require an extra network call to persist. Further, by having the logic (and even code) reside in the database, the high availability, management, observability, etc. of the power of the Oracle database can be implicitly and inherently leveraged. 

This is a unique capability of the Oracle database as it includes Java, and, as we'll see in this blog, JavaScript runtime engines within the database itself. 

Hugging Face develops tools for building applications using machine learning. It is most notable for its transformers library built for natural language processing applications and its platform that allows users to share machine learning models and datasets.  It has become extremely popular over the last couple of years.

In short, we will do the following:

  1. Create an account and select a model.
  2. Create the Oracle database and a database user.
  3. Add a wallet with a certificate to allow HTTPS calls.
  4. Create a table.
  5. Run a short JavaScript program in the database to make a call to the Cohere or Hugging Face AI model and store the results in the table. 
  6.  Optionally query these results using SQL, JSON, REST, etc.

Setup Cohere Account and AI Model

Go to the Cohere website and Sign Up. Go to your Profile, and click the Settings button.

Click on Dashboard, then API Keys. Create a key (or use trial) and copy its value for use later.
Cohere Dashboard: API keys

Click on API Reference, and select the hamburger menu/icon on the left of any page to view the API Reference list. Select one. In this case, we will select generate section and Co.Generate.

API Reference List

Select Language and then select the JavaScript option in order to see a sample code snippet to call the model.

Sample code snippet to call model

Setup Hugging Face Account and AI Model

Go to the Hugging Face website and Sign Up. Go to your Profile, and click the Settings button.

Click on Access Tokens, create a token, and copy its value for use later.

Access tokens tab

Click on Models and select a model. In this case, we will select a model under the Natural Language Processing section for Question Answering.

Select a model

Select a model (perhaps one of the more popular ones), notice the information in the Model Card on the left, and then select the Deploy drop-down menu on the right.

Deploy drop-down menu

Select Inference API and then select the JavaScript option in order to see a sample code snippet to call the model.

Inference API

Setup Oracle Database

We can use any flavor of the Oracle database 23c.  On the cloud, we can use Oracle always free autonomous database later this year when 23c should be available. We can use the Oracle 23c Free version as it's available now and we can simply install or use a container image locally. Or of course, we could use the local for dev and cloud for production, etc. and either of these options is very quick to set up.

Oracle Database Free Option

You can go here to set up the Oracle Database Free 23c.

Using the container image is very simple. You can simply execute the one line below, replacing -e ORACLE_PASSWORD=Welcome12345 with a password of your choice and replacing -v oracle-volume:/somedirectory with a directory location (or omitting it entirely if you only wish to have in-memory database). Notice the --add-host docker.for.mac.host.internal:host-gateway param that allows calls out of the container. This is the setting for Macs and will be different if running on a different OS.

docker pull gvenzl/oracle-free ; docker run -d -p 1521:1521 --add-host docker.for.mac.host.internal:host-gateway -e ORACLE_PASSWORD=Welcome12345 gvenzl/oracle-free


Setup SQLcl (or Database Actions) and Connect

You can install SQLcl to manage the database by using the following steps:

  1. Download and install from this location. This will provide an [SQLcl_INSTALL_DIR]/bin/sql executable that we will use to administer the database. For convenience, you may add [SQLcl_INSTALL_DIR]/bin to your PATH.

  2. Login replacing [SQLcl_INSTALL_DIR] with the location of your SQLcl and replacing Welcome12345 with the one you provided as ORACLE_PASSWORD when creating the database.

Here is an example when using a local install (e.g., Oracle Database Free container image):

Shell
 
[SQLcl_INSTALL_DIR]/bin/sql  /nolog
SQL> connect sys@//localhost:1521/freepdb1 as sysdba
Password? (**********?) *************
Connected.

         

And here is an example when using a cloud database (e.g., Oracle Always Free Autonomous):

SQL
 
[SQLcl_INSTALL_DIR]/bin/sql  /nolog
SQL> set cloudconfig /Users/pparkins/Downloads/Wallet_xr.zip 
SQL> connect admin@mydb_tp
Password? (**********?) *************
Connected.


Create the Users With Appropriate Privileges That Will Call Cohere And/Or Hugging Face via JavaScript

Run the following SQL files to create the aijs user and its necessary grants and ACLs (note that these can be further tightened for better security). 

 
SQL> @sql/create_aijs_user.sql


Now connect the user and create a table to store the results of the call to Hugging Face:

 
SQL> connect aijs/Welcome12345;

SQL> create table huggingfacejson (id json);

SQL> create table coherejson (id json); 


Now everything is ready to run!

Run Cohere or Hugging Face Queries From the JavaScript Code in the Database

Cohere Example

 
SQL> @sql/coherequery.sql

L/SQL procedure successfully completed.


Then, check the JSON results stored in the table by doing a SQL query.

 
SQL> select * from coherejson;

[{"id":"1b42f8f9-ea6d-4a65-8f02-bdcf89d9bd79","generations":[{"id":"d6467c0b-4a78-4dd4-b822-c1e2bfa4ecb0","text":"\n LLMs or Large Language Models are artificial intelligence tools that can read, summarize and translate texts and"}],"prompt":"Please explain to me how LLMs work","meta":{"api_version":{"version":"1"}}}    


Looking at the code we just executed we can see the JavaScript module created and the function being called:

create or replace mle module cohere_module
language javascript as

import "mle-js-fetch";
import "mle-js-oracledb";

export async function cohereDemo(apiToken) {

    if (apiToken === undefined) {
        throw Error("must provide an API token");
    }

    const modelId = "generate";
    const restAPI = `https://api.cohere.ai/v1/${modelId}`;

    const headers = {
                         accept: 'application/json',
                         'content-type': 'application/json',
                         "Authorization": `Bearer ${apiToken}`
                       };

    const payload = {
     max_tokens: 20,
     return_likelihoods: 'NONE',
     truncate: 'END',
     prompt: 'Please explain to me how LLMs work'
    };

    const resp = await fetch(
        restAPI, {
            method: "POST",
            headers: headers,
            body: JSON.stringify(payload),
            credentials: "include"
        }
    );
    const resp_json = await resp.json();

    session.execute(
        `INSERT INTO COHEREJSON (id) VALUES (:resp_json)`,
           [ JSON.stringify(resp_json) ]
    );

}
/

create or replace procedure coherequery(
    p_API_token varchar2
) as mle module cohere_module
signature 'cohereDemo(string)';
/

-- this is how you can test the API call
begin
    utl_http.set_wallet('system:');
    coherequery('[yourcoheretokenhere]');
end;
/


Hugging Face Example

 
SQL> @sql/huggingfacequery.sql PL/SQL procedure successfully completed. 


Then, check the JSON results stored in the table by doing a SQL query.

 
SQL> select * from huggingfacejson; [{"score":0.16964051127433777,"token":2053,"token_str":"no","sequence":"the answer to the universe is no."},{"score":0.07344779372215271,"token":2498,"token_str":"nothing","sequence":"the answer to the universe is nothing."},{"score":0.05803246051073074,"token":2748,"token_str":"yes","sequence":"the answer to the universe is yes."},{"score":0.043957971036434174,"token":4242,"token_str":"unknown","sequence":"the answer to the universe is unknown."},{"score":0.04015727713704109,"token":3722,"token_str":"simple","sequence":"the answer to the universe is simple."}]


Looking at the code we just executed we can see the JavaScript module created and the function being called:

create or replace mle module huggingface_module
language javascript as

import "mle-js-fetch";
import "mle-js-oracledb";

export async function huggingfaceDemo(apiToken) {

    if (apiToken === undefined) {
        throw Error("must provide an API token");
    }

    const payload = {
        inputs: "The answer to the universe is [MASK]."
    };
    const modelId = "bert-base-uncased";

    const headers = {
        "Authorization": `Bearer ${apiToken}`
    };

    const restAPI = `https://api-inference.huggingface.co/models/${modelId}`;

    const resp = await fetch(
        restAPI, {
            method: "POST",
            headers: headers,
            body: JSON.stringify(payload),
            credentials: "include"
        }
    );

    const resp_json = await resp.json();

    session.execute(
        `INSERT INTO HUGGINGFACEJSON (id) VALUES (:resp_json)`,
        [ JSON.stringify(resp_json) ]
    );
}
/

create or replace procedure huggingfacequery(
    p_API_token varchar2
) as mle module huggingface_module
signature 'huggingfaceDemo(string)';
/

-- this is how you can test the API call
begin
    utl_http.set_wallet('system:');
    huggingfacequery('[hf_yourhuggingfacetokenhere]');
end;
/


Useful parameters and debug information for Hugging Face can be found here.

The results can now be queried, analyzed, etc. using SQL or JSON (simultaneously, thanks to the new JSON duality feature), REST, or even the MongoDB API.

Oracle Database works well for AI for a number of reasons, particularly as it is a vector database with capabilities such as the following already available today:

  • Native data types for vector representation and storage: RAW, BLOB, JSON
  • In-memory column store to store and search vector embeddings w/SIMD kernels for amazing performance
  • Extensible indexing framework to create data-model specific indices (e.g., text, spatial)
  • Native Oracle machine learning APIs - Modeling, classification, scoring, clustering, etc.
  • DMLs, parallel loading, partitioning, advanced compression, parallel query, RAC, sharding, etc.

It is also possible to call Oracle OCI AI and other services from within the Oracle database.

Conclusion

The article went into how to call Cohere and Hugging Face APIs from within the Oracle database using JavaScript, thus demonstrating a powerful combination of features perfectly suited to a broad array of AI solutions and friendly to JavaScript developers.

Some other blogs related to JavaScript in the database, in general, the MultiLingual Engine (MLE) that makes it possible, etc. can be found in Martin Bach's posts and this post about importing JavaScript ES Modules in 23c.

I look forward to any comments or questions you may have and really appreciate your time reading.

AI JSON Oracle Database REST sql JavaScript

Opinions expressed by DZone contributors are their own.

Related

  • Exploring the New Boolean Data Type in Oracle 23c AI
  • Select ChatGPT From SQL? You Bet!
  • Apache Ranger and AWS EMR Automated Installation and Integration Series (5): Windows AD + Open-Source Ranger
  • Apache Ranger and AWS EMR Automated Installation and Integration Series (4): OpenLDAP + Open-Source Ranger

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!