Skip to content

Commit 26edca8

Browse files
authored
samples: create topic model (#28)
1 parent 50fb4e9 commit 26edca8

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# [START contactcenterinsights_create_issue_model]
16+
from google.cloud import contact_center_insights_v1
17+
18+
19+
def create_issue_model(project_id: str) -> contact_center_insights_v1.IssueModel:
20+
# Construct a parent resource.
21+
parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path(
22+
project_id, "us-central1"
23+
)
24+
25+
# Construct an issue model.
26+
issue_model = contact_center_insights_v1.IssueModel()
27+
issue_model.display_name = "my-model"
28+
issue_model.input_data_config.filter = "medium=\"CHAT\""
29+
30+
# Call the Insights client to create an issue model.
31+
insights_client = contact_center_insights_v1.ContactCenterInsightsClient()
32+
issue_model_operation = insights_client.create_issue_model(
33+
parent=parent, issue_model=issue_model
34+
)
35+
36+
issue_model = issue_model_operation.result(timeout=86400)
37+
print(f"Created an issue model named {issue_model.name}")
38+
return issue_model
39+
40+
41+
# [END contactcenterinsights_create_issue_model]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
import google.auth
16+
17+
from google.cloud import contact_center_insights_v1
18+
19+
import pytest
20+
21+
import create_issue_model
22+
23+
MIN_CONVERSATION_COUNT = 10000
24+
25+
26+
@pytest.fixture
27+
def project_id():
28+
_, project_id = google.auth.default()
29+
return project_id
30+
31+
32+
@pytest.fixture
33+
def insights_client():
34+
return contact_center_insights_v1.ContactCenterInsightsClient()
35+
36+
37+
@pytest.fixture
38+
def count_conversations(project_id, insights_client):
39+
# Check if the project has the minimum number of conversations required to create an issue model.
40+
# See https://cloud.google.com/contact-center/insights/docs/topic-model.
41+
list_request = contact_center_insights_v1.ListConversationsRequest()
42+
list_request.page_size = 1000
43+
list_request.parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path(
44+
project_id, "us-central1"
45+
)
46+
conversations = insights_client.list_conversations(request=list_request)
47+
conversation_count = len(list(conversations))
48+
49+
yield conversation_count
50+
51+
52+
@pytest.fixture
53+
def issue_model_resource(project_id, insights_client, count_conversations):
54+
conversation_count = count_conversations
55+
if conversation_count >= MIN_CONVERSATION_COUNT:
56+
# Create an issue model.
57+
issue_model = create_issue_model.create_issue_model(project_id)
58+
yield issue_model
59+
60+
# Delete the issue model.
61+
insights_client.delete_issue_model(name=issue_model.name)
62+
else:
63+
yield None
64+
65+
66+
def test_create_issue_model(capsys, issue_model_resource):
67+
issue_model = issue_model_resource
68+
if issue_model:
69+
out, err = capsys.readouterr()
70+
assert "Created {}".format(issue_model.name) in out

0 commit comments

Comments
 (0)