### Run Example Script in JupyterLab Source: https://github.com/massquantity/librecommender/blob/master/docker/README.md Navigates to the examples directory within the running JupyterLab instance and executes a Python script named 'pure_ranking_example.py'. This demonstrates how to run pre-included example scripts. ```shell cd examples %run pure_ranking_example.py ``` -------------------------------- ### Start Redis Server Source: https://github.com/massquantity/librecommender/blob/master/docs/source/serving_guide/python.md Command to start a Redis server, which is required for certain serving operations, such as loading models from disk to Redis. ```bash redis-server ``` -------------------------------- ### Clone LibRecommender Repository Source: https://github.com/massquantity/librecommender/blob/master/docs/source/serving_guide/python.md Commands to clone the LibRecommender repository and navigate into the libserving directory, which is the assumed working directory for the serving examples. ```bash git clone https://github.com/massquantity/LibRecommender.git cd LibRecommender/libserving ``` -------------------------------- ### Request Recommendations from KNN Model Server Source: https://github.com/massquantity/librecommender/blob/master/docs/source/serving_guide/python.md Example of how to make HTTP POST requests to the Sanic server to get recommendations from the deployed KNN model. It shows both a Python script example and a curl command. ```bash # make requests python request.py --user 1 --n_rec 10 --algo knn curl -d '{"user": 1, "n_rec": 10}' -X POST http://127.0.0.1:8000/knn/recommend # {'Recommend result for user 1': ['480', '589', '2571', '260', '2028', '1198', '1387', '1214', '1291', '1197']} ``` -------------------------------- ### Start Docker Compose for Serving Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/rust_serving_guide.md Starts the services defined in the docker-compose-rs.yml file, including the Actix serving application and Redis. This command is used to deploy the embedding-based recommendation system in a containerized environment. ```bash $ sudo docker compose -f docker-compose-rs.yml up # start docker compose, which will load faiss index ``` -------------------------------- ### Start TensorFlow Serving Docker Container Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/python_serving_guide.md This bash command initiates a TensorFlow Serving instance using Docker. It maps the necessary ports, mounts the local model directory, and sets environment variables for the model name and base path. Ensure Docker is installed and the model files are in the specified directory. ```bash $ MODEL_NAME=din $ MODEL_PATH=tf_model $ sudo docker run --rm -t -p 8501:8501 --mount type=bind,source=$(pwd),target=$(pwd) -e MODEL_BASE_PATH=$(pwd)/${MODEL_PATH} -e MODEL_NAME=${MODEL_NAME} tensorflow/serving:2.8.2 ``` -------------------------------- ### Start Docker Compose for TensorFlow Serving Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/rust_serving_guide.md Starts both the Actix serving application and the TensorFlow serving instances using Docker Compose. This command is used to deploy TensorFlow-based recommendation models, requiring specific configurations in the docker-compose files. ```bash sudo docker compose -f docker-compose-rs.yml -f docker-compose-tf-serving.yml up ``` -------------------------------- ### Combined Docker Compose for TF and Embedding Models Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/rust_serving_guide.md Instructions for starting both TensorFlow Serving and the Actix serving application using Docker Compose for hybrid serving scenarios. ```APIDOC ## Deploy TensorFlow Serving and Actix Serving via Docker Compose ### Description Starts both the Actix serving application and TensorFlow Serving using Docker Compose. This allows for serving both embedding-based and TensorFlow-based models concurrently. Ensure environment variables like `MODEL_BASE_PATH` and `MODEL_NAME` are correctly configured in the respective Docker Compose files. ### Method Bash Command ### Endpoint N/A ### Parameters #### Command Arguments - **-f docker-compose-rs.yml** - Specifies the Docker Compose file for Actix serving and Redis. - **-f docker-compose-tf-serving.yml** - Specifies the Docker Compose file for TensorFlow Serving. ### Request Example ```bash sudo docker compose -f docker-compose-rs.yml -f docker-compose-tf-serving.yml up ``` ## Serve Recommendations (TensorFlow via Docker Compose) ### Description Sends a POST request to the Actix server to get recommendations from a deployed TensorFlow model when using the combined Docker Compose setup. ### Method HTTP POST ### Endpoint `/tf/recommend` ### Parameters #### Query Parameters N/A #### Request Body - **user** (string) - Required - The user ID for whom to generate recommendations. - **n_rec** (integer) - Required - The number of recommendations to return. ### Request Example ```json { "user": "1", "n_rec": 10 } ``` ### Response #### Success Response (200) - **rec_list** (array of strings) - A list of recommended item IDs. #### Response Example ```json { "rec_list": ["858", "260", "2355", "1287", "527", "2371", "1220", "377", "1968", "3362"] } ``` ``` -------------------------------- ### Start Redis Server for Model Serving Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/python_serving_guide.md Command to start a Redis server, which is often used as a backend for storing and retrieving model data or features during the serving process. Ensure Redis is running before starting the Sanic server. ```bash $ redis-server ``` -------------------------------- ### Deploy with Docker Compose for Serving Source: https://github.com/massquantity/librecommender/blob/master/docs/source/serving_guide/rust.md Provides commands to launch services using Docker Compose. It shows how to start the Actix serving application and TensorFlow Serving or FAISS-based services by specifying the appropriate Compose files and configuring environment variables like `MODEL_TYPE` and `MODEL_BASE_PATH`. ```bash $ cd LibRecommender/libserving $ sudo docker compose -f docker-compose-rs.yml up # start docker compose, which will load faiss index $ sudo docker compose -f docker-compose-rs.yml -f docker-compose-tf-serving.yml up ``` -------------------------------- ### Install LibRecommender Source: https://github.com/massquantity/librecommender/blob/master/docs/source/tutorial.md Installs the latest version of the LibRecommender library using pip. This is a prerequisite for running the tutorial and using the library's functionalities. ```bash pip install -U LibRecommender ``` -------------------------------- ### KNN-based Model Serving Source: https://github.com/massquantity/librecommender/blob/master/docs/source/serving_guide/rust.md Instructions for serving KNN-based models, including model training, serialization to Redis, and setting up the Rust Actix server. ```APIDOC ## KNN-based Model Serving ### Description This section details the process of serving KNN-based recommendation models using Rust and Actix. It includes training the model, saving it in a format compatible with Redis, and configuring the Actix server for recommendations. ### Steps: 1. **Navigate to `libserving` directory:** ```bash cd LibRecommender/libserving ``` 2. **Train and save KNN model:** ```python3 from libreco.algorithms import ItemCF from libreco.data import DatasetPure from libserving.serialization import knn2redis, save_knn train_data, data_info = DatasetPure.build_trainset(...) model = ItemCF(...) model.fit(...) path = "knn_model" save_knn(path, model, k=10) knn2redis(path, host="localhost", port=6379, db=0) ``` 3. **Configure environment variables and build/run the server:** ```bash cd actix_serving export PORT=8080 MODEL_TYPE=knn WORKERS=8 # Develop mode cargo run --package actix_serving --bin actix_serving # Production mode cargo build --release ./target/release/actix_serving ``` ### API Endpoint: #### POST /knn/recommend ##### Description Provides KNN-based recommendations for a given user. ##### Method `POST` ##### Endpoint `/knn/recommend` ##### Parameters * **Request Body**: * `user` (string) - Required - The ID of the user for whom to generate recommendations. * `n_rec` (integer) - Optional - The number of recommendations to return. Defaults to 10. ##### Request Example ```json { "user": "1", "n_rec": 10 } ``` ##### Success Response (200) * **rec_list** (array of strings) - A list of recommended item IDs. ``` -------------------------------- ### Install LibRecommender Source: https://github.com/massquantity/librecommender/blob/master/examples/tutorial.ipynb Installs the latest version of the LibRecommender library using pip. This is a prerequisite for running the rest of the tutorial. ```python !pip install -U LibRecommender ``` -------------------------------- ### Run Actix Serving Application for Embeddings (Rust) Source: https://github.com/massquantity/librecommender/blob/master/docs/source/serving_guide/rust.md Builds and runs the Actix web serving application for embedding-based models. This process assumes Rust bindings for Faiss have been successfully installed. It supports development and production modes. ```bash cd actix_serving export PORT=8080 MODEL_TYPE=embed WORKERS=8 # assign environment variables cargo run # develop mode, this uses rust faiss ``` ```bash cargo build --release # production mode ./target/release/actix_serving ``` -------------------------------- ### Batch User Recommendation with Filtering Options Source: https://github.com/massquantity/librecommender/blob/master/docs/source/user_guide/recommendation.md Demonstrates how to get recommendations for multiple users simultaneously. Supports filtering previously consumed items, random sampling based on scores for diversity, and batch processing. The output is a dictionary mapping user IDs to recommended item arrays. ```python3 >>> model.recommend_user(user=[1, 2, 3], n_rec=3, filter_consumed=True, random_rec=False) # returns {1: array([2529, 1196, 2916]), 2: array([ 541, 750, 1299]), 3: array([3183, 2722, 2672])} ``` -------------------------------- ### Model Retraining with New Users and Items using LightGCN Source: https://context7.com/massquantity/librecommender/llms.txt Demonstrates how to efficiently retrain recommendation models, specifically LightGCN, with new user and item data without requiring a full retraining from scratch. This involves saving the initial model and data information, then loading them to incorporate new interactions. This approach is crucial for maintaining model relevance in dynamic environments. The example shows initial training, model saving, and the setup for retraining with new data. ```python from libreco.algorithms import LightGCN from libreco.data import DatasetPure, random_split import pandas as pd # Initial training data = pd.read_csv("initial_data.dat", sep="::", names=["user", "item", "label", "time"]) train, test = random_split(data, test_size=0.2) train_data, data_info = DatasetPure.build_trainset(train) model = LightGCN(task="ranking", data_info=data_info, embed_size=16, n_epochs=10, lr=1e-3, batch_size=2048) model.fit(train_data, neg_sampling=True, verbose=2) # Save original model data_info.save(path="model_path", model_name="lightgcn") model.save(path="model_path", model_name="lightgcn") # New data arrives with new users/items new_data = pd.read_csv("new_interactions.dat", sep="::", names=["user", "item", "label", "time"]) # To retrain, you would typically load the data_info and model, then fit again # with the combined or new data, potentially adjusting parameters for gradual learning. ``` -------------------------------- ### Test Recommendation Service API Source: https://github.com/massquantity/librecommender/blob/master/docs/source/serving_guide/rust.md Demonstrates how to send POST requests to the Actix serving API to get recommendations from both TensorFlow and embedding-based models. It shows the expected JSON payload for the request and the format of the recommendation list returned by the API. ```bash $ curl -d '{"user": "1", "n_rec": 10}' -H "Content-Type: application/json" -X POST http://0.0.0.0:8080/embed/recommend # {"rec_list":["2628","1552","260","969","2193","733","1573","1917","1037","10"]} $ curl -d '{"user": "1", "n_rec": 10}' -H "Content-Type: application/json" -X POST http://0.0.0.0:8080/tf/recommend # {"rec_list":["858","260","2355","1287","527","2371","1220","377","1968","3362"]} ``` -------------------------------- ### Request KNN Recommendations via cURL (Bash) Source: https://github.com/massquantity/librecommender/blob/master/docs/source/serving_guide/rust.md Sends a POST request to the Actix serving API to get KNN-based recommendations. Expects a JSON payload with 'user' and 'n_rec' and returns a JSON response with the recommended item list. ```bash curl -d '{"user": "1", "n_rec": 10}' -H "Content-Type: application/json" -X POST http://0.0.0.0:8080/knn/recommend # {"rec_list":["1806","1687","3799","807","1303","1860","3847","3616","1696","1859"]} ``` -------------------------------- ### Python: Include Features Example with YouTubeRanking Source: https://github.com/massquantity/librecommender/blob/master/README.md Demonstrates how to use the YouTubeRanking algorithm with feature data. It includes data splitting, dataset preparation with sparse and dense features, model training, and prediction/recommendation. This example requires numpy and pandas libraries. ```python import numpy as np import pandas as pd from libreco.data import split_by_ratio_chrono, DatasetFeat from libreco.algorithms import YouTubeRanking # feat data, algorithm YouTubeRanking data = pd.read_csv("examples/sample_data/sample_movielens_merged.csv", sep=",", header=0) # split into train and test data based on time train_data, test_data = split_by_ratio_chrono(data, test_size=0.2) # specify complete columns information sparse_col = ["sex", "occupation", "genre1", "genre2", "genre3"] dense_col = ["age"] user_col = ["sex", "age", "occupation"] item_col = ["genre1", "genre2", "genre3"] train_data, data_info = DatasetFeat.build_trainset( train_data, user_col, item_col, sparse_col, dense_col ) test_data = DatasetFeat.build_testset(test_data) print(data_info) # n_users: 5962, n_items: 3226, data sparsity: 0.4185 % ytb_ranking = YouTubeRanking( task="ranking", data_info=data_info, embed_size=16, n_epochs=3, lr=1e-4, batch_size=512, use_bn=True, hidden_units=(128, 64, 32), ) ytb_ranking.fit( train_data, neg_sampling=True, verbose=2, shuffle=True, eval_data=test_data, metrics=["loss", "roc_auc", "precision", "recall", "map", "ndcg"], ) # predict preference of user 2211 to item 110 ytb_ranking.predict(user=2211, item=110) # recommend 7 items for user 2211 ytb_ranking.recommend_user(user=2211, n_rec=7) # cold-start prediction ytb_ranking.predict(user="ccc", item="not item", cold_start="average") # cold-start recommendation ytb_ranking.recommend_user(user="are we good?", n_rec=7, cold_start="popular") ``` -------------------------------- ### Embed-based Model Serving Source: https://github.com/massquantity/librecommender/blob/master/docs/source/serving_guide/rust.md Instructions for serving embedding-based models, including model training, serialization to Redis, and setting up the Rust Actix server with Faiss integration. ```APIDOC ## Embed-based Model Serving ### Description This section outlines serving embedding-based recommendation models using Rust and Actix, with a focus on integrating Rust bindings for Faiss. It covers model training, saving embeddings, and configuring the Actix server. ### Prerequisites: * Install Rust bindings for Faiss. This may involve building Faiss from source and setting `LD_LIBRARY_PATH`. ### Steps: 1. **Navigate to `libserving` directory:** ```bash cd LibRecommender/libserving ``` 2. **Train and save Embed model:** ```python3 from libreco.algorithms import ALS from libreco.data import DatasetPure from libserving.serialization import embed2redis, save_embed, save_faiss_index train_data, data_info = DatasetPure.build_trainset(...) model = ALS(...) model.fit(...) path = "embed_model" save_embed(path, model) embed2redis(path, host="localhost", port=6379, db=0) save_faiss_index(path, model) # Note: uses Python Faiss ``` 3. **Configure environment variables and build/run the server:** ```bash cd actix_serving export PORT=8080 MODEL_TYPE=embed WORKERS=8 # Develop mode (uses Rust Faiss) cargo run # Production mode cargo build --release ./target/release/actix_serving ``` ### API Endpoint: #### POST /embed/recommend ##### Description Provides embedding-based recommendations for a given user. ##### Method `POST` ##### Endpoint `/embed/recommend` ##### Parameters * **Request Body**: * `user` (string) - Required - The ID of the user for whom to generate recommendations. * `n_rec` (integer) - Optional - The number of recommendations to return. Defaults to 10. ##### Request Example ```json { "user": "1", "n_rec": 10 } ``` ##### Success Response (200) * **rec_list** (array of strings) - A list of recommended item IDs. ``` -------------------------------- ### Run Sanic Server for Embed-based Recommendations Source: https://github.com/massquantity/librecommender/blob/master/docs/source/serving_guide/python.md This bash command deploys an embed-based recommendation model using a Sanic web server. It starts the server in development mode with access logs enabled. The server expects requests to the /embed/recommend endpoint. ```bash sanic sanic_serving.embed_deploy:app --dev --access-logs -v --workers 1 # run sanic server ``` -------------------------------- ### Shell: Build LibRecommender from Source Source: https://github.com/massquantity/librecommender/blob/master/README.md Installs LibRecommender by building it from the source code. This involves cloning the repository and then performing a local installation using pip. ```shell $ git clone https://github.com/massquantity/LibRecommender.git $ cd LibRecommender $ pip install . ``` -------------------------------- ### TensorFlow Model Serving Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/rust_serving_guide.md This section details how to save a TensorFlow-based model, deploy it using TensorFlow Serving, and serve recommendations via an Actix web server. ```APIDOC ## Save TensorFlow Model ### Description Saves a trained TensorFlow model to disk in JSON format and then serializes it to Redis. ### Method Python Script ### Endpoint N/A ### Parameters #### Path Parameters - **path** (string) - Required - Directory to save the model. #### Query Parameters N/A #### Request Body N/A ### Request Example ```python from libreco.algorithms import DIN from libreco.data import DatasetFeat from libserving.serialization import save_tf, tf2redis train_data, data_info = DatasetFeat.build_trainset(...) model = DIN(...) model.fit(...) path = "tf_model" save_tf(path, model, version=1) tf2redis(path, host="localhost", port=6379, db=0) ``` ### Response #### Success Response (200) N/A (Output is file saving and Redis loading) #### Response Example N/A ## Deploy TensorFlow Serving ### Description Starts a TensorFlow Serving instance using Docker, mounting the local model directory and specifying the model name. ### Method Bash Command ### Endpoint N/A ## Start Actix Serving (TensorFlow Mode) ### Description Sets environment variables for the Actix serving environment and starts the server in development or production mode for TensorFlow models. ### Method Bash Command ### Endpoint N/A ### Parameters #### Environment Variables - **PORT** (integer) - Required - Port for the Actix server. - **MODEL_TYPE** (string) - Required - Set to "tf" for TensorFlow models. - **WORKERS** (integer) - Required - Number of worker threads. ### Request Example ```bash export PORT=8080 export MODEL_TYPE=tf export WORKERS=8 ``` ## Serve Recommendations (TensorFlow) ### Description Sends a POST request to the Actix server to get recommendations from a deployed TensorFlow model. ### Method HTTP POST ### Endpoint `/tf/recommend` ### Parameters #### Query Parameters N/A #### Request Body - **user** (string) - Required - The user ID for whom to generate recommendations. - **n_rec** (integer) - Required - The number of recommendations to return. ### Request Example ```json { "user": "1", "n_rec": 10 } ``` ### Response #### Success Response (200) - **rec_list** (array of strings) - A list of recommended item IDs. #### Response Example ```json { "rec_list": ["858", "260", "2355", "1287", "527", "2371", "1220", "377", "1968", "3362"] } ``` ``` -------------------------------- ### Get Recommendations for Multiple Users Source: https://github.com/massquantity/librecommender/blob/master/examples/tutorial.ipynb Generates recommendations for a batch of user IDs. This is an efficient way to get recommendations for multiple users simultaneously. The output is a dictionary mapping each user ID to their respective recommended item IDs. ```python model.recommend_user(user=[1, 2, 3], n_rec=3) ``` -------------------------------- ### Display Data Shape and Sample Rows Source: https://github.com/massquantity/librecommender/blob/master/docs/source/tutorial.md After loading the data, this code snippet shows how to get the shape of the resulting DataFrame and display a random sample of 10 rows to inspect the data. ```python >>> data = load_ml_1m() >>> data.shape ``` ```python >>> data.iloc[random.choices(range(len(data)), k=10)] # randomly select 10 rows ``` -------------------------------- ### Display Data Shape and Sample Source: https://github.com/massquantity/librecommender/blob/master/examples/tutorial.ipynb Loads the MovieLens 1M dataset using the `load_ml_1m` function and prints its shape. It then displays a random sample of 10 rows to give a preview of the loaded data. ```python data = load_ml_1m() print("data shape:", data.shape) ``` ```python data.iloc[random.choices(range(len(data)), k=10)] # randomly select 10 rows ``` -------------------------------- ### Start LibRecommender Docker Container Source: https://github.com/massquantity/librecommender/blob/master/docker/README.md Starts a Docker container for LibRecommender, exposing the JupyterLab interface. It maps port 8888 inside the container to port 8889 on the host machine. Users can change the host port if needed. ```shell docker run --rm -p 8889:8888 massquantity/librecommender:latest ``` -------------------------------- ### Prepare New Data for Retraining Source: https://github.com/massquantity/librecommender/blob/master/docs/source/tutorial.md Splits existing data into training and evaluation sets for retraining. This prepares a subset of data, often new incoming data, for updating the recommender model without starting from scratch. ```python3 >>> second_half_data = data[(len(data) // 2) :] >>> train_data, eval_data = random_split(second_half_data, multi_ratios=[0.8, 0.2]) ``` -------------------------------- ### Start LibRecommender Docker Container with Data Mount Source: https://github.com/massquantity/librecommender/blob/master/docker/README.md Starts a Docker container for LibRecommender and mounts the current host directory to '/root/data' inside the container in read-only mode. This allows using custom data stored on the host machine with the library. ```shell docker run --rm -p 8889:8888 -v $(pwd):/root/data:ro massquantity/librecommender:latest ``` -------------------------------- ### Run Sanic Server for LibRecommender Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/python_serving_guide.md Start the Sanic web server to serve recommendations from a TensorFlow model. This command runs the server in development mode with access logs enabled. The server listens for incoming recommendation requests. ```bash $ sanic sanic_serving.tf_deploy:app --dev --access-logs -v --workers 1 # run sanic server ``` -------------------------------- ### Pure Collaborative-Filtering with LightGCN in Python Source: https://github.com/massquantity/librecommender/blob/master/README.md This example demonstrates training a collaborative-filtering recommender system using the LightGCN algorithm. It covers data loading, splitting, dataset building, model training with evaluation metrics, final evaluation, and prediction/recommendation, including cold-start scenarios. Dependencies include pandas, numpy, and specific modules from libreco. ```python import numpy as np import pandas as pd from libreco.data import random_split, DatasetPure from libreco.algorithms import LightGCN # pure data, algorithm LightGCN from libreco.evaluation import evaluate data = pd.read_csv("examples/sample_data/sample_movielens_rating.dat", sep="::", names=["user", "item", "label", "time"]) # split whole data into three folds for training, evaluating and testing train_data, eval_data, test_data = random_split(data, multi_ratios=[0.8, 0.1, 0.1]) train_data, data_info = DatasetPure.build_trainset(train_data) eval_data = DatasetPure.build_evalset(eval_data) test_data = DatasetPure.build_testset(test_data) print(data_info) # n_users: 5894, n_items: 3253, data sparsity: 0.4172 % lightgcn = LightGCN( task="ranking", data_info=data_info, loss_type="bpr", embed_size=16, n_epochs=3, lr=1e-3, batch_size=2048, num_neg=1, device="cuda", ) # monitor metrics on eval data during training lightgcn.fit( train_data, neg_sampling=True, verbose=2, eval_data=eval_data, metrics=["loss", "roc_auc", "precision", "recall", "ndcg"], ) # do final evaluation on test data evaluate( model=lightgcn, data=test_data, neg_sampling=True, metrics=["loss", "roc_auc", "precision", "recall", "ndcg"], ) # predict preference of user 2211 to item 110 lightgcn.predict(user=2211, item=110) # recommend 7 items for user 2211 lightgcn.recommend_user(user=2211, n_rec=7) # cold-start prediction lightgcn.predict(user="ccc", item="not item", cold_start="average") # cold-start recommendation lightgcn.recommend_user(user="are we good?", n_rec=7, cold_start="popular") ``` -------------------------------- ### Inspect TensorFlow SavedModel with CLI Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/python_serving_guide.md This bash command shows how to use the TensorFlow SavedModel CLI to inspect the structure and signature definitions of a saved DIN model. It requires the path to the model directory and assumes TensorFlow is installed. ```bash $ saved_model_cli show --dir tf_model/din/1 --all ``` -------------------------------- ### Initialize and Train PinSage Models (PyTorch and DGL) Source: https://github.com/massquantity/librecommender/blob/master/docs/source/internal/implementation_details.md This snippet demonstrates how to initialize and train both the PyTorch and DGL versions of the PinSage model. It shows setting the task, data information, loss type, and paradigm. The PyTorch version offers more control over sampling, while the DGL version is generally faster. Ensure `DatasetFeat` and the respective PinSage classes are imported. ```python >>> sparse_col = ["sex", "occupation", "genre1", "genre2", "genre3"] >>> dense_col = ["age"] >>> user_col = ["sex", "age", "occupation"] >>> item_col = ["genre1", "genre2", "genre3"] >>> train_data, data_info = DatasetFeat.build_trainset(train, user_col, item_col, sparse_col, dense_col) >>> from libreco.algorithms import PinSage, PinSageDGL >>> model = PinSage( # PyTorch version >>> task, >>> data_info, >>> loss_type="cross_entropy", # or "focal", "bpr", "max_margin" >>> paradigm="u2i", # or "i2i" >>> ) >>> model = PinSageDGL( # DGL version >>> task, >>> data_info, >>> loss_type="max_margin", >>> paradigm="i2i", >>> ) >>> model.fit(train_data) ``` -------------------------------- ### User Recommendation with Dynamic Features and Sequences Source: https://github.com/massquantity/librecommender/blob/master/docs/source/user_guide/recommendation.md Illustrates how to generate recommendations for a user by considering their dynamic features and historical item sequence. This is supported by 'feat' models and specific sequence-aware models. 'cold_start' parameter can be set to 'popular' to handle new users. ```python3 >>> model.recommend_user(user=1, n_rec=7, cold_start="popular", >>> user_feats={"sex": "F", "occupation": 2, "age": 23}, >>> seq=[1, 22, 333]) ``` -------------------------------- ### Get TensorFlow Serving Model Status via REST API Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/python_serving_guide.md This bash command uses `curl` to query the status of a deployed model ('din' in this case) from the TensorFlow Serving REST API. It assumes the Docker container is running and accessible on localhost:8501. ```bash $ curl http://localhost:8501/v1/models/din ``` -------------------------------- ### Make Recommendation Request to TensorFlow Service Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/rust_serving_guide.md Sends a POST request to the Actix serving API to get recommendations from the TensorFlow model. The request includes user ID and number of recommendations, with the response providing a list of recommended item IDs from the TensorFlow model. ```bash $ curl -d '{"user": "1", "n_rec": 10}' -H "Content-Type: application/json" -X POST http://0.0.0.0:8080/tf/recommend # {"rec_list":["858","260","2355","1287","527","2371","1220","377","1968","3362"]} ``` -------------------------------- ### Request Recommendations from Sanic Server Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/python_serving_guide.md Make a recommendation request to the running Sanic server. This example shows how to use a Python script or a curl command to request recommendations for a specific user and number of items. The server responds with a JSON object containing the recommendation results. ```bash $ python request.py --user 1 --n_rec 10 --algo tf ``` ```bash $ curl -d '{"user": 1, "n_rec": 10}' -X POST http://127.0.0.1:8000/tf/recommend ``` -------------------------------- ### Initialize LightGCN Model with BPR Loss Source: https://github.com/massquantity/librecommender/blob/master/docs/source/internal/implementation_details.md This code example demonstrates initializing the LightGCN model for ranking tasks using the Bayesian Personalized Ranking (BPR) loss function. The 'loss_type' parameter is explicitly set to 'bpr', which is a common choice for this model. This configuration is suitable for optimizing pairwise ranking performance. ```python >>> lightgcn = LightGCN( >>> "ranking", >>> data_info, >>> loss_type="bpr", >>> ) ``` -------------------------------- ### Save and Load TensorFlow DIN Model with Libreco Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/python_serving_guide.md This Python snippet demonstrates how to train a DIN model using Libreco, save it in TensorFlow's SavedModel format, and then load it into Redis. It assumes prior installation of Libreco and its dependencies, and requires specifying a model saving path. ```python >>> from libreco.algorithms import DIN >>> from libreco.data import DatasetFeat >>> from libserving.serialization import save_tf, tf2redis >>> train_data, data_info = DatasetFeat.build_trainset(...) >>> model = DIN(...) >>> model.fit(...) # train model >>> path = "tf_model" # specify model saving directory >>> save_tf(path, model, version=1) # save model in json format >>> tf2redis(path, host="localhost", port=6379, db=0) # load json from path and save model to redis ``` -------------------------------- ### Split Multi-Value Features Using LibRecommender Function Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/user_guide.md This example illustrates the use of the `split_multi_value` function from LibRecommender to transform original multi-value features (like genres separated by a delimiter) into distinct sub-features. It shows how to specify the column to split, the separator, maximum length, padding value, and user/item columns. ```python multi_value_col = ["genre"] multi_sparse_col, multi_user_col, multi_item_col = split_multi_value( data, multi_value_col, sep="|", max_len=[3], pad_val=["missing"], user_col=user_col, item_col=item_col ) ``` -------------------------------- ### TwoTower Model Initialization and Training (Python) Source: https://github.com/massquantity/librecommender/blob/master/docs/source/internal/implementation_details.md Initializes and trains a TwoTower model using provided data. Requires specifying user, item, and sparse/dense columns for data preparation. Supports various loss types and self-supervised learning patterns. Negative sampling is enabled during fitting. ```python3 >>> # must provide item sparse cols if `ssl_pattern` is not None >>> sparse_col = ["sex", "occupation", "genre1", "genre2", "genre3"] >>> dense_col = ["age"] >>> user_col = ["sex", "age", "occupation"] >>> item_col = ["genre1", "genre2", "genre3"] >>> train_data, data_info = DatasetFeat.build_trainset(train, user_col, item_col, sparse_col, dense_col) >>> model = TwoTower( >>> "ranking", >>> data_info, >>> loss_type="softmax", >>> embed_size=16, >>> norm_embed=True, >>> use_correction=True, >>> temperature=0.1, >>> ssl_pattern=None, # options: "rfm", "rfm-complementary", "cfm" >>> ) >>> model.fit(train_data, neg_sampling=True) ``` -------------------------------- ### Save and Deploy TensorFlow DIN Model with Libreco Source: https://github.com/massquantity/librecommender/blob/master/docs/source/serving_guide/python.md Demonstrates saving a trained DIN model using Libreco's `save_tf` function and then loading it into Redis for serving using `tf2redis`. It assumes TensorFlow Serving is installed via Docker and outlines the necessary steps for model deployment and interaction. ```python3 from libreco.algorithms import DIN from libreco.data import DatasetFeat from libserving.serialization import save_tf, tf2redis train_data, data_info = DatasetFeat.build_trainset(...) model = DIN(...) model.fit() # train model path = "tf_model" # specify model saving directory save_tf(path, model, version=1) # save model in json format tf2redis(path, host="localhost", port=6379, db=0) # load json from path and save model to redis ``` -------------------------------- ### Model Training and Evaluation Configuration in LibRecommender Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/user_guide.md Illustrates the configuration options for training a model and performing evaluations within LibRecommender. Key parameters include `eval_data`, `metrics`, `k` (for metrics), `eval_batch_size`, and `eval_user_num`. These parameters allow control over the evaluation process, including data subsets, metrics used, and computational resource management. ```python >>> model.fit( train_data, verbose=2, shuffle=True, eval_data=eval_data, metrics=metrics, k=10, # parameter of metrics, e.g. recall at k, ndcg at k eval_batch_size=8192, eval_user_num=100, ) ``` -------------------------------- ### Pull TensorFlow Serving Docker Image Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/python_serving_guide.md Command to pull a specific version of the TensorFlow Serving Docker image. Using a specific version (e.g., 2.8.2) is recommended to avoid compatibility issues, as newer versions might not work correctly with LibRecommender's TensorFlow model serving setup. ```bash $ sudo docker pull tensorflow/serving:2.8.2 ``` -------------------------------- ### Run Actix Serving Application (Rust) Source: https://github.com/massquantity/librecommender/blob/master/docs/source/serving_guide/rust.md Builds and runs the Actix web serving application in Rust. Supports both development and production modes. Assumes environment variables like PORT, MODEL_TYPE, and WORKERS are set. ```bash cd actix_serving export PORT=8080 MODEL_TYPE=knn WORKERS=8 # assign environment variables cargo run --package actix_serving --bin actix_serving # develop mode ``` ```bash cargo build --release # production mode ./target/release/actix_serving ``` -------------------------------- ### Shell: Install LibRecommender using pip Source: https://github.com/massquantity/librecommender/blob/master/README.md Installs the LibRecommender package from PyPI. This is the recommended method for installing the library and its dependencies. ```shell $ pip install -U LibRecommender ``` -------------------------------- ### Save and Serve Embed Model with libserving Source: https://github.com/massquantity/librecommender/blob/master/docs/md_doc/python_serving_guide.md Saves a trained Embed-based model (e.g., ALS) to disk in JSON format and then loads it to be served via Redis. It also demonstrates saving a FAISS index for faster similarity searching. Includes instructions for running the Sanic server and making requests. ```python from libreco.algorithms import ALS from libreco.data import DatasetPure from libserving.serialization import embed2redis, save_embed train_data, data_info = DatasetPure.build_trainset(...) model = ALS(...) model.fit(...) # train model path = "embed_model" # specify model saving directory save_embed(path, model) # save model in json format embed2redis(path, host="localhost", port=6379, db=0) # load json from path and save model to redis ``` ```python from libserving.serialization import save_faiss_index save_faiss_index(path, model) ``` ```bash $ sanic sanic_serving.embed_deploy:app --dev --access-logs -v --workers 1 # run sanic server # make requests $ python request.py --user 1 --n_rec 10 --algo embed $ curl -d '{"user": 1, "n_rec": 10}' -X POST http://127.0.0.1:8000/embed/recommend # {'Recommend result for user 1': ['593', '1270', '318', '2858', '1196', '2571', '1617', '260', '1200', '457']} ```