Call an Embedded Function with SQL and REST APIs

You can call an embedded function with SQL and REST APIs.

Perform the following steps:
  1. 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 ID
    • database_name: Name of the database
    • user_name: OML username
    • password: Password for the OML user
    • host: 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}'")
  2. 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 pyqEval function which then calls the user-defined Python function in a SQL query.

    The following code uses the pyqEval function to call the errorModelPredict function that was previously created. The function also passes the X 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]]
  3. 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 the token 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]]}