Package: recombee-api-client
A Python 3 client for easy use of theRecombeerecommendation API.If you don’t have an account at Recombee yet, you can create a free accounthere.Documentation of the API can be found atdocs.recombee.com.InstallationInstall the client with pip:$pipinstallrecombee-api-clientExamplesBasic examplefromrecombee_api_client.api_clientimportRecombeeClient,Regionfromrecombee_api_client.exceptionsimportAPIExceptionfromrecombee_api_client.api_requestsimport*importrandomclient=RecombeeClient('--my-database-id--','--db-private-token--',region=Region.US_WEST)#Generate some random purchases of items by usersPROBABILITY_PURCHASED=0.1NUM=100purchase_requests=[]foruser_idin["user-%s"%iforiinrange(NUM)]:foritem_idin["item-%s"%iforiinrange(NUM)]:ifrandom.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 dataprint('Send purchases')client.send(Batch(purchase_requests))# Get recommendations for user 'user-25'response=client.send(RecommendItemsToUser('user-25',5))print("Recommended items:%s"%response)# User scrolled down - get next 3 recommended itemsresponse=client.send(RecommendNextItems(response['recommId'],3))print("Next recommended items:%s"%response)exceptAPIExceptionase:print(e)Using property valuesfromrecombee_api_client.api_clientimportRecombeeClient,Regionfromrecombee_api_client.api_requestsimportAddItemProperty,SetItemValues,AddPurchasefromrecombee_api_client.api_requestsimportRecommendItemsToItem,SearchItems,Batch,ResetDatabaseimportrandomNUM=100PROBABILITY_PURCHASED=0.1client=RecombeeClient('--my-database-id--','--db-private-token--',region=Region.AP_SE)# Clear the entire databaseclient.send(ResetDatabase())# We will use computers as items in this example# Computers have four properties#   - price (floating point number)#   - number of processor cores (integer number)#   - description (string)#   - image (url of computer's photo)# Add properties of itemsclient.send(AddItemProperty('price','double'))client.send(AddItemProperty('num-cores','int'))client.send(AddItemProperty('description','string'))client.send(AddItemProperty('image','image'))# Prepare requests for setting a catalog of computersrequests=[SetItemValues("computer-%s"%i,#itemId#values:{'price':random.uniform(500,2000),'num-cores':random.randrange(1,9),'description':'Great computer','image':'http://examplesite.com/products/computer-%s.jpg'%i},cascade_create=True# Use cascadeCreate for creating item# with given itemId if it doesn't exist)foriinrange(NUM)]# Send catalog to the recommender systemclient.send(Batch(requests))# Prepare some purchases of items by usersrequests=[]items=["computer-%s"%iforiinrange(NUM)]users=["user-%s"%iforiinrange(NUM)]foritem_idinitems:#Use cascadeCreate to create unexisting userspurchasing_users=[user_idforuser_idinusersifrandom.random()<PROBABILITY_PURCHASED]requests+=[AddPurchase(user_id,item_id,cascade_create=True)foruser_idinpurchasing_users]# Send purchases to the recommender systemclient.send(Batch(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('computer-6','user-42',5,filter="'num-cores'>=3"))print("Recommended items with at least 3 processor cores:%s"%recommended)# Recommend only items that are more expensive then currently viewed item (up-sell)recommended=client.send(RecommendItemsToItem('computer-6','user-42',5,filter="'price' > context_item[\"price\"]"))print("Recommended up-sell items:%s"%recommended)# Filters, boosters and other settings can be also set in the Admin UI (admin.recombee.com)# when scenario is specifiedrecommended=client.send(RecommendItemsToItem('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('user-42','computers',5,scenario='search_top'))print("Matched items:%s"%matches)Exception handlingFor 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:fromrecombee_api_client.exceptionsimport*try:recommended=client.send(RecommendItemsToItem('computer-6','user-42',5,filter="'price' > context_item[\"price\"]"))exceptResponseExceptionase:#Handle errorneous request => use fallbackexceptApiTimeoutExceptionase:#Handle timeout => use fallbackexceptAPIExceptionase:#APIException is parent of both ResponseException and ApiTimeoutException