|
| 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 { Content, GenerationConfig, HarmBlockMethod, HarmBlockThreshold, HarmCategory, Modality, SafetySetting, getGenerativeModel } from '../src'; |
| 20 | +import { |
| 21 | + testConfigs |
| 22 | +} from './constants'; |
| 23 | + |
| 24 | +// Token counts are only expected to differ by at most this number of tokens. |
| 25 | +// Set to 1 for whitespace that is not always present. |
| 26 | +const TOKEN_COUNT_DELTA = 1; |
| 27 | + |
| 28 | +describe('Generate Content', () => { |
| 29 | + testConfigs.forEach(testConfig => { |
| 30 | + describe(`${testConfig.toString()}`, () => { |
| 31 | + |
| 32 | + it('generateContent', async () => { |
| 33 | + const generationConfig: GenerationConfig = { |
| 34 | + temperature: 0, |
| 35 | + topP: 0, |
| 36 | + responseMimeType: 'text/plain' |
| 37 | + }; |
| 38 | + |
| 39 | + const safetySettings: SafetySetting[] = [ |
| 40 | + { |
| 41 | + category: HarmCategory.HARM_CATEGORY_HARASSMENT, |
| 42 | + threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE, |
| 43 | + method: HarmBlockMethod.PROBABILITY |
| 44 | + }, |
| 45 | + { |
| 46 | + category: HarmCategory.HARM_CATEGORY_HATE_SPEECH, |
| 47 | + threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE, |
| 48 | + method: HarmBlockMethod.SEVERITY |
| 49 | + }, |
| 50 | + { |
| 51 | + category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, |
| 52 | + threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE |
| 53 | + }, |
| 54 | + { |
| 55 | + category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, |
| 56 | + threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE |
| 57 | + } |
| 58 | + ]; |
| 59 | + |
| 60 | + const systemInstruction: Content = { |
| 61 | + role: 'system', |
| 62 | + parts: [ |
| 63 | + { |
| 64 | + text: 'You are a friendly and helpful assistant.' |
| 65 | + } |
| 66 | + ] |
| 67 | + }; |
| 68 | + |
| 69 | + const model = getGenerativeModel(testConfig.ai, { |
| 70 | + model: testConfig.model, |
| 71 | + generationConfig, |
| 72 | + safetySettings, |
| 73 | + systemInstruction |
| 74 | + }); |
| 75 | + |
| 76 | + const result = await model.generateContent( |
| 77 | + 'Where is Google headquarters located? Answer with the city name only.' |
| 78 | + ); |
| 79 | + const response = result.response; |
| 80 | + |
| 81 | + const trimmedText = response.text().trim(); |
| 82 | + expect(trimmedText).to.equal('Mountain View'); |
| 83 | + |
| 84 | + expect(response.usageMetadata).to.not.be.null; |
| 85 | + expect(response.usageMetadata!.promptTokenCount).to.be.closeTo( |
| 86 | + 21, |
| 87 | + TOKEN_COUNT_DELTA |
| 88 | + ); |
| 89 | + expect(response.usageMetadata!.candidatesTokenCount).to.be.closeTo( |
| 90 | + 4, |
| 91 | + TOKEN_COUNT_DELTA |
| 92 | + ); |
| 93 | + expect(response.usageMetadata!.totalTokenCount).to.be.closeTo( |
| 94 | + 25, |
| 95 | + TOKEN_COUNT_DELTA * 2 |
| 96 | + ); |
| 97 | + expect(response.usageMetadata!.promptTokensDetails).to.not.be.null; |
| 98 | + expect(response.usageMetadata!.promptTokensDetails!.length).to.equal(1); |
| 99 | + expect(response.usageMetadata!.promptTokensDetails![0].modality).to.equal( |
| 100 | + Modality.TEXT |
| 101 | + ); |
| 102 | + expect(response.usageMetadata!.promptTokensDetails![0].tokenCount).to.equal( |
| 103 | + 21 |
| 104 | + ); |
| 105 | + expect(response.usageMetadata!.candidatesTokensDetails).to.not.be.null; |
| 106 | + expect(response.usageMetadata!.candidatesTokensDetails!.length).to.equal(1); |
| 107 | + expect( |
| 108 | + response.usageMetadata!.candidatesTokensDetails![0].modality |
| 109 | + ).to.equal(Modality.TEXT); |
| 110 | + expect( |
| 111 | + response.usageMetadata!.candidatesTokensDetails![0].tokenCount |
| 112 | + ).to.be.closeTo(4, TOKEN_COUNT_DELTA); |
| 113 | + }); |
| 114 | + // TODO (dlarocque): Test generateContentStream |
| 115 | + }) |
| 116 | + }) |
| 117 | +}); |
0 commit comments