Skip to content

Commit

Permalink
Don't unnecessarily reload encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
hmarr committed Jul 17, 2023
1 parent 1b3a097 commit 5aa8bf9
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import OpenAI from "openai";
import { getEncoding } from "js-tiktoken";
import { Tiktoken, getEncoding } from "js-tiktoken";
import { FunctionDef, formatFunctionDefinitions } from "./functions";

type Message = OpenAI.Chat.CompletionCreateParams.CreateChatCompletionRequestNonStreaming.Message;
type Function = OpenAI.Chat.CompletionCreateParams.CreateChatCompletionRequestNonStreaming.Function;

let encoder: Tiktoken | undefined;

/**
* Estimate the number of tokens a prompt will use.
* @param {Object} prompt OpenAI prompt data
Expand All @@ -27,8 +29,10 @@ export function promptTokensEstimate({ messages, functions }: { messages: Messag
* @returns The number of tokens in the string
*/
export function stringTokens(s: string): number {
const encoding = getEncoding("cl100k_base");
return encoding.encode(s).length;
if (!encoder) {
encoder = getEncoding("cl100k_base");
}
return encoder.encode(s).length;
}

/**
Expand Down

0 comments on commit 5aa8bf9

Please sign in to comment.