Skip to content

Commit 8703828

Browse files
sborisenkoxtetiana-karasovat-karasovagcf-owl-bot[bot]kweinmeister
authored
docs(samples): Retail. Prediction samples (#412)
* Update README * the bash scripts are added * Update README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update user_environment_setup.sh * Update user_import_data_to_catalog.sh * Update README.md * Add predictions samples. * Add predictions samples unit-tests. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: tetiana-karasova <[email protected]> Co-authored-by: t-karasova <[email protected]> Co-authored-by: t-karasova <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Karl Weinmeister <[email protected]>
1 parent 58a5881 commit 8703828

File tree

6 files changed

+482
-0
lines changed

6 files changed

+482
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* [START retail_prediction_get_prediction_with_filtering]
19+
* Call Retail API to get predictions from Recommendation AI using filtering.
20+
*/
21+
22+
package prediction;
23+
24+
import com.google.cloud.retail.v2.PredictRequest;
25+
import com.google.cloud.retail.v2.PredictResponse;
26+
import com.google.cloud.retail.v2.PredictionServiceClient;
27+
import com.google.cloud.retail.v2.Product;
28+
import com.google.cloud.retail.v2.ProductDetail;
29+
import com.google.cloud.retail.v2.UserEvent;
30+
import com.google.protobuf.Value;
31+
import java.io.IOException;
32+
33+
public class FilteringPrediction {
34+
35+
public static void main(String[] args) {
36+
String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
37+
String placementId = System.getenv("GOOGLE_CLOUD_PLACEMENT");
38+
String predictPlacement =
39+
String.format(
40+
"projects/%s/locations/global/catalogs/default_catalog/placements/%s",
41+
projectId, placementId);
42+
43+
predict(predictPlacement);
44+
}
45+
46+
public static void predict(String predictPlacement) {
47+
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
48+
PredictResponse predictResponse =
49+
predictionServiceClient.predict(getPredictRequest(predictPlacement));
50+
System.out.printf("Predict response: %n%s", predictResponse);
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
56+
private static PredictRequest getPredictRequest(String predictPlacement) {
57+
// create product object
58+
Product product =
59+
Product.newBuilder()
60+
.setId("55106") // Id of real product
61+
.build();
62+
63+
// create product detail object
64+
ProductDetail productDetail = ProductDetail.newBuilder().setProduct(product).build();
65+
66+
// create user event object
67+
UserEvent userEvent =
68+
UserEvent.newBuilder()
69+
.setEventType("detail-page-view")
70+
.setVisitorId("281790") // Unique identifier to track visitors
71+
.addProductDetails(productDetail)
72+
.build();
73+
74+
PredictRequest predictRequest =
75+
PredictRequest.newBuilder()
76+
.setPlacement(predictPlacement)
77+
.setUserEvent(userEvent)
78+
// TRY DIFFERENT FILTER HERE:
79+
.setFilter("filterOutOfStockItems")
80+
// TRY TO UPDATE `strictFiltering` HERE:
81+
.putParams("strictFiltering", Value.newBuilder().setBoolValue(true).build())
82+
.build();
83+
System.out.printf("Predict request: %n%s", predictRequest);
84+
85+
return predictRequest;
86+
}
87+
}
88+
89+
// [END retail_prediction_get_prediction_with_filtering]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* [START retail_prediction_get_prediction_with_params]
19+
* Call Retail API to get predictions from Recommendation AI using parameters.
20+
*/
21+
22+
package prediction;
23+
24+
import com.google.cloud.retail.v2.PredictRequest;
25+
import com.google.cloud.retail.v2.PredictResponse;
26+
import com.google.cloud.retail.v2.PredictionServiceClient;
27+
import com.google.cloud.retail.v2.Product;
28+
import com.google.cloud.retail.v2.ProductDetail;
29+
import com.google.cloud.retail.v2.UserEvent;
30+
import com.google.protobuf.Value;
31+
import java.io.IOException;
32+
33+
public class PredictionWithParameters {
34+
35+
public static void main(String[] args) {
36+
String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
37+
String placementId = System.getenv("GOOGLE_CLOUD_PLACEMENT");
38+
String predictPlacement =
39+
String.format(
40+
"projects/%s/locations/global/catalogs/default_catalog/placements/%s",
41+
projectId, placementId);
42+
43+
predict(predictPlacement);
44+
}
45+
46+
public static void predict(String predictPlacement) {
47+
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
48+
PredictResponse predictResponse =
49+
predictionServiceClient.predict(getPredictRequest(predictPlacement));
50+
System.out.printf("Predict response: %n%s", predictResponse);
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
56+
private static PredictRequest getPredictRequest(String predictPlacement) {
57+
// create product object
58+
Product product =
59+
Product.newBuilder()
60+
.setId("55106") // Id of real product
61+
.build();
62+
63+
// create product detail object
64+
ProductDetail productDetail = ProductDetail.newBuilder().setProduct(product).build();
65+
66+
// create user event object
67+
UserEvent userEvent =
68+
UserEvent.newBuilder()
69+
.setEventType("detail-page-view")
70+
.setVisitorId("281790") // Unique identifier to track visitors
71+
.addProductDetails(productDetail)
72+
.build();
73+
74+
PredictRequest predictRequest =
75+
PredictRequest.newBuilder()
76+
.setPlacement(predictPlacement) // Placement is used to identify the Serving Config name
77+
.setUserEvent(userEvent) // Context about the user is required for event logging
78+
// TRY TO ADD/UPDATE PARAMETERS `priceRerankLevel` OR `diversityLevel` HERE:
79+
.putParams(
80+
"priceRerankLevel",
81+
Value.newBuilder().setStringValue("low-price-reranking").build())
82+
.build();
83+
System.out.printf("Predict request: %n%s", predictRequest);
84+
85+
return predictRequest;
86+
}
87+
}
88+
89+
// [END retail_prediction_get_prediction_with_params]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* [START retail_prediction_get_simple_prediction]
19+
* Call Retail API to get predictions from Recommendation AI using simple request.
20+
*/
21+
22+
package prediction;
23+
24+
import com.google.cloud.retail.v2.PredictRequest;
25+
import com.google.cloud.retail.v2.PredictResponse;
26+
import com.google.cloud.retail.v2.PredictionServiceClient;
27+
import com.google.cloud.retail.v2.Product;
28+
import com.google.cloud.retail.v2.ProductDetail;
29+
import com.google.cloud.retail.v2.UserEvent;
30+
import com.google.protobuf.Value;
31+
import java.io.IOException;
32+
33+
public class SimplePrediction {
34+
35+
public static void main(String[] args) {
36+
String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
37+
String placementId = System.getenv("GOOGLE_CLOUD_PLACEMENT");
38+
String predictPlacement =
39+
String.format(
40+
"projects/%s/locations/global/catalogs/default_catalog/placements/%s",
41+
projectId, placementId);
42+
43+
predict(predictPlacement);
44+
}
45+
46+
public static void predict(String predictPlacement) {
47+
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
48+
PredictResponse predictResponse =
49+
predictionServiceClient.predict(getPredictRequest(predictPlacement));
50+
System.out.printf("Predict response: %n%s", predictResponse);
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
56+
private static PredictRequest getPredictRequest(String predictPlacement) {
57+
// create product object
58+
Product product =
59+
Product.newBuilder()
60+
.setId("55106") // Id of real product
61+
.build();
62+
63+
// create product detail object
64+
ProductDetail productDetail = ProductDetail.newBuilder().setProduct(product).build();
65+
66+
// create user event object
67+
UserEvent userEvent =
68+
UserEvent.newBuilder()
69+
.setEventType("detail-page-view")
70+
.setVisitorId("281790") // Unique identifier to track visitors
71+
.addProductDetails(productDetail)
72+
.build();
73+
74+
PredictRequest predictRequest =
75+
PredictRequest.newBuilder()
76+
.setPlacement(predictPlacement) // Placement is used to identify the Serving Config name
77+
.setUserEvent(userEvent) // Context about the user is required for event logging
78+
.putParams("returnProduct", Value.newBuilder().setBoolValue(true).build())
79+
.build();
80+
System.out.printf("Predict request: %n%s", predictRequest);
81+
82+
return predictRequest;
83+
}
84+
}
85+
86+
// [END retail_prediction_get_simple_prediction]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package prediction;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static prediction.FilteringPrediction.predict;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
@RunWith(JUnit4.class)
32+
public class FilteringPredictionTest {
33+
34+
private ByteArrayOutputStream bout;
35+
private PrintStream originalPrintStream;
36+
37+
@Before
38+
public void setUp() throws IOException, InterruptedException {
39+
bout = new ByteArrayOutputStream();
40+
PrintStream out = new PrintStream(bout);
41+
originalPrintStream = System.out;
42+
System.setOut(out);
43+
}
44+
45+
@Test
46+
public void testPredict() {
47+
String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
48+
String placementId = System.getenv("GOOGLE_CLOUD_PLACEMENT");
49+
String predictPlacement =
50+
String.format(
51+
"projects/%s/locations/global/catalogs/default_catalog/placements/%s",
52+
projectId, placementId);
53+
54+
predict(predictPlacement);
55+
56+
String outputResult = bout.toString();
57+
58+
assertThat(outputResult).contains("Predict request");
59+
assertThat(outputResult).contains("filter: \"filterOutOfStockItems\"");
60+
assertThat(outputResult).contains("Predict response");
61+
}
62+
63+
@After
64+
public void tearDown() {
65+
System.out.flush();
66+
System.setOut(originalPrintStream);
67+
}
68+
}

0 commit comments

Comments
 (0)