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
944efd2c
Commit
944efd2c
authored
Apr 03, 2024
by
Timothy J. Baek
Browse files
feat: sanitise response content
parent
4a09342a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
4 deletions
+25
-4
src/lib/components/chat/Messages/ResponseMessage.svelte
src/lib/components/chat/Messages/ResponseMessage.svelte
+10
-4
src/lib/utils/index.ts
src/lib/utils/index.ts
+15
-0
No files found.
src/lib/components/chat/Messages/ResponseMessage.svelte
View file @
944efd2c
...
@@ -17,7 +17,11 @@
...
@@ -17,7 +17,11 @@
import { config, settings } from '$lib/stores';
import { config, settings } from '$lib/stores';
import { synthesizeOpenAISpeech } from '$lib/apis/openai';
import { synthesizeOpenAISpeech } from '$lib/apis/openai';
import { imageGenerations } from '$lib/apis/images';
import { imageGenerations } from '$lib/apis/images';
import { extractSentences } from '$lib/utils';
import {
extractSentences,
revertSanitizedResponseContent,
sanitizeResponseContent
} from '$lib/utils';
import Name from './Name.svelte';
import Name from './Name.svelte';
import ProfileImage from './ProfileImage.svelte';
import ProfileImage from './ProfileImage.svelte';
...
@@ -56,7 +60,7 @@
...
@@ -56,7 +60,7 @@
let loadingSpeech = false;
let loadingSpeech = false;
let generatingImage = false;
let generatingImage = false;
$: tokens = marked.lexer(message.content);
$: tokens = marked.lexer(
sanitizeResponseContent(
message.content)
)
;
const renderer = new marked.Renderer();
const renderer = new marked.Renderer();
...
@@ -405,8 +409,10 @@
...
@@ -405,8 +409,10 @@
{:else}
{:else}
{#each tokens as token}
{#each tokens as token}
{#if token.type === 'code'}
{#if token.type === 'code'}
<!-- {token.text} -->
<CodeBlock
<CodeBlock lang={token.lang} code={token.text} />
lang={token.lang}
code={revertSanitizedResponseContent(token.text)}
/>
{:else}
{:else}
{@html marked.parse(token.raw, {
{@html marked.parse(token.raw, {
...defaults,
...defaults,
...
...
src/lib/utils/index.ts
View file @
944efd2c
...
@@ -31,6 +31,21 @@ export const getModels = async (token: string) => {
...
@@ -31,6 +31,21 @@ export const getModels = async (token: string) => {
// Helper functions
// Helper functions
//////////////////////////
//////////////////////////
export
const
sanitizeResponseContent
=
(
content
:
string
)
=>
{
return
content
.
replace
(
/<
\|[
a-z
]
*$/
,
''
)
.
replace
(
/<
\|[
a-z
]
+
\|
$/
,
''
)
.
replace
(
/<$/
,
''
)
.
replaceAll
(
/<
\|[
a-z
]
+
\|
>/g
,
'
'
)
.
replaceAll
(
/<br
\s?\/?
>/gi
,
'
\n
'
)
.
replaceAll
(
'
<
'
,
'
<
'
)
.
trim
();
};
export
const
revertSanitizedResponseContent
=
(
content
:
string
)
=>
{
return
content
.
replaceAll
(
'
<
'
,
'
<
'
);
};
export
const
capitalizeFirstLetter
=
(
string
)
=>
{
export
const
capitalizeFirstLetter
=
(
string
)
=>
{
return
string
.
charAt
(
0
).
toUpperCase
()
+
string
.
slice
(
1
);
return
string
.
charAt
(
0
).
toUpperCase
()
+
string
.
slice
(
1
);
};
};
...
...
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