Skip to content

Commit 5fbe929

Browse files
committed
added readme, testcases and config files
1 parent 68c0821 commit 5fbe929

File tree

6 files changed

+290
-9
lines changed

6 files changed

+290
-9
lines changed

ads/opctl/operator/lowcode/anomaly/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The operator will run in your local environment without requiring any additional
5858

5959
## 4. Running anomaly detection on the local container
6060

61-
To run the anomaly detection detection operator within a local container, follow these steps:
61+
To run the anomaly detection operator within a local container, follow these steps:
6262

6363
Use the command below to build the anomaly detection container.
6464

@@ -106,7 +106,7 @@ ads operator run -f ~/anomaly/anomaly.yaml --backend-config ~/anomaly/backend_op
106106

107107
## 5. Running anomaly detection in the Data Science job within container runtime
108108

109-
To execute the anomaly detection detection operator within a Data Science job using container runtime, please follow the steps outlined below:
109+
To execute the anomaly detection operator within a Data Science job using container runtime, please follow the steps outlined below:
110110

111111
You can use the following command to build the anomaly detection container. This step can be skipped if you have already done this for running the operator within a local container.
112112

@@ -155,7 +155,7 @@ ads opctl watch <OCID>
155155

156156
## 6. Running anomaly detection in the Data Science job within conda runtime
157157

158-
To execute the anomaly detection detection operator within a Data Science job using conda runtime, please follow the steps outlined below:
158+
To execute the anomaly detection operator within a Data Science job using conda runtime, please follow the steps outlined below:
159159

