Skip to content

Commit 1440a05

Browse files
authoredMay 31, 2024
Remove the model field from the countTokens request payload (#162)
The `model` isn't a required part of the `countTokens` request, as it's already part of the request URL.
1 parent 83ec4ac commit 1440a05

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed
 

‎.changeset/fifty-masks-leave.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@google/generative-ai": patch
3+
---
4+
5+
Removed the `model` field from the internally formatted payload of `countToken` requests as it was unnecessary.

‎packages/main/src/methods/count-tokens.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export async function countTokens(
3333
Task.COUNT_TOKENS,
3434
apiKey,
3535
false,
36-
JSON.stringify({ ...params, model }),
36+
JSON.stringify(params),
3737
requestOptions,
3838
);
3939
return response.json();

‎packages/main/test-integration/node/count-tokens.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,28 @@ describe("countTokens", function () {
6666
const response = await model.countTokens(countTokensRequest);
6767
expect(response.totalTokens).to.equal(3);
6868
});
69+
it("counts tokens with GenerateContentRequest", async () => {
70+
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || "");
71+
const model = genAI.getGenerativeModel({
72+
model: "gemini-1.5-flash-latest",
73+
safetySettings: [
74+
{
75+
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
76+
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
77+
},
78+
],
79+
});
80+
const countTokensRequest: CountTokensRequest = {
81+
generateContentRequest: {
82+
contents: [
83+
{
84+
role: "user",
85+
parts: [{ text: "count me again with a different result" }],
86+
},
87+
],
88+
},
89+
};
90+
const response = await model.countTokens(countTokensRequest);
91+
expect(response.totalTokens).to.equal(8);
92+
});
6993
});

0 commit comments

Comments
 (0)