Skip to content

Commit a39f781

Browse files
committed
Adjust for combination of system message and funcs
1 parent fbee4ed commit a39f781

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ export function promptTokensEstimate({ messages, functions }: { messages: Messag
2020
if (functions) {
2121
tokens += functionsTokensEstimate(functions as any as FunctionDef[]);
2222
}
23+
24+
// If there's a system message _and_ functions are present, subtract three tokens
25+
if (functions && messages.find(m => m.role === "system")) {
26+
tokens -= 3;
27+
}
28+
2329
return tokens;
2430
}
2531

tests/token-counts.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ const TEST_CASES: Example[] = [
148148
{ role: "assistant", content: "", function_call: { name: "do_stuff", arguments: `{"foo":"bar", "baz":\n\n 1.5}` } },
149149
],
150150
tokens: 25,
151+
},
152+
{
153+
messages: [
154+
{ "role": "system", "content": "Hello" },
155+
{ "role": "user", "content": "Hi there" },
156+
],
157+
functions: [
158+
{
159+
"name": "do_stuff",
160+
"parameters": { "type": "object", "properties": {} }
161+
}
162+
],
163+
tokens: 35,
151164
}
152165
];
153166

0 commit comments

Comments
 (0)