Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

38 Commits

Repository files navigation

RecombeeApiClient

A Ruby client for easy use of the Recombee recommendation API.

If you don't have an account at Recombee yet, you can create a free account here.

Documentation of the API can be found at docs.recombee.com.

Installation

Add this line to your application's Gemfile:

gem'recombee_api_client'

And then execute:

$ bundle

Or install it yourself as:

$ gem install recombee_api_client

Examples

Basic example

require'recombee_api_client'includeRecombeeApiClientclient=RecombeeClient.new('--my-database-id--','--db-private-token--',{:region=>'us-west'})# Generate some random purchases of items by usersNUM=100PROBABILITY_PURCHASED=0.1users=(1..NUM).map{ |i| "user-#{i}"}items=(1..NUM).map{ |i| "item-#{i}"}purchases=[]users.eachdo |user_id|
purchased=items.select{ |_| rand(0.0..1.0) < PROBABILITY_PURCHASED}purchased.each{ |item_id| purchases.push(AddPurchase.new(user_id,item_id,'cascadeCreate'=>true)# Use cascadeCreate to create the# yet non-existing users and items)}endbegin# Send the data to Recombee, use Batch for faster processing of larger dataclient.send(Batch.new(purchases))# Get recommendations for user 'user-25'response=client.send(RecommendItemsToUser.new('user-25',5))puts"Recommended items for user-25: #{response}"# User scrolled down - get next 3 recommended itemsresponse=client.send(RecommendNextItems.new(response['recommId'],3))puts"Next recommended items for user-25: #{response}"rescueAPIError=>eputse# Use fallbackend

Using property values

require'recombee_api_client'includeRecombeeApiClientNUM=100PROBABILITY_PURCHASED=0.1client=RecombeeClient.new('--my-database-id--','--db-private-token--',{:region=>'ap-se'})client.send(ResetDatabase.new)# Clear everything from the database (asynchronous)# We will use computers as items in this example# Computers have five properties # - price (floating point number)# - number of processor cores (integer number)# - description (string)# - date from which it is in stock (timestamp)# - image (url of computer's photo)# Add properties of itemsclient.send(AddItemProperty.new('price','double'))client.send(AddItemProperty.new('num-cores','int'))client.send(AddItemProperty.new('description','string'))client.send(AddItemProperty.new('in_stock_from','timestamp'))client.send(AddItemProperty.new('image','image'))# Prepare requests for setting a catalog of computersrequests=(1..NUM).mapdo |i|
SetItemValues.new("computer-#{i}",#itemId#values:{'price'=>rand(15000.0 .. 25000.0),'num-cores'=>rand(1..8),'description'=>'Great computer','in_stock_from'=>DateTime.now,'image'=>"http://examplesite.com/products/computer-#{i}.jpg"},#optional parameters:{:cascade_create=>true# Use cascade_create for creating item# with given itemId, if it doesn't exist})end# Send catalog to the recommender systemputsclient.send(Batch.new(requests))# Prepare some purchases of items by usersrequests=[](1..NUM).map{|i| "computer-#{i}"}.eachdo |item_id|
user_ids=(1..NUM).map{|i| "user-#{i}"}user_ids=user_ids.select{ |_| rand(0.0..1.0) < PROBABILITY_PURCHASED}# Use cascade_create to create unexisting usersuser_ids.each{ |user_id| requests.push(AddPurchase.new(user_id,item_id,:cascade_create=>true))}end# Send purchases to the recommender systemclient.send(Batch.new(requests))# Get 5 recommendations for user-42, who is currently viewing computer-6# Recommend only computers that have at least 3 coresrecommended=client.send(RecommendItemsToItem.new('computer-6','user-42',5,{:filter=>"'num-cores'>=3"}))puts"Recommended items with at least 3 processor cores: #{recommended}"# Recommend only items that are more expensive then currently viewed item (up-sell)recommended=client.send(RecommendItemsToItem.new('computer-6','user-42',5,{:filter=>"'price' > context_item[\"price\"]"}))puts"Recommended up-sell items: #{recommended}"# Filters, boosters and other settings can be also set in the Admin UI (admin.recombee.com)# when scenario is specifiedrecommended=client.send(RecommendItemsToItem.new('computer-6','user-42',5,{:scenario=>'product_detail'}))# Perform personalized full-text search with a user's search query (e.g. 'computers').matches=client.send(SearchItems.new('user-42','computers',5,{:scenario=>'search_top'}))puts"Matched items: #{matches}"

Exception handling

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.

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.

Example:

beginrecommended=client.send(RecommendItemsToItem.new('computer-6','user-42',5,{'filter'=>"'price' > context_item[\"price\"]"}))rescueResponseError=>e# Handle errorneous request => use fallbackrescueApiTimeout=>e# Handle timeout => use fallbackrescueAPIError=>e# APIError is parent of both ResponseError and ApiTimeoutend

About

Ruby client for easy use of the Recombee recommendation API

Resources

Stars

8 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages