@@ -223,6 +223,10 @@ const TEST_SYSTEM_INSTRUCTION = {
223
223
role : constants . SYSTEM_ROLE ,
224
224
parts : [ { text : 'system instruction' } ] ,
225
225
} ;
226
+ const TEST_SYSTEM_INSTRUCTION_1 = {
227
+ role : constants . SYSTEM_ROLE ,
228
+ parts : [ { text : 'system instruction1' } ] ,
229
+ } ;
226
230
const TEST_SYSTEM_INSTRUCTION_WRONG_ROLE = {
227
231
role : 'WRONG_ROLE' ,
228
232
parts : [ { text : 'system instruction' } ] ,
@@ -339,6 +343,118 @@ describe('GenerativeModel startChat', () => {
339
343
const actualBody = fetchSpy . calls . allArgs ( ) [ 0 ] [ 1 ] . body ;
340
344
expect ( actualBody ) . toEqual ( expectedBody ) ;
341
345
} ) ;
346
+ it ( 'pass system instruction to remote endpoint from GenerativeModel constructor' , async ( ) => {
347
+ const expectedResult = TEST_MODEL_RESPONSE ;
348
+ const fetchResult = Promise . resolve (
349
+ new Response ( JSON . stringify ( expectedResult ) , fetchResponseObj )
350
+ ) ;
351
+ const fetchSpy = spyOn ( global , 'fetch' ) . and . returnValue ( fetchResult ) ;
352
+ const req = 'How are you doing today?' ;
353
+ const model = new GenerativeModel ( {
354
+ model : 'gemini-pro' ,
355
+ project : PROJECT ,
356
+ location : LOCATION ,
357
+ googleAuth : FAKE_GOOGLE_AUTH ,
358
+ systemInstruction : TEST_SYSTEM_INSTRUCTION ,
359
+ } ) ;
360
+ const chat = model . startChat ( {
361
+ history : TEST_USER_CHAT_MESSAGE ,
362
+ } ) ;
363
+ const expectedBody =
364
+ '{"contents":[{"role":"user","parts":[{"text":"How are you doing today?"}]},{"role":"user","parts":[{"text":"How are you doing today?"}]}],"systemInstruction":{"role":"system","parts":[{"text":"system instruction"}]}}' ;
365
+ await chat . sendMessage ( req ) ;
366
+ // @ts -ignore
367
+ const actualBody = fetchSpy . calls . allArgs ( ) [ 0 ] [ 1 ] . body ;
368
+ expect ( actualBody ) . toEqual (
369
+ expectedBody ,
370
+ `unit test failed in chat.sendMessage with ${ actualBody } not equal to ${ expectedBody } `
371
+ ) ;
372
+ } ) ;
373
+ it ( 'pass system instruction to remote endpoint from startChat' , async ( ) => {
374
+ const expectedResult = TEST_MODEL_RESPONSE ;
375
+ const fetchResult = Promise . resolve (
376
+ new Response ( JSON . stringify ( expectedResult ) , fetchResponseObj )
377
+ ) ;
378
+ const fetchSpy = spyOn ( global , 'fetch' ) . and . returnValue ( fetchResult ) ;
379
+ const req = 'How are you doing today?' ;
380
+ const model = new GenerativeModel ( {
381
+ model : 'gemini-pro' ,
382
+ project : PROJECT ,
383
+ location : LOCATION ,
384
+ googleAuth : FAKE_GOOGLE_AUTH ,
385
+ systemInstruction : TEST_SYSTEM_INSTRUCTION_1 ,
386
+ } ) ;
387
+ const chat = model . startChat ( {
388
+ history : TEST_USER_CHAT_MESSAGE ,
389
+ // this is different from constructor
390
+ systemInstruction : TEST_SYSTEM_INSTRUCTION ,
391
+ } ) ;
392
+ const expectedBody =
393
+ '{"contents":[{"role":"user","parts":[{"text":"How are you doing today?"}]},{"role":"user","parts":[{"text":"How are you doing today?"}]}],"systemInstruction":{"role":"system","parts":[{"text":"system instruction"}]}}' ;
394
+ await chat . sendMessage ( req ) ;
395
+ // @ts -ignore
396
+ const actualBody = fetchSpy . calls . allArgs ( ) [ 0 ] [ 1 ] . body ;
397
+ expect ( actualBody ) . toEqual (
398
+ expectedBody ,
399
+ `unit test failed in chat.sendMessage with ${ actualBody } not equal to ${ expectedBody } `
400
+ ) ;
401
+ } ) ;
402
+ it ( 'pass system instruction with wrong role to remote endpoint from GenerativeModel constructor' , async ( ) => {
403
+ const expectedResult = TEST_MODEL_RESPONSE ;
404
+ const fetchResult = Promise . resolve (
405
+ new Response ( JSON . stringify ( expectedResult ) , fetchResponseObj )
406
+ ) ;
407
+ const fetchSpy = spyOn ( global , 'fetch' ) . and . returnValue ( fetchResult ) ;
408
+ const req = 'How are you doing today?' ;
409
+ const model = new GenerativeModel ( {
410
+ model : 'gemini-pro' ,
411
+ project : PROJECT ,
412
+ location : LOCATION ,
413
+ googleAuth : FAKE_GOOGLE_AUTH ,
414
+ systemInstruction : TEST_SYSTEM_INSTRUCTION_WRONG_ROLE ,
415
+ } ) ;
416
+ const chat = model . startChat ( {
417
+ history : TEST_USER_CHAT_MESSAGE ,
418
+ } ) ;
419
+ const expectedBody =
420
+ '{"contents":[{"role":"user","parts":[{"text":"How are you doing today?"}]},{"role":"user","parts":[{"text":"How are you doing today?"}]}],"systemInstruction":{"role":"system","parts":[{"text":"system instruction"}]}}' ;
421
+ await chat . sendMessage ( req ) ;
422
+ // @ts -ignore
423
+ const actualBody = fetchSpy . calls . allArgs ( ) [ 0 ] [ 1 ] . body ;
424
+ expect ( actualBody ) . toEqual (
425
+ expectedBody ,
426
+ `unit test failed in chat.sendMessage with ${ actualBody } not equal to ${ expectedBody } `
427
+ ) ;
428
+ } ) ;
429
+ it ( 'pass system instruction with wrong role to remote endpoint from startChat' , async ( ) => {
430
+ const expectedResult = TEST_MODEL_RESPONSE ;
431
+ const fetchResult = Promise . resolve (
432
+ new Response ( JSON . stringify ( expectedResult ) , fetchResponseObj )
433
+ ) ;
434
+ const fetchSpy = spyOn ( global , 'fetch' ) . and . returnValue ( fetchResult ) ;
435
+ const req = 'How are you doing today?' ;
436
+ const model = new GenerativeModel ( {
437
+ model : 'gemini-pro' ,
438
+ project : PROJECT ,
439
+ location : LOCATION ,
440
+ googleAuth : FAKE_GOOGLE_AUTH ,
441
+ systemInstruction : TEST_SYSTEM_INSTRUCTION_1 ,
442
+ } ) ;
443
+ const chat = model . startChat ( {
444
+ history : TEST_USER_CHAT_MESSAGE ,
445
+ // this is different from constructor
446
+ systemInstruction : TEST_SYSTEM_INSTRUCTION_WRONG_ROLE ,
447
+ } ) ;
448
+ const expectedBody =
449
+ '{"contents":[{"role":"user","parts":[{"text":"How are you doing today?"}]},{"role":"user","parts":[{"text":"How are you doing today?"}]}],"systemInstruction":{"role":"system","parts":[{"text":"system instruction"}]}}' ;
450
+ await chat . sendMessage ( req ) ;
451
+ // @ts -ignore
452
+ const actualBody = fetchSpy . calls . allArgs ( ) [ 0 ] [ 1 ] . body ;
453
+ expect ( actualBody ) . toEqual (
454
+ expectedBody ,
455
+ `unit test failed in chat.sendMessage with ${ actualBody } not equal to ${ expectedBody } `
456
+ ) ;
457
+ } ) ;
342
458
} ) ;
343
459
344
460
describe ( 'GenerativeModelPreview startChat' , ( ) => {
@@ -429,6 +545,118 @@ describe('GenerativeModelPreview startChat', () => {
429
545
const actualBody = fetchSpy . calls . allArgs ( ) [ 0 ] [ 1 ] . body ;
430
546
expect ( actualBody ) . toEqual ( expectedBody ) ;
431
547
} ) ;
548
+ it ( 'pass system instruction to remote endpoint from GenerativeModelPreview constructor' , async ( ) => {
549
+ const expectedResult = TEST_MODEL_RESPONSE ;
550
+ const fetchResult = Promise . resolve (
551
+ new Response ( JSON . stringify ( expectedResult ) , fetchResponseObj )
552
+ ) ;
553
+ const fetchSpy = spyOn ( global , 'fetch' ) . and . returnValue ( fetchResult ) ;
554
+ const req = 'How are you doing today?' ;
555
+ const model = new GenerativeModelPreview ( {
556
+ model : 'gemini-pro' ,
557
+ project : PROJECT ,
558
+ location : LOCATION ,
559
+ googleAuth : FAKE_GOOGLE_AUTH ,
560
+ systemInstruction : TEST_SYSTEM_INSTRUCTION ,
561
+ } ) ;
562
+ const chat = model . startChat ( {
563
+ history : TEST_USER_CHAT_MESSAGE ,
564
+ } ) ;
565
+ const expectedBody =
566
+ '{"contents":[{"role":"user","parts":[{"text":"How are you doing today?"}]},{"role":"user","parts":[{"text":"How are you doing today?"}]}],"systemInstruction":{"role":"system","parts":[{"text":"system instruction"}]}}' ;
567
+ await chat . sendMessage ( req ) ;
568
+ // @ts -ignore
569
+ const actualBody = fetchSpy . calls . allArgs ( ) [ 0 ] [ 1 ] . body ;
570
+ expect ( actualBody ) . toEqual (
571
+ expectedBody ,
572
+ `unit test failed in chat.sendMessage with ${ actualBody } not equal to ${ expectedBody } `
573
+ ) ;
574
+ } ) ;
575
+ it ( 'pass system instruction to remote endpoint from startChat' , async ( ) => {
576
+ const expectedResult = TEST_MODEL_RESPONSE ;
577
+ const fetchResult = Promise . resolve (
578
+ new Response ( JSON . stringify ( expectedResult ) , fetchResponseObj )
579
+ ) ;
580
+ const fetchSpy = spyOn ( global , 'fetch' ) . and . returnValue ( fetchResult ) ;
581
+ const req = 'How are you doing today?' ;
582
+ const model = new GenerativeModelPreview ( {
583
+ model : 'gemini-pro' ,
584
+ project : PROJECT ,
585
+ location : LOCATION ,
586
+ googleAuth : FAKE_GOOGLE_AUTH ,
587
+ systemInstruction : TEST_SYSTEM_INSTRUCTION_1 ,
588
+ } ) ;
589
+ const chat = model . startChat ( {
590
+ history : TEST_USER_CHAT_MESSAGE ,
591
+ // this is different from constructor
592
+ systemInstruction : TEST_SYSTEM_INSTRUCTION ,
593
+ } ) ;
594
+ const expectedBody =
595
+ '{"contents":[{"role":"user","parts":[{"text":"How are you doing today?"}]},{"role":"user","parts":[{"text":"How are you doing today?"}]}],"systemInstruction":{"role":"system","parts":[{"text":"system instruction"}]}}' ;
596
+ await chat . sendMessage ( req ) ;
597
+ // @ts -ignore
598
+ const actualBody = fetchSpy . calls . allArgs ( ) [ 0 ] [ 1 ] . body ;
599
+ expect ( actualBody ) . toEqual (
600
+ expectedBody ,
601
+ `unit test failed in chat.sendMessage with ${ actualBody } not equal to ${ expectedBody } `
602
+ ) ;
603
+ } ) ;
604
+ it ( 'pass system instruction with wrong role to remote endpoint from GenerativeModelPreview constructor' , async ( ) => {
605
+ const expectedResult = TEST_MODEL_RESPONSE ;
606
+ const fetchResult = Promise . resolve (
607
+ new Response ( JSON . stringify ( expectedResult ) , fetchResponseObj )
608
+ ) ;
609
+ const fetchSpy = spyOn ( global , 'fetch' ) . and . returnValue ( fetchResult ) ;
610
+ const req = 'How are you doing today?' ;
611
+ const model = new GenerativeModelPreview ( {
612
+ model : 'gemini-pro' ,
613
+ project : PROJECT ,
614
+ location : LOCATION ,
615
+ googleAuth : FAKE_GOOGLE_AUTH ,
616
+ systemInstruction : TEST_SYSTEM_INSTRUCTION_WRONG_ROLE ,
617
+ } ) ;
618
+ const chat = model . startChat ( {
619
+ history : TEST_USER_CHAT_MESSAGE ,
620
+ } ) ;
621
+ const expectedBody =
622
+ '{"contents":[{"role":"user","parts":[{"text":"How are you doing today?"}]},{"role":"user","parts":[{"text":"How are you doing today?"}]}],"systemInstruction":{"role":"system","parts":[{"text":"system instruction"}]}}' ;
623
+ await chat . sendMessage ( req ) ;
624
+ // @ts -ignore
625
+ const actualBody = fetchSpy . calls . allArgs ( ) [ 0 ] [ 1 ] . body ;
626
+ expect ( actualBody ) . toEqual (
627
+ expectedBody ,
628
+ `unit test failed in chat.sendMessage with ${ actualBody } not equal to ${ expectedBody } `
629
+ ) ;
630
+ } ) ;
631
+ it ( 'pass system instruction with wrong role to remote endpoint from startChat' , async ( ) => {
632
+ const expectedResult = TEST_MODEL_RESPONSE ;
633
+ const fetchResult = Promise . resolve (
634
+ new Response ( JSON . stringify ( expectedResult ) , fetchResponseObj )
635
+ ) ;
636
+ const fetchSpy = spyOn ( global , 'fetch' ) . and . returnValue ( fetchResult ) ;
637
+ const req = 'How are you doing today?' ;
638
+ const model = new GenerativeModelPreview ( {
639
+ model : 'gemini-pro' ,
640
+ project : PROJECT ,
641
+ location : LOCATION ,
642
+ googleAuth : FAKE_GOOGLE_AUTH ,
643
+ systemInstruction : TEST_SYSTEM_INSTRUCTION_1 ,
644
+ } ) ;
645
+ const chat = model . startChat ( {
646
+ history : TEST_USER_CHAT_MESSAGE ,
647
+ // this is different from constructor
648
+ systemInstruction : TEST_SYSTEM_INSTRUCTION_WRONG_ROLE ,
649
+ } ) ;
650
+ const expectedBody =
651
+ '{"contents":[{"role":"user","parts":[{"text":"How are you doing today?"}]},{"role":"user","parts":[{"text":"How are you doing today?"}]}],"systemInstruction":{"role":"system","parts":[{"text":"system instruction"}]}}' ;
652
+ await chat . sendMessage ( req ) ;
653
+ // @ts -ignore
654
+ const actualBody = fetchSpy . calls . allArgs ( ) [ 0 ] [ 1 ] . body ;
655
+ expect ( actualBody ) . toEqual (
656
+ expectedBody ,
657
+ `unit test failed in chat.sendMessage with ${ actualBody } not equal to ${ expectedBody } `
658
+ ) ;
659
+ } ) ;
432
660
} ) ;
433
661
434
662
describe ( 'GenerativeModel generateContent' , ( ) => {
0 commit comments