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
fffd42e4
Commit
fffd42e4
authored
Apr 13, 2024
by
Jun Siang Cheah
Browse files
fix: replace all instances of prompt:start and prompt:end
parent
db817fcf
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
7 deletions
+9
-7
src/lib/utils/index.ts
src/lib/utils/index.ts
+9
-7
No files found.
src/lib/utils/index.ts
View file @
fffd42e4
...
@@ -473,19 +473,21 @@ export const blobToFile = (blob, fileName) => {
...
@@ -473,19 +473,21 @@ export const blobToFile = (blob, fileName) => {
// {{prompt:start:<length>}} will be replaced with the first <length> characters of the prompt
// {{prompt:start:<length>}} will be replaced with the first <length> characters of the prompt
// {{prompt:end:<length>}} will be replaced with the last <length> characters of the prompt
// {{prompt:end:<length>}} will be replaced with the last <length> characters of the prompt
// Character length is used as we don't have the ability to tokenize 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
);
template
=
template
.
replace
(
/{{prompt}}/g
,
prompt
);
// Replace {{prompt:start:<length>}} with the first <length> characters of the prompt
// Replace all instances of {{prompt:start:<length>}} with the first <length> characters of the prompt
const
startMatch
=
template
.
match
(
/{{prompt:start:
(\d
+
)
}}/
);
const
startRegex
=
/{{prompt:start:
(\d
+
)
}}/g
;
if
(
startMatch
)
{
let
startMatch
:
RegExpMatchArray
|
null
;
while
((
startMatch
=
startRegex
.
exec
(
template
))
!==
null
)
{
const
length
=
parseInt
(
startMatch
[
1
]);
const
length
=
parseInt
(
startMatch
[
1
]);
template
=
template
.
replace
(
startMatch
[
0
],
prompt
.
substring
(
0
,
length
));
template
=
template
.
replace
(
startMatch
[
0
],
prompt
.
substring
(
0
,
length
));
}
}
// Replace {{prompt:end:<length>}} with the last <length> characters of the prompt
// Replace all instances of {{prompt:end:<length>}} with the last <length> characters of the prompt
const
endMatch
=
template
.
match
(
/{{prompt:end:
(\d
+
)
}}/
);
const
endRegex
=
/{{prompt:end:
(\d
+
)
}}/g
;
if
(
endMatch
)
{
let
endMatch
:
RegExpMatchArray
|
null
;
while
((
endMatch
=
endRegex
.
exec
(
template
))
!==
null
)
{
const
length
=
parseInt
(
endMatch
[
1
]);
const
length
=
parseInt
(
endMatch
[
1
]);
template
=
template
.
replace
(
endMatch
[
0
],
prompt
.
substring
(
prompt
.
length
-
length
));
template
=
template
.
replace
(
endMatch
[
0
],
prompt
.
substring
(
prompt
.
length
-
length
));
}
}
...
...
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