Skip to content

Commit 6a4e8be

Browse files
authored
samples: create topic model (#84)
1 parent 2ea3ddf commit 6a4e8be

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2021 Google Inc.
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 com.example.contactcenterinsights;
18+
19+
// [START contactcenterinsights_create_issue_model]
20+
21+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
22+
import com.google.cloud.contactcenterinsights.v1.IssueModel;
23+
import com.google.cloud.contactcenterinsights.v1.LocationName;
24+
import java.io.IOException;
25+
26+
public class CreateIssueModel {
27+
28+
public static void main(String[] args) throws Exception, IOException {
29+
// TODO(developer): Replace this variable before running the sample.
30+
String projectId = "my_project_id";
31+
32+
createIssueModel(projectId);
33+
}
34+
35+
public static IssueModel createIssueModel(String projectId) throws Exception, IOException {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests. After completing all of your requests, call
38+
// the "close" method on the client to safely clean up any remaining background resources.
39+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
40+
// Construct a parent resource.
41+
LocationName parent = LocationName.of(projectId, "us-central1");
42+
43+
// Construct an issue model.
44+
IssueModel issueModel =
45+
IssueModel.newBuilder()
46+
.setDisplayName("my-model")
47+
.setInputDataConfig(
48+
IssueModel.InputDataConfig.newBuilder().setFilter("medium=\"CHAT\"").build())
49+
.build();
50+
51+
// Call the Insights client to create an issue model.
52+
IssueModel response = client.createIssueModelAsync(parent, issueModel).get();
53+
System.out.printf("Created %s%n", response.getName());
54+
return response;
55+
}
56+
}
57+
}
58+
59+
// [END contactcenterinsights_create_issue_model]
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2021 Google Inc.
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 com.example.contactcenterinsights;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.contactcenterinsights.v1.ContactCenterInsightsClient;
23+
import com.google.cloud.contactcenterinsights.v1.IssueModel;
24+
import com.google.cloud.contactcenterinsights.v1.ListConversationsRequest;
25+
import com.google.cloud.contactcenterinsights.v1.ListConversationsResponse;
26+
import com.google.cloud.contactcenterinsights.v1.LocationName;
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.IOException;
29+
import java.io.PrintStream;
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.BeforeClass;
33+
import org.junit.Test;
34+
import org.junit.runner.RunWith;
35+
import org.junit.runners.JUnit4;
36+
37+
@RunWith(JUnit4.class)
38+
public class CreateIssueModelIT {
39+
40+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
41+
private static final int MIN_CONVERSATION_COUNT = 10000;
42+
private ByteArrayOutputStream bout;
43+
private PrintStream out;
44+
private String issueModelName;
45+
private int conversationCount;
46+
47+
private static void requireEnvVar(String varName) {
48+
assertNotNull(String.format(varName), String.format(varName));
49+
}
50+
51+
@BeforeClass
52+
public static void checkRequirements() {
53+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
54+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
55+
}
56+
57+
@Before
58+
public void setUp() throws IOException {
59+
bout = new ByteArrayOutputStream();
60+
out = new PrintStream(bout);
61+
System.setOut(out);
62+
63+
// Check if the project has the minimum number of conversations required to create
64+
// an issue model. See https://cloud.google.com/contact-center/insights/docs/topic-model.
65+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
66+
LocationName parent = LocationName.of(PROJECT_ID, "us-central1");
67+
ListConversationsRequest.Builder listRequest =
68+
ListConversationsRequest.newBuilder().setParent(parent.toString()).setPageSize(1000);
69+
70+
conversationCount = 0;
71+
while (conversationCount < MIN_CONVERSATION_COUNT) {
72+
ListConversationsResponse listResponse =
73+
client.listConversationsCallable().call(listRequest.build());
74+
75+
if (listResponse.getConversationsCount() == 0) {
76+
break;
77+
}
78+
conversationCount += listResponse.getConversationsCount();
79+
80+
if (listResponse.getNextPageToken().isEmpty()) {
81+
break;
82+
}
83+
listRequest.setPageToken(listResponse.getNextPageToken());
84+
}
85+
}
86+
}
87+
88+
@After
89+
public void tearDown() throws Exception, IOException {
90+
if (conversationCount >= MIN_CONVERSATION_COUNT) {
91+
try (ContactCenterInsightsClient client = ContactCenterInsightsClient.create()) {
92+
client.deleteIssueModelAsync(issueModelName);
93+
}
94+
}
95+
System.setOut(null);
96+
}
97+
98+
@Test
99+
public void testCreateIssueModel() throws Exception, IOException {
100+
if (conversationCount >= MIN_CONVERSATION_COUNT) {
101+
IssueModel issueModel = CreateIssueModel.createIssueModel(PROJECT_ID);
102+
issueModelName = issueModel.getName();
103+
assertThat(bout.toString()).contains(issueModelName);
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)