Skip to content

Commit 5f057a8

Browse files
deliaqiNeenu1995
andauthored
samples: update sample code for knowledge base (#898)
* update sample code for knowledge base * chore: revert the nightly integration test project to gcloud-devel (#899) Fixes #880 Fixes #881 * sample: minor change for consistent output format * revert license year to 2018 Co-authored-by: Neenu Shaji <[email protected]>
1 parent c3f1176 commit 5f057a8

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
lines changed

dialogflow/snippets/src/main/java/com/example/dialogflow/ConversationProfileManagement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public static void createConversationProfileArticleSuggestion(
125125
.setConversationProfile(targetConversationProfile)
126126
.build());
127127
System.out.println("====================");
128-
System.out.println("Conversation Profile created:");
128+
System.out.println("Conversation Profile created:\n");
129129
System.out.format("Display name: %s\n", createdConversationProfile.getDisplayName());
130130
System.out.format("Name: %s\n", createdConversationProfile.getName());
131131
}

dialogflow/snippets/src/main/java/com/example/dialogflow/KnowledgeBaseManagement.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,39 @@
1919
// [START dialogflow_create_knowledge_base]
2020

2121
import com.google.api.gax.rpc.ApiException;
22-
import com.google.cloud.dialogflow.v2beta1.KnowledgeBase;
23-
import com.google.cloud.dialogflow.v2beta1.KnowledgeBasesClient;
24-
import com.google.cloud.dialogflow.v2beta1.ProjectName;
22+
import com.google.cloud.dialogflow.v2.KnowledgeBase;
23+
import com.google.cloud.dialogflow.v2.KnowledgeBasesClient;
24+
import com.google.cloud.dialogflow.v2.LocationName;
2525
import java.io.IOException;
2626

2727
public class KnowledgeBaseManagement {
2828

29+
public static void main(String[] args) throws ApiException, IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "my-project-id";
32+
String location = "my-location";
33+
34+
// Set display name of the new knowledge base
35+
String knowledgeBaseDisplayName = "my-knowledge-base-display-name";
36+
37+
// Create a knowledge base
38+
createKnowledgeBase(projectId, location, knowledgeBaseDisplayName);
39+
}
40+
2941
// Create a Knowledge base
30-
public static KnowledgeBase createKnowledgeBase(String projectId, String displayName)
42+
public static void createKnowledgeBase(String projectId, String location, String displayName)
3143
throws ApiException, IOException {
3244
// Instantiates a client
3345
try (KnowledgeBasesClient knowledgeBasesClient = KnowledgeBasesClient.create()) {
34-
KnowledgeBase knowledgeBase = KnowledgeBase.newBuilder().setDisplayName(displayName).build();
35-
ProjectName projectName = ProjectName.of(projectId);
36-
KnowledgeBase response = knowledgeBasesClient.createKnowledgeBase(projectName, knowledgeBase);
46+
KnowledgeBase targetKnowledgeBase =
47+
KnowledgeBase.newBuilder().setDisplayName(displayName).build();
48+
LocationName parent = LocationName.of(projectId, location);
49+
KnowledgeBase createdKnowledgeBase =
50+
knowledgeBasesClient.createKnowledgeBase(parent, targetKnowledgeBase);
51+
System.out.println("====================");
3752
System.out.format("Knowledgebase created:\n");
38-
System.out.format("Display Name: %s \n", response.getDisplayName());
39-
System.out.format("Knowledge ID: %s \n", response.getName());
40-
41-
return response;
53+
System.out.format("Display Name: %s\n", createdKnowledgeBase.getDisplayName());
54+
System.out.format("Name: %s\n", createdKnowledgeBase.getName());
4255
}
4356
}
4457
}

dialogflow/snippets/src/test/java/com/example/dialogflow/CreateKnowledgeBaseTest.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static junit.framework.TestCase.assertNotNull;
2121

22-
import com.google.cloud.dialogflow.v2beta1.DeleteKnowledgeBaseRequest;
23-
import com.google.cloud.dialogflow.v2beta1.KnowledgeBase;
24-
import com.google.cloud.dialogflow.v2beta1.KnowledgeBasesClient;
22+
import com.google.cloud.dialogflow.v2.DeleteKnowledgeBaseRequest;
23+
import com.google.cloud.dialogflow.v2.KnowledgeBasesClient;
2524
import java.io.ByteArrayOutputStream;
2625
import java.io.IOException;
2726
import java.io.PrintStream;
@@ -38,13 +37,23 @@
3837
public class CreateKnowledgeBaseTest {
3938

4039
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
40+
private static final String LOCATION = "global";
41+
private static final String ID_PREFIX_IN_OUTPUT = "Name: ";
4142
private static String KNOWLEDGE_DISPLAY_NAME = UUID.randomUUID().toString();
42-
private ByteArrayOutputStream bout;
43-
private PrintStream out;
4443
private String knowledgeBaseName;
44+
private ByteArrayOutputStream bout;
45+
private PrintStream newOutputStream;
46+
private PrintStream originalOutputStream;
4547

4648
private static void requireEnvVar(String varName) {
47-
assertNotNull(String.format(varName), String.format(varName));
49+
assertNotNull(System.getenv(varName));
50+
}
51+
52+
// Extract the name of created resource from "Name: %s\n" in sample code output
53+
private static String getResourceNameFromOutputString(String output) {
54+
return output.substring(
55+
output.lastIndexOf(ID_PREFIX_IN_OUTPUT) + ID_PREFIX_IN_OUTPUT.length(),
56+
output.length() - 1);
4857
}
4958

5059
@BeforeClass
@@ -56,27 +65,30 @@ public static void checkRequirements() {
5665
@Before
5766
public void setUp() {
5867
bout = new ByteArrayOutputStream();
59-
out = new PrintStream(bout);
60-
System.setOut(out);
68+
newOutputStream = new PrintStream(bout);
69+
System.setOut(newOutputStream);
6170
}
6271

6372
@After
6473
public void tearDown() throws IOException {
74+
if (knowledgeBaseName == null) {
75+
return;
76+
}
77+
6578
// Delete the created knowledge base
6679
try (KnowledgeBasesClient client = KnowledgeBasesClient.create()) {
6780
DeleteKnowledgeBaseRequest request =
6881
DeleteKnowledgeBaseRequest.newBuilder().setName(knowledgeBaseName).setForce(true).build();
6982
client.deleteKnowledgeBase(request);
7083
}
71-
System.setOut(null);
84+
System.setOut(originalOutputStream);
7285
}
7386

7487
@Test
7588
public void testCreateKnowledgeBase() throws Exception {
76-
KnowledgeBase knowledgeBase =
77-
KnowledgeBaseManagement.createKnowledgeBase(PROJECT_ID, KNOWLEDGE_DISPLAY_NAME);
78-
knowledgeBaseName = knowledgeBase.getName();
79-
String got = bout.toString();
80-
assertThat(got).contains(KNOWLEDGE_DISPLAY_NAME);
89+
KnowledgeBaseManagement.createKnowledgeBase(PROJECT_ID, LOCATION, KNOWLEDGE_DISPLAY_NAME);
90+
String output = bout.toString();
91+
assertThat(output).contains(KNOWLEDGE_DISPLAY_NAME);
92+
knowledgeBaseName = getResourceNameFromOutputString(output);
8193
}
8294
}

0 commit comments

Comments
 (0)