Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(tokenization): support alternative model name, `gpt-35*` instead of `gpt-3.5*` (#869)

Marc Klingen374dbf971689
diff --git a/prisma/migrations/20240111152124_add_gpt_35_pricing/migration.sql b/prisma/migrations/20240111152124_add_gpt_35_pricing/migration.sql
new file mode 100644
index 0000000000..45e69c4cba
--- /dev/null
+++ b/prisma/migrations/20240111152124_add_gpt_35_pricing/migration.sql
@@ -0,0 +1,16 @@
+-- Migration to add gpt-35 spelling
+
+INSERT INTO pricings (
+ id,
+ model_name,
+ pricing_unit,
+ price,
+ currency,
+ token_type
+)
+VALUES
+ ('clqqpc2pr000008l3hvy63gxy1','gpt-35-turbo-1106', 'PER_1000_TOKENS', 0.001, 'USD', 'PROMPT'),
+ ('clqqpcb6d000208l3atrfbmou1','gpt-35-turbo-1106', 'PER_1000_TOKENS', 0.002, 'USD', 'COMPLETION'),
+ ('clqqpdh45000008lfgrnx76cv1','gpt-35-turbo-instruct', 'PER_1000_TOKENS', 0.0015, 'USD', 'PROMPT'),
+ ('clqqpdjya000108lf3s4b4c4m1','gpt-35-turbo-instruct', 'PER_1000_TOKENS', 0.002, 'USD', 'COMPLETION')
+ON CONFLICT (id) DO NOTHING;
\ No newline at end of file
diff --git a/src/__tests__/usage.servertest.ts b/src/__tests__/usage.servertest.ts
index 14c00e5053..45f599fba6 100644
--- a/src/__tests__/usage.servertest.ts
+++ b/src/__tests__/usage.servertest.ts
@@ -4,11 +4,13 @@ describe("Token Count Functions", () => {
describe("token count for strings", () => {
[
{ model: "gpt-3.5", tokens: 114 },
+ { model: "gpt-35", tokens: 114 },
{ model: "gpt-4-1106-preview", tokens: 114 },
{ model: "gpt-4-vision-preview", tokens: 114 },
{ model: "claude", tokens: 118 },
{ model: "claude-instant-1.2", tokens: 118 },
{ model: "gpt-3.5-turbo-1106", tokens: 114 },
+ { model: "gpt-35-turbo-1106", tokens: 114 },
].forEach(({ model, tokens }) => {
it(`should return token count ${tokens} for ${model}`, () => {
const result = tokenCount({
@@ -71,6 +73,7 @@ describe("Token Count Functions", () => {
[
{ model: "gpt-4", tokens: 44 },
{ model: "gpt-3.5-turbo-16k-0613", tokens: 44 },
+ { model: "gpt-35-turbo-16k-0613", tokens: 44 },
{ model: "claude-instant-1.2", tokens: 48 },
].forEach(({ model, tokens }) => {
it(`should return token count ${tokens} for ${model}`, () => {
diff --git a/src/features/ingest/lib/usage.ts b/src/features/ingest/lib/usage.ts
index cdd7e869fb..8921750a4e 100644
--- a/src/features/ingest/lib/usage.ts
+++ b/src/features/ingest/lib/usage.ts
@@ -24,29 +24,30 @@ export function tokenCount(p: {
model: string;
text: unknown;
}): number | undefined {
+ const model = cleanModelString(p.model);
if (
p.text === null ||
p.text === undefined ||
(Array.isArray(p.text) && p.text.length === 0)
) {
return undefined;
- } else if (isOpenAiModel(p.model)) {
+ } else if (isOpenAiModel(model)) {
return isChatMessageArray(p.text)
? openAiChatTokenCount({
- model: p.model,
+ model: model,
messages: p.text,
})
: isString(p.text)
- ? openAiStringTokenCount({ model: p.model, text: p.text })
+ ? openAiStringTokenCount({ model: model, text: p.text })
: openAiStringTokenCount({
- model: p.model,
+ model: model,
text: JSON.stringify(p.text),
});
- } else if (isClaudeModel(p.model)) {
+ } else if (isClaudeModel(model)) {
return isString(p.text)
- ? claudeStringTokenCount({ model: p.model, text: p.text })
+ ? claudeStringTokenCount({ model: model, text: p.text })
: claudeStringTokenCount({
- model: p.model,
+ model: model,
text: JSON.stringify(p.text),
});
} else {
@@ -223,7 +224,8 @@ export function calculateTokenCost(
completionTokens: Decimal;
},
): Decimal | undefined {
- const pricing = pricingList.filter((p) => p.modelName === input.model);
+ const model = cleanModelString(input.model);
+ const pricing = pricingList.filter((p) => p.modelName === model);
if (pricing.length === 0) {
console.log("no pricing found for model", input.model);
@@ -298,3 +300,6 @@ const calculateValue = (
return undefined;
}
};
+
+const cleanModelString = (model: string) =>
+ model.toLowerCase().replaceAll("gpt-35", "gpt-3.5");