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
0 commit comments