This commit is contained in:
Timothy Jaeryang Baek
2025-08-21 03:38:26 +04:00
parent 86011e40be
commit 094a16ab49
11 changed files with 193 additions and 241 deletions
+39 -1
View File
@@ -12,6 +12,7 @@
blobToFile,
compressImage,
extractInputVariables,
getAge,
getCurrentDateTime,
getFormattedDate,
getFormattedTime,
@@ -31,6 +32,7 @@
import FilesOverlay from '../chat/MessageInput/FilesOverlay.svelte';
import Commands from '../chat/MessageInput/Commands.svelte';
import InputVariablesModal from '../chat/MessageInput/InputVariablesModal.svelte';
import { getSessionUser } from '$lib/apis/auths';
export let placeholder = $i18n.t('Send a Message');
@@ -116,11 +118,47 @@
text = text.replaceAll('{{USER_LOCATION}}', String(location));
}
const sessionUser = getSessionUser(localStorage.token);
if (text.includes('{{USER_NAME}}')) {
const name = $user?.name || 'User';
const name = sessionUser?.name || 'User';
text = text.replaceAll('{{USER_NAME}}', name);
}
if (text.includes('{{USER_BIO}}')) {
const bio = sessionUser?.bio || '';
if (bio) {
text = text.replaceAll('{{USER_BIO}}', bio);
}
}
if (text.includes('{{USER_GENDER}}')) {
const gender = sessionUser?.gender || '';
if (gender) {
text = text.replaceAll('{{USER_GENDER}}', gender);
}
}
if (text.includes('{{USER_BIRTH_DATE}}')) {
const birthDate = sessionUser?.date_of_birth || '';
if (birthDate) {
text = text.replaceAll('{{USER_BIRTH_DATE}}', birthDate);
}
}
if (text.includes('{{USER_AGE}}')) {
const birthDate = sessionUser?.date_of_birth || '';
if (birthDate) {
// calculate age using date
const age = getAge(birthDate);
text = text.replaceAll('{{USER_AGE}}', age);
}
}
if (text.includes('{{USER_LANGUAGE}}')) {
const language = localStorage.getItem('locale') || 'en-US';
text = text.replaceAll('{{USER_LANGUAGE}}', language);
+10 -24
View File
@@ -44,11 +44,6 @@
copyToClipboard,
getMessageContentParts,
createMessagesList,
extractSentencesForAudio,
promptTemplate,
splitStream,
sleep,
removeDetails,
getPromptVariables,
processDetails,
removeAllDetails
@@ -1655,6 +1650,14 @@
);
await tick();
let userLocation;
if ($settings?.userLocation) {
userLocation = await getAndUpdateUserLocation(localStorage.token).catch((err) => {
console.error(err);
return undefined;
});
}
const stream =
model?.info?.params?.stream_response ??
$settings?.params?.stream_response ??
@@ -1665,16 +1668,7 @@
params?.system || $settings.system
? {
role: 'system',
content: `${promptTemplate(
params?.system ?? $settings?.system ?? '',
$user?.name,
$settings?.userLocation
? await getAndUpdateUserLocation(localStorage.token).catch((err) => {
console.error(err);
return undefined;
})
: undefined
)}`
content: `${params?.system ?? $settings?.system ?? ''}`
}
: undefined,
..._messages.map((message) => ({
@@ -1752,15 +1746,7 @@
memory: $settings?.memory ?? false
},
variables: {
...getPromptVariables(
$user?.name,
$settings?.userLocation
? await getAndUpdateUserLocation(localStorage.token).catch((err) => {
console.error(err);
return undefined;
})
: undefined
)
...getPromptVariables($user?.name, $settings?.userLocation ? userLocation : undefined)
},
model_item: $models.find((m) => m.id === model.id),
+41 -2
View File
@@ -38,6 +38,7 @@
extractContentFromFile,
extractCurlyBraceWords,
extractInputVariables,
getAge,
getCurrentDateTime,
getFormattedDate,
getFormattedTime,
@@ -73,6 +74,7 @@
import { KokoroWorker } from '$lib/workers/KokoroWorker';
import InputVariablesModal from './MessageInput/InputVariablesModal.svelte';
import Voice from '../icons/Voice.svelte';
import { getSessionUser } from '$lib/apis/auths';
const i18n = getContext('i18n');
export let onChange: Function = () => {};
@@ -176,11 +178,47 @@
text = text.replaceAll('{{USER_LOCATION}}', String(location));
}
const sessionUser = getSessionUser(localStorage.token);
if (text.includes('{{USER_NAME}}')) {
const name = $_user?.name || 'User';
const name = sessionUser?.name || 'User';
text = text.replaceAll('{{USER_NAME}}', name);
}
if (text.includes('{{USER_BIO}}')) {
const bio = sessionUser?.bio || '';
if (bio) {
text = text.replaceAll('{{USER_BIO}}', bio);
}
}
if (text.includes('{{USER_GENDER}}')) {
const gender = sessionUser?.gender || '';
if (gender) {
text = text.replaceAll('{{USER_GENDER}}', gender);
}
}
if (text.includes('{{USER_BIRTH_DATE}}')) {
const birthDate = sessionUser?.date_of_birth || '';
if (birthDate) {
text = text.replaceAll('{{USER_BIRTH_DATE}}', birthDate);
}
}
if (text.includes('{{USER_AGE}}')) {
const birthDate = sessionUser?.date_of_birth || '';
if (birthDate) {
// calculate age using date
const age = getAge(birthDate);
text = text.replaceAll('{{USER_AGE}}', age);
}
}
if (text.includes('{{USER_LANGUAGE}}')) {
const language = localStorage.getItem('locale') || 'en-US';
text = text.replaceAll('{{USER_LANGUAGE}}', language);
@@ -872,7 +910,8 @@
: `${WEBUI_BASE_URL}/static/favicon.png`)}
/>
<div class="translate-y-[0.5px]">
{$i18n.t('Talk to model')}: <span class=" font-medium">{atSelectedModel.name}</span>
{$i18n.t('Talk to model')}:
<span class=" font-medium">{atSelectedModel.name}</span>
</div>
</div>
<div>
+12 -73
View File
@@ -980,77 +980,6 @@ export const getPromptVariables = (user_name, user_location) => {
};
};
/**
* @param {string} template - The template string containing placeholders.
* @returns {string} The template string with the placeholders replaced by the prompt.
*/
export const promptTemplate = (
template: string,
user_name?: string,
user_location?: string
): string => {
// Get the current date
const currentDate = new Date();
// Format the date to YYYY-MM-DD
const formattedDate =
currentDate.getFullYear() +
'-' +
String(currentDate.getMonth() + 1).padStart(2, '0') +
'-' +
String(currentDate.getDate()).padStart(2, '0');
// Format the time to HH:MM:SS AM/PM
const currentTime = currentDate.toLocaleTimeString('en-US', {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: true
});
// Get the current weekday
const currentWeekday = getWeekday();
// Get the user's timezone
const currentTimezone = getUserTimezone();
// Get the user's language
const userLanguage = localStorage.getItem('locale') || 'en-US';
// Replace {{CURRENT_DATETIME}} in the template with the formatted datetime
template = template.replace('{{CURRENT_DATETIME}}', `${formattedDate} ${currentTime}`);
// Replace {{CURRENT_DATE}} in the template with the formatted date
template = template.replace('{{CURRENT_DATE}}', formattedDate);
// Replace {{CURRENT_TIME}} in the template with the formatted time
template = template.replace('{{CURRENT_TIME}}', currentTime);
// Replace {{CURRENT_WEEKDAY}} in the template with the current weekday
template = template.replace('{{CURRENT_WEEKDAY}}', currentWeekday);
// Replace {{CURRENT_TIMEZONE}} in the template with the user's timezone
template = template.replace('{{CURRENT_TIMEZONE}}', currentTimezone);
// Replace {{USER_LANGUAGE}} in the template with the user's language
template = template.replace('{{USER_LANGUAGE}}', userLanguage);
if (user_name) {
// Replace {{USER_NAME}} in the template with the user's name
template = template.replace('{{USER_NAME}}', user_name);
}
if (user_location) {
// Replace {{USER_LOCATION}} in the template with the current location
template = template.replace('{{USER_LOCATION}}', user_location);
} else {
// Replace {{USER_LOCATION}} in the template with 'Unknown' if no location is provided
template = template.replace('{{USER_LOCATION}}', 'LOCATION_UNKNOWN');
}
return template;
};
/**
* This function is used to replace placeholders in a template string with the provided prompt.
* The placeholders can be in the following formats:
@@ -1085,8 +1014,6 @@ export const titleGenerationTemplate = (template: string, prompt: string): strin
}
);
template = promptTemplate(template);
return template;
};
@@ -1589,3 +1516,15 @@ export const querystringValue = (key: string): string | null => {
const urlParams = new URLSearchParams(querystring);
return urlParams.get(key);
};
export const getAge = (birthDate) => {
const today = new Date();
const bDate = new Date(birthDate);
let age = today.getFullYear() - bDate.getFullYear();
const m = today.getMonth() - bDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < bDate.getDate())) {
age--;
}
return age.toString();
};