|
| 1 | +# RecombeeApiClient |
| 2 | + |
| 3 | +A Ruby client for easy use of the [Recombee](https://www.recombee.com/) recommendation API. |
| 4 | + |
| 5 | +Documentation of the API can be found at [docs.recombee.com](https://docs.recombee.com/). |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +Add this line to your application's Gemfile: |
| 10 | + |
| 11 | +```ruby |
| 12 | +gem 'recombee_api_client' |
| 13 | +``` |
| 14 | + |
| 15 | +And then execute: |
| 16 | + |
| 17 | + $ bundle |
| 18 | + |
| 19 | +Or install it yourself as: |
| 20 | + |
| 21 | + $ gem install recombee_api_client |
| 22 | + |
| 23 | +## Examples |
| 24 | + |
| 25 | +### Basic example |
| 26 | +```ruby |
| 27 | +require 'recombee_api_client' |
| 28 | +include RecombeeApiClient |
| 29 | + |
| 30 | +# Prepare some items and users |
| 31 | +NUM = 100 |
| 32 | +my_users = (1..NUM).map { |i| "user-#{i}" } |
| 33 | +my_items = (1..NUM).map { |i| "item-#{i}" } |
| 34 | + |
| 35 | +#Generate some random purchases of items by users |
| 36 | +PROBABILITY_PURCHASED = 0.1 |
| 37 | +my_purchases = [] |
| 38 | +my_users.each do |user| |
| 39 | + p = my_items.select { |_| rand(0.0..1.0) < PROBABILITY_PURCHASED } |
| 40 | + p.each { |item| my_purchases.push('userId' => user, 'itemId' => item) } |
| 41 | +end |
| 42 | + |
| 43 | +# Use Recombee recommender |
| 44 | +client = RecombeeClient.new('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L') |
| 45 | +begin |
| 46 | + # Send the data to Recombee, use Batch for faster processing |
| 47 | + puts 'Send users' |
| 48 | + client.send(Batch.new(my_users.map { |userId| AddUser.new(userId) })) |
| 49 | + puts 'Send items' |
| 50 | + client.send(Batch.new(my_items.map { |itemId| AddItem.new(itemId) })) |
| 51 | + puts 'Send purchases' |
| 52 | + client.send(Batch.new(my_purchases.map { |p| AddPurchase.new(p['userId'], p['itemId'], 0) })) |
| 53 | + |
| 54 | + # Get recommendations for user 'user-25' |
| 55 | + puts 'Recommend for a user' |
| 56 | + recommended = client.send(UserBasedRecommendation.new('user-25', 5, 'rotationRate' => 0)) |
| 57 | + puts "Recommended items: #{recommended}" |
| 58 | +rescue ResponseError => e |
| 59 | + puts e |
| 60 | +end |
| 61 | +``` |
| 62 | + |
| 63 | +### Using property values |
| 64 | +```ruby |
| 65 | +#!/usr/bin/env ruby |
| 66 | + |
| 67 | +require 'recombee_api_client' |
| 68 | +include RecombeeApiClient |
| 69 | + |
| 70 | +NUM = 100 |
| 71 | +PROBABILITY_PURCHASED = 0.1 |
| 72 | + |
| 73 | +client = RecombeeClient.new('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L') |
| 74 | +client.send(ResetDatabase.new) |
| 75 | +# We will use computers as items in this example |
| 76 | +# Computers have three properties |
| 77 | +# - price (floating point number) |
| 78 | +# - number of processor cores (integer number) |
| 79 | +# - description (string) |
| 80 | + |
| 81 | +# Add properties of items |
| 82 | +client.send(AddItemProperty.new('price', 'double')) |
| 83 | +client.send(AddItemProperty.new('num-cores', 'int')) |
| 84 | +client.send(AddItemProperty.new('description', 'string')) |
| 85 | + |
| 86 | +# Prepare requests for setting a catalog of computers |
| 87 | +requests = (1..NUM).map do |i| |
| 88 | + SetItemValues.new( |
| 89 | + "computer-#{i}", #itemId |
| 90 | + #values: |
| 91 | + { |
| 92 | + 'price' => rand(15000.0 .. 25000.0), |
| 93 | + 'num-cores' => rand(1..8), |
| 94 | + 'description' => 'Great computer', |
| 95 | + '!cascadeCreate' => true # Use !cascadeCreate for creating item |
| 96 | + # with given itemId, if it doesn't exist |
| 97 | + } |
| 98 | + ) |
| 99 | +end |
| 100 | + |
| 101 | +# Send catalog to the recommender system |
| 102 | +client.send(Batch.new(requests)) |
| 103 | + |
| 104 | +# Prepare some purchases of items by users |
| 105 | +requests = [] |
| 106 | +(1..NUM).map{|i| "computer-#{i}"}.each do |item_id| |
| 107 | + user_ids = (1..NUM).map{|i| "user-#{i}"} |
| 108 | + user_ids = user_ids.select { |_| rand(0.0..1.0) < PROBABILITY_PURCHASED } |
| 109 | + # Use cascadeCreate to create unexisting users |
| 110 | + user_ids.each { |user_id| requests.push(AddPurchase.new(user_id, item_id, 0, 'cascadeCreate' => true)) } |
| 111 | +end |
| 112 | + |
| 113 | +# Send purchases to the recommender system |
| 114 | +client.send(Batch.new(requests)) |
| 115 | + |
| 116 | +# Get 5 recommendations for user-42, who is currently viewing computer-6 |
| 117 | +recommended = client.send(ItemBasedRecommendation.new('computer-6', 5, 'targetUserId' => 'user-42') ) |
| 118 | +puts "Recommended items: #{recommended}" |
| 119 | + |
| 120 | +# Get 5 recommendations for user-42, but recommend only computers that |
| 121 | +# have at least 3 cores |
| 122 | +recommended = client.send( |
| 123 | + ItemBasedRecommendation.new('computer-6', 5, {'targetUserId' => 'user-42', 'filter' => "'num-cores'>=3"}) |
| 124 | + ) |
| 125 | +puts "Recommended items with at least 3 processor cores: #{recommended}" |
| 126 | + |
| 127 | +# Get 5 recommendations for user-42, but recommend only items that |
| 128 | +# are more expensive then currently viewed item (up-sell) |
| 129 | +recommended = client.send( |
| 130 | + ItemBasedRecommendation.new('computer-6', 5, |
| 131 | + {'targetUserId' => 'user-42', 'filter' => "'price' > context_item[\"price\"]"}) |
| 132 | + ) |
| 133 | +puts "Recommended up-sell items: #{recommended}" |
| 134 | +``` |
| 135 | + |
| 136 | +### Exception handling |
| 137 | + |
| 138 | +For the sake of brevity, the above examples omit exception handling. However, various exceptions can occur while processing request, for example because of adding an already existing item, submitting interaction of nonexistent user or because of timeout. |
| 139 | + |
| 140 | +We are doing our best to provide the fastest and most reliable service, but production-level applications must implement a fallback solution since errors can always happen. The fallback might be, for example, showing the most popular items from the current category, or not displaying recommendations at all. |
| 141 | + |
| 142 | +Example: |
| 143 | +```ruby |
| 144 | + |
| 145 | +begin |
| 146 | + recommended = client.send( |
| 147 | + ItemBasedRecommendation.new('computer-6', 5, |
| 148 | + {'targetUserId' => 'user-42', 'filter' => "'price' > context_item[\"price\"]"}) |
| 149 | + ) |
| 150 | +rescue ResponseError => e |
| 151 | + #Handle errorneous request => use fallback |
| 152 | +rescue ApiTimeout => e |
| 153 | + #Handle timeout => use fallback |
| 154 | +rescue APIError => e |
| 155 | + #APIError is parent of both ResponseError and ApiTimeout |
| 156 | +end |
| 157 | +``` |
0 commit comments