add count_tokens api

This commit is contained in:
musistudio
2025-10-06 08:35:26 +08:00
parent 9958bfbc6f
commit 55e8dcf48a
2 changed files with 8 additions and 1 deletions

View File

@@ -5,10 +5,17 @@ import { join } from "path";
import fastifyStatic from "@fastify/static";
import { readdirSync, statSync, readFileSync, writeFileSync, existsSync } from "fs";
import { homedir } from "os";
import {calculateTokenCount} from "./utils/router";
export const createServer = (config: any): Server => {
const server = new Server(config);
server.app.post("/v1/messages/count_tokens", async (req, reply) => {
const {messages, tools, system} = req.body;
const tokenCount = calculateTokenCount(messages, system, tools);
return { "input_tokens": tokenCount }
});
// Add endpoint to read config.json with access control
server.app.get("/api/config", async (req, reply) => {
return await readConfigFile();

View File

@@ -9,7 +9,7 @@ import { readFile } from 'fs/promises'
const enc = get_encoding("cl100k_base");
const calculateTokenCount = (
export const calculateTokenCount = (
messages: MessageParam[],
system: any,
tools: Tool[]