refac: prompt variables

This commit is contained in:
Timothy Jaeryang Baek
2025-01-29 21:56:51 -08:00
parent d6c82939e6
commit cc99673906
8 changed files with 49 additions and 18 deletions

View File

@@ -45,7 +45,8 @@
promptTemplate,
splitStream,
sleep,
removeDetailsWithReasoning
removeDetailsWithReasoning,
getPromptVariables
} from '$lib/utils';
import { generateChatCompletion } from '$lib/apis/ollama';
@@ -628,7 +629,7 @@
} catch (e) {
// Remove the failed doc from the files array
files = files.filter((f) => f.name !== url);
toast.error(e);
toast.error(`${e}`);
}
};
@@ -1558,10 +1559,17 @@
files: (files?.length ?? 0) > 0 ? files : undefined,
tool_ids: selectedToolIds.length > 0 ? selectedToolIds : undefined,
features: {
image_generation: imageGenerationEnabled,
web_search: webSearchEnabled
},
variables: {
...getPromptVariables(
$user.name,
$settings?.userLocation ? await getAndUpdateUserLocation(localStorage.token) : undefined
)
},
session_id: $socket?.id,
chat_id: $chatId,

View File

@@ -766,6 +766,19 @@ export const blobToFile = (blob, fileName) => {
return file;
};
export const getPromptVariables = (user_name, user_location) => {
return {
'{{USER_NAME}}': user_name,
'{{USER_LOCATION}}': user_location || 'Unknown',
'{{CURRENT_DATETIME}}': getCurrentDateTime(),
'{{CURRENT_DATE}}': getFormattedDate(),
'{{CURRENT_TIME}}': getFormattedTime(),
'{{CURRENT_WEEKDAY}}': getWeekday(),
'{{CURRENT_TIMEZONE}}': getUserTimezone(),
'{{USER_LANGUAGE}}': localStorage.getItem('locale') || 'en-US'
};
};
/**
* @param {string} template - The template string containing placeholders.
* @returns {string} The template string with the placeholders replaced by the prompt.