-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from paradite/feat/token-counter
Feat/token counter
- Loading branch information
Showing
11 changed files
with
753 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// jest.config.ts | ||
import type { JestConfigWithTsJest } from 'ts-jest'; | ||
|
||
// https://kulshekhar.github.io/ts-jest/docs/guides/esm-support | ||
|
||
const jestConfig: JestConfigWithTsJest = { | ||
// [...] | ||
extensionsToTreatAsEsm: ['.ts'], | ||
moduleNameMapper: { | ||
'^(\\.{1,2}/.*)\\.js$': '$1', | ||
}, | ||
transform: { | ||
// '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest` | ||
// '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest` | ||
'^.+\\.tsx?$': [ | ||
'ts-jest', | ||
{ | ||
useESM: true, | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
export default jestConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,3 @@ | ||
export enum ModelEnum { | ||
'gpt-4' = 'gpt-4', | ||
'gpt-4o' = 'gpt-4o', | ||
'gpt-4o-mini' = 'gpt-4o-mini', | ||
'claude-3-5-sonnet-20240620' = 'claude-3-5-sonnet-20240620', | ||
} | ||
|
||
export enum NonModelEnum { | ||
'chatgpt' = 'chatgpt', | ||
} | ||
|
||
export const AllModels = Object.values(ModelEnum); | ||
|
||
export const AllModelLikes = [...AllModels, ...Object.values(NonModelEnum)]; | ||
|
||
export type ModelLike = ModelEnum | NonModelEnum; | ||
|
||
export const AI_PROVIDERS = { | ||
OPENAI: 'openai', | ||
ANTHROPIC: 'anthropic', | ||
} as const; | ||
|
||
export type AI_PROVIDER_TYPE = (typeof AI_PROVIDERS)[keyof typeof AI_PROVIDERS]; | ||
|
||
export type ModelInfo = { | ||
name: string; | ||
provider: AI_PROVIDER_TYPE; | ||
contextWindowTokenLimit: number; | ||
outputTokenLimit: number; | ||
pricePerMillionInputTokens: number | null; | ||
pricePerMillionOutputTokens: number | null; | ||
}; | ||
|
||
export const ModelInfoMap: Record<ModelLike, ModelInfo> = { | ||
[ModelEnum['gpt-4']]: { | ||
name: 'GPT-4', | ||
provider: AI_PROVIDERS.OPENAI, | ||
contextWindowTokenLimit: 128000, | ||
outputTokenLimit: 4096, | ||
pricePerMillionInputTokens: 30, | ||
pricePerMillionOutputTokens: 60, | ||
}, | ||
[ModelEnum['gpt-4o']]: { | ||
name: 'GPT-4o', | ||
provider: AI_PROVIDERS.OPENAI, | ||
contextWindowTokenLimit: 128000, | ||
outputTokenLimit: 4096, | ||
pricePerMillionInputTokens: 5, | ||
pricePerMillionOutputTokens: 15, | ||
}, | ||
[ModelEnum['gpt-4o-mini']]: { | ||
name: 'GPT-4o mini', | ||
provider: AI_PROVIDERS.OPENAI, | ||
contextWindowTokenLimit: 128000, | ||
outputTokenLimit: 4096, | ||
pricePerMillionInputTokens: 0.15, | ||
pricePerMillionOutputTokens: 0.6, | ||
}, | ||
[ModelEnum['claude-3-5-sonnet-20240620']]: { | ||
name: 'Claude 3.5 Sonnet', | ||
provider: AI_PROVIDERS.ANTHROPIC, | ||
contextWindowTokenLimit: 200000, | ||
outputTokenLimit: 4096, | ||
pricePerMillionInputTokens: 3, | ||
pricePerMillionOutputTokens: 15, | ||
}, | ||
[NonModelEnum['chatgpt']]: { | ||
name: 'ChatGPT', | ||
provider: AI_PROVIDERS.OPENAI, | ||
contextWindowTokenLimit: 4096, | ||
outputTokenLimit: 4096, | ||
pricePerMillionInputTokens: null, | ||
pricePerMillionOutputTokens: null, | ||
}, | ||
}; | ||
export * from './model'; | ||
export * from './provider'; | ||
export * from './modelInfo'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export enum ModelEnum { | ||
'gpt-4' = 'gpt-4', | ||
'gpt-4o' = 'gpt-4o', | ||
'gpt-4o-mini' = 'gpt-4o-mini', | ||
'claude-3-5-sonnet-20240620' = 'claude-3-5-sonnet-20240620', | ||
} | ||
|
||
export enum NonModelEnum { | ||
'chatgpt' = 'chatgpt', | ||
} | ||
|
||
export const AllModels = Object.values(ModelEnum); | ||
|
||
export const AllModelLikes = [...AllModels, ...Object.values(NonModelEnum)]; | ||
|
||
export type ModelLike = ModelEnum | NonModelEnum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { ModelEnum, ModelLike, NonModelEnum } from './model'; | ||
import { AI_PROVIDER_TYPE, AI_PROVIDERS } from './provider'; | ||
|
||
export type ModelInfo = { | ||
name: string; | ||
provider: AI_PROVIDER_TYPE; | ||
contextWindowTokenLimit: number; | ||
outputTokenLimit: number; | ||
pricePerMillionInputTokens: number | null; | ||
pricePerMillionOutputTokens: number | null; | ||
tokenizerId: string | null; | ||
}; | ||
|
||
export const ModelInfoMap: Record<ModelLike, ModelInfo> = { | ||
[ModelEnum['gpt-4']]: { | ||
name: 'GPT-4', | ||
provider: AI_PROVIDERS.OPENAI, | ||
contextWindowTokenLimit: 128000, | ||
outputTokenLimit: 4096, | ||
pricePerMillionInputTokens: 30, | ||
pricePerMillionOutputTokens: 60, | ||
tokenizerId: 'Xenova/gpt-4', | ||
}, | ||
[ModelEnum['gpt-4o']]: { | ||
name: 'GPT-4o', | ||
provider: AI_PROVIDERS.OPENAI, | ||
contextWindowTokenLimit: 128000, | ||
outputTokenLimit: 4096, | ||
pricePerMillionInputTokens: 5, | ||
pricePerMillionOutputTokens: 15, | ||
tokenizerId: 'Xenova/gpt-4o', | ||
}, | ||
[ModelEnum['gpt-4o-mini']]: { | ||
name: 'GPT-4o mini', | ||
provider: AI_PROVIDERS.OPENAI, | ||
contextWindowTokenLimit: 128000, | ||
outputTokenLimit: 4096, | ||
pricePerMillionInputTokens: 0.15, | ||
pricePerMillionOutputTokens: 0.6, | ||
tokenizerId: 'Xenova/gpt-4o', | ||
}, | ||
[ModelEnum['claude-3-5-sonnet-20240620']]: { | ||
name: 'Claude 3.5 Sonnet', | ||
provider: AI_PROVIDERS.ANTHROPIC, | ||
contextWindowTokenLimit: 200000, | ||
outputTokenLimit: 4096, | ||
pricePerMillionInputTokens: 3, | ||
pricePerMillionOutputTokens: 15, | ||
tokenizerId: 'Xenova/claude-tokenizer', | ||
}, | ||
[NonModelEnum['chatgpt']]: { | ||
name: 'ChatGPT', | ||
provider: AI_PROVIDERS.OPENAI, | ||
contextWindowTokenLimit: 4096, | ||
outputTokenLimit: 4096, | ||
pricePerMillionInputTokens: null, | ||
pricePerMillionOutputTokens: null, | ||
tokenizerId: null, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const AI_PROVIDERS = { | ||
OPENAI: 'openai', | ||
ANTHROPIC: 'anthropic', | ||
} as const; | ||
|
||
export type AI_PROVIDER_TYPE = (typeof AI_PROVIDERS)[keyof typeof AI_PROVIDERS]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { AllModels, ModelInfoMap } from '../src'; | ||
import { AutoTokenizer } from '@xenova/transformers'; | ||
|
||
describe('llm', () => { | ||
it('tokenizer works', async () => { | ||
const testSentence = | ||
"Many words map to one token, but some don't: indivisible."; | ||
const results: string[] = []; | ||
for (let i = 0; i < AllModels.length; i++) { | ||
const model = AllModels[i]; | ||
if (ModelInfoMap[model].tokenizerId) { | ||
const tokenizer = await AutoTokenizer.from_pretrained( | ||
ModelInfoMap[model].tokenizerId | ||
); | ||
const tokens = tokenizer.encode(testSentence); | ||
expect(tokens.length).toBeGreaterThanOrEqual(14); | ||
expect(tokens.length).toBeLessThanOrEqual(17); | ||
results.push(`${model}: ${tokens.length}`); | ||
} | ||
} | ||
console.log(`Test sentence: ${testSentence}\n${results.join('\n')}`); | ||
}, 5000); | ||
}); |
Oops, something went wrong.