In this blog, 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 AI, the results of which are stored in the database and can then be accessed using , , Rest, or any other language. All of the source code is, of course, available . Hugging Face SQL JSON here A typical sequence of actions in applications, both in general and particularly in AI applications, involves an application utilizing an AI service. The acquired information is then stored in a database, ready for retrieval, subsequent processing (such as using OML), analysis, and future queries. When the command originates directly from the database, the data is promptly stored and gains higher reliability. This approach is in-process and eliminates the need for an additional network call for persistence. Additionally, by housing the logic and even code within the database, the remarkable features of the robust Oracle database, including high availability, management, and observability, are naturally harnessed. This distinct capability is a hallmark of the Oracle database, which encompasses both Java and, as detailed in this blog, JavaScript runtime engines within the database itself. Hugging Face specializes in crafting tools for constructing applications using machine learning. Particularly notable is its transformers library designed for natural language processing applications, along with its platform facilitating the sharing of machine learning models and datasets. It has garnered significant popularity in recent years. In short, we’ll… Create a cohere.com or huggingface.co account, create and copy an access token, and select a model. Create the Oracle database. Create the database user and table(s). 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. Query these results using SQL, JSON, Rest, etc. Setup Cohere account and AI model Go to https://cohere.com and , go to your , and click the button. Sign Up Profile Settings Click on , then API Keys, create a key (or use trial) and copy its value for use later. Dashboard Click on and select the hamburger menu/icon on the left of any page to view the API Reference list and select one. In this case, we will select section and API Reference generate Co.Generate. Select and then select the option in order to see a sample code snippet to call the model. Ianguage JavaScript Setup Hugging Face account and AI model Go to https://huggingface.co and , go to your , and click the button. Sign Up Profile Settings Click on , create a token, and copy its value for use later. Access Tokens Click on and select a model. In this case, we will select a model under the section for Models Natural Language Processing Question Answering. Select a model (perhaps one of the more popular ones), notice the information in the on the left, and then select the drop-down menu on the right. Model Card Deploy Select and then select the option in order to see a sample code snippet to call the model. Inference API JavaScript 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… You can go here to set up the Free Oracle Database. Using the container image is very simple. You can simply pull the image: docker pull container-registry.oracle.com/database/free:latest Then, execute the one line below, replacing with a password of your choice (you can also add a parameter with a directory location in order to persist changes across restarts). Notice the param that allows calls out of the container. This is the setting for Macs and will be different if running on a different OS. -e ORACLE_PASSWORD=Welcome12345 -v oracle-volume:/somedirectory --add-host docker.for.mac.host.internal:host-gateway 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: Downloading and install from this location https://www.oracle.com/database/sqldeveloper/technologies/sqlcl/download/ This will provide an executable that we will use to administer the database. For convenience, you may add to your PATH. [SQLcl_INSTALL_DIR]/bin/sql [SQLcl_INSTALL_DIR]/bin Login replacing with the location of your SQLcl and replacing with the one you provided as when creating the database Here is an example when using a local install (eg Oracle Database Free container image): [SQLcl_INSTALL_DIR] Welcome12345 ORACLE_PASSWORD [SQLcl_INSTALL_DIR]/bin/sql /nolog SQL> connect sys@//localhost:1521/freepdb1 as sysdba 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 user and its necessary grants and ACLs (note that these can be further tightened for better security). aijs @sql/create_aijs_acl.sql SQL> @sql/create_aijs_user.sql``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 either the Cohere or HuggingFace queries from the JavaScript code in the database Cohere Example SQL> @sql/coherequery.sql PL/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 is the perfect database 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 & 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. Plus, many other capabilities which, used together, make Oracle a better Vector Database 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 blog went into how to call 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 and . Thanks to Martin and Lucas for all their help. in Martin Bach’s blogs this post about JavaScript modules I look forward to any comments or questions you may have and really appreciate your time reading. Also published . here