Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/ai/integration/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import {
} from '../src';
import { testConfigs } from './constants';

describe('Chat Session', () => {
describe('Chat Session', function () {
this.timeout(20_000);
testConfigs.forEach(testConfig => {
describe(`${testConfig.toString()}`, () => {
const commonGenerationConfig: GenerationConfig = {
Expand Down
8 changes: 7 additions & 1 deletion packages/ai/integration/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ const backendNames: Map<BackendType, string> = new Map([
[BackendType.VERTEX_AI, 'Vertex AI']
]);

const modelNames: readonly string[] = ['gemini-2.0-flash', 'gemini-2.5-flash'];
const modelNames: readonly string[] = [
'gemini-2.0-flash-001',
'gemini-2.0-flash-lite-001',
'gemini-2.5-flash',
'gemini-2.5-flash-lite',
'gemini-3-pro-preview'
];

// The Live API requires a different set of models, and they're different for each backend.
const liveModelNames: Map<BackendType, string[]> = new Map([
Expand Down
41 changes: 32 additions & 9 deletions packages/ai/integration/count-tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,18 @@ describe('Count Tokens', () => {
};
const response = await model.countTokens([imagePart]);

let expectedImageTokens: number;
if (testConfig.model === 'gemini-3-pro-preview') {
expectedImageTokens =
testConfig.ai.backend.backendType === BackendType.GOOGLE_AI
? 1089
: 1120;
} else {
expectedImageTokens = 258;
}
Comment on lines +121 to +129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic for determining expectedImageTokens is duplicated in a few places in this file (e.g., in the 'text, image, and audio input' test). To improve maintainability, consider extracting it into a shared helper function. This would centralize the logic and make future updates easier.


if (testConfig.ai.backend.backendType === BackendType.GOOGLE_AI) {
const expectedImageTokens = 259;
expect(response.totalTokens).to.equal(expectedImageTokens);
expect(response.totalTokens).to.equal(expectedImageTokens + 1); // There will be 1 unexpected text token
expect(response.totalBillableCharacters).to.be.undefined; // Incorrect behavior
expect(response.promptTokensDetails!.length).to.equal(2);
expect(response.promptTokensDetails![0]).to.deep.equal({
Expand All @@ -129,19 +138,18 @@ describe('Count Tokens', () => {
});
expect(response.promptTokensDetails![1]).to.deep.equal({
modality: Modality.IMAGE,
tokenCount: 258
tokenCount: expectedImageTokens
});
} else if (
testConfig.ai.backend.backendType === BackendType.VERTEX_AI
) {
const expectedImageTokens = 258;
expect(response.totalTokens).to.equal(expectedImageTokens);
expect(response.totalBillableCharacters).to.be.undefined; // Incorrect behavior
expect(response.promptTokensDetails!.length).to.equal(1);
// Note: No text tokens are present for Vertex AI with image-only input.
expect(response.promptTokensDetails![0]).to.deep.equal({
modality: Modality.IMAGE,
tokenCount: 258
tokenCount: expectedImageTokens
});
expect(response.promptTokensDetails![0].tokenCount).to.equal(
expectedImageTokens
Expand Down Expand Up @@ -220,13 +228,23 @@ describe('Count Tokens', () => {
expect(response.promptTokensDetails).to.exist;
expect(response.promptTokensDetails!.length).to.equal(3);

let expectedImageTokenCount;
if (testConfig.model === 'gemini-3-pro-preview') {
expectedImageTokenCount =
testConfig.ai.backend.backendType === BackendType.GOOGLE_AI
? 1089
: 1120;
} else {
expectedImageTokenCount = 258;
}

expect(imageDetails).to.deep.equal({
modality: Modality.IMAGE,
tokenCount: 258
tokenCount: expectedImageTokenCount
});

if (testConfig.ai.backend.backendType === BackendType.GOOGLE_AI) {
expect(response.totalTokens).to.equal(267);
expect(response.totalTokens).to.equal(expectedImageTokenCount + 9);
expect(response.totalBillableCharacters).to.be.undefined;
expect(textDetails).to.deep.equal({
modality: Modality.TEXT,
Expand All @@ -239,7 +257,7 @@ describe('Count Tokens', () => {
} else if (
testConfig.ai.backend.backendType === BackendType.VERTEX_AI
) {
expect(response.totalTokens).to.equal(261);
expect(response.totalTokens).to.equal(expectedImageTokenCount + 3);
expect(textDetails).to.deep.equal({
modality: Modality.TEXT,
tokenCount: 3
Expand Down Expand Up @@ -269,7 +287,12 @@ describe('Count Tokens', () => {

const response = await model.countTokens([filePart]);

const expectedFileTokens = 258;
let expectedFileTokens: number;
if (testConfig.model === 'gemini-3-pro-preview') {
expectedFileTokens = 1120;
} else {
expectedFileTokens = 258;
}
expect(response.totalTokens).to.equal(expectedFileTokens);
expect(response.totalBillableCharacters).to.be.undefined;
expect(response.promptTokensDetails).to.exist;
Expand Down
15 changes: 9 additions & 6 deletions packages/ai/integration/generate-content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import { testConfigs } from './constants';

describe('Generate Content', function () {
this.timeout(20_000);
this.timeout(90_000); // gemini 3 requests take a long time, especially when using google search and url context.
testConfigs.forEach(testConfig => {
describe(`${testConfig.toString()}`, () => {
const commonGenerationConfig: GenerationConfig = {
Expand Down Expand Up @@ -175,8 +175,9 @@ describe('Generate Content', function () {
describe('URL Context', async () => {
// URL Context is not supported in Google AI for gemini-2.0-flash
if (
testConfig.ai.backend.backendType === BackendType.GOOGLE_AI &&
testConfig.model === 'gemini-2.0-flash'
['gemini-2.0-flash-001', 'gemini-2.0-flash-lite-001'].includes(
testConfig.model
) // Models that don't support URL Context
) {
Comment on lines 177 to 181
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic in this if condition has been updated to be model-specific, rather than backend-specific. However, the comment on the preceding line (176) // URL Context is not supported in Google AI for gemini-2.0-flash is now outdated and potentially misleading. Please consider updating or removing it to align with the new implementation.

return;
}
Expand Down Expand Up @@ -232,9 +233,7 @@ describe('Generate Content', function () {
const urlContextMetadata =
response.candidates?.[0].urlContextMetadata;
const groundingMetadata = response.candidates?.[0].groundingMetadata;
expect(trimmedText).to.contain(
'hypermedia information retrieval initiative'
);
expect(trimmedText.length).to.be.greaterThan(0);
expect(urlContextMetadata?.urlMetadata).to.exist;
expect(
urlContextMetadata?.urlMetadata.length
Expand Down Expand Up @@ -302,6 +301,10 @@ describe('Generate Content', function () {
});

it('generateContent: code execution', async () => {
if (testConfig.model === 'gemini-2.0-flash-lite-001') {
// This model does not support code execution
return;
}
const model = getGenerativeModel(testConfig.ai, {
model: testConfig.model,
generationConfig: commonGenerationConfig,
Expand Down
Loading