mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-13 05:12:55 -07:00
Co-authored-by: Jack Wotherspoon <jackwoth@google.com>
This commit is contained in:
@@ -145,4 +145,40 @@ describe('calculateRequestTokenCount', () => {
|
|||||||
|
|
||||||
expect(count).toBe(3000);
|
expect(count).toBe(3000);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should use countTokens API for PDF requests', async () => {
|
||||||
|
vi.mocked(mockContentGenerator.countTokens).mockResolvedValue({
|
||||||
|
totalTokens: 5160,
|
||||||
|
});
|
||||||
|
const request = [
|
||||||
|
{ inlineData: { mimeType: 'application/pdf', data: 'pdf_data' } },
|
||||||
|
];
|
||||||
|
|
||||||
|
const count = await calculateRequestTokenCount(
|
||||||
|
request,
|
||||||
|
mockContentGenerator,
|
||||||
|
model,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(count).toBe(5160);
|
||||||
|
expect(mockContentGenerator.countTokens).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should use fixed estimate for PDFs in fallback', async () => {
|
||||||
|
vi.mocked(mockContentGenerator.countTokens).mockRejectedValue(
|
||||||
|
new Error('API error'),
|
||||||
|
);
|
||||||
|
const request = [
|
||||||
|
{ inlineData: { mimeType: 'application/pdf', data: 'large_pdf_data' } },
|
||||||
|
];
|
||||||
|
|
||||||
|
const count = await calculateRequestTokenCount(
|
||||||
|
request,
|
||||||
|
mockContentGenerator,
|
||||||
|
model,
|
||||||
|
);
|
||||||
|
|
||||||
|
// PDF estimate: 25800 tokens (~100 pages at 258 tokens/page)
|
||||||
|
expect(count).toBe(25800);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ const ASCII_TOKENS_PER_CHAR = 0.25;
|
|||||||
const NON_ASCII_TOKENS_PER_CHAR = 1.3;
|
const NON_ASCII_TOKENS_PER_CHAR = 1.3;
|
||||||
// Fixed token estimate for images
|
// Fixed token estimate for images
|
||||||
const IMAGE_TOKEN_ESTIMATE = 3000;
|
const IMAGE_TOKEN_ESTIMATE = 3000;
|
||||||
|
// Fixed token estimate for PDFs (~100 pages at 258 tokens/page)
|
||||||
|
// See: https://ai.google.dev/gemini-api/docs/document-processing
|
||||||
|
const PDF_TOKEN_ESTIMATE = 25800;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Estimates token count for parts synchronously using a heuristic.
|
* Estimates token count for parts synchronously using a heuristic.
|
||||||
@@ -34,15 +37,19 @@ export function estimateTokenCountSync(parts: Part[]): number {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// For images, we use a fixed safe estimate (3,000 tokens) covering
|
// For images and PDFs, we use fixed safe estimates:
|
||||||
// up to 4K resolution on Gemini 3.
|
// - Images: 3,000 tokens (covers up to 4K resolution on Gemini 3)
|
||||||
|
// - PDFs: 25,800 tokens (~100 pages at 258 tokens/page)
|
||||||
// See: https://ai.google.dev/gemini-api/docs/vision#token_counting
|
// See: https://ai.google.dev/gemini-api/docs/vision#token_counting
|
||||||
|
// See: https://ai.google.dev/gemini-api/docs/document-processing
|
||||||
const inlineData = 'inlineData' in part ? part.inlineData : undefined;
|
const inlineData = 'inlineData' in part ? part.inlineData : undefined;
|
||||||
const fileData = 'fileData' in part ? part.fileData : undefined;
|
const fileData = 'fileData' in part ? part.fileData : undefined;
|
||||||
const mimeType = inlineData?.mimeType || fileData?.mimeType;
|
const mimeType = inlineData?.mimeType || fileData?.mimeType;
|
||||||
|
|
||||||
if (mimeType?.startsWith('image/')) {
|
if (mimeType?.startsWith('image/')) {
|
||||||
totalTokens += IMAGE_TOKEN_ESTIMATE;
|
totalTokens += IMAGE_TOKEN_ESTIMATE;
|
||||||
|
} else if (mimeType?.startsWith('application/pdf')) {
|
||||||
|
totalTokens += PDF_TOKEN_ESTIMATE;
|
||||||
} else {
|
} else {
|
||||||
// For other non-text parts (functionCall, functionResponse, etc.),
|
// For other non-text parts (functionCall, functionResponse, etc.),
|
||||||
// we fallback to the JSON string length heuristic.
|
// we fallback to the JSON string length heuristic.
|
||||||
|
|||||||
Reference in New Issue
Block a user