@@ -34,7 +34,12 @@ passing it your Google Cloud project ID and location. Then create a reference to
34
34
a generative model.
35
35
36
36
``` 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' );
38
43
39
44
const project = ' your-cloud-project' ;
40
45
const location = ' us-central1' ;
@@ -333,8 +338,7 @@ async function countTokens() {
333
338
const request = {
334
339
contents: [{role: ' user' , parts: [{text: ' How are you doing today?' }]}],
335
340
};
336
- const result = await generativeModel .countTokens (request );
337
- const response = result .response ;
341
+ const response = await generativeModel .countTokens (request );
338
342
console .log (' count tokens response: ' , JSON .stringify (response ));
339
343
}
340
344
@@ -353,54 +357,62 @@ data source for grounding.
353
357
### Grounding using Google Search (Preview)
354
358
355
359
``` typescript
356
- const generativeModelPreview = vertexAI .preview .getGenerativeModel ({
360
+ async function generateContentWithGoogleSearchGrounding() {
361
+ const generativeModelPreview = vertexAI .preview .getGenerativeModel ({
357
362
model: textModel ,
358
363
// The following parameters are optional
359
364
// They can also be passed to individual content generation requests
360
365
safetySettings: [{category: HarmCategory .HARM_CATEGORY_DANGEROUS_CONTENT , threshold: HarmBlockThreshold .BLOCK_MEDIUM_AND_ABOVE }],
361
366
generationConfig: {maxOutputTokens: 256 },
362
367
});
363
368
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
+
376
384
```
377
385
378
386
### Grounding using Vertex AI Search (Preview)
379
387
380
388
``` typescript
381
- const generativeModelPreview = vertexAI .preview .getGenerativeModel ({
389
+ async function generateContentWithVertexAISearchGrounding() {
390
+ const generativeModelPreview = vertexAI .preview .getGenerativeModel ({
382
391
model: textModel ,
383
392
// The following parameters are optional
384
393
// They can also be passed to individual content generation requests
385
394
safetySettings: [{category: HarmCategory .HARM_CATEGORY_DANGEROUS_CONTENT , threshold: HarmBlockThreshold .BLOCK_MEDIUM_AND_ABOVE }],
386
395
generationConfig: {maxOutputTokens: 256 },
387
396
});
388
397
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
+
404
416
```
405
417
406
418
## License
0 commit comments