Skip to content

Commit 907ad74

Browse files
yyyu-googlecopybara-github
authored andcommitted
fix: pass tools from getGenerativeModel to chatSession.
PiperOrigin-RevId: 613294961
1 parent bbaf78a commit 907ad74

File tree

2 files changed

+75
-12
lines changed

2 files changed

+75
-12
lines changed

src/models/generative_models.ts

+1
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ export class GenerativeModelPreview {
304304
location: this.location,
305305
googleAuth: this.googleAuth,
306306
publisher_model_endpoint: this.publisherModelEndpoint,
307+
tools: this.tools,
307308
};
308309

309310
if (request) {

system_test/end_to_end_sample_test.ts

+74-12
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,10 @@ describe('generateContent', () => {
434434
});
435435
const response = result.response;
436436
const groundingMetadata = response.candidates[0].groundingMetadata;
437-
expect(groundingMetadata).toBeDefined();
437+
expect(!!groundingMetadata).toBeTruthy();
438438
if (groundingMetadata) {
439-
expect(groundingMetadata.groundingAttributions).toBeTruthy();
440-
expect(groundingMetadata.webSearchQueries).toBeTruthy();
439+
expect(!!groundingMetadata.groundingAttributions).toBeTruthy();
440+
expect(!!groundingMetadata.webSearchQueries).toBeTruthy();
441441
}
442442
});
443443
it('should return grounding metadata when passed GoogleSearchRetriever in generateContent', async () => {
@@ -450,10 +450,10 @@ describe('generateContent', () => {
450450
});
451451
const response = result.response;
452452
const groundingMetadata = response.candidates[0].groundingMetadata;
453-
expect(groundingMetadata).toBeDefined();
453+
expect(!!groundingMetadata).toBeTruthy();
454454
if (groundingMetadata) {
455-
expect(groundingMetadata.groundingAttributions).toBeTruthy();
456-
expect(groundingMetadata.webSearchQueries).toBeTruthy();
455+
expect(!!groundingMetadata.groundingAttributions).toBeTruthy();
456+
expect(!!groundingMetadata.webSearchQueries).toBeTruthy();
457457
}
458458
});
459459
it('in preview should return grounding metadata when passed GoogleSearchRetriever in getGenerativeModel', async () => {
@@ -466,10 +466,10 @@ describe('generateContent', () => {
466466
});
467467
const response = result.response;
468468
const groundingMetadata = response.candidates[0].groundingMetadata;
469-
expect(groundingMetadata).toBeDefined();
469+
expect(!!groundingMetadata).toBeTruthy();
470470
if (groundingMetadata) {
471-
expect(groundingMetadata.groundingAttributions).toBeTruthy();
472-
expect(groundingMetadata.webSearchQueries).toBeTruthy();
471+
expect(!!groundingMetadata.groundingAttributions).toBeTruthy();
472+
expect(!!groundingMetadata.webSearchQueries).toBeTruthy();
473473
}
474474
});
475475
it('in preview should return grounding metadata when passed GoogleSearchRetriever in generateContent', async () => {
@@ -482,10 +482,10 @@ describe('generateContent', () => {
482482
});
483483
const response = result.response;
484484
const groundingMetadata = response.candidates[0].groundingMetadata;
485-
expect(groundingMetadata).toBeDefined();
485+
expect(!!groundingMetadata).toBeTruthy();
486486
if (groundingMetadata) {
487-
expect(groundingMetadata.groundingAttributions).toBeTruthy();
488-
expect(groundingMetadata.webSearchQueries).toBeTruthy();
487+
expect(!!groundingMetadata.groundingAttributions).toBeTruthy();
488+
expect(!!groundingMetadata.webSearchQueries).toBeTruthy();
489489
}
490490
});
491491
});
@@ -514,6 +514,68 @@ describe('sendMessage', () => {
514514
);
515515
expect(chat.history.length).toBe(2);
516516
});
517+
it('should return grounding metadata when passed GoogleSearchRetriever in getGenerativeModel', async () => {
518+
const generativeTextModel = vertex_ai.getGenerativeModel({
519+
model: 'gemini-pro',
520+
tools: TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL,
521+
});
522+
const chat = generativeTextModel.startChat();
523+
const result = await chat.sendMessage('Why is the sky blue?');
524+
const response = result.response;
525+
const groundingMetadata = response.candidates[0].groundingMetadata;
526+
expect(!!groundingMetadata).toBeTruthy();
527+
if (groundingMetadata) {
528+
expect(!!groundingMetadata.groundingAttributions).toBeTruthy();
529+
expect(!!groundingMetadata.webSearchQueries).toBeTruthy();
530+
}
531+
});
532+
it('should return grounding metadata when passed GoogleSearchRetriever in startChat', async () => {
533+
const generativeTextModel = vertex_ai.getGenerativeModel({
534+
model: 'gemini-pro',
535+
});
536+
const chat = generativeTextModel.startChat({
537+
tools: TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL,
538+
});
539+
const result = await chat.sendMessage('Why is the sky blue?');
540+
const response = result.response;
541+
const groundingMetadata = response.candidates[0].groundingMetadata;
542+
expect(!!groundingMetadata).toBeTruthy();
543+
if (groundingMetadata) {
544+
expect(!!groundingMetadata.groundingAttributions).toBeTruthy();
545+
expect(!!groundingMetadata.webSearchQueries).toBeTruthy();
546+
}
547+
});
548+
it('in preview should return grounding metadata when passed GoogleSearchRetriever in getGenerativeModel', async () => {
549+
const generativeTextModel = vertex_ai.preview.getGenerativeModel({
550+
model: 'gemini-pro',
551+
tools: TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL,
552+
});
553+
const chat = generativeTextModel.startChat();
554+
const result = await chat.sendMessage('Why is the sky blue?');
555+
const response = result.response;
556+
const groundingMetadata = response.candidates[0].groundingMetadata;
557+
expect(!!groundingMetadata).toBeTruthy();
558+
if (groundingMetadata) {
559+
expect(!!groundingMetadata.groundingAttributions).toBeTruthy();
560+
expect(!!groundingMetadata.webSearchQueries).toBeTruthy();
561+
}
562+
});
563+
it('in preview should return grounding metadata when passed GoogleSearchRetriever in startChat', async () => {
564+
const generativeTextModel = vertex_ai.preview.getGenerativeModel({
565+
model: 'gemini-pro',
566+
});
567+
const chat = generativeTextModel.startChat({
568+
tools: TOOLS_WITH_GOOGLE_SEARCH_RETRIEVAL,
569+
});
570+
const result = await chat.sendMessage('Why is the sky blue?');
571+
const response = result.response;
572+
const groundingMetadata = response.candidates[0].groundingMetadata;
573+
expect(!!groundingMetadata).toBeTruthy();
574+
if (groundingMetadata) {
575+
expect(!!groundingMetadata.groundingAttributions).toBeTruthy();
576+
expect(!!groundingMetadata.webSearchQueries).toBeTruthy();
577+
}
578+
});
517579
});
518580

519581
describe('sendMessageStream', () => {

0 commit comments

Comments
 (0)