feat: chat action integration

This commit is contained in:
Timothy J. Baek 2024-07-11 18:47:38 -07:00
parent eb10001eb7
commit 14c0efe300
4 changed files with 78 additions and 1 deletions

View File

@ -104,6 +104,45 @@ export const chatCompleted = async (token: string, body: ChatCompletedForm) => {
return res;
};
type ChatActionForm = {
model: string;
messages: string[];
chat_id: string;
};
export const chatAction = async (token: string, action_id: string, body: ChatActionForm) => {
let error = null;
const res = await fetch(`${WEBUI_BASE_URL}/api/chat/actions/${action_id}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...(token && { authorization: `Bearer ${token}` })
},
body: JSON.stringify(body)
})
.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;
} else {
error = err;
}
return null;
});
if (error) {
throw error;
}
return res;
};
export const getTaskConfig = async (token: string = '') => {
let error = null;

View File

@ -52,7 +52,7 @@
import { createOpenAITextStream } from '$lib/apis/streaming';
import { queryMemory } from '$lib/apis/memories';
import { getAndUpdateUserLocation, getUserSettings } from '$lib/apis/users';
import { chatCompleted, generateTitle, generateSearchQuery } from '$lib/apis';
import { chatCompleted, generateTitle, generateSearchQuery, chatAction } from '$lib/apis';
import Banner from '../common/Banner.svelte';
import MessageInput from '$lib/components/chat/MessageInput.svelte';
@ -401,6 +401,39 @@
}
};
const chatActionHandler = async (actionId, modelId, responseMessageId) => {
const res = await chatAction(localStorage.token, actionId, {
model: modelId,
messages: messages.map((m) => ({
id: m.id,
role: m.role,
content: m.content,
info: m.info ? m.info : undefined,
timestamp: m.timestamp
})),
chat_id: $chatId,
session_id: $socket?.id,
id: responseMessageId
}).catch((error) => {
toast.error(error);
messages.at(-1).error = { content: error };
return null;
});
if (res !== null) {
// Update chat history with the new messages
for (const message of res.messages) {
history.messages[message.id] = {
...history.messages[message.id],
...(history.messages[message.id].content !== message.content
? { originalContent: history.messages[message.id].content }
: {}),
...message
};
}
}
};
const getChatEventEmitter = async (modelId: string, chatId: string = '') => {
return setInterval(() => {
$socket?.emit('usage', {
@ -1533,6 +1566,7 @@
{sendPrompt}
{continueGeneration}
{regenerateResponse}
{chatActionHandler}
/>
</div>
</div>

View File

@ -22,6 +22,7 @@
export let sendPrompt: Function;
export let continueGeneration: Function;
export let regenerateResponse: Function;
export let chatActionHandler: Function;
export let user = $_user;
export let prompt;
@ -335,6 +336,7 @@
copyToClipboard={copyToClipboardWithToast}
{continueGeneration}
{regenerateResponse}
{chatActionHandler}
on:save={async (e) => {
console.log('save', e);

View File

@ -55,6 +55,7 @@
export let copyToClipboard: Function;
export let continueGeneration: Function;
export let regenerateResponse: Function;
export let chatActionHandler: Function;
let model = null;
$: model = $models.find((m) => m.id === message.model);
@ -1030,6 +1031,7 @@
? 'visible'
: 'invisible group-hover:visible'} p-1.5 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition regenerate-response-button"
on:click={() => {
chatActionHandler(action.id, message.model, message.id);
console.log('action');
}}
>