Spaces:
Paused
Paused
File size: 9,419 Bytes
0e759d2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
import { removeDefaultProperty } from "./llmExtract";
import { trimToTokenLimit } from "./llmExtract";
import { encoding_for_model } from "@dqbd/tiktoken";
jest.mock("@dqbd/tiktoken", () => ({
encoding_for_model: jest.fn(),
}));
describe("removeDefaultProperty", () => {
it("should remove the default property from a simple object", () => {
const input = { default: "test", test: "test" };
const expectedOutput = { test: "test" };
expect(removeDefaultProperty(input)).toEqual(expectedOutput);
});
it("should remove the default property from a nested object", () => {
const input = {
default: "test",
nested: { default: "nestedTest", test: "nestedTest" },
};
const expectedOutput = { nested: { test: "nestedTest" } };
expect(removeDefaultProperty(input)).toEqual(expectedOutput);
});
it("should remove the default property from an array of objects", () => {
const input = {
array: [
{ default: "test1", test: "test1" },
{ default: "test2", test: "test2" },
],
};
const expectedOutput = { array: [{ test: "test1" }, { test: "test2" }] };
expect(removeDefaultProperty(input)).toEqual(expectedOutput);
});
it("should handle objects without a default property", () => {
const input = { test: "test" };
const expectedOutput = { test: "test" };
expect(removeDefaultProperty(input)).toEqual(expectedOutput);
});
it("should handle null and non-object inputs", () => {
expect(removeDefaultProperty(null)).toBeNull();
expect(removeDefaultProperty("string")).toBe("string");
expect(removeDefaultProperty(123)).toBe(123);
});
});
describe("trimToTokenLimit", () => {
const mockEncode = jest.fn();
const mockFree = jest.fn();
const mockEncoder = {
encode: mockEncode,
free: mockFree,
};
beforeEach(() => {
jest.clearAllMocks();
(encoding_for_model as jest.Mock).mockReturnValue(mockEncoder);
});
it("should return original text if within token limit", () => {
const text = "This is a test text";
mockEncode.mockReturnValue(new Array(5)); // Simulate 5 tokens
const result = trimToTokenLimit(text, 10, "gpt-4o");
expect(result).toEqual({
text,
numTokens: 5,
warning: undefined
});
expect(mockEncode).toHaveBeenCalledWith(text);
expect(mockFree).toHaveBeenCalled();
});
it("should trim text and return warning when exceeding token limit", () => {
const text = "This is a longer text that needs to be trimmed";
mockEncode
.mockReturnValueOnce(new Array(20)) // First call for full text
.mockReturnValueOnce(new Array(8)); // Second call for trimmed text
const result = trimToTokenLimit(text, 10, "gpt-4o");
expect(result.text.length).toBeLessThan(text.length);
expect(result.numTokens).toBe(8);
expect(result.warning).toContain("automatically trimmed");
expect(mockEncode).toHaveBeenCalledTimes(2);
expect(mockFree).toHaveBeenCalled();
});
it("should append previous warning if provided", () => {
const text = "This is a test text that is too long";
const previousWarning = "Previous warning message";
mockEncode
.mockReturnValueOnce(new Array(15))
.mockReturnValueOnce(new Array(8));
const result = trimToTokenLimit(text, 10, "gpt-4o", previousWarning);
expect(result.warning).toContain("automatically trimmed");
expect(result.warning).toContain(previousWarning);
});
it("should use fallback approach when encoder throws error", () => {
const text = "This is some text to test fallback";
mockEncode.mockImplementation(() => {
throw new Error("Encoder error");
});
const result = trimToTokenLimit(text, 10, "gpt-4o");
expect(result.text.length).toBeLessThanOrEqual(30); // 10 tokens * 3 chars per token
expect(result.numTokens).toBe(10);
expect(result.warning).toContain("Failed to derive number of LLM tokens");
});
it("should handle empty text", () => {
const text = "";
mockEncode.mockReturnValue([]);
const result = trimToTokenLimit(text, 10, "gpt-4o");
expect(result).toEqual({
text: "",
numTokens: 0,
warning: undefined
});
expect(mockFree).toHaveBeenCalled();
});
it("should handle large token limits (128k)", () => {
const text = "A".repeat(384000); // Assuming ~3 chars per token, this would be ~128k tokens
mockEncode
.mockReturnValueOnce(new Array(130000)) // First check shows it's too long
.mockReturnValueOnce(new Array(127000)); // Second check shows it's within limit after trim
const result = trimToTokenLimit(text, 128000, "gpt-4o");
expect(result.text.length).toBeLessThan(text.length);
expect(result.numTokens).toBe(127000);
expect(result.warning).toContain("automatically trimmed");
expect(mockEncode).toHaveBeenCalledTimes(2);
expect(mockFree).toHaveBeenCalled();
});
it("should handle large token limits (512k) with 32k context window", () => {
const text = "A".repeat(1536000); // Assuming ~3 chars per token, this would be ~512k tokens
mockEncode
.mockReturnValueOnce(new Array(520000)) // First check shows it's too long
.mockReturnValueOnce(new Array(32000)); // Second check shows it's within context limit after trim
const result = trimToTokenLimit(text, 32000, "gpt-4o");
expect(result.text.length).toBeLessThan(text.length);
expect(result.numTokens).toBe(32000);
expect(result.warning).toContain("automatically trimmed");
expect(mockEncode).toHaveBeenCalledTimes(2);
expect(mockFree).toHaveBeenCalled();
});
it("should preserve text when under token limit", () => {
const text = "Short text";
mockEncode.mockReturnValue(new Array(5)); // 5 tokens
const result = trimToTokenLimit(text, 10, "gpt-4o");
expect(result.text).toBe(text);
expect(result.numTokens).toBe(5);
expect(result.warning).toBeUndefined();
expect(mockFree).toHaveBeenCalled();
});
it("should append new warning to previous warning", () => {
const text = "A".repeat(300);
const previousWarning = "Previous warning message";
mockEncode
.mockReturnValueOnce(new Array(100))
.mockReturnValueOnce(new Array(50));
const result = trimToTokenLimit(text, 50, "gpt-4o", previousWarning);
expect(result.warning).toContain("automatically trimmed");
expect(result.warning).toContain(previousWarning);
expect(mockFree).toHaveBeenCalled();
});
it("should handle encoder initialization failure gracefully", () => {
const text = "Sample text";
(encoding_for_model as jest.Mock).mockImplementationOnce(() => {
throw new Error("Encoder initialization failed");
});
const result = trimToTokenLimit(text, 10, "gpt-4o");
expect(result.text.length).toBeLessThanOrEqual(30); // 10 tokens * 3 chars
expect(result.warning).toContain("Failed to derive number of LLM tokens");
expect(mockFree).not.toHaveBeenCalled();
});
it("should handle encoding errors during trimming", () => {
const text = "Sample text";
mockEncode.mockImplementation(() => {
throw new Error("Encoding failed");
});
const result = trimToTokenLimit(text, 10, "gpt-4o");
expect(result.text.length).toBeLessThanOrEqual(30);
expect(result.warning).toContain("Failed to derive number of LLM tokens");
expect(mockFree).toHaveBeenCalled();
});
it("should handle very small token limits", () => {
const text = "This is a test sentence that should be trimmed significantly";
mockEncode
.mockReturnValueOnce(new Array(20))
.mockReturnValueOnce(new Array(3));
const result = trimToTokenLimit(text, 3, "gpt-4o");
expect(result.text.length).toBeLessThan(text.length);
expect(result.numTokens).toBe(3);
expect(result.warning).toContain("automatically trimmed");
expect(mockFree).toHaveBeenCalled();
});
it("should handle unicode characters", () => {
const text = "Hello ๐ World ๐";
mockEncode
.mockReturnValueOnce(new Array(8))
.mockReturnValueOnce(new Array(4));
const result = trimToTokenLimit(text, 4, "gpt-4o");
expect(result.text.length).toBeLessThan(text.length);
expect(result.numTokens).toBe(4);
expect(result.warning).toContain("automatically trimmed");
expect(mockFree).toHaveBeenCalled();
});
it("should handle multiple trimming iterations", () => {
const text = "A".repeat(1000);
mockEncode
.mockReturnValueOnce(new Array(300))
.mockReturnValueOnce(new Array(200))
.mockReturnValueOnce(new Array(100))
.mockReturnValueOnce(new Array(50));
const result = trimToTokenLimit(text, 50, "gpt-4o");
expect(result.text.length).toBeLessThan(text.length);
expect(result.numTokens).toBe(50);
expect(result.warning).toContain("automatically trimmed");
expect(mockEncode).toHaveBeenCalledTimes(4);
expect(mockFree).toHaveBeenCalled();
});
it("should handle exact token limit match", () => {
const text = "Exact token limit text";
mockEncode.mockReturnValue(new Array(10));
const result = trimToTokenLimit(text, 10, "gpt-4o");
expect(result.text).toBe(text);
expect(result.numTokens).toBe(10);
expect(result.warning).toBeUndefined();
expect(mockFree).toHaveBeenCalled();
});
});
|