Skip to content

Commit 9451ca6

Browse files
committed
Release: v6.2.0
- Added recommend-next-item-segments endpoint support - Added `searchQuery` parameter support for composite recommendations
1 parent 5ccce05 commit 9451ca6

10 files changed

Lines changed: 149 additions & 3 deletions

recombee_api_client/api_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def __get_base_uri(options: dict, region: str) -> str:
105105

106106
@staticmethod
107107
def __get_http_headers(additional_headers: dict = None) -> dict:
108-
headers = {"User-Agent": "recombee-python-api-client/6.1.0"}
108+
headers = {"User-Agent": "recombee-python-api-client/6.2.0"}
109109
if additional_headers:
110110
headers.update(additional_headers)
111111
return headers

recombee_api_client/api_requests/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
from recombee_api_client.api_requests.recommend_item_segments_to_item_segment import (
8383
RecommendItemSegmentsToItemSegment,
8484
)
85+
from recombee_api_client.api_requests.recommend_next_item_segments import (
86+
RecommendNextItemSegments,
87+
)
8588
from recombee_api_client.api_requests.composite_recommendation import (
8689
CompositeRecommendation,
8790
)

recombee_api_client/api_requests/composite_recommendation.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class CompositeRecommendation(Request):
1515
"""
16-
Composite Recommendation returns both a *source entity* (e.g., an Item or [Item Segment](https://docs.recombee.com/segmentations.html)) and a list of related recommendations in a single response.
16+
Composite Recommendation returns both a *source entity* (e.g., an Item or [Item Segment](https://docs.recombee.com/segmentations)) and a list of related recommendations in a single response.
1717
1818
It is ideal for use cases such as personalized homepage sections (*Articles from <category>*), *Because You Watched <movie>*, or *Artists Related to Your Favorite Artist <artist>*.
1919
@@ -79,6 +79,9 @@ class CompositeRecommendation(Request):
7979
:param segment_id: ID of the segment from `contextSegmentationId` for which the recommendations are to be generated.
8080
8181
82+
:param search_query: Search query provided by the user. It is used for the full-text search. Only applicable if the *scenario* corresponds to a search scenario.
83+
84+
8285
:param cascade_create: If the entity for the source recommendation does not exist in the database, returns a list of non-personalized recommendations and creates the user in the database. This allows, for example, rotations in the following recommendations for that entity, as the entity will be already known to the system.
8386
8487
@@ -101,6 +104,7 @@ def __init__(
101104
user_id: str = DEFAULT,
102105
logic: Logic = DEFAULT,
103106
segment_id: str = DEFAULT,
107+
search_query: str = DEFAULT,
104108
cascade_create: bool = DEFAULT,
105109
source_settings: CompositeRecommendationStageParameters = DEFAULT,
106110
result_settings: CompositeRecommendationStageParameters = DEFAULT,
@@ -115,6 +119,7 @@ def __init__(
115119
self.user_id = user_id
116120
self.logic = logic
117121
self.segment_id = segment_id
122+
self.search_query = search_query
118123
self.cascade_create = cascade_create
119124
self.source_settings = source_settings
120125
self.result_settings = result_settings
@@ -135,6 +140,8 @@ def get_body_parameters(self) -> dict:
135140
p["logic"] = serialize_to_json(self.logic)
136141
if self.segment_id is not DEFAULT:
137142
p["segmentId"] = serialize_to_json(self.segment_id)
143+
if self.search_query is not DEFAULT:
144+
p["searchQuery"] = serialize_to_json(self.search_query)
138145
if self.cascade_create is not DEFAULT:
139146
p["cascadeCreate"] = serialize_to_json(self.cascade_create)
140147
if self.source_settings is not DEFAULT:
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from recombee_api_client.api_requests.request import Request
2+
from recombee_api_client.utils.serialize_to_json import serialize_to_json
3+
from typing import Union, List
4+
from datetime import datetime
5+
import uuid
6+
7+
DEFAULT = uuid.uuid4()
8+
9+
10+
class RecommendNextItemSegments(Request):
11+
"""
12+
Returns Item segments that shall be shown to a user as next recommendations when the user e.g. scrolls the page down (*infinite scroll*) or goes to the next page.
13+
14+
It accepts `recommId` of a base recommendation request (e.g., request from the first page) and the number of segments that shall be returned (`count`).
15+
The base request can be one of:
16+
- [Recommend Item Segments to Item](https://docs.recombee.com/api#recommend-item-segments-to-item)
17+
- [Recommend Item Segments to User](https://docs.recombee.com/api#recommend-item-segments-to-user)
18+
- [Recommend Item Segments to Item Segment](https://docs.recombee.com/api#recommend-item-segments-to-item-segment)
19+
- [Search Item Segments](https://docs.recombee.com/api#search-item-segments)
20+
21+
All the other parameters are inherited from the base request.
22+
23+
*Recommend next Item segments* can be called many times for a single `recommId` and each call returns different (previously not recommended) segments.
24+
The number of *Recommend next Item segments* calls performed so far is returned in the `numberNextRecommsCalls` field.
25+
26+
*Recommend next Item segments* can be requested up to 30 minutes after the base request or a previous *Recommend next Item segments* call.
27+
28+
For billing purposes, each call to *Recommend next Item segments* is counted as a separate recommendation request.
29+
30+
Required parameters:
31+
32+
:param recomm_id: ID of the base recommendation request for which next recommendations should be returned
33+
34+
:param count: Number of item segments to be recommended
35+
36+
37+
"""
38+
39+
def __init__(self, recomm_id: str, count: int):
40+
super().__init__(
41+
path="/recomms/next/item-segments/%s" % (recomm_id),
42+
method="post",
43+
timeout=3000,
44+
ensure_https=False,
45+
)
46+
self.recomm_id = recomm_id
47+
self.count = count
48+
49+
def get_body_parameters(self) -> dict:
50+
"""
51+
Values of body parameters as a dictionary (name of parameter: value of the parameter).
52+
"""
53+
p = dict()
54+
p["count"] = serialize_to_json(self.count)
55+
56+
return p
57+
58+
def get_query_parameters(self) -> dict:
59+
"""
60+
Values of query parameters as a dictionary (name of parameter: value of the parameter).
61+
"""
62+
params = dict()
63+
64+
return params

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setup(
1212
name="recombee-api-client",
13-
version="6.1.0",
13+
version="6.2.0",
1414
description="Client for Recombee recommendation API",
1515
long_description=long_description,
1616
url="https://github.com/Recombee/python-api-client",

tests/test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,15 @@
326326
RecommendItemSegmentsToItemSegmentTestCase
327327
)
328328
)
329+
from tests.test_cases.test_recommend_next_item_segments import (
330+
RecommendNextItemSegmentsTestCase,
331+
)
332+
333+
suite.addTests(
334+
unittest.defaultTestLoader.loadTestsFromTestCase(
335+
RecommendNextItemSegmentsTestCase
336+
)
337+
)
329338
from tests.test_cases.test_composite_recommendation import (
330339
CompositeRecommendationTestCase,
331340
)

tests/test_cases/composite_recommendation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def create_request(
2626
user_id=None,
2727
logic=None,
2828
segment_id=None,
29+
search_query=None,
2930
cascade_create=None,
3031
source_settings=None,
3132
result_settings=None,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# This file is auto-generated, do not edit
5+
#
6+
7+
import time
8+
from abc import ABC, abstractmethod
9+
from tests.test_cases.recombee_test import (
10+
RecombeeTest,
11+
InteractionsTest,
12+
RecommendationsTest,
13+
)
14+
from recombee_api_client.exceptions import ResponseException
15+
from recombee_api_client.api_requests import *
16+
17+
18+
class NextItemSegmentsRecommendationTest(RecombeeTest, ABC):
19+
20+
@abstractmethod
21+
def create_request(self, recomm_id, count):
22+
pass
23+
24+
def test_next_item_segments_recommendation(self):
25+
26+
# it 'rejects request with invalid recommId'
27+
req = RecommendNextItemSegments("invalid_recomm_id", 5)
28+
try:
29+
self.client.send(req)
30+
self.fail()
31+
except ResponseException as ex:
32+
self.assertEqual(ex.status_code, 400)
33+
# it 'rejects request to recommId which does not return item-segments'
34+
req = RecommendItemsToUser("entity_id", 3)
35+
resp = self.client.send(req)
36+
req = self.create_request(resp["recommId"], 5)
37+
try:
38+
self.client.send(req)
39+
self.fail()
40+
except ResponseException as ex:
41+
self.assertEqual(ex.status_code, 400)

tests/test_cases/test_composite_recommendation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def create_request(
2121
user_id=DEFAULT,
2222
logic=DEFAULT,
2323
segment_id=DEFAULT,
24+
search_query=DEFAULT,
2425
cascade_create=DEFAULT,
2526
source_settings=DEFAULT,
2627
result_settings=DEFAULT,
@@ -33,6 +34,7 @@ def create_request(
3334
user_id=user_id,
3435
logic=logic,
3536
segment_id=segment_id,
37+
search_query=search_query,
3638
cascade_create=cascade_create,
3739
source_settings=source_settings,
3840
result_settings=result_settings,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# This file is auto-generated, do not edit
5+
#
6+
7+
from tests.test_cases.next_item_segments_recommendation import (
8+
NextItemSegmentsRecommendationTest,
9+
)
10+
from recombee_api_client.api_requests.recommend_next_item_segments import (
11+
RecommendNextItemSegments,
12+
DEFAULT,
13+
)
14+
15+
16+
class RecommendNextItemSegmentsTestCase(NextItemSegmentsRecommendationTest):
17+
18+
def create_request(self, recomm_id, count):
19+
return RecommendNextItemSegments(recomm_id, count)

0 commit comments

Comments
 (0)