|
| 1 | +# Introduction |
| 2 | + |
| 3 | +This codes showcase how to use [Oracle Fn (Functions)](https://fnproject.io/) to execute Model deployment predict endpoint. |
| 4 | + |
| 5 | +## Pre-Requisites |
| 6 | + |
| 7 | +To be able to test the code described on this page, ensure you have access to the Oracle Data Science Model Deploy in your tenancy as well as Oracle Cloud Applications (aka Functions). |
| 8 | + |
| 9 | +## Local Enviroment Setup |
| 10 | + |
| 11 | +To be able to run this example, you have to install OCI SDK and configure your [API Auth Token](https://docs.oracle.com/en-us/iaas/Content/Registry/Tasks/registrygettingauthtoken.htm). |
| 12 | + |
| 13 | +The OCI API Auth Token is used for the OCI CLI and Python SDK, as well as all other OCI SDK supported languages. Follow the guidance from the online documentation to configure it: <https://docs.oracle.com/en-us/iaas/Content/API/Concepts/apisigningkey.htm> |
| 14 | + |
| 15 | +At the high level the instructions are: |
| 16 | + |
| 17 | +- (1) `login` into your Oracle Cloud |
| 18 | +- (2) `select your account` from the top right dropdown menu |
| 19 | +- (3) generate a new `API Auth Key` |
| 20 | +- (4) download the private key and store it into your `$HOME/.oci` folder on your local machine |
| 21 | +- (5) copy the suggested configuration and store it into config file under your home directy `$HOME/.oci/config` |
| 22 | +- (6) change the `$HOME/.oci/config` with the suggested configuration in (5) to point to your private key |
| 23 | +- (7) test the SDK or CLI |
| 24 | + |
| 25 | +For more detailed explanations, follow the instructions from our public documentation. |
| 26 | + |
| 27 | +### Install Desktop Container Management |
| 28 | + |
| 29 | +This example requires a desktop tool to build, run, launch and push the containers. We support: |
| 30 | + |
| 31 | +- [Docker Desktop](<https://docs.docker.com/get-docker>) |
| 32 | +- [Rancher Desktop](<https://rancherdesktop.io/>) |
| 33 | + |
| 34 | +### Install and Configure Oracle Functions |
| 35 | + |
| 36 | +To be able to execute this example you have to install the configure Oracle Fn on your local environment. |
| 37 | + |
| 38 | +- Install Oracle Fn <https://fnproject.io/tutorials/install/> |
| 39 | +- Create new context: ```fn create context <your-tenancy-name> --provider oracle``` |
| 40 | +- Update the Fn context to point to your region, for example: ```fn update context api-url https://functions.<your-region>.oci.oraclecloud.com``` |
| 41 | +- Update the Fn registry to point to your OCIR: ```fn update context registry <region-key>.ocir.io/<your-tenancy-namespace>/<your-repo-name-prefix>``` |
| 42 | +- Update the Fn contex to point to your compartment: ```fn update context oracle.compartment-id <your-compartment-ocid>``` |
| 43 | + |
| 44 | +# Create OCI Function for invoking Model Deployment predict endpoint |
| 45 | +This project helps create a function that will be used as a backend for API Gateway. API Gateway endpoint can then be used as a proxy server infront of Model deployment endpoint. Refer [page](https://docs.oracle.com/en-us/iaas/Content/Functions/Tasks/functionscreatingfirst.htm) to refer details about creating a sample function. |
| 46 | +For more examples, refer functions examples [repo](https://github.com/oracle-samples/oracle-functions-samples). |
| 47 | + |
| 48 | +# Adding Function as API Gateway Backend |
| 49 | +Refer [page](https://docs.oracle.com/en-us/iaas/Content/APIGateway/Tasks/apigatewayusingfunctionsbackend.htm) to find details about how to use function as API Gateway. |
| 50 | + |
| 51 | +# Build using fn cli |
| 52 | +```bash |
| 53 | +fn -v deploy --app <app-name> |
| 54 | +``` |
| 55 | + |
| 56 | +# oci-cli based function invocation |
| 57 | +```bash |
| 58 | +oci fn function invoke --function-id <function-ocid> --file "-" --body '{"ENDPOINT":"<predict-url>", "PAYLOAD": "<json-payload>"}' |
| 59 | +``` |
| 60 | + |
| 61 | +## Sample: |
| 62 | +```bash |
| 63 | +oci fn function invoke --function-id <function-ocid> --file "-" --body '{"ENDPOINT":"https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<md-ocid>/predict", "PAYLOAD": "{\"index\": \"1\"}"}' |
| 64 | +``` |
| 65 | + |
| 66 | +# fn cli based invocation |
| 67 | +```bash |
| 68 | +fn invoke <app-name> <function-name> |
| 69 | +``` |
| 70 | + |
| 71 | +## Sample: |
| 72 | +```bash |
| 73 | +echo -n '{"ENDPOINT":"https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<md-ocid>/predict", "PAYLOAD": "{\"index\": \"1\"}"}' | fn invoke <app-name> <function-name> |
| 74 | +``` |
| 75 | + |
| 76 | +# More information |
| 77 | +The sample code in [func.py](./func.py) also shows how to get headers and request body. Required headers can also be passed to downstream call, if needed. |
| 78 | +Other ways of function invocation can be found [here](https://docs.oracle.com/en-us/iaas/Content/Functions/Tasks/functionsinvokingfunctions.htm) |
0 commit comments