1
- import { expect } from "chai" ;
2
- import { Modality , getGenerativeModel , getVertexAI } from "../src" ;
3
- import { MODEL_NAME , generationConfig , systemInstruction , safetySettings } from "./constants" ;
4
- import { initializeApp } from "@firebase/app" ;
5
- import { FIREBASE_CONFIG } from "./firebase-config" ;
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { expect } from 'chai' ;
19
+ import { Modality , getGenerativeModel , getVertexAI } from '../src' ;
20
+ import {
21
+ MODEL_NAME ,
22
+ generationConfig ,
23
+ systemInstruction ,
24
+ safetySettings
25
+ } from './constants' ;
26
+ import { initializeApp } from '@firebase/app' ;
27
+ import { FIREBASE_CONFIG } from './firebase-config' ;
6
28
7
29
// Token counts are only expected to differ by at most this number of tokens.
8
30
// Set to 1 for whitespace that is not always present.
9
31
const TOKEN_COUNT_DELTA = 1 ;
10
32
11
33
describe ( 'Generate Content' , ( ) => {
12
-
13
- before ( ( ) => initializeApp ( FIREBASE_CONFIG ) )
34
+ before ( ( ) => initializeApp ( FIREBASE_CONFIG ) ) ;
14
35
15
36
it ( 'generateContent' , async ( ) => {
16
- const vertexAI = getVertexAI ( ) ;
17
- const model = getGenerativeModel (
18
- vertexAI ,
19
- {
20
- model : MODEL_NAME ,
21
- generationConfig,
22
- systemInstruction,
23
- safetySettings
24
- }
25
- ) ;
37
+ const vertexAI = getVertexAI ( ) ;
38
+ const model = getGenerativeModel ( vertexAI , {
39
+ model : MODEL_NAME ,
40
+ generationConfig,
41
+ systemInstruction,
42
+ safetySettings
43
+ } ) ;
26
44
27
- const result = await model . generateContent ( "Where is Google headquarters located? Answer with the city name only." ) ;
45
+ const result = await model . generateContent (
46
+ 'Where is Google headquarters located? Answer with the city name only.'
47
+ ) ;
28
48
const response = result . response ;
29
-
49
+
30
50
const trimmedText = response . text ( ) . trim ( ) ;
31
51
expect ( trimmedText ) . to . equal ( 'Mountain View' ) ;
32
52
33
53
expect ( response . usageMetadata ) . to . not . be . null ;
34
- expect ( response . usageMetadata ! . promptTokenCount ) . to . be . closeTo ( 21 , TOKEN_COUNT_DELTA ) ;
35
- expect ( response . usageMetadata ! . candidatesTokenCount ) . to . be . closeTo ( 4 , TOKEN_COUNT_DELTA ) ;
36
- expect ( response . usageMetadata ! . totalTokenCount ) . to . be . closeTo ( 25 , TOKEN_COUNT_DELTA * 2 ) ;
54
+ expect ( response . usageMetadata ! . promptTokenCount ) . to . be . closeTo (
55
+ 21 ,
56
+ TOKEN_COUNT_DELTA
57
+ ) ;
58
+ expect ( response . usageMetadata ! . candidatesTokenCount ) . to . be . closeTo (
59
+ 4 ,
60
+ TOKEN_COUNT_DELTA
61
+ ) ;
62
+ expect ( response . usageMetadata ! . totalTokenCount ) . to . be . closeTo (
63
+ 25 ,
64
+ TOKEN_COUNT_DELTA * 2
65
+ ) ;
37
66
expect ( response . usageMetadata ! . promptTokensDetails ) . to . not . be . null ;
38
67
expect ( response . usageMetadata ! . promptTokensDetails ! . length ) . to . equal ( 1 ) ;
39
- expect ( response . usageMetadata ! . promptTokensDetails ! [ 0 ] . modality ) . to . equal ( Modality . TEXT ) ;
40
- expect ( response . usageMetadata ! . promptTokensDetails ! [ 0 ] . tokenCount ) . to . equal ( 21 ) ;
68
+ expect ( response . usageMetadata ! . promptTokensDetails ! [ 0 ] . modality ) . to . equal (
69
+ Modality . TEXT
70
+ ) ;
71
+ expect ( response . usageMetadata ! . promptTokensDetails ! [ 0 ] . tokenCount ) . to . equal (
72
+ 21
73
+ ) ;
41
74
expect ( response . usageMetadata ! . candidatesTokensDetails ) . to . not . be . null ;
42
75
expect ( response . usageMetadata ! . candidatesTokensDetails ! . length ) . to . equal ( 1 ) ;
43
- expect ( response . usageMetadata ! . candidatesTokensDetails ! [ 0 ] . modality ) . to . equal ( Modality . TEXT ) ;
44
- expect ( response . usageMetadata ! . candidatesTokensDetails ! [ 0 ] . tokenCount ) . to . equal ( 4 ) ;
76
+ expect (
77
+ response . usageMetadata ! . candidatesTokensDetails ! [ 0 ] . modality
78
+ ) . to . equal ( Modality . TEXT ) ;
79
+ expect (
80
+ response . usageMetadata ! . candidatesTokensDetails ! [ 0 ] . tokenCount
81
+ ) . to . equal ( 4 ) ;
45
82
} ) ;
46
83
// TODO (dlarocque): Test generateContentStream
47
- } ) ;
84
+ } ) ;
0 commit comments