@@ -34,7 +34,12 @@ passing it your Google Cloud project ID and location. Then create a reference to
3434a generative model.
3535
3636``` typescript
37- const {VertexAI, HarmCategory, HarmBlockThreshold, GoogleSearchRetrievalTool, RetrievalTool} = require (' @google-cloud/vertexai' );
37+ const {
38+ FunctionDeclarationSchemaType,
39+ HarmBlockThreshold,
40+ HarmCategory,
41+ VertexAI
42+ } = require (' @google-cloud/vertexai' );
3843
3944const project = ' your-cloud-project' ;
4045const location = ' us-central1' ;
@@ -333,8 +338,7 @@ async function countTokens() {
333338 const request = {
334339 contents: [{role: ' user' , parts: [{text: ' How are you doing today?' }]}],
335340 };
336- const result = await generativeModel .countTokens (request );
337- const response = result .response ;
341+ const response = await generativeModel .countTokens (request );
338342 console .log (' count tokens response: ' , JSON .stringify (response ));
339343}
340344
@@ -353,54 +357,62 @@ data source for grounding.
353357### Grounding using Google Search (Preview)
354358
355359``` typescript
356- const generativeModelPreview = vertexAI .preview .getGenerativeModel ({
360+ async function generateContentWithGoogleSearchGrounding() {
361+ const generativeModelPreview = vertexAI .preview .getGenerativeModel ({
357362 model: textModel ,
358363 // The following parameters are optional
359364 // They can also be passed to individual content generation requests
360365 safetySettings: [{category: HarmCategory .HARM_CATEGORY_DANGEROUS_CONTENT , threshold: HarmBlockThreshold .BLOCK_MEDIUM_AND_ABOVE }],
361366 generationConfig: {maxOutputTokens: 256 },
362367 });
363368
364- const googleSearchRetrievalTool: GoogleSearchRetrievalTool = {
365- googleSearchRetrieval: {
366- disableAttribution: false ,
367- },
368- };
369- const response = await generativeModelPreview .generateContent ({
370- contents: [{role: ' user' , parts: [{text: ' Why is the sky blue?' }]}],
371- tools: [googleSearchRetrievalTool ],
372- }).response ;
373- const groundingMetadata = response .candidates [0 ].groundingMetadata ;
374- console .log (" Response of grounding is: " , JSON .stringify (response ));
375- console .log (" Grounding metadata is: " , JSON .stringify (groundingMetadata ));
369+ const googleSearchRetrievalTool = {
370+ googleSearchRetrieval: {
371+ disableAttribution: false ,
372+ },
373+ };
374+ const result = await generativeModelPreview .generateContent ({
375+ contents: [{role: ' user' , parts: [{text: ' Why is the sky blue?' }]}],
376+ tools: [googleSearchRetrievalTool ],
377+ })
378+ const response = result .response ;
379+ const groundingMetadata = response .candidates [0 ].groundingMetadata ;
380+ console .log (" GroundingMetadata is: " , JSON .stringify (groundingMetadata ));
381+ }
382+ generateContentWithGoogleSearchGrounding ();
383+
376384```
377385
378386### Grounding using Vertex AI Search (Preview)
379387
380388``` typescript
381- const generativeModelPreview = vertexAI .preview .getGenerativeModel ({
389+ async function generateContentWithVertexAISearchGrounding() {
390+ const generativeModelPreview = vertexAI .preview .getGenerativeModel ({
382391 model: textModel ,
383392 // The following parameters are optional
384393 // They can also be passed to individual content generation requests
385394 safetySettings: [{category: HarmCategory .HARM_CATEGORY_DANGEROUS_CONTENT , threshold: HarmBlockThreshold .BLOCK_MEDIUM_AND_ABOVE }],
386395 generationConfig: {maxOutputTokens: 256 },
387396 });
388397
389- const vertexAiRetrievalTool: RetrievalTool = {
390- retrieval: {
391- vertexAiSearch: {
392- datastore = ' projects/.../locations/.../collections/.../dataStores/...' ,
393- }
394- disableAttribution : false ,
395- },
396- };
397- const response = await generativeModelPreview .generateContent ({
398- contents: [{role: ' user' , parts: [{text: ' Why is the sky blue?' }]}],
399- tools: [googleSearchRetrievalTool ],
400- }).response ;
401- const groundingMetadata = response .candidates [0 ].groundingMetadata ;
402- console .log (" Response of grounding is: " , JSON .stringify (response ));
403- console .log (" Grounding metadata is: " , JSON .stringify (groundingMetadata ));
398+ const vertexAIRetrievalTool = {
399+ retrieval: {
400+ vertexAiSearch: {
401+ datastore = ' projects/.../locations/.../collections/.../dataStores/...' ,
402+ },
403+ disableAttribution: false ,
404+ },
405+ };
406+ const result = await generativeModelPreview .generateContent ({
407+ contents: [{role: ' user' , parts: [{text: ' Why is the sky blue?' }]}],
408+ tools: [vertexAIRetrievalTool ],
409+ })
410+ const response = result .response ;
411+ const groundingMetadata = response .candidates [0 ].groundingMetadata ;
412+ console .log (" Grounding metadata is: " , JSON .stringify (groundingMetadata ));
413+ }
414+ generateContentWithVertexAISearchGrounding ();
415+
404416```
405417
406418## License
0 commit comments