From 6a15f97f15848b4b136fdf4e664355b6e45feaeb Mon Sep 17 00:00:00 2001 From: Steven Tsao Date: Sat, 3 May 2025 01:05:59 -0700 Subject: [PATCH] feat: add token limit support and tokenizer to message handling - Import `CL100K` tokenizer from `@anysphere/priompt/dist/tokenizer`. - Update `/message` endpoint to accept `numTokens` query parameter. - Parse `numTokens` and use it in the rendering process. - Replace OpenAI chat completion logic with rendering using the tokenizer and token limit. --- examples/src/index.ts | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/examples/src/index.ts b/examples/src/index.ts index 71be22f0..f91b78c5 100644 --- a/examples/src/index.ts +++ b/examples/src/index.ts @@ -1,4 +1,5 @@ import { promptToOpenAIChatMessages, promptToOpenAIChatRequest, render, renderun } from '@anysphere/priompt'; +import { CL100K } from '@anysphere/priompt/dist/tokenizer'; import { handlePriomptPreview } from './priompt-preview-handlers'; import { ArvidStory, ExamplePrompt, SimplePrompt } from './prompt'; import fastifyCors from "@fastify/cors"; @@ -54,32 +55,20 @@ async function main() { return reply.type("text/plain").send(`Welcome to Priompt examples.`); }); S.get("/message", async (request, reply) => { - const query = request.query as { message: string; name: string }; - if (query.message === undefined || query.name === undefined) { - return reply.status(400).send("Bad request; message and name are required."); - } - const message = query.message as string; - const name = query.name as string; - const prompt = ExamplePrompt({ message, name }, { dump: process.env.NODE_ENV === "development" }); - const output = await render(prompt, { - model: "gpt-3.5-turbo" - }); - - const requestConfig: CreateChatCompletionRequest = { - model: "gpt-3.5-turbo", - messages: promptToOpenAIChatMessages(output.prompt), - }; + const query = request.query as { message: string; name: string; numTokens: string }; + const tokenLimit = parseInt(query.numTokens); - try { - const openaiResult = await openai.createChatCompletion(requestConfig); + const prompt = ExamplePrompt({ + message: query.message, + name: query.name + }); - const openaiOutput = openaiResult.data.choices[0].message; + const rendered = await render(prompt, { + tokenizer: CL100K, + tokenLimit: tokenLimit + }); - return reply.type("text/plain").send(openaiOutput?.content); - } catch (error) { - console.error(error); - return reply.status(500).send("Internal server error."); - } + return rendered; }); S.get("/database", async (request, reply) => { const query = request.query as { message: string; confuse: string | undefined; };