diff --git a/src/lib/apis/ollama/index.ts b/src/lib/apis/ollama/index.ts index 7c4e809e5..4618acc4d 100644 --- a/src/lib/apis/ollama/index.ts +++ b/src/lib/apis/ollama/index.ts @@ -1,4 +1,5 @@ import { OLLAMA_API_BASE_URL } from '$lib/constants'; +import { promptTemplate } from '$lib/utils'; export const getOllamaUrls = async (token: string = '') => { let error = null; @@ -144,7 +145,7 @@ export const generateTitle = async ( ) => { let error = null; - template = template.replace(/{{prompt}}/g, prompt); + template = promptTemplate(template, prompt); console.log(template); diff --git a/src/lib/apis/openai/index.ts b/src/lib/apis/openai/index.ts index 7d0d9415e..a498e788e 100644 --- a/src/lib/apis/openai/index.ts +++ b/src/lib/apis/openai/index.ts @@ -1,4 +1,5 @@ import { OPENAI_API_BASE_URL } from '$lib/constants'; +import { promptTemplate } from '$lib/utils'; export const getOpenAIUrls = async (token: string = '') => { let error = null; @@ -273,7 +274,7 @@ export const generateTitle = async ( ) => { let error = null; - template = template.replace(/{{prompt}}/g, prompt); + template = promptTemplate(template, prompt); console.log(template); diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 338430bb5..90e402d82 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -468,6 +468,33 @@ export const blobToFile = (blob, fileName) => { return file; }; +// promptTemplate replaces any occurrences of the following in the template with the prompt +// {{prompt}} will be replaced with the prompt +// {{prompt:start:}} will be replaced with the first characters of the prompt +// {{prompt:end:}} will be replaced with the last characters of the prompt +// Character length is used as we don't have the ability to tokenize the prompt +export const promptTemplate = (template: string, prompt: string) => { + template = template.replace(/{{prompt}}/g, prompt); + + // Replace all instances of {{prompt:start:}} with the first characters of the prompt + const startRegex = /{{prompt:start:(\d+)}}/g; + let startMatch: RegExpMatchArray | null; + while ((startMatch = startRegex.exec(template)) !== null) { + const length = parseInt(startMatch[1]); + template = template.replace(startMatch[0], prompt.substring(0, length)); + } + + // Replace all instances of {{prompt:end:}} with the last characters of the prompt + const endRegex = /{{prompt:end:(\d+)}}/g; + let endMatch: RegExpMatchArray | null; + while ((endMatch = endRegex.exec(template)) !== null) { + const length = parseInt(endMatch[1]); + template = template.replace(endMatch[0], prompt.substring(prompt.length - length)); + } + + return template; +}; + export const approximateToHumanReadable = (nanoseconds: number) => { const seconds = Math.floor((nanoseconds / 1e9) % 60); const minutes = Math.floor((nanoseconds / 6e10) % 60);