- Using Oracle Spatial AI on Autonomous Database Serverless
- Use Spatial AI with OML Embedded Python Execution
- Call an Embedded Function with SQL and REST APIs
Call an Embedded Function with SQL and REST APIs
You can call an embedded function with SQL and REST APIs.
- Get an access token before calling OML embedded execution API from SQL or
REST.As a prerequisite, note the following information for your ADB environment:
tenant_name:
Tenancy IDdatabase_name:
Name of the databaseuser_name:
OML usernamepassword:
Password for the OML userhost:
Root domain
Perform a REST request to get an access token. The REST request can be done using different approaches. For example, the following code shows how to get a token using a REST call through Python.
import json import requests import warnings import os token=None response = requests.post( f"https://{host}:443/omlusers/tenants/{tenant_name}/databases/{database_name}/api/oauth2/v1/token", headers={"Content-Type": "application/json", "Accept": "application/json"}, json={"grant_type": "password", "username": username, "password": password}, verify=False) token = response.json()["accessToken"] print(f"token='{token}'")
- Call an Embedded Python Function from SQL.
An access token must be set always before performing a call to OML Embedded Execution from SQL. Set the access token in the token store through SQL or PL/SQL and the
pyqSetAuthToken
function.exec pyqSetAuthToken('<access-token>');
Call the OML's
The following code uses thepyqEval
function which then calls the user-defined Python function in a SQL query.pyqEval
function to call theerrorModelPredict
function that was previously created. The function also passes theX
parameter consisting of a single observation.select * from table(pyqEval( par_lst => '{"X": [[30.6005898, 342200.000]]}', out_fmt => 'JSON', scr_name => 'errorModelPredict' ));
The result from the preceding code consists of the predicted median income for the given observation.
NAME VALUE [[48228.470695050346]]
- Call an Embedded Python Function from REST.Make a successful REST request by passing the Spatial AI function-specific parameters within the
parameters
field as a JSON string.The following examples use CURL to send a request that calls the
errorModelPredict
function with the parameter X containing a single observation. Note that an access token must first be obtained. In this example, the access token is set in thetoken
environment variable and is passed in the request.curl -i -k -X POST --header 'Authorization: Bearer ${token}' \ --header 'Content-Type: application/json' --header 'Accept: application/json' \ -d '{ "oml_connect": true, "parameters": "{\"X\": [[30.6005898, 342200.000]]}" }' \ "${host}:8080/oml/tenants/${tenant_name}/databases/${database_name}/api/py-scripts/v1/do-eval/errorModelPredict"
The following shows a sample response that includes the request status code and the output of the function representing the estimated income value for the given observation.
HTTP/1.1 200 OK Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1;mode=block Strict-Transport-Security: max-age=31536000; includeSubDomains X-Content-Type-Options: nosniff Content-Security-Policy: frame-ancestors 'none' Set-Cookie: JSESSIONID=node0nyjijo5nrw2swfj850bvbauc43.node0; Path=/oml; Secure; HttpOnly Expires: Thu, 01 Jan 1970 00:00:00 GMT Content-Type: application/json Content-Length: 32 {"result":[[48228.470695050346]]}