Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
34a6d555
Commit
34a6d555
authored
May 10, 2024
by
Jun Siang Cheah
Browse files
feat: add {{prompt:middletruncate:<length>}} to title gen
also harden the replacement logic
parent
9a957670
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
15 deletions
+32
-15
src/lib/utils/index.ts
src/lib/utils/index.ts
+32
-15
No files found.
src/lib/utils/index.ts
View file @
34a6d555
...
...
@@ -472,22 +472,39 @@ export const blobToFile = (blob, fileName) => {
return
file
;
};
export
const
promptTemplate
=
(
template
:
string
,
prompt
:
string
)
=>
{
prompt
=
prompt
.
replace
(
/{{prompt}}|{{prompt:start:
\d
+}}|{{prompt:end:
\d
+}}/g
,
''
);
template
=
template
.
replace
(
/{{prompt}}/g
,
prompt
);
// Replace all instances of {{prompt:start:<length>}} with the first <length> characters of the prompt
template
=
template
.
replace
(
/{{prompt:start:
(\d
+
)
}}/g
,
(
match
,
length
)
=>
prompt
.
substring
(
0
,
parseInt
(
length
))
);
// Replace all instances of {{prompt:end:<length>}} with the last <length> characters of the prompt
template
=
template
.
replace
(
/{{prompt:end:
(\d
+
)
}}/g
,
(
match
,
length
)
=>
prompt
.
slice
(
-
parseInt
(
length
))
/**
* This function is used to replace placeholders in a template string with the provided prompt.
* The placeholders can be in the following formats:
* - `{{prompt}}`: This will be replaced with the entire prompt.
* - `{{prompt:start:<length>}}`: This will be replaced with the first <length> characters of the prompt.
* - `{{prompt:end:<length>}}`: This will be replaced with the last <length> characters of the prompt.
* - `{{prompt:middletruncate:<length>}}`: This will be replaced with the prompt truncated to <length> characters, with '...' in the middle.
*
* @param {string} template - The template string containing placeholders.
* @param {string} prompt - The string to replace the placeholders with.
* @returns {string} The template string with the placeholders replaced by the prompt.
*/
export
const
promptTemplate
=
(
template
:
string
,
prompt
:
string
):
string
=>
{
return
template
.
replace
(
/{{prompt}}|{{prompt:start:
(\d
+
)
}}|{{prompt:end:
(\d
+
)
}}|{{prompt:middletruncate:
(\d
+
)
}}/g
,
(
match
,
startLength
,
endLength
,
middleLength
)
=>
{
if
(
match
===
'
{{prompt}}
'
)
{
return
prompt
;
}
else
if
(
match
.
startsWith
(
'
{{prompt:start:
'
))
{
return
prompt
.
substring
(
0
,
startLength
);
}
else
if
(
match
.
startsWith
(
'
{{prompt:end:
'
))
{
return
prompt
.
slice
(
-
endLength
);
}
else
if
(
match
.
startsWith
(
'
{{prompt:middletruncate:
'
))
{
if
(
prompt
.
length
<=
middleLength
)
{
return
prompt
;
}
const
start
=
prompt
.
slice
(
0
,
Math
.
ceil
(
middleLength
/
2
));
const
end
=
prompt
.
slice
(
-
Math
.
floor
(
middleLength
/
2
));
return
`
${
start
}
...
${
end
}
`
;
}
return
''
;
}
);
return
template
;
};
export
const
approximateToHumanReadable
=
(
nanoseconds
:
number
)
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment