|
| 1 | +import { callLLMChatCompletion, LLMMessage } from "./llmUtils"; |
| 2 | +import { Config } from "./config"; |
| 3 | +import { countMessageTokens } from "./tokenCounter"; |
| 4 | + |
| 5 | +interface ChatWithAiArgs { |
| 6 | + prompt: string; |
| 7 | + userInput: string; |
| 8 | + fullMessageHistory: LLMMessage[]; |
| 9 | + permanentMemory: string[]; |
| 10 | + tokenLimit: number; |
| 11 | + debug?: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +export async function chatWithAI({ |
| 15 | + prompt, |
| 16 | + userInput, |
| 17 | + fullMessageHistory, |
| 18 | + permanentMemory: permanentMemory, |
| 19 | + tokenLimit: tokenLimit, |
| 20 | + debug = false, |
| 21 | +}: ChatWithAiArgs): Promise<string> { |
| 22 | + while (true) { |
| 23 | + try { |
| 24 | + const model = Config.fast_llm_model; |
| 25 | + const sendTokenLimit = tokenLimit - 1000; |
| 26 | + |
| 27 | + const currentContext: LLMMessage[] = [ |
| 28 | + { role: "system", content: prompt }, |
| 29 | + { role: "system", content: `Permanent memory: ${permanentMemory}` }, |
| 30 | + ]; |
| 31 | + |
| 32 | + let nextMessageToAddIndex = fullMessageHistory.length - 1; |
| 33 | + let currentTokensUsed = 0; |
| 34 | + const insertionIndex = currentContext.length; |
| 35 | + |
| 36 | + currentTokensUsed = countMessageTokens(currentContext, model); |
| 37 | + currentTokensUsed += countMessageTokens( |
| 38 | + [{ role: "user", content: userInput }], |
| 39 | + model |
| 40 | + ); |
| 41 | + |
| 42 | + while (nextMessageToAddIndex >= 0) { |
| 43 | + const messageToAdd = fullMessageHistory[nextMessageToAddIndex]; |
| 44 | + const tokensToAdd = countMessageTokens([messageToAdd], model); |
| 45 | + |
| 46 | + if (currentTokensUsed + tokensToAdd > sendTokenLimit) { |
| 47 | + break; |
| 48 | + } |
| 49 | + |
| 50 | + currentContext.splice( |
| 51 | + insertionIndex, |
| 52 | + 0, |
| 53 | + fullMessageHistory[nextMessageToAddIndex] |
| 54 | + ); |
| 55 | + currentTokensUsed += tokensToAdd; |
| 56 | + nextMessageToAddIndex -= 1; |
| 57 | + } |
| 58 | + |
| 59 | + currentContext.push({ role: "user", content: userInput }); |
| 60 | + const tokensRemaining = tokenLimit - currentTokensUsed; |
| 61 | + |
| 62 | + if (debug) { |
| 63 | + console.log(`Token limit: ${tokenLimit}`); |
| 64 | + console.log(`Send Token Count: ${currentTokensUsed}`); |
| 65 | + console.log(`Tokens remaining for response: ${tokensRemaining}`); |
| 66 | + console.log("------------ CONTEXT SENT TO AI ---------------"); |
| 67 | + for (const message of currentContext) { |
| 68 | + if (message.role === "system" && message.content === prompt) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + console.log( |
| 72 | + `${message.role.charAt(0).toUpperCase() + message.role.slice(1)}: ${ |
| 73 | + message.content |
| 74 | + }` |
| 75 | + ); |
| 76 | + console.log(); |
| 77 | + } |
| 78 | + console.log("----------- END OF CONTEXT ----------------"); |
| 79 | + } |
| 80 | + |
| 81 | + const assistantReply = await callLLMChatCompletion( |
| 82 | + currentContext, |
| 83 | + model, |
| 84 | + undefined /* temperature */, |
| 85 | + tokensRemaining |
| 86 | + ); |
| 87 | + |
| 88 | + fullMessageHistory.push({ role: "user", content: userInput }); |
| 89 | + fullMessageHistory.push({ |
| 90 | + role: "assistant", |
| 91 | + content: assistantReply, |
| 92 | + }); |
| 93 | + |
| 94 | + return assistantReply; |
| 95 | + } catch (error) { |
| 96 | + console.error("Error calling chat", error); |
| 97 | + throw error; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments