Skip to content

Commit 6c9623c

Browse files
russellwheatleymikehardy
authored andcommitted
count-tokens.ts
1 parent 6fb9585 commit 6c9623c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

packages/ai/lib/methods/count-tokens.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,28 @@
1818
import { CountTokensRequest, CountTokensResponse, RequestOptions } from '../types';
1919
import { Task, makeRequest } from '../requests/request';
2020
import { ApiSettings } from '../types/internal';
21+
import { BackendType } from '../public-types';
22+
import * as GoogleAIMapper from '../googleai-mappers';
2123

2224
export async function countTokens(
2325
apiSettings: ApiSettings,
2426
model: string,
2527
params: CountTokensRequest,
2628
requestOptions?: RequestOptions,
2729
): Promise<CountTokensResponse> {
30+
let body: string = '';
31+
if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
32+
const mappedParams = GoogleAIMapper.mapCountTokensRequest(params, model);
33+
body = JSON.stringify(mappedParams);
34+
} else {
35+
body = JSON.stringify(params);
36+
}
2837
const response = await makeRequest(
2938
model,
3039
Task.COUNT_TOKENS,
3140
apiSettings,
3241
false,
33-
JSON.stringify(params),
42+
body,
3443
requestOptions,
3544
);
3645
return response.json();

0 commit comments

Comments
 (0)