|
| 1 | +***************** |
| 2 | +RecombeeApiClient |
| 3 | +***************** |
| 4 | + |
| 5 | +A Python client for easy use of the `Recombee <https://www.recombee.com/>`_ recommendation API. |
| 6 | + |
| 7 | +If you don't have an account at Recombee yet, you can create a free account `here <https://www.recombee.com/>`_. |
| 8 | + |
| 9 | +Documentation of the API can be found at `docs.recombee.com <https://docs.recombee.com/)>`_. |
| 10 | + |
| 11 | +============= |
| 12 | +Installation |
| 13 | +============= |
| 14 | + |
| 15 | +Install the client with pip: |
| 16 | + |
| 17 | +.. code-block:: bash |
| 18 | +
|
| 19 | + $ pip install recombee-api-client |
| 20 | +
|
| 21 | +======== |
| 22 | +Examples |
| 23 | +======== |
| 24 | + |
| 25 | +------------- |
| 26 | +Basic example |
| 27 | +------------- |
| 28 | + |
| 29 | +.. code-block:: python |
| 30 | +
|
| 31 | + from recombee_api_client.api_client import RecombeeClient |
| 32 | + from recombee_api_client.exceptions import APIException |
| 33 | + from recombee_api_client.api_requests import AddUser, AddItem, AddPurchase, UserBasedRecommendation, Batch |
| 34 | + import random |
| 35 | +
|
| 36 | + # Prepare some items and users |
| 37 | + NUM = 100 |
| 38 | + my_users = ["user-%s" % i for i in range(NUM) ] |
| 39 | + my_items = ["item-%s" % i for i in range(NUM) ] |
| 40 | +
|
| 41 | + #Generate some random purchases of items by users |
| 42 | + PROBABILITY_PURCHASED = 0.1 |
| 43 | + my_purchases = [] |
| 44 | + for user_id in my_users: |
| 45 | + p = [item_id for item_id in my_items if random.random() < PROBABILITY_PURCHASED] |
| 46 | + for item_id in p: |
| 47 | + my_purchases.append({'userId': user_id, 'itemId': item_id}) |
| 48 | +
|
| 49 | + # Use Recombee recommender |
| 50 | + client = RecombeeClient('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L') |
| 51 | +
|
| 52 | + try: |
| 53 | + # Send the data to Recombee, use Batch for faster processing |
| 54 | + print('Send users') |
| 55 | + client.send(Batch([AddUser(user_id) for user_id in my_users])) |
| 56 | + print('Send items') |
| 57 | + client.send(Batch([AddItem(item_id) for item_id in my_items])) |
| 58 | + print('Send purchases') |
| 59 | + client.send(Batch([AddPurchase(p['userId'], p['itemId']) for p in my_purchases])) |
| 60 | +
|
| 61 | + # Get recommendations for user 'user-25' |
| 62 | + print('Recommend for a user') |
| 63 | + recommended = client.send(UserBasedRecommendation('user-25', 5, {'rotationRate': 0})) |
| 64 | + print("Recommended items: %s" % recommended) |
| 65 | + except APIException as e: |
| 66 | + print(e) |
| 67 | +
|
| 68 | +--------------------- |
| 69 | +Using property values |
| 70 | +--------------------- |
| 71 | + |
| 72 | +.. code-block:: python |
| 73 | +
|
| 74 | +
|
| 75 | + from recombee_api_client.api_client import RecombeeClient |
| 76 | + from recombee_api_client.api_requests import AddItemProperty, SetItemValues, AddPurchase |
| 77 | + from recombee_api_client.api_requests import ItemBasedRecommendation, Batch, ResetDatabase |
| 78 | + import random |
| 79 | +
|
| 80 | + NUM = 100 |
| 81 | + PROBABILITY_PURCHASED = 0.1 |
| 82 | +
|
| 83 | + client = RecombeeClient('client-test', 'jGGQ6ZKa8rQ1zTAyxTc0EMn55YPF7FJLUtaMLhbsGxmvwxgTwXYqmUk5xVZFw98L') |
| 84 | + |
| 85 | + #Clear the entire database |
| 86 | + client.send(ResetDatabase()) |
| 87 | + |
| 88 | + # We will use computers as items in this example |
| 89 | + # Computers have three properties |
| 90 | + # - price (floating point number) |
| 91 | + # - number of processor cores (integer number) |
| 92 | + # - description (string) |
| 93 | +
|
| 94 | + # Add properties of items |
| 95 | + client.send(AddItemProperty('price', 'double')) |
| 96 | + client.send(AddItemProperty('num-cores', 'int')) |
| 97 | + client.send(AddItemProperty('description', 'string')) |
| 98 | +
|
| 99 | + # Prepare requests for setting a catalog of computers |
| 100 | + requests = [SetItemValues( |
| 101 | + "computer-%s" % i, #itemId |
| 102 | + #values: |
| 103 | + { |
| 104 | + 'price': random.uniform(500, 2000), |
| 105 | + 'num-cores': random.randrange(1,9), |
| 106 | + 'description': 'Great computer', |
| 107 | + '!cascadeCreate': True # Use !cascadeCreate for creating item |
| 108 | + # with given itemId, if it doesn't exist |
| 109 | + } |
| 110 | + ) for i in range(NUM)] |
| 111 | + |
| 112 | +
|
| 113 | + # Send catalog to the recommender system |
| 114 | + client.send(Batch(requests)) |
| 115 | +
|
| 116 | + # Prepare some purchases of items by users |
| 117 | + requests = [] |
| 118 | + items = ["computer-%s" % i for i in range(NUM)] |
| 119 | + users = ["user-%s" % i for i in range(NUM)] |
| 120 | +
|
| 121 | + for item_id in items: |
| 122 | + #Use cascadeCreate to create unexisting users |
| 123 | + purchasing_users = [user_id for user_id in users if random.random() < PROBABILITY_PURCHASED] |
| 124 | + requests += [AddPurchase(user_id, item_id, {'cascadeCreate': True}) for user_id in purchasing_users] |
| 125 | +
|
| 126 | + # Send purchases to the recommender system |
| 127 | + client.send(Batch(requests)) |
| 128 | +
|
| 129 | + # Get 5 recommendations for user-42, who is currently viewing computer-6 |
| 130 | + recommended = client.send(ItemBasedRecommendation('computer-6', 5, {'targetUserId': 'user-42'}) ) |
| 131 | + print("Recommended items: %s" % recommended) |
| 132 | +
|
| 133 | + # Get 5 recommendations for user-42, but recommend only computers that |
| 134 | + # have at least 3 cores |
| 135 | + recommended = client.send( |
| 136 | + ItemBasedRecommendation('computer-6', 5, {'targetUserId': 'user-42', 'filter': "'num-cores'>=3"}) |
| 137 | + ) |
| 138 | + print("Recommended items with at least 3 processor cores: %s" % recommended) |
| 139 | +
|
| 140 | + # Get 5 recommendations for user-42, but recommend only items that |
| 141 | + # are more expensive then currently viewed item (up-sell) |
| 142 | + recommended = client.send( |
| 143 | + ItemBasedRecommendation('computer-6', 5, |
| 144 | + {'targetUserId': 'user-42', 'filter': "'price' > context_item[\"price\"]"}) |
| 145 | + ) |
| 146 | + print("Recommended up-sell items: %s" % recommended) |
| 147 | + |
| 148 | +------------------ |
| 149 | +Exception handling |
| 150 | +------------------ |
| 151 | + |
| 152 | +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. |
| 153 | + |
| 154 | +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. |
| 155 | + |
| 156 | +Example: |
| 157 | + |
| 158 | +.. code-block:: python |
| 159 | +
|
| 160 | + try: |
| 161 | + recommended = client.send( |
| 162 | + ItemBasedRecommendation('computer-6', 5, |
| 163 | + {'targetUserId': 'user-42', 'filter': "'price' > context_item[\"price\"]"}) |
| 164 | + ) |
| 165 | + except ResponseException as e: |
| 166 | + #Handle errorneous request => use fallback |
| 167 | + except ApiTimeoutException as e: |
| 168 | + #Handle timeout => use fallback |
| 169 | + except APIException as e: |
| 170 | + #APIException is parent of both ResponseException and ApiTimeoutException |
0 commit comments