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
4727e5cb
Unverified
Commit
4727e5cb
authored
Jun 13, 2024
by
Timothy Jaeryang Baek
Committed by
GitHub
Jun 13, 2024
Browse files
Merge pull request #3128 from open-webui/voice-enh
enh: voice
parents
bdd2ac00
b8136951
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
670 additions
and
354 deletions
+670
-354
backend/main.py
backend/main.py
+68
-0
src/lib/apis/index.ts
src/lib/apis/index.ts
+40
-0
src/lib/components/chat/Chat.svelte
src/lib/components/chat/Chat.svelte
+112
-104
src/lib/components/chat/MessageInput.svelte
src/lib/components/chat/MessageInput.svelte
+3
-4
src/lib/components/chat/MessageInput/CallOverlay.svelte
src/lib/components/chat/MessageInput/CallOverlay.svelte
+413
-244
src/lib/components/chat/Messages.svelte
src/lib/components/chat/Messages.svelte
+1
-1
src/lib/components/chat/Settings/Interface.svelte
src/lib/components/chat/Settings/Interface.svelte
+32
-0
src/lib/utils/index.ts
src/lib/utils/index.ts
+1
-1
No files found.
backend/main.py
View file @
4727e5cb
...
...
@@ -494,6 +494,9 @@ def filter_pipeline(payload, user):
if
"title"
in
payload
:
del
payload
[
"title"
]
if
"task"
in
payload
:
del
payload
[
"task"
]
return
payload
...
...
@@ -835,6 +838,71 @@ async def generate_search_query(form_data: dict, user=Depends(get_verified_user)
"messages"
:
[{
"role"
:
"user"
,
"content"
:
content
}],
"stream"
:
False
,
"max_tokens"
:
30
,
"task"
:
True
,
}
print
(
payload
)
try
:
payload
=
filter_pipeline
(
payload
,
user
)
except
Exception
as
e
:
return
JSONResponse
(
status_code
=
e
.
args
[
0
],
content
=
{
"detail"
:
e
.
args
[
1
]},
)
if
model
[
"owned_by"
]
==
"ollama"
:
return
await
generate_ollama_chat_completion
(
OpenAIChatCompletionForm
(
**
payload
),
user
=
user
)
else
:
return
await
generate_openai_chat_completion
(
payload
,
user
=
user
)
@
app
.
post
(
"/api/task/emoji/completions"
)
async
def
generate_emoji
(
form_data
:
dict
,
user
=
Depends
(
get_verified_user
)):
print
(
"generate_emoji"
)
model_id
=
form_data
[
"model"
]
if
model_id
not
in
app
.
state
.
MODELS
:
raise
HTTPException
(
status_code
=
status
.
HTTP_404_NOT_FOUND
,
detail
=
"Model not found"
,
)
# Check if the user has a custom task model
# If the user has a custom task model, use that model
if
app
.
state
.
MODELS
[
model_id
][
"owned_by"
]
==
"ollama"
:
if
app
.
state
.
config
.
TASK_MODEL
:
task_model_id
=
app
.
state
.
config
.
TASK_MODEL
if
task_model_id
in
app
.
state
.
MODELS
:
model_id
=
task_model_id
else
:
if
app
.
state
.
config
.
TASK_MODEL_EXTERNAL
:
task_model_id
=
app
.
state
.
config
.
TASK_MODEL_EXTERNAL
if
task_model_id
in
app
.
state
.
MODELS
:
model_id
=
task_model_id
print
(
model_id
)
model
=
app
.
state
.
MODELS
[
model_id
]
template
=
'''
Your task is to reflect the speaker's likely facial expression through a fitting emoji. Interpret emotions from the message and reflect their facial expression using fitting, diverse emojis (e.g., 😊, 😢, 😡, 😱).
Message: """{{prompt}}"""
'''
content
=
title_generation_template
(
template
,
form_data
[
"prompt"
],
user
.
model_dump
()
)
payload
=
{
"model"
:
model_id
,
"messages"
:
[{
"role"
:
"user"
,
"content"
:
content
}],
"stream"
:
False
,
"max_tokens"
:
4
,
"chat_id"
:
form_data
.
get
(
"chat_id"
,
None
),
"task"
:
True
,
}
print
(
payload
)
...
...
src/lib/apis/index.ts
View file @
4727e5cb
...
...
@@ -205,6 +205,46 @@ export const generateTitle = async (
return
res
?.
choices
[
0
]?.
message
?.
content
.
replace
(
/
[
"'
]
/g
,
''
)
??
'
New Chat
'
;
};
export
const
generateEmoji
=
async
(
token
:
string
=
''
,
model
:
string
,
prompt
:
string
,
chat_id
?:
string
)
=>
{
let
error
=
null
;
const
res
=
await
fetch
(
`
${
WEBUI_BASE_URL
}
/api/task/emoji/completions`
,
{
method
:
'
POST
'
,
headers
:
{
Accept
:
'
application/json
'
,
'
Content-Type
'
:
'
application/json
'
,
Authorization
:
`Bearer
${
token
}
`
},
body
:
JSON
.
stringify
({
model
:
model
,
prompt
:
prompt
,
...(
chat_id
&&
{
chat_id
:
chat_id
})
})
})
.
then
(
async
(
res
)
=>
{
if
(
!
res
.
ok
)
throw
await
res
.
json
();
return
res
.
json
();
})
.
catch
((
err
)
=>
{
console
.
log
(
err
);
if
(
'
detail
'
in
err
)
{
error
=
err
.
detail
;
}
return
null
;
});
if
(
error
)
{
throw
error
;
}
return
res
?.
choices
[
0
]?.
message
?.
content
.
replace
(
/
[
"'
]
/g
,
''
)
??
null
;
};
export
const
generateSearchQuery
=
async
(
token
:
string
=
''
,
model
:
string
,
...
...
src/lib/components/chat/Chat.svelte
View file @
4727e5cb
...
...
@@ -64,6 +64,8 @@
export let chatIdProp = '';
let loaded = false;
const eventTarget = new EventTarget();
let stopResponseFlag = false;
let autoScroll = true;
let processing = '';
...
...
@@ -300,7 +302,7 @@
// Chat functions
//////////////////////////
const submitPrompt = async (userPrompt,
_user = null
) => {
const submitPrompt = async (userPrompt,
{ _raw = false } = {}
) => {
let _responses = [];
console.log('submitPrompt', $chatId);
...
...
@@ -344,7 +346,6 @@
parentId: messages.length !== 0 ? messages.at(-1).id : null,
childrenIds: [],
role: 'user',
user: _user ?? undefined,
content: userPrompt,
files: _files.length > 0 ? _files : undefined,
timestamp: Math.floor(Date.now() / 1000), // Unix epoch
...
...
@@ -362,15 +363,13 @@
// Wait until history/message have been updated
await tick();
// Send prompt
_responses = await sendPrompt(userPrompt, userMessageId);
_responses = await sendPrompt(userPrompt, userMessageId, { newChat: true });
}
return _responses;
};
const sendPrompt = async (prompt, parentId, modelId = null, newChat =
true
) => {
const sendPrompt = async (prompt, parentId,
{
modelId = null, newChat =
false } = {}
) => {
let _responses = [];
// If modelId is provided, use it, else use selected model
...
...
@@ -490,7 +489,6 @@
responseMessage.userContext = userContext;
const chatEventEmitter = await getChatEventEmitter(model.id, _chatId);
if (webSearchEnabled) {
await getWebSearchResults(model.id, parentId, responseMessageId);
}
...
...
@@ -503,8 +501,6 @@
}
_responses.push(_response);
console.log('chatEventEmitter', chatEventEmitter);
if (chatEventEmitter) clearInterval(chatEventEmitter);
} else {
toast.error($i18n.t(`Model {{modelId}} not found`, { modelId }));
...
...
@@ -513,88 +509,9 @@
);
await chats.set(await getChatList(localStorage.token));
return _responses;
};
const getWebSearchResults = async (model: string, parentId: string, responseId: string) => {
const responseMessage = history.messages[responseId];
responseMessage.statusHistory = [
{
done: false,
action: 'web_search',
description: $i18n.t('Generating search query')
}
];
messages = messages;
const prompt = history.messages[parentId].content;
let searchQuery = await generateSearchQuery(localStorage.token, model, messages, prompt).catch(
(error) => {
console.log(error);
return prompt;
}
);
if (!searchQuery) {
toast.warning($i18n.t('No search query generated'));
responseMessage.statusHistory.push({
done: true,
error: true,
action: 'web_search',
description: 'No search query generated'
});
messages = messages;
}
responseMessage.statusHistory.push({
done: false,
action: 'web_search',
description: $i18n.t(`Searching "{{searchQuery}}"`, { searchQuery })
});
messages = messages;
const results = await runWebSearch(localStorage.token, searchQuery).catch((error) => {
console.log(error);
toast.error(error);
return null;
});
if (results) {
responseMessage.statusHistory.push({
done: true,
action: 'web_search',
description: $i18n.t('Searched {{count}} sites', { count: results.filenames.length }),
query: searchQuery,
urls: results.filenames
});
if (responseMessage?.files ?? undefined === undefined) {
responseMessage.files = [];
}
responseMessage.files.push({
collection_name: results.collection_name,
name: searchQuery,
type: 'web_search_results',
urls: results.filenames
});
messages = messages;
} else {
responseMessage.statusHistory.push({
done: true,
error: true,
action: 'web_search',
description: 'No search results found'
});
messages = messages;
}
};
const sendPromptOllama = async (model, userPrompt, responseMessageId, _chatId) => {
let _response = null;
...
...
@@ -676,6 +593,8 @@
array.findIndex((i) => JSON.stringify(i) === JSON.stringify(item)) === index
);
eventTarget.dispatchEvent(new CustomEvent('chat:start'));
const [res, controller] = await generateChatCompletion(localStorage.token, {
model: model.id,
messages: messagesBody,
...
...
@@ -745,6 +664,9 @@
continue;
} else {
responseMessage.content += data.message.content;
eventTarget.dispatchEvent(
new CustomEvent('chat', { detail: { content: data.message.content } })
);
messages = messages;
}
} else {
...
...
@@ -771,21 +693,13 @@
messages = messages;
if ($settings.notificationEnabled && !document.hasFocus()) {
const notification = new Notification(
selectedModelfile
? `${
selectedModelfile.title.charAt(0).toUpperCase() +
selectedModelfile.title.slice(1)
}`
: `${model.id}`,
{
body: responseMessage.content,
icon: selectedModelfile?.imageUrl ?? `${WEBUI_BASE_URL}/static/favicon.png`
}
);
const notification = new Notification(`${model.id}`, {
body: responseMessage.content,
icon: `${WEBUI_BASE_URL}/static/favicon.png`
});
}
if ($settings.responseAutoCopy) {
if ($settings
?
.responseAutoCopy
?? false
) {
copyToClipboard(responseMessage.content);
}
...
...
@@ -846,6 +760,7 @@
stopResponseFlag = false;
await tick();
eventTarget.dispatchEvent(new CustomEvent('chat:finish'));
if (autoScroll) {
scrollToBottom();
...
...
@@ -887,6 +802,8 @@
scrollToBottom();
eventTarget.dispatchEvent(new CustomEvent('chat:start'));
try {
const [res, controller] = await generateOpenAIChatCompletion(
localStorage.token,
...
...
@@ -1007,6 +924,7 @@
continue;
} else {
responseMessage.content += value;
eventTarget.dispatchEvent(new CustomEvent('chat', { detail: { content: value } }));
messages = messages;
}
...
...
@@ -1057,6 +975,8 @@
stopResponseFlag = false;
await tick();
eventTarget.dispatchEvent(new CustomEvent('chat:finish'));
if (autoScroll) {
scrollToBottom();
}
...
...
@@ -1123,9 +1043,12 @@
let userPrompt = userMessage.content;
if ((userMessage?.models ?? [...selectedModels]).length == 1) {
await sendPrompt(userPrompt, userMessage.id, undefined, false);
// If user message has only one model selected, sendPrompt automatically selects it for regeneration
await sendPrompt(userPrompt, userMessage.id);
} else {
await sendPrompt(userPrompt, userMessage.id, message.model, false);
// If there are multiple models selected, use the model of the response message for regeneration
// e.g. many model chat
await sendPrompt(userPrompt, userMessage.id, { modelId: message.model });
}
}
};
...
...
@@ -1191,6 +1114,84 @@
}
};
const getWebSearchResults = async (model: string, parentId: string, responseId: string) => {
const responseMessage = history.messages[responseId];
responseMessage.statusHistory = [
{
done: false,
action: 'web_search',
description: $i18n.t('Generating search query')
}
];
messages = messages;
const prompt = history.messages[parentId].content;
let searchQuery = await generateSearchQuery(localStorage.token, model, messages, prompt).catch(
(error) => {
console.log(error);
return prompt;
}
);
if (!searchQuery) {
toast.warning($i18n.t('No search query generated'));
responseMessage.statusHistory.push({
done: true,
error: true,
action: 'web_search',
description: 'No search query generated'
});
messages = messages;
}
responseMessage.statusHistory.push({
done: false,
action: 'web_search',
description: $i18n.t(`Searching "{{searchQuery}}"`, { searchQuery })
});
messages = messages;
const results = await runWebSearch(localStorage.token, searchQuery).catch((error) => {
console.log(error);
toast.error(error);
return null;
});
if (results) {
responseMessage.statusHistory.push({
done: true,
action: 'web_search',
description: $i18n.t('Searched {{count}} sites', { count: results.filenames.length }),
query: searchQuery,
urls: results.filenames
});
if (responseMessage?.files ?? undefined === undefined) {
responseMessage.files = [];
}
responseMessage.files.push({
collection_name: results.collection_name,
name: searchQuery,
type: 'web_search_results',
urls: results.filenames
});
messages = messages;
} else {
responseMessage.statusHistory.push({
done: true,
error: true,
action: 'web_search',
description: 'No search results found'
});
messages = messages;
}
};
const getTags = async () => {
return await getTagsById(localStorage.token, $chatId).catch(async (error) => {
return [];
...
...
@@ -1206,7 +1207,14 @@
</title>
</svelte:head>
<CallOverlay {submitPrompt} bind:files />
<CallOverlay
{submitPrompt}
{stopResponse}
bind:files
modelId={selectedModelIds?.at(0) ?? null}
chatId={$chatId}
{eventTarget}
/>
{#if !chatIdProp || (loaded && chatIdProp)}
<div
...
...
src/lib/components/chat/MessageInput.svelte
View file @
4727e5cb
...
...
@@ -348,7 +348,6 @@
<Models
bind:this={modelsElement}
bind:prompt
bind:user
bind:chatInputPlaceholder
{messages}
on:select={(e) => {
...
...
@@ -467,7 +466,7 @@
document.getElementById('chat-textarea')?.focus();
if ($settings?.speechAutoSend ?? false) {
submitPrompt(prompt
, user
);
submitPrompt(prompt);
}
}}
/>
...
...
@@ -476,7 +475,7 @@
class="w-full flex gap-1.5"
on:submit|preventDefault={() => {
// check if selectedModels support image input
submitPrompt(prompt
, user
);
submitPrompt(prompt);
}}
>
<div
...
...
@@ -718,7 +717,7 @@
// Submit the prompt when Enter key is pressed
if (prompt !== '' && e.key === 'Enter' && !e.shiftKey) {
submitPrompt(prompt
, user
);
submitPrompt(prompt);
}
}
}}
...
...
src/lib/components/chat/MessageInput/CallOverlay.svelte
View file @
4727e5cb
This diff is collapsed.
Click to expand it.
src/lib/components/chat/Messages.svelte
View file @
4727e5cb
...
...
@@ -79,7 +79,7 @@
history.currentId = userMessageId;
await tick();
await sendPrompt(userPrompt, userMessageId
, undefined, false
);
await sendPrompt(userPrompt, userMessageId);
};
const updateChatMessages = async () => {
...
...
src/lib/components/chat/Settings/Interface.svelte
View file @
4727e5cb
...
...
@@ -20,9 +20,12 @@
// Interface
let defaultModelId = '';
let showUsername = false;
let chatBubble = true;
let chatDirection: 'LTR' | 'RTL' = 'LTR';
let showEmojiInCall = false;
const toggleSplitLargeChunks = async () => {
splitLargeChunks = !splitLargeChunks;
saveSettings({ splitLargeChunks: splitLargeChunks });
...
...
@@ -43,6 +46,11 @@
saveSettings({ showUsername: showUsername });
};
const toggleEmojiInCall = async () => {
showEmojiInCall = !showEmojiInCall;
saveSettings({ showEmojiInCall: showEmojiInCall });
};
const toggleTitleAutoGenerate = async () => {
titleAutoGenerate = !titleAutoGenerate;
saveSettings({
...
...
@@ -88,8 +96,12 @@
onMount(async () => {
titleAutoGenerate = $settings?.title?.auto ?? true;
responseAutoCopy = $settings.responseAutoCopy ?? false;
showUsername = $settings.showUsername ?? false;
showEmojiInCall = $settings.showEmojiInCall ?? false;
chatBubble = $settings.chatBubble ?? true;
widescreenMode = $settings.widescreenMode ?? false;
splitLargeChunks = $settings.splitLargeChunks ?? false;
...
...
@@ -192,6 +204,26 @@
</div>
</div>
<div>
<div class=" py-0.5 flex w-full justify-between">
<div class=" self-center text-xs font-medium">{$i18n.t('Display Emoji in Call')}</div>
<button
class="p-1 px-3 text-xs flex rounded transition"
on:click={() => {
toggleEmojiInCall();
}}
type="button"
>
{#if showEmojiInCall === true}
<span class="ml-2 self-center">{$i18n.t('On')}</span>
{:else}
<span class="ml-2 self-center">{$i18n.t('Off')}</span>
{/if}
</button>
</div>
</div>
{#if !$settings.chatBubble}
<div>
<div class=" py-0.5 flex w-full justify-between">
...
...
src/lib/utils/index.ts
View file @
4727e5cb
...
...
@@ -436,7 +436,7 @@ export const removeEmojis = (str) => {
export
const
extractSentences
=
(
text
)
=>
{
// Split the paragraph into sentences based on common punctuation marks
const
sentences
=
text
.
split
(
/
(?<
=
[
.!?
])
/
);
const
sentences
=
text
.
split
(
/
(?<
=
[
.!?
])
\s
+
/
);
return
sentences
.
map
((
sentence
)
=>
removeEmojis
(
sentence
.
trim
()))
...
...
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