Crate reco_forge

source ·
Expand description

This crate provides an interface for users to turn any dataset with titles and descriptions into a recommendation system. It uses the BERT model to create embeddings for each item in the dataset and then finds recommendations based on the user’s input.

To run the examples, you can clone the git repository at https://github.com/jameslk3/reco-forge and then run the following commands:

cargo run --example description
cargo run --example item

Example usage:

use reco_forge::{create_model, pass_description, Data, Tensor, HashMap}; // Can also use pass_item

fn main() -> Result<(), Box<dyn std::error::Error>> {
   let mut path = String::new();
   println!("Please enter the file path:");

   let model_wrapped: Result<HashMap<Data, Option<Tensor>>, ()>;

   loop {
       std::io::stdin().read_line(&mut path).expect("Failed to read line");
       path = path.trim().to_string();

       if let Ok(model) = create_model(&path) {
           model_wrapped = Ok(model);
           break;
       } else {
           println!("File path is not valid or file cannot be deserialized, please input the correct file path and try again:");
           path.clear();
       }
   }

  let model: HashMap<Data, Option<Tensor>> = model_wrapped.unwrap();

   println!("Input tags that you would like to use to filter, else enter NONE");
   let mut tags_input: String = String::new();
   std::io::stdin().read_line(&mut tags_input).expect("Failed to read line");
   tags_input = tags_input.trim().to_string();

   println!("Describe what you want to be recommended:");
   let mut query: String = String::new();
   std::io::stdin().read_line(&mut query).expect("Failed to read line");
   query = query.trim().to_string();
   println!();

   let recommendations = pass_description(&model, query, tags_input, 10);
   match recommendations {
       Ok(recommendations) => {
           println!("Recommendations:");
           for recommendation in recommendations {
               println!("{}% {}", (recommendation.1 * 100.0).round(), recommendation.0);
           }
       },
       Err(_) => println!("No recommendations found"),
   }
   Ok(())
}

// Examples are also provided in the examples folder of the git repository at https://github.com/jameslk3/reco-forge.

Required JSON file format:

[
	{
		"id": int,
		"name": "string",
		"summary": "string",
		"tags": ["string1", "string2"]
	},
	{
		...
	}
]

Structs§

  • The struct that holds the data for one item in the model
  • A hash map implemented with quadratic probing and SIMD lookup.
  • The core struct for manipulating tensors.

Functions§