Skip to content

Commit 509e95d

Browse files
authored
docs(samples): Created Initial Sample for Generative AI app builder - Search (GoogleCloudPlatform#7827)
- The Updated Client Library has been published. - The Test Project has the API/Search Engine configured. - Snippet Prefix exposed in tool.
1 parent fe4a7fa commit 509e95d

4 files changed

Lines changed: 217 additions & 0 deletions

File tree

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
/dataproc @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/dee-data-ai
7070
/dialogflow @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/dee-data-ai
7171
/dialogflow-cx @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/dee-data-ai
72+
/discoveryengine @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/dee-data-ai
7273
/document-ai @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/dee-data-ai
7374
/jobs @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/dee-data-ai
7475
/language @GoogleCloudPlatform/java-samples-reviewers @yoshi-approver @GoogleCloudPlatform/dee-data-ai

discoveryengine/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.example.discoveryengine</groupId>
5+
<artifactId>discoveryengine-snippets</artifactId>
6+
<packaging>jar</packaging>
7+
<name>Generative AI App Builder Snippets</name>
8+
<url>https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/discoveryengine</url>
9+
10+
<!--
11+
The parent pom defines common style checks and testing strategies for our samples.
12+
Removing or replacing it should not affect the execution of the samples in anyway.
13+
-->
14+
<parent>
15+
<groupId>com.google.cloud.samples</groupId>
16+
<artifactId>shared-configuration</artifactId>
17+
<version>1.2.0</version>
18+
</parent>
19+
20+
<properties>
21+
<maven.compiler.target>1.8</maven.compiler.target>
22+
<maven.compiler.source>1.8</maven.compiler.source>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
</properties>
25+
26+
27+
<!-- [START genappbuilder_install_with_bom] -->
28+
<dependencyManagement>
29+
<dependencies>
30+
<dependency>
31+
<groupId>com.google.cloud</groupId>
32+
<artifactId>libraries-bom</artifactId>
33+
<version>26.13.0</version>
34+
<type>pom</type>
35+
<scope>import</scope>
36+
</dependency>
37+
</dependencies>
38+
</dependencyManagement>
39+
40+
<dependencies>
41+
<dependency>
42+
<!-- TODO: version must be removed once the libraries-bom updates to new version -->
43+
<groupId>com.google.cloud</groupId>
44+
<artifactId>google-cloud-discoveryengine</artifactId>
45+
<version>0.12.0</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.google.api.grpc</groupId>
49+
<artifactId>proto-google-cloud-discoveryengine-v1beta</artifactId>
50+
<version>0.12.0</version>
51+
</dependency>
52+
<!-- [END genappbuilder_install_with_bom] -->
53+
<dependency>
54+
<groupId>junit</groupId>
55+
<artifactId>junit</artifactId>
56+
<version>4.13.2</version>
57+
<scope>test</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.google.truth</groupId>
61+
<artifactId>truth</artifactId>
62+
<version>1.1.3</version>
63+
<scope>test</scope>
64+
</dependency>
65+
</dependencies>
66+
</project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2023 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 discoveryengine.v1beta;
18+
19+
// [START genappbuilder_search]
20+
21+
import com.google.cloud.discoveryengine.v1beta.SearchRequest;
22+
import com.google.cloud.discoveryengine.v1beta.SearchResponse;
23+
import com.google.cloud.discoveryengine.v1beta.SearchServiceClient;
24+
import com.google.cloud.discoveryengine.v1beta.ServingConfigName;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
28+
public class Search {
29+
public static void main() throws IOException, ExecutionException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
// Project ID or project number of the Cloud project you want to use.
32+
String projectId = "PROJECT_ID";
33+
// Location of the search engine. Options: "global"
34+
String location = "global";
35+
// Collection containing the datastore.
36+
String collectionId = "default_collection";
37+
// Datastore/Search Engine ID.
38+
String searchEngineId = "DATA_STORE_ID";
39+
// Serving configuration. Options: "default_search"
40+
String servingConfigId = "default_search";
41+
// Search Query for the search engine.
42+
String searchQuery = "Google";
43+
search(projectId, location, collectionId, searchEngineId, servingConfigId, searchQuery);
44+
}
45+
46+
/** Performs a search on a given datastore/search engine. */
47+
public static void search(
48+
String projectId,
49+
String location,
50+
String collectionId,
51+
String searchEngineId,
52+
String servingConfigId,
53+
String searchQuery)
54+
throws IOException, ExecutionException {
55+
// Initialize client that will be used to send requests. This client only needs to be created
56+
// once, and can be reused for multiple requests. After completing all of your requests, call
57+
// the `searchServiceClient.close()` method on the client to safely
58+
// clean up any remaining background resources.
59+
try (SearchServiceClient searchServiceClient = SearchServiceClient.create()) {
60+
SearchRequest request =
61+
SearchRequest.newBuilder()
62+
.setServingConfig(
63+
ServingConfigName.formatProjectLocationCollectionDataStoreServingConfigName(
64+
projectId, location, collectionId, searchEngineId, servingConfigId))
65+
.setQuery(searchQuery)
66+
.setPageSize(10)
67+
.build();
68+
SearchResponse response = searchServiceClient.search(request).getPage().getResponse();
69+
for (SearchResponse.SearchResult element : response.getResultsList()) {
70+
System.out.println("Response content: " + element);
71+
}
72+
}
73+
}
74+
}
75+
// [END genappbuilder_search]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2023 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 discoveryengine.v1beta;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
public class SearchTest {
29+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
30+
private static final String LOCATION = "global";
31+
private static final String COLLECTION_ID = "default_collection";
32+
private static final String SEARCH_ENGINE_ID = "test-search-engine";
33+
private static final String SERVING_CONFIG_ID = "default_search";
34+
private static final String SEARCH_QUERY = "Google";
35+
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
private PrintStream originalPrintStream;
39+
40+
private static void requireEnvVar(String varName) {
41+
assertNotNull(
42+
String.format("Environment variable '%s' must be set to perform these tests.", varName),
43+
System.getenv(varName));
44+
}
45+
46+
@Before
47+
public void checkRequirements() {
48+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
49+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
50+
}
51+
52+
@Before
53+
public void setUp() {
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
originalPrintStream = System.out;
57+
System.setOut(out);
58+
}
59+
60+
@Test
61+
public void testSearch() throws Exception {
62+
Search.search(
63+
PROJECT_ID, LOCATION, COLLECTION_ID, SEARCH_ENGINE_ID, SERVING_CONFIG_ID, SEARCH_QUERY);
64+
String got = bout.toString();
65+
66+
assertThat(got).contains("Response content:");
67+
assertThat(got).contains("Google");
68+
}
69+
70+
@After
71+
public void tearDown() {
72+
System.out.flush();
73+
System.setOut(originalPrintStream);
74+
}
75+
}

0 commit comments

Comments
 (0)