Skip to content

Commit bc5bfdd

Browse files
committed
feat(generative-ai): migrate grounding samples to new GenAI SDK
1 parent b25e80d commit bc5bfdd

2 files changed

Lines changed: 47 additions & 50 deletions

File tree

generative-ai/snippets/grounding/groundingPrivateDataBasic.js

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313
// limitations under the License.
1414

1515
// [START generativeaionvertexai_grounding_private_data_basic]
16-
const {
17-
VertexAI,
18-
HarmCategory,
19-
HarmBlockThreshold,
20-
} = require('@google-cloud/vertexai');
16+
const {GoogleGenAI} = require('@google/genai');
2117

2218
/**
2319
* TODO(developer): Update these variables before running the sample.
@@ -28,41 +24,43 @@ async function generateContentWithVertexAISearchGrounding(
2824
model = 'gemini-2.0-flash-001',
2925
dataStoreId = 'DATASTORE_ID'
3026
) {
31-
// Initialize Vertex with your Cloud project and location
32-
const vertexAI = new VertexAI({project: projectId, location: location});
33-
34-
const generativeModelPreview = vertexAI.preview.getGenerativeModel({
35-
model: model,
36-
// The following parameters are optional
37-
// They can also be passed to individual content generation requests
38-
safetySettings: [
39-
{
40-
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
41-
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
42-
},
43-
],
44-
generationConfig: {maxOutputTokens: 256},
27+
// Initialize cleint with your Cloud project and location
28+
const client = new GoogleGenAI({
29+
vertexai: true,
30+
project: projectId,
31+
location: location,
4532
});
4633

47-
const vertexAIRetrievalTool = {
48-
retrieval: {
49-
vertexAiSearch: {
50-
datastore: `projects/${projectId}/locations/global/collections/default_collection/dataStores/${dataStoreId}`,
34+
const tools = [
35+
{
36+
retrieval: {
37+
vertexAiSearch: {
38+
datastore: `projects/${projectId}/locations/global/collections/default_collection/dataStores/${dataStoreId}`,
39+
},
5140
},
52-
disableAttribution: false,
5341
},
54-
};
42+
];
5543

56-
const request = {
44+
const result = await client.models.generateContent({
45+
model: model,
5746
contents: [{role: 'user', parts: [{text: 'Why is the sky blue?'}]}],
58-
tools: [vertexAIRetrievalTool],
59-
};
47+
config: {
48+
tools: tools,
49+
maxOutputTokens: 256,
50+
safetySettings: [
51+
{
52+
category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
53+
threshold: 'BLOCK_MEDIUM_AND_ABOVE',
54+
},
55+
],
56+
},
57+
});
6058

61-
const result = await generativeModelPreview.generateContent(request);
62-
const response = result.response;
63-
const groundingMetadata = response.candidates[0];
64-
console.log('Response: ', JSON.stringify(response.candidates[0]));
65-
console.log('GroundingMetadata is: ', JSON.stringify(groundingMetadata));
59+
console.log('Response: ', result.text);
60+
console.log(
61+
'GroundingMetadata: ',
62+
JSON.stringify(result.candidates[0].groundingMetadata)
63+
);
6664
}
6765
// [END generativeaionvertexai_grounding_private_data_basic]
6866

generative-ai/snippets/grounding/groundingPublicDataBasic.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
// [START generativeaionvertexai_grounding_public_data_basic]
16-
const {VertexAI} = require('@google-cloud/vertexai');
16+
const {GoogleGenAI} = require('@google/genai');
1717

1818
/**
1919
* TODO(developer): Update these variables before running the sample.
@@ -23,31 +23,30 @@ async function generateContentWithGoogleSearchGrounding(
2323
location = 'us-central1',
2424
model = 'gemini-2.0-flash-001'
2525
) {
26-
// Initialize Vertex with your Cloud project and location
27-
const vertexAI = new VertexAI({project: projectId, location: location});
28-
29-
const generativeModelPreview = vertexAI.preview.getGenerativeModel({
30-
model: model,
31-
generationConfig: {maxOutputTokens: 256},
26+
// Initialize client with your Cloud project and location
27+
const client = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
3231
});
3332

3433
const googleSearchTool = {
3534
googleSearch: {},
3635
};
3736

38-
const request = {
37+
const result = await client.models.generateContent({
38+
model: model,
3939
contents: [{role: 'user', parts: [{text: 'Why is the sky blue?'}]}],
40-
tools: [googleSearchTool],
41-
};
42-
43-
const result = await generativeModelPreview.generateContent(request);
44-
const response = await result.response;
45-
const groundingMetadata = response.candidates[0].groundingMetadata;
40+
config: {
41+
tools: [googleSearchTool],
42+
maxOutputTokens: 256,
43+
},
44+
});
45+
console.log('Response: ', JSON.stringify(result.text));
4646
console.log(
47-
'Response: ',
48-
JSON.stringify(response.candidates[0].content.parts[0].text)
47+
'GroundingMetadata is: ',
48+
JSON.stringify(result.candidates[0].groundingMetadata)
4949
);
50-
console.log('GroundingMetadata is: ', JSON.stringify(groundingMetadata));
5150
}
5251
// [END generativeaionvertexai_grounding_public_data_basic]
5352

0 commit comments

Comments
 (0)