-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for new formats of content #21
Comments
I guess you could add a lib that extracts the size from the images like https://www.npmjs.com/package/image-size I'm not quite sure if you can guess what detail level is chosen when you do not send it (when it's in Funny there are two description of costs in the official docs. The one that you posted and this: https://platform.openai.com/docs/guides/vision/low-or-high-fidelity-image-understanding
I'm not quite sure why two time 65 should be 129 but hey 🤷♂️😁 |
function calculateHighDetailTokens(width: number, height: number): number {
// First, check if the image needs to be scaled to fit within the 2048 x 2048 size limit
if (width > 2048 || height > 2048) {
const aspectRatio = width / height;
if (width > height) {
width = 2048;
height = Math.round(2048 / aspectRatio);
} else {
height = 2048;
width = Math.round(2048 * aspectRatio);
}
}
// Next, scale the image so that the shortest side is 768px
const minSideLength = 768;
const currentMinSide = Math.min(width, height);
if (currentMinSide > minSideLength) {
const scaleFactor = minSideLength / currentMinSide;
width = Math.round(width * scaleFactor);
height = Math.round(height * scaleFactor);
}
// Calculate how many 512px tiles the image is composed of
const tilesWide = Math.ceil(width / 512);
const tilesHigh = Math.ceil(height / 512);
const totalTiles = tilesWide * tilesHigh;
// The token cost for each tile is 170, with an additional 85 tokens added at the end
const totalTokens = totalTiles * 170 + 85;
return totalTokens;
}
// Example usage
console.log(calculateHighDetailTokens(1024, 1024)); // Should output 765
console.log(calculateHighDetailTokens(2048, 4096)); // Should output 1105 In our project, we actually only use high mode, and the front-end knows the image width and height when uploading images. So I asked gpt to write a code to calculate the code in high mode. This temporarily solves the problem of image message token calculation.😂 |
openai supports passing images and text at the same time, but the token calculation rules for images depend on the image size and processing mode. So I think we need to artificially supplement the two parameters of the image size and processing mode to calculate the token of the image.
The text was updated successfully, but these errors were encountered: