( chatGptPrompt: ChatGPTMessage[], model?: "3" | "3-16k" | "4", maxTokens?: number, maxTokensOut: number = MAX_TOKENS_OUT, )
| 71 | } |
| 72 | |
| 73 | export function removeOldestFunctionCalls( |
| 74 | chatGptPrompt: ChatGPTMessage[], |
| 75 | model?: "3" | "3-16k" | "4", |
| 76 | maxTokens?: number, |
| 77 | maxTokensOut: number = MAX_TOKENS_OUT, |
| 78 | ): ChatGPTMessage[] { |
| 79 | /** Remove old function calls if over the context limit **/ |
| 80 | let tokenCount = getTokenCount(chatGptPrompt); |
| 81 | maxTokens = |
| 82 | maxTokens ?? (model === "3" ? 4096 : model === "3-16k" ? 16384 : 8192); |
| 83 | const originalTokenCount = tokenCount; |
| 84 | let numberRemoved = 0; |
| 85 | // Keep removing until under the context limit |
| 86 | while (tokenCount >= maxTokens - maxTokensOut) { |
| 87 | // Removes the oldest function call |
| 88 | const oldestFunctionCallIndex = chatGptPrompt.findIndex( |
| 89 | // 204 since 4 tokens are added to the prompt for each message |
| 90 | (m) => m.role === "function" && getTokenCount([m]) >= 204, |
| 91 | ); |
| 92 | if (oldestFunctionCallIndex === -1) { |
| 93 | // No function calls left to remove |
| 94 | break; |
| 95 | } |
| 96 | chatGptPrompt[oldestFunctionCallIndex].content = "Cut for brevity"; |
| 97 | tokenCount = getTokenCount(chatGptPrompt); |
| 98 | numberRemoved += 1; |
| 99 | } |
| 100 | console.info( |
| 101 | "Removed " + |
| 102 | numberRemoved + |
| 103 | " function calls due to context limit. Original token count: " + |
| 104 | originalTokenCount + |
| 105 | ", new token count: " + |
| 106 | tokenCount, |
| 107 | ); |
| 108 | return chatGptPrompt; |
| 109 | } |
| 110 | |
| 111 | export function getJsonMIMEType<inObj>( |
| 112 | inputDict: Record<string, inObj> | undefined | null, |
no test coverage detected