| 1 | // @ts-nocheck |
| 2 | // Generated by continue |
| 3 | import { ChatMessage, MessagePart } from "../index.js"; |
| 4 | import { |
| 5 | compileChatMessages, |
| 6 | countTokens, |
| 7 | countTokensAsync, |
| 8 | pruneLinesFromBottom, |
| 9 | pruneLinesFromTop, |
| 10 | pruneRawPromptFromTop, |
| 11 | pruneStringFromBottom, |
| 12 | pruneStringFromTop, |
| 13 | } from "./countTokens.js"; |
| 14 | |
| 15 | describe.skip("countTokens", () => { |
| 16 | it("should count tokens for a simple string", () => { |
| 17 | const content = "Hello world!"; |
| 18 | const tokenCount = countTokens(content, "gpt-4"); |
| 19 | expect(tokenCount).toBeGreaterThan(0); |
| 20 | }); |
| 21 | |
| 22 | it("should count tokens for an array of MessagePart", () => { |
| 23 | const content: MessagePart[] = [{ type: "text", text: "Hello world!" }]; |
| 24 | const tokenCount = countTokens(content, "gpt-4"); |
| 25 | expect(tokenCount).toBeGreaterThan(0); |
| 26 | }); |
| 27 | }); |
| 28 | |
| 29 | describe.skip("countTokensAsync", () => { |
| 30 | it("should count tokens asynchronously for a simple string", async () => { |
| 31 | const content = "Hello world!"; |
| 32 | const tokenCount = await countTokensAsync(content, "gpt-4"); |
| 33 | expect(tokenCount).toBeGreaterThan(0); |
| 34 | }); |
| 35 | |
| 36 | it("should count tokens asynchronously for an array of MessagePart", async () => { |
| 37 | const content: MessagePart[] = [{ type: "text", text: "Hello world!" }]; |
| 38 | const tokenCount = await countTokensAsync(content, "gpt-4"); |
| 39 | expect(tokenCount).toBeGreaterThan(0); |
| 40 | }); |
| 41 | }); |
| 42 | |
| 43 | describe.skip("pruneLinesFromTop", () => { |
| 44 | it("should prune lines from the top to fit within max tokens", () => { |
| 45 | const prompt = "Line 1\nLine 2\nLine 3\nLine 4"; |
| 46 | const pruned = pruneLinesFromTop(prompt, 5, "gpt-4"); |
| 47 | expect(pruned.split("\n").length).toBeLessThan(prompt.split("\n").length); |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | describe.skip("pruneLinesFromBottom", () => { |
| 52 | it("should prune lines from the bottom to fit within max tokens", () => { |
| 53 | const prompt = "Line 1\nLine 2\nLine 3\nLine 4"; |
| 54 | const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4"); |
| 55 | expect(pruned.split("\n").length).toBeLessThan(prompt.split("\n").length); |
| 56 | }); |
| 57 | }); |
| 58 | |
| 59 | describe.skip("pruneRawPromptFromTop", () => { |
| 60 | it("should prune string from the top based on maxTokens", () => { |
| 61 | const result = pruneRawPromptFromTop("gpt-4", 5, "Hello world!", 2); |
| 62 | expect(result.length).toBeLessThan("Hello world!".length); |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | describe.skip("pruneStringFromTop", () => { |
| 67 | it("should prune string from the top based on maxTokens", () => { |
| 68 | const result = pruneStringFromTop("gpt-4", 5, "Hello world!"); |
| 69 | expect(result.length).toBeLessThan("Hello world!".length); |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | describe.skip("pruneStringFromBottom", () => { |
| 74 | it("should prune string from the bottom based on maxTokens", () => { |
| 75 | const result = pruneStringFromBottom("gpt-4", 5, "Hello world!"); |
| 76 | expect(result.length).toBeLessThan("Hello world!".length); |
| 77 | }); |
| 78 | }); |
| 79 | |
| 80 | describe.skip("compileChatMessages", () => { |
| 81 | it("should compile and handle an empty or undefined message list", () => { |
| 82 | const compiled = compileChatMessages("gpt-4", undefined, 100, 50, false); |
| 83 | expect(compiled.length).toBe(0); |
| 84 | }); |
| 85 | |
| 86 | it("should compile chat messages without truncating if within context length", () => { |
| 87 | const msgs: ChatMessage[] = [ |
| 88 | { role: "user", content: "Hello world!" }, |
| 89 | { role: "assistant", content: "Hi there!" }, |
| 90 | ]; |
| 91 | const compiled = compileChatMessages("gpt-4", msgs, 100, 10, false); |
| 92 | expect(compiled.length).toBe(2); |
| 93 | }); |
| 94 | |
| 95 | it("should throw an error if maxTokens is close to or exceeds contextLength", () => { |
| 96 | expect(() => { |
| 97 | compileChatMessages("gpt-4", [], 100, 90, false); |
| 98 | }).toThrow(); |
| 99 | }); |
| 100 | |
| 101 | it("should filter out any empty or system messages", () => { |
| 102 | const msgs: ChatMessage[] = [ |
| 103 | { role: "system", content: "" }, |
| 104 | { role: "user", content: "" }, |
| 105 | { role: "assistant", content: "Hi there!" }, |
| 106 | ]; |
| 107 | const compiled = compileChatMessages("gpt-4", msgs, 100, 10, false); |
| 108 | expect(compiled.length).toBe(1); |
| 109 | }); |
| 110 | }); |