|
1 |
| -import { createOpenAI } from '@ai-sdk/openai'; |
2 |
| -import { getEncoding } from 'js-tiktoken'; |
| 1 | +import { createOpenAI } from '@ai-sdk/openai' |
| 2 | +import { getEncoding } from 'js-tiktoken' |
3 | 3 |
|
4 |
| -import { RecursiveCharacterTextSplitter } from './text-splitter'; |
| 4 | +import { RecursiveCharacterTextSplitter } from './text-splitter' |
5 | 5 |
|
6 | 6 | // Providers
|
7 | 7 | const openai = createOpenAI({
|
8 | 8 | apiKey: import.meta.env.VITE_OPENAI_API_KEY!,
|
9 | 9 | baseURL: import.meta.env.VITE_OPENAI_ENDPOINT || 'https://api.openai.com/v1',
|
10 |
| -}); |
| 10 | +}) |
11 | 11 |
|
12 |
| -const customModel = import.meta.env.VITE_OPENAI_MODEL || 'o3-mini'; |
| 12 | +const customModel = import.meta.env.VITE_OPENAI_MODEL || 'o3-mini' |
13 | 13 |
|
14 | 14 | // Models
|
15 | 15 |
|
16 | 16 | export const o3MiniModel = openai(customModel, {
|
17 | 17 | // reasoningEffort: customModel.startsWith('o') ? 'medium' : undefined,
|
18 | 18 | structuredOutputs: true,
|
19 |
| -}); |
| 19 | +}) |
20 | 20 |
|
21 |
| -const MinChunkSize = 140; |
22 |
| -const encoder = getEncoding('o200k_base'); |
| 21 | +const MinChunkSize = 140 |
| 22 | +const encoder = getEncoding('o200k_base') |
23 | 23 |
|
24 | 24 | // trim prompt to maximum context size
|
25 | 25 | export function trimPrompt(
|
26 | 26 | prompt: string,
|
27 | 27 | contextSize = Number(import.meta.env.VITE_CONTEXT_SIZE) || 128_000,
|
28 | 28 | ) {
|
29 | 29 | if (!prompt) {
|
30 |
| - return ''; |
| 30 | + return '' |
31 | 31 | }
|
32 | 32 |
|
33 |
| - const length = encoder.encode(prompt).length; |
| 33 | + const length = encoder.encode(prompt).length |
34 | 34 | if (length <= contextSize) {
|
35 |
| - return prompt; |
| 35 | + return prompt |
36 | 36 | }
|
37 | 37 |
|
38 |
| - const overflowTokens = length - contextSize; |
| 38 | + const overflowTokens = length - contextSize |
39 | 39 | // on average it's 3 characters per token, so multiply by 3 to get a rough estimate of the number of characters
|
40 |
| - const chunkSize = prompt.length - overflowTokens * 3; |
| 40 | + const chunkSize = prompt.length - overflowTokens * 3 |
41 | 41 | if (chunkSize < MinChunkSize) {
|
42 |
| - return prompt.slice(0, MinChunkSize); |
| 42 | + return prompt.slice(0, MinChunkSize) |
43 | 43 | }
|
44 | 44 |
|
45 | 45 | const splitter = new RecursiveCharacterTextSplitter({
|
46 | 46 | chunkSize,
|
47 | 47 | chunkOverlap: 0,
|
48 |
| - }); |
49 |
| - const trimmedPrompt = splitter.splitText(prompt)[0] ?? ''; |
| 48 | + }) |
| 49 | + const trimmedPrompt = splitter.splitText(prompt)[0] ?? '' |
50 | 50 |
|
51 | 51 | // last catch, there's a chance that the trimmed prompt is same length as the original prompt, due to how tokens are split & innerworkings of the splitter, handle this case by just doing a hard cut
|
52 | 52 | if (trimmedPrompt.length === prompt.length) {
|
53 |
| - return trimPrompt(prompt.slice(0, chunkSize), contextSize); |
| 53 | + return trimPrompt(prompt.slice(0, chunkSize), contextSize) |
54 | 54 | }
|
55 | 55 |
|
56 | 56 | // recursively trim until the prompt is within the context size
|
57 |
| - return trimPrompt(trimmedPrompt, contextSize); |
| 57 | + return trimPrompt(trimmedPrompt, contextSize) |
58 | 58 | }
|
0 commit comments