Skip to main content

Client for Recombee recommendation API

Project description

A Python client for easy use of the Recombee recommendation API. Both Python 2 and Python 3 are supported.

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

Install the client with pip:

$ pip install recombee-api-client

(use pip3 instead of pip if you use Python 3)

Examples

Basic example

from recombee_api_client.api_client import RecombeeClient
from recombee_api_client.exceptions import APIException
from recombee_api_client.api_requests import AddPurchase, UserBasedRecommendation, Batch
import random

client = RecombeeClient('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L')

#Generate some random purchases of items by users
PROBABILITY_PURCHASED = 0.1
NUM = 100
purchase_requests = []

for user_id in ["user-%s" % i for i in range(NUM) ]:
  for item_id in ["item-%s" % i for i in range(NUM) ]:
    if random.random() < PROBABILITY_PURCHASED:

      request = AddPurchase(user_id, item_id, cascade_create=True)
      purchase_requests.append(request)

try:
    # Send the data to Recombee, use Batch for faster processing of larger data
    print('Send purchases')
    client.send(Batch(purchase_requests))

    # Get recommendations for user 'user-25'
    recommended = client.send(UserBasedRecommendation('user-25', 5))
    print("Recommended items: %s" % recommended)

except APIException as e:
    print(e)

Using property values

from recombee_api_client.api_client import RecombeeClient
from recombee_api_client.api_requests import AddItemProperty, SetItemValues, AddPurchase
from recombee_api_client.api_requests import ItemBasedRecommendation, Batch, ResetDatabase
import random

NUM = 100
PROBABILITY_PURCHASED = 0.1

client = RecombeeClient('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L')

#Clear the entire database
client.send(ResetDatabase())

# We will use computers as items in this example
# Computers have three properties
#   - price (floating point number)
#   - number of processor cores (integer number)
#   - description (string)

# Add properties of items
client.send(AddItemProperty('price', 'double'))
client.send(AddItemProperty('num-cores', 'int'))
client.send(AddItemProperty('description', 'string'))

# Prepare requests for setting a catalog of computers
requests = [SetItemValues(
    "computer-%s" % i, #itemId
    #values:
    {
      'price': random.uniform(500, 2000),
      'num-cores': random.randrange(1,9),
      'description': 'Great computer',
    },
    cascade_create=True   # Use cascadeCreate for creating item
                          # with given itemId if it doesn't exist
  ) for i in range(NUM)]


# Send catalog to the recommender system
client.send(Batch(requests))

# Prepare some purchases of items by users
requests = []
items = ["computer-%s" % i for i in range(NUM)]
users = ["user-%s" % i for i in range(NUM)]

for item_id in items:
    #Use cascadeCreate to create unexisting users
    purchasing_users = [user_id for user_id in users if random.random() < PROBABILITY_PURCHASED]
    requests += [AddPurchase(user_id, item_id, cascade_create=True) for user_id in purchasing_users]

# Send purchases to the recommender system
client.send(Batch(requests))

# Get 5 recommendations for user-42, who is currently viewing computer-6
recommended = client.send(ItemBasedRecommendation('computer-6', 5, target_user_id='user-42'))
print("Recommended items: %s" % recommended)

# Get 5 recommendations for user-42, but recommend only computers that
# have at least 3 cores
recommended = client.send(
    ItemBasedRecommendation('computer-6', 5, target_user_id='user-42', filter="'num-cores'>=3")
)
print("Recommended items with at least 3 processor cores: %s" % recommended)

# Get 5 recommendations for user-42, but recommend only items that
# are more expensive then currently viewed item (up-sell)
recommended = client.send(
    ItemBasedRecommendation('computer-6', 5, target_user_id='user-42', filter="'price' > context_item[\"price\"]")
)
print("Recommended up-sell items: %s" % recommended)

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:

try:
  recommended = client.send(
  ItemBasedRecommendation('computer-6', 5,target_user_id='user-42', filter="'price' > context_item[\"price\"]")
  )
except ResponseException as e:
  #Handle errorneous request => use fallback
except ApiTimeoutException as e:
  #Handle timeout => use fallback
except APIException as e:
  #APIException is parent of both ResponseException and ApiTimeoutException

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

recombee-api-client-1.4.0.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

recombee_api_client-1.4.0-py2.py3-none-any.whl (92.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file recombee-api-client-1.4.0.tar.gz.

File metadata

File hashes

Hashes for recombee-api-client-1.4.0.tar.gz
Algorithm Hash digest
SHA256 c1b08670fbc2d8e02d27b767a0343e9bea2f619e78e05ad12203ba68caf95742
MD5 292fbd79541333e2499c6723310d9ca0
BLAKE2b-256 531f50b91d0f5ceed239831beb53c925d78d44f007c672f93f4454c08d4c7044

See more details on using hashes here.

File details

Details for the file recombee_api_client-1.4.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for recombee_api_client-1.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 18f2c1976acc1bcfb2aa633ffef226c94e86eacbbcc9acff810609a6779fdeaa
MD5 61e41359d696fe54c3f01e525028aa28
BLAKE2b-256 67f4c072c8ad5e3e6f07ae449fd0035e09d2203806cf7b3dbed0e55c05845f8a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page