continue-custom/core/llm/countTokens.test.ts

176 lines
6 KiB
TypeScript

// @ts-nocheck
// Generated by continue
import { ChatMessage, MessagePart } from "../index.js";
import {
compileChatMessages,
countTokens,
countTokensAsync,
pruneLinesFromBottom,
pruneLinesFromTop,
pruneRawPromptFromTop,
pruneStringFromBottom,
pruneStringFromTop,
} from "./countTokens.js";
describe.skip("countTokens", () => {
it("should count tokens for a simple string", () => {
const content = "Hello world!";
const tokenCount = countTokens(content, "gpt-4");
expect(tokenCount).toBeGreaterThan(0);
});
it("should count tokens for an array of MessagePart", () => {
const content: MessagePart[] = [{ type: "text", text: "Hello world!" }];
const tokenCount = countTokens(content, "gpt-4");
expect(tokenCount).toBeGreaterThan(0);
});
});
describe.skip("countTokensAsync", () => {
it("should count tokens asynchronously for a simple string", async () => {
const content = "Hello world!";
const tokenCount = await countTokensAsync(content, "gpt-4");
expect(tokenCount).toBeGreaterThan(0);
});
it("should count tokens asynchronously for an array of MessagePart", async () => {
const content: MessagePart[] = [{ type: "text", text: "Hello world!" }];
const tokenCount = await countTokensAsync(content, "gpt-4");
expect(tokenCount).toBeGreaterThan(0);
});
});
describe.skip("pruneLinesFromTop", () => {
it("should prune lines from the top to fit within max tokens", () => {
const prompt = "Line 1\nLine 2\nLine 3\nLine 4";
const pruned = pruneLinesFromTop(prompt, 5, "gpt-4");
expect(pruned.split("\n").length).toBeLessThan(prompt.split("\n").length);
});
it("should return the original prompt if it's within max tokens", () => {
const prompt = "Line 1\nLine 2";
const pruned = pruneLinesFromTop(prompt, 10, "gpt-4");
expect(pruned).toEqual(prompt);
});
it("should return an empty string if maxTokens is 0", () => {
const prompt = "Line 1\nLine 2\nLine 3\nLine 4";
const pruned = pruneLinesFromTop(prompt, 0, "gpt-4");
expect(pruned).toEqual("");
});
it("should handle an empty prompt string", () => {
const prompt = "";
const pruned = pruneLinesFromTop(prompt, 5, "gpt-4");
expect(pruned).toEqual("");
});
it("should handle a prompt with a single line that exceeds maxTokens", () => {
const prompt =
"This is a single long line that will exceed the token limit";
const pruned = pruneLinesFromTop(prompt, 5, "gpt-4");
expect(pruned).toEqual("");
});
it("should correctly prune when all lines together exceed maxTokens but individual lines do not", () => {
const prompt = "L1\nL2\nL3\nL4";
const pruned = pruneLinesFromTop(prompt, 5, "gpt-4");
expect(pruned).toEqual("L3\nL4");
});
});
describe.skip("pruneLinesFromBottom", () => {
it("should prune lines from the bottom to fit within max tokens", () => {
const prompt = "Line 1\nLine 2\nLine 3\nLine 4";
const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4");
expect(pruned.split("\n").length).toBeLessThan(prompt.split("\n").length);
});
it("should return the original prompt if it's within max tokens", () => {
const prompt = "Line 1\nLine 2";
const pruned = pruneLinesFromBottom(prompt, 10, "gpt-4");
expect(pruned).toEqual(prompt);
});
it("should return an empty string if maxTokens is 0", () => {
const prompt = "Line 1\nLine 2\nLine 3\nLine 4";
const pruned = pruneLinesFromBottom(prompt, 0, "gpt-4");
expect(pruned).toEqual("");
});
it("should handle an empty prompt string", () => {
const prompt = "";
const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4");
expect(pruned).toEqual("");
});
it("should handle a prompt with a single line that exceeds maxTokens", () => {
const prompt =
"This is a single long line that will exceed the token limit";
const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4");
expect(pruned).toEqual("");
});
it("should correctly prune when all lines together exceed maxTokens but individual lines do not", () => {
const prompt = "L1\nL2\nL3\nL4";
const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4");
expect(pruned).toEqual("L1\nL2");
});
});
describe.skip("pruneRawPromptFromTop", () => {
it("should prune string from the top based on maxTokens", () => {
const result = pruneRawPromptFromTop("gpt-4", 5, "Hello world!", 2);
expect(result.length).toBeLessThan("Hello world!".length);
});
});
describe.skip("pruneStringFromTop", () => {
it("should prune string from the top based on maxTokens", () => {
const result = pruneStringFromTop("gpt-4", 5, "Hello world!");
expect(result.length).toBeLessThan("Hello world!".length);
});
});
describe.skip("pruneStringFromBottom", () => {
it("should prune string from the bottom based on maxTokens", () => {
const result = pruneStringFromBottom("gpt-4", 5, "Hello world!");
expect(result.length).toBeLessThan("Hello world!".length);
});
});
describe.skip("compileChatMessages", () => {
it("should compile and handle an empty or undefined message list", () => {
const compiled = compileChatMessages("gpt-4", undefined, 100, 50, false);
expect(compiled.length).toBe(0);
});
it("should compile chat messages without truncating if within context length", () => {
const msgs: ChatMessage[] = [
{ role: "user", content: "Hello world!" },
{ role: "assistant", content: "Hi there!" },
];
const compiled = compileChatMessages("gpt-4", msgs, 100, 10, false);
expect(compiled.length).toBe(2);
});
it("should throw an error if maxTokens is close to or exceeds contextLength", () => {
expect(() => {
compileChatMessages("gpt-4", [], 100, 90, false);
}).toThrow();
});
it("should filter out any empty or system messages", () => {
const msgs: ChatMessage[] = [
{ role: "system", content: "" },
{ role: "user", content: "" },
{ role: "assistant", content: "Hi there!" },
];
const compiled = compileChatMessages("gpt-4", msgs, 100, 10, false);
expect(compiled.length).toBe(1);
});
});