feat: submit prompt integration

This commit is contained in:
Timothy J. Baek 2024-06-07 00:04:47 -07:00
parent 404bb3fd67
commit 4e640daf83
3 changed files with 59 additions and 12 deletions

View File

@ -298,6 +298,7 @@
////////////////////////// //////////////////////////
const submitPrompt = async (userPrompt, _user = null) => { const submitPrompt = async (userPrompt, _user = null) => {
let _responses = [];
console.log('submitPrompt', $chatId); console.log('submitPrompt', $chatId);
selectedModels = selectedModels.map((modelId) => selectedModels = selectedModels.map((modelId) =>
@ -379,11 +380,14 @@
files = []; files = [];
// Send prompt // Send prompt
await sendPrompt(userPrompt, userMessageId); _responses = await sendPrompt(userPrompt, userMessageId);
} }
return _responses;
}; };
const sendPrompt = async (prompt, parentId, modelId = null) => { const sendPrompt = async (prompt, parentId, modelId = null) => {
let _responses = [];
const _chatId = JSON.parse(JSON.stringify($chatId)); const _chatId = JSON.parse(JSON.stringify($chatId));
await Promise.all( await Promise.all(
@ -470,11 +474,14 @@
await getWebSearchResults(model.id, parentId, responseMessageId); await getWebSearchResults(model.id, parentId, responseMessageId);
} }
let _response = null;
if (model?.owned_by === 'openai') { if (model?.owned_by === 'openai') {
await sendPromptOpenAI(model, prompt, responseMessageId, _chatId); _response = await sendPromptOpenAI(model, prompt, responseMessageId, _chatId);
} else if (model) { } else if (model) {
await sendPromptOllama(model, prompt, responseMessageId, _chatId); _response = await sendPromptOllama(model, prompt, responseMessageId, _chatId);
} }
_responses.push(_response);
console.log('chatEventEmitter', chatEventEmitter); console.log('chatEventEmitter', chatEventEmitter);
@ -486,6 +493,8 @@
); );
await chats.set(await getChatList(localStorage.token)); await chats.set(await getChatList(localStorage.token));
return _responses;
}; };
const getWebSearchResults = async (model: string, parentId: string, responseId: string) => { const getWebSearchResults = async (model: string, parentId: string, responseId: string) => {
@ -560,6 +569,8 @@
}; };
const sendPromptOllama = async (model, userPrompt, responseMessageId, _chatId) => { const sendPromptOllama = async (model, userPrompt, responseMessageId, _chatId) => {
let _response = null;
model = model.id; model = model.id;
const responseMessage = history.messages[responseMessageId]; const responseMessage = history.messages[responseMessageId];
@ -670,6 +681,7 @@
await chatCompletedHandler(model, messages); await chatCompletedHandler(model, messages);
} }
_response = responseMessage.content;
break; break;
} }
@ -806,9 +818,12 @@
const _title = await generateChatTitle(userPrompt); const _title = await generateChatTitle(userPrompt);
await setChatTitle(_chatId, _title); await setChatTitle(_chatId, _title);
} }
return _response;
}; };
const sendPromptOpenAI = async (model, userPrompt, responseMessageId, _chatId) => { const sendPromptOpenAI = async (model, userPrompt, responseMessageId, _chatId) => {
let _response = null;
const responseMessage = history.messages[responseMessageId]; const responseMessage = history.messages[responseMessageId];
const docs = messages const docs = messages
@ -925,6 +940,8 @@
await chatCompletedHandler(model.id, messages); await chatCompletedHandler(model.id, messages);
} }
_response = responseMessage.content;
break; break;
} }
@ -1000,6 +1017,8 @@
const _title = await generateChatTitle(userPrompt); const _title = await generateChatTitle(userPrompt);
await setChatTitle(_chatId, _title); await setChatTitle(_chatId, _title);
} }
return _response;
}; };
const handleOpenAIError = async (error, res: Response | null, model, responseMessage) => { const handleOpenAIError = async (error, res: Response | null, model, responseMessage) => {
@ -1195,7 +1214,7 @@
</title> </title>
</svelte:head> </svelte:head>
<CallOverlay /> <CallOverlay {submitPrompt} />
{#if !chatIdProp || (loaded && chatIdProp)} {#if !chatIdProp || (loaded && chatIdProp)}
<div <div

View File

@ -897,7 +897,11 @@
class=" text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-850 transition rounded-full p-2 self-center" class=" text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-850 transition rounded-full p-2 self-center"
type="button" type="button"
on:click={() => { on:click={() => {
showCallOverlay.set(true); if (selectedModels.length > 1) {
toast.error($i18n.t('Select only one model to call'));
} else {
showCallOverlay.set(true);
}
}} }}
> >
<Headphone className="size-6" /> <Headphone className="size-6" />

View File

@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { showCallOverlay } from '$lib/stores'; import { settings, showCallOverlay } from '$lib/stores';
import { onMount, tick, getContext } from 'svelte'; import { onMount, tick, getContext } from 'svelte';
import { blobToFile, calculateSHA256, findWordIndices } from '$lib/utils'; import { blobToFile, calculateSHA256, findWordIndices } from '$lib/utils';
@ -8,9 +8,14 @@
const i18n = getContext('i18n'); const i18n = getContext('i18n');
export let submitPrompt: Function;
let loading = false; let loading = false;
let confirmed = false; let confirmed = false;
let assistantSpeaking = false;
let assistantAudio = null;
let rmsLevel = 0; let rmsLevel = 0;
let hasStartedSpeaking = false; let hasStartedSpeaking = false;
@ -103,6 +108,14 @@
// Check if initial speech/noise has started // Check if initial speech/noise has started
const hasSound = domainData.some((value) => value > 0); const hasSound = domainData.some((value) => value > 0);
if (hasSound) { if (hasSound) {
if (assistantSpeaking) {
speechSynthesis.cancel();
if (assistantAudio) {
assistantAudio.pause();
assistantAudio.currentTime = 0;
}
}
hasStartedSpeaking = true; hasStartedSpeaking = true;
lastSoundTime = Date.now(); lastSoundTime = Date.now();
} }
@ -140,6 +153,22 @@
if (res) { if (res) {
toast.success(res.text); toast.success(res.text);
const _responses = await submitPrompt(res.text);
console.log(_responses);
if (_responses.at(0)) {
const response = _responses[0];
if (response) {
assistantSpeaking = true;
if ($settings?.audio?.TTSEngine ?? '') {
speechSynthesis.speak(new SpeechSynthesisUtterance(response));
} else {
console.log('openai');
}
}
}
} }
}; };
@ -277,12 +306,7 @@
</div> </div>
<div> <div>
<button <button type="button">
on:click={() => {
loading = !loading;
}}
type="button"
>
<div class=" line-clamp-1 text-sm font-medium"> <div class=" line-clamp-1 text-sm font-medium">
{#if loading} {#if loading}
Thinking... Thinking...