160160
You can use the following command to build the anomaly detection conda environment.
161161

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
type: recommender
2+
version: v1
3+
conda_type: service
4+
name: Recommender Operator
5+
gpu: no
6+
keywords:
7+
- Recommender
8+
backends:
9+
- job
10+
- operator.local
11+
description: |
12+
Recommender Systems are designed to suggest relevant items, products, or content to users based on their
13+
preferences and behaviors. These systems are widely used in various industries such as e-commerce, entertainment,
14+
and social media to enhance user experience by providing personalized recommendations. They help in increasing user
15+
engagement, satisfaction, and sales by predicting what users might like or need based on their past interactions
16+
and the preferences of similar users.
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# Recommender Operator
2+
3+
Recommender Systems are designed to suggest relevant items, products, or content to users based on their preferences and behaviors. These systems are widely used in various industries such as e-commerce, entertainment, and social media to enhance user experience by providing personalized recommendations. They help in increasing user engagement, satisfaction, and sales by predicting what users might like or need based on their past interactions and the preferences of similar users.
4+
5+
6+
Below are the steps to configure and run the Recommender Operator on different resources.
7+
8+
## 1. Prerequisites
9+
10+
Follow the [CLI Configuration](https://accelerated-data-science.readthedocs.io/en/latest/user_guide/cli/opctl/configure.html) steps from the ADS documentation. This step is mandatory as it sets up default values for different options while running the Recommender Operator on OCI Data Science jobs or OCI Data Flow applications. If you have previously done this and used a flexible shape, make sure to adjust `ml_job_config.ini` with shape config details and `docker_registry` information.
11+
12+
- ocpus = 1
13+
- memory_in_gbs = 16
14+
- docker_registry = `<iad.ocir.io/namespace/>`
15+
16+
## 2. Generating configs
17+
18+
To generate starter configs, run the command below. This will create a list of YAML configs and place them in the `output` folder.
19+
20+
```bash
21+
ads operator init -t recommender --overwrite --output ~/recommender/
22+
```
23+
24+
The most important files expected to be generated are:
25+
26+
- `recommender.yaml`: Contains recommender related configuration.
27+
- `backend_operator_local_python_config.yaml`: This includes a local backend configuration for running recommender in a local environment. The environment should be set up manually before running the operator.
28+
- `backend_operator_local_container_config.yaml`: This includes a local backend configuration for running recommender within a local container. The container should be built before running the operator. Please refer to the instructions below for details on how to accomplish this.
29+
- `backend_job_container_config.yaml`: Contains Data Science job-related config to run recommender in a Data Science job within a container (BYOC) runtime. The container should be built and published before running the operator. Please refer to the instructions below for details on how to accomplish this.
30+
- `backend_job_python_config.yaml`: Contains Data Science job-related config to run recommender in a Data Science job within a conda runtime. The conda should be built and published before running the operator.
31+
32+
All generated configurations should be ready to use without the need for any additional adjustments. However, they are provided as starter kit configurations that can be customized as needed.
33+
34+
## 3. Running recommender on the local conda environment
35+
36+
To run recommender locally, create and activate a new conda environment (`ads-recommender`). Install all the required libraries listed in the `environment.yaml` file.
37+
38+
```yaml
39+
- report-creator
40+
- cerberus
41+
- "git+https://github.com/oracle/accelerated-data-science.git@feature/recommender#egg=oracle-ads"
42+
```
43+
44+
Please review the previously generated `recommender.yaml` file using the `init` command, and make any necessary adjustments to the input and output file locations. By default, it assumes that the files should be located in the same folder from which the `init` command was executed.
45+
46+
Use the command below to verify the recommender config.
47+
48+
```bash
49+
ads operator verify -f ~/recommender/recommender.yaml
50+
```
51+
52+
Use the following command to run the recommender within the `ads-recommender` conda environment.
53+
54+
```bash
55+
ads operator run -f ~/recommender/recommender.yaml -b local
56+
```
57+
58+
The operator will run in your local environment without requiring any additional modifications.
59+
60+
## 4. Running recommender on the local container
61+
62+
To run the recommender operator within a local container, follow these steps:
63+
64+
Use the command below to build the recommender container.
65+
66+
```bash
67+
ads operator build-image -t recommender
68+
```
69+
70+
This will create a new `recommender:v1` image, with `/etc/operator` as the designated working directory within the container.
71+
72+
73+
Check the `backend_operator_local_container_config.yaml` config file. By default, it should have a `volume` section with the `.oci` configs folder mounted.
74+
75+
```yaml
76+
volume:
77+
- "/Users/<user>/.oci:/root/.oci"
78+
```
79+
80+
Mounting the OCI configs folder is only required if an OCI Object Storage bucket will be used to store the input recommender data or output recommender result. The input/output folders can also be mounted to the container.
81+
82+
```yaml
83+
volume:
84+
- /Users/<user>/.oci:/root/.oci
85+
- /Users/<user>/recommender/data:/etc/operator/data
86+
- /Users/<user>/recommender/result:/etc/operator/result
87+
```
88+
89+
The full config can look like:
90+
```yaml
91+
kind: operator.local
92+
spec:
93+
image: recommender:v1
94+
volume:
95+
- /Users/<user>/.oci:/root/.oci
96+
- /Users/<user>/recommender/data:/etc/operator/data
97+
- /Users/<user>/recommender/result:/etc/operator/result
98+
type: container
99+
version: v1
100+
```
101+
102+
Run the recommender within a container using the command below:
103+
104+
```bash
105+
ads operator run -f ~/recommender/recommender.yaml --backend-config ~/recommender/backend_operator_local_container_config.yaml
106+
```
107+
108+
## 5. Running recommender in the Data Science job within container runtime
109+
110+
To execute the recommender operator within a Data Science job using container runtime, please follow the steps outlined below:
111+
112+
You can use the following command to build the recommender container. This step can be skipped if you have already done this for running the operator within a local container.
113+
114+
```bash
115+
ads operator build-image -t recommender
116+
```
117+
118+
This will create a new `recommender:v1` image, with `/etc/operator` as the designated working directory within the container.
119+
120+
Publish the `recommender:v1` container to the [Oracle Container Registry](https://docs.public.oneportal.content.oci.oraclecloud.com/en-us/iaas/Content/Registry/home.htm). To become familiar with OCI, read the documentation links posted below.
121+
122+
- [Access Container Registry](https://docs.public.oneportal.content.oci.oraclecloud.com/en-us/iaas/Content/Registry/Concepts/registryoverview.htm#access)
123+
- [Create repositories](https://docs.public.oneportal.content.oci.oraclecloud.com/en-us/iaas/Content/Registry/Tasks/registrycreatingarepository.htm#top)
124+
- [Push images](https://docs.public.oneportal.content.oci.oraclecloud.com/en-us/iaas/Content/Registry/Tasks/registrypushingimagesusingthedockercli.htm#Pushing_Images_Using_the_Docker_CLI)
125+
126+
To publish `recommender:v1` to OCR, use the command posted below:
127+
128+
```bash
129+
ads operator publish-image recommender:v1 --registry <iad.ocir.io/tenancy/>
130+
```
131+
132+
After the container is published to OCR, it can be used within Data Science jobs service. Check the `backend_job_container_config.yaml` config file. It should contain pre-populated infrastructure and runtime sections. The runtime section should contain an image property, something like `image: iad.ocir.io/<tenancy>/recommender:v1`. More details about supported options can be found in the ADS Jobs documentation - [Run a Container](https://accelerated-data-science.readthedocs.io/en/latest/user_guide/jobs/run_container.html).
133+
134+
Adjust the `recommender.yaml` config with proper input/output folders. When the recommender is run in the Data Science job, it will not have access to local folders. Therefore, input data and output folders should be placed in the Object Storage bucket. Open the `recommender.yaml` and adjust the following fields:
135+
136+
```yaml
137+
input_data:
138+
url: oci://bucket@namespace/recommender/input_data/data.csv
139+
output_directory:
140+
url: oci://bucket@namespace/recommender/result/
141+
```
142+
143+
Run the recommender on the Data Science jobs using the command posted below:
144+
145+
```bash
146+
ads operator run -f ~/recommender/recommender.yaml --backend-config ~/recommender/backend_job_container_config.yaml
147+
```
148+
149+
The logs can be monitored using the `ads opctl watch` command.
150+
151+
```bash
152+
ads opctl watch <OCID>
153+
```
154+
155+
## 6. Running recommender in the Data Science job within conda runtime
156+
157+
To execute the recommender operator within a Data Science job using conda runtime, please follow the steps outlined below:
158+
159+
You can use the following command to build the recommender conda environment.
160+
161+
```bash
162+
ads operator build-conda -t recommender
163+
```
164+
165+
This will create a new `recommender_v1` conda environment and place it in the folder specified within `ads opctl configure` command.
166+
167+
Use the command below to Publish the `recommender_v1` conda environment to the Object Storage bucket.
168+
169+
```bash
170+
ads operator publish-conda -t recommender
171+
```
172+
More details about configuring CLI can be found here - [Configuring CLI](https://accelerated-data-science.readthedocs.io/en/latest/user_guide/cli/opctl/configure.html)
173+
174+
175+
After the conda environment is published to Object Storage, it can be used within Data Science jobs service. Check the `backend_job_python_config.yaml` config file. It should contain pre-populated infrastructure and runtime sections. The runtime section should contain a `conda` section.
176+
177+
```yaml
178+
conda:
179+
type: published
180+
uri: oci://bucket@namespace/conda_environments/cpu/recommender/1/recommender_v1
181+
```
182+
183+
More details about supported options can be found in the ADS Jobs documentation - [Run a Python Workload](https://accelerated-data-science.readthedocs.io/en/latest/user_guide/jobs/run_python.html).
184+
185+
Adjust the `recommender.yaml` config with proper input/output folders. When the recommender is run in the Data Science job, it will not have access to local folders. Therefore, input data and output folders should be placed in the Object Storage bucket. Open the `recommender.yaml` and adjust the following fields:
186+
187+
```yaml
188+
input_data:
189+
url: oci://bucket@namespace/recommender/input_data/data.csv
190+
output_directory:
191+
url: oci://bucket@namespace/recommender/result/
192+
test_data:
193+
url: oci://bucket@namespace/recommender/input_data/test.csv
194+
```
195+
196+
Run the recommender on the Data Science jobs using the command posted below:
197+
198+
```bash
199+
ads operator run -f ~/recommender/recommender.yaml --backend-config ~/recommender/backend_job_python_config.yaml
200+
```
201+
202+
The logs can be monitored using the `ads opctl watch` command.
203+
204+
```bash
205+
ads opctl watch <OCID>
206+
```

ads/opctl/operator/lowcode/recommender/cmd.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ def init(**kwargs: Dict) -> str:
2828
The YAML specification generated based on the schema.
2929
"""
3030

31-
default_detector = [{"name": "<type>.<entity>", "action": "mask"}]
32-
3331
return YamlGenerator(
3432
schema=_load_yaml_from_uri(__file__.replace("cmd.py", "schema.yaml"))
35-
).generate_example_dict(
36-
values={"type": kwargs.get("type"), "detectors": default_detector}
37-
)
33+
).generate_example_dict(values={"type": kwargs.get("type")})

ads/opctl/operator/lowcode/recommender/schema.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ type:
2121
type: string
2222
default: recommender
2323
meta:
24-
description: "Type should always be `pii` when using a pii operator"
24+
description: "Type should always be `recommender` when using a recommender operator"
2525

2626

2727
spec:
2828
required: true
29+
type: dict
2930
schema:
3031
user_data:
3132
required: true
@@ -83,6 +84,7 @@ spec:
8384
item_data:
8485
required: true
8586
type: dict
87+
default: {"url": "item_data.csv"}
8688
meta:
8789
description: "This should contain item related attribute"
8890
schema:
@@ -134,6 +136,7 @@ spec:
134136

135137
interactions_data:
136138
required: true
139+
default: {"url": "interactions_data.csv"}
137140
meta:
138141
description: "This should include interactions between items and users"
139142
schema:
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright (c) 2023, 2024 Oracle and/or its affiliates.
4+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
5+
6+
import os
7+
import subprocess
8+
import tempfile
9+
from time import sleep
10+
11+
import yaml
12+
13+
DATASET_PREFIX = f"{os.path.dirname(os.path.abspath(__file__))}/../data/recommendation/"
14+
15+
16+
def test_recommender():
17+
user_file = f"{DATASET_PREFIX}users.csv"
18+
item_file = f"{DATASET_PREFIX}items.csv"
19+
interation_file = f"{DATASET_PREFIX}interactions.csv"
20+
21+
yaml_params = {
22+
"kind": "operator",
23+
"type": "recommender",
24+
"version": "v1",
25+
"spec": {
26+
"user_data": {
27+
"url": user_file,
28+
},
29+
"item_data": {
30+
"url": item_file,
31+
},
32+
"interactions_data": {
33+
"url": interation_file,
34+
},
35+
"output_directory": {
36+
"url": "results",
37+
},
38+
"top_k": 4,
39+
"model_name": "svd",
40+
"user_column": "user_id",
41+
"item_column": "movie_id",
42+
"interaction_column": "rating",
43+
"recommendations_filename": "recommendations.csv",
44+
"generate_report": True,
45+
"report_filename": "report.html"
46+
}
47+
}
48+
49+
with tempfile.TemporaryDirectory() as tmpdirname:
50+
recommender_yaml_filename = f"{tmpdirname}/recommender.yaml"
51+
output_dirname = f"{tmpdirname}/results"
52+
yaml_params["spec"]["output_directory"]["url"] = output_dirname
53+
with open(recommender_yaml_filename, "w") as f:
54+
f.write(yaml.dump(yaml_params))
55+
sleep(0.5)
56+
subprocess.run(
57+
f"ads operator run -f {recommender_yaml_filename} --debug", shell=True
58+
)
59+
sleep(0.1)
60+
assert os.path.exists(f"{output_dirname}/report.html"), "Report not generated."

0 commit comments

Comments
 (0)