From db817fcf29cdeede00244c8eb8bbbc726fdf4b39 Mon Sep 17 00:00:00 2001 From: Jun Siang Cheah Date: Sat, 13 Apr 2024 18:26:50 +0100 Subject: [PATCH 1/3] feat: add {{prompt:start:length}} and {{prompt:end:length}} to title gen --- src/lib/apis/ollama/index.ts | 3 ++- src/lib/apis/openai/index.ts | 3 ++- src/lib/utils/index.ts | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/lib/apis/ollama/index.ts b/src/lib/apis/ollama/index.ts index 7c4e809e5..237c16425 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 { templatePrompt } 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 = templatePrompt(template, prompt); console.log(template); diff --git a/src/lib/apis/openai/index.ts b/src/lib/apis/openai/index.ts index 7d0d9415e..1502b4a8c 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 { templatePrompt } 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 = templatePrompt(template, prompt); console.log(template); diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index e9a4e229b..8a3cdf163 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -467,3 +467,28 @@ export const blobToFile = (blob, fileName) => { const file = new File([blob], fileName, { type: blob.type }); return file; }; + +// templatePrompt 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 templatePrompt = (template, prompt) => { + template = template.replace(/{{prompt}}/g, prompt); + + // Replace {{prompt:start:}} with the first characters of the prompt + const startMatch = template.match(/{{prompt:start:(\d+)}}/); + if (startMatch) { + const length = parseInt(startMatch[1]); + template = template.replace(startMatch[0], prompt.substring(0, length)); + } + + // Replace {{prompt:end:}} with the last characters of the prompt + const endMatch = template.match(/{{prompt:end:(\d+)}}/); + if (endMatch) { + const length = parseInt(endMatch[1]); + template = template.replace(endMatch[0], prompt.substring(prompt.length - length)); + } + + return template; +}; From fffd42e4d782b37fe7771d9aaa4ff8653b79c88b Mon Sep 17 00:00:00 2001 From: Jun Siang Cheah Date: Sat, 13 Apr 2024 18:44:49 +0100 Subject: [PATCH 2/3] fix: replace all instances of prompt:start and prompt:end --- src/lib/utils/index.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 8a3cdf163..3ea827837 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -473,19 +473,21 @@ export const blobToFile = (blob, fileName) => { // {{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 templatePrompt = (template, prompt) => { +export const templatePrompt = (template: string, prompt: string) => { template = template.replace(/{{prompt}}/g, prompt); - // Replace {{prompt:start:}} with the first characters of the prompt - const startMatch = template.match(/{{prompt:start:(\d+)}}/); - if (startMatch) { + // 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 {{prompt:end:}} with the last characters of the prompt - const endMatch = template.match(/{{prompt:end:(\d+)}}/); - if (endMatch) { + // 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)); } From 0c441b588c2414e96e522476164d7889140b3dbd Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 14 Apr 2024 17:04:24 -0400 Subject: [PATCH 3/3] refac: naming convention --- src/lib/apis/ollama/index.ts | 4 ++-- src/lib/apis/openai/index.ts | 4 ++-- src/lib/utils/index.ts | 7 ++++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/lib/apis/ollama/index.ts b/src/lib/apis/ollama/index.ts index 237c16425..4618acc4d 100644 --- a/src/lib/apis/ollama/index.ts +++ b/src/lib/apis/ollama/index.ts @@ -1,5 +1,5 @@ import { OLLAMA_API_BASE_URL } from '$lib/constants'; -import { templatePrompt } from '$lib/utils'; +import { promptTemplate } from '$lib/utils'; export const getOllamaUrls = async (token: string = '') => { let error = null; @@ -145,7 +145,7 @@ export const generateTitle = async ( ) => { let error = null; - template = templatePrompt(template, 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 1502b4a8c..a498e788e 100644 --- a/src/lib/apis/openai/index.ts +++ b/src/lib/apis/openai/index.ts @@ -1,5 +1,5 @@ import { OPENAI_API_BASE_URL } from '$lib/constants'; -import { templatePrompt } from '$lib/utils'; +import { promptTemplate } from '$lib/utils'; export const getOpenAIUrls = async (token: string = '') => { let error = null; @@ -274,7 +274,7 @@ export const generateTitle = async ( ) => { let error = null; - template = templatePrompt(template, prompt); + template = promptTemplate(template, prompt); console.log(template); diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 132c57d82..90e402d82 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -468,12 +468,12 @@ export const blobToFile = (blob, fileName) => { return file; }; -// templatePrompt replaces any occurrences of the following in the template with the prompt +// 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 templatePrompt = (template: string, prompt: string) => { +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 @@ -493,7 +493,8 @@ export const templatePrompt = (template: string, prompt: string) => { } return template; - +}; + export const approximateToHumanReadable = (nanoseconds: number) => { const seconds = Math.floor((nanoseconds / 1e9) % 60); const minutes = Math.floor((nanoseconds / 6e10) % 60);