mirror of
https://github.com/open-webui/open-webui
synced 2024-11-16 21:42:58 +00:00
refac
This commit is contained in:
parent
bf5a62298c
commit
7349e1d8c8
@ -358,29 +358,6 @@
|
|||||||
// Wait until history/message have been updated
|
// Wait until history/message have been updated
|
||||||
await tick();
|
await tick();
|
||||||
|
|
||||||
// Create new chat if only one message in messages
|
|
||||||
if (messages.length == 1) {
|
|
||||||
if ($settings.saveChatHistory ?? true) {
|
|
||||||
chat = await createNewChat(localStorage.token, {
|
|
||||||
id: $chatId,
|
|
||||||
title: $i18n.t('New Chat'),
|
|
||||||
models: selectedModels,
|
|
||||||
system: $settings.system ?? undefined,
|
|
||||||
options: {
|
|
||||||
...($settings.params ?? {})
|
|
||||||
},
|
|
||||||
messages: messages,
|
|
||||||
history: history,
|
|
||||||
tags: [],
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
await chats.set(await getChatList(localStorage.token));
|
|
||||||
await chatId.set(chat.id);
|
|
||||||
} else {
|
|
||||||
await chatId.set('local');
|
|
||||||
}
|
|
||||||
await tick();
|
|
||||||
}
|
|
||||||
// Send prompt
|
// Send prompt
|
||||||
_responses = await sendPrompt(userPrompt, userMessageId);
|
_responses = await sendPrompt(userPrompt, userMessageId);
|
||||||
}
|
}
|
||||||
@ -390,15 +367,78 @@
|
|||||||
|
|
||||||
const sendPrompt = async (prompt, parentId, modelId = null) => {
|
const sendPrompt = async (prompt, parentId, modelId = null) => {
|
||||||
let _responses = [];
|
let _responses = [];
|
||||||
|
|
||||||
|
// If modelId is provided, use it, else use selected model
|
||||||
|
let selectedModelIds = modelId
|
||||||
|
? [modelId]
|
||||||
|
: atSelectedModel !== undefined
|
||||||
|
? [atSelectedModel.id]
|
||||||
|
: selectedModels;
|
||||||
|
|
||||||
|
// Create response messages for each selected model
|
||||||
|
const responseMessageIds = {};
|
||||||
|
for (const modelId of selectedModelIds) {
|
||||||
|
const model = $models.filter((m) => m.id === modelId).at(0);
|
||||||
|
|
||||||
|
if (model) {
|
||||||
|
let responseMessageId = uuidv4();
|
||||||
|
let responseMessage = {
|
||||||
|
parentId: parentId,
|
||||||
|
id: responseMessageId,
|
||||||
|
childrenIds: [],
|
||||||
|
role: 'assistant',
|
||||||
|
content: '',
|
||||||
|
model: model.id,
|
||||||
|
modelName: model.name ?? model.id,
|
||||||
|
userContext: null,
|
||||||
|
timestamp: Math.floor(Date.now() / 1000) // Unix epoch
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add message to history and Set currentId to messageId
|
||||||
|
history.messages[responseMessageId] = responseMessage;
|
||||||
|
history.currentId = responseMessageId;
|
||||||
|
|
||||||
|
// Append messageId to childrenIds of parent message
|
||||||
|
if (parentId !== null) {
|
||||||
|
history.messages[parentId].childrenIds = [
|
||||||
|
...history.messages[parentId].childrenIds,
|
||||||
|
responseMessageId
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
responseMessageIds[modelId] = responseMessageId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await tick();
|
||||||
|
|
||||||
|
// Create new chat if only one message in messages
|
||||||
|
if (messages.length == 2) {
|
||||||
|
if ($settings.saveChatHistory ?? true) {
|
||||||
|
chat = await createNewChat(localStorage.token, {
|
||||||
|
id: $chatId,
|
||||||
|
title: $i18n.t('New Chat'),
|
||||||
|
models: selectedModels,
|
||||||
|
system: $settings.system ?? undefined,
|
||||||
|
options: {
|
||||||
|
...($settings.params ?? {})
|
||||||
|
},
|
||||||
|
messages: messages,
|
||||||
|
history: history,
|
||||||
|
tags: [],
|
||||||
|
timestamp: Date.now()
|
||||||
|
});
|
||||||
|
await chats.set(await getChatList(localStorage.token));
|
||||||
|
await chatId.set(chat.id);
|
||||||
|
} else {
|
||||||
|
await chatId.set('local');
|
||||||
|
}
|
||||||
|
await tick();
|
||||||
|
}
|
||||||
|
|
||||||
const _chatId = JSON.parse(JSON.stringify($chatId));
|
const _chatId = JSON.parse(JSON.stringify($chatId));
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
(modelId
|
selectedModelIds.map(async (modelId) => {
|
||||||
? [modelId]
|
|
||||||
: atSelectedModel !== undefined
|
|
||||||
? [atSelectedModel.id]
|
|
||||||
: selectedModels
|
|
||||||
).map(async (modelId) => {
|
|
||||||
console.log('modelId', modelId);
|
console.log('modelId', modelId);
|
||||||
const model = $models.filter((m) => m.id === modelId).at(0);
|
const model = $models.filter((m) => m.id === modelId).at(0);
|
||||||
|
|
||||||
@ -416,33 +456,8 @@
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create response message
|
let responseMessageId = responseMessageIds[modelId];
|
||||||
let responseMessageId = uuidv4();
|
let responseMessage = history.messages[responseMessageId];
|
||||||
let responseMessage = {
|
|
||||||
parentId: parentId,
|
|
||||||
id: responseMessageId,
|
|
||||||
childrenIds: [],
|
|
||||||
role: 'assistant',
|
|
||||||
content: '',
|
|
||||||
model: model.id,
|
|
||||||
modelName: model.name ?? model.id,
|
|
||||||
userContext: null,
|
|
||||||
timestamp: Math.floor(Date.now() / 1000) // Unix epoch
|
|
||||||
};
|
|
||||||
|
|
||||||
// Add message to history and Set currentId to messageId
|
|
||||||
history.messages[responseMessageId] = responseMessage;
|
|
||||||
history.currentId = responseMessageId;
|
|
||||||
|
|
||||||
// Append messageId to childrenIds of parent message
|
|
||||||
if (parentId !== null) {
|
|
||||||
history.messages[parentId].childrenIds = [
|
|
||||||
...history.messages[parentId].childrenIds,
|
|
||||||
responseMessageId
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
await tick();
|
|
||||||
|
|
||||||
let userContext = null;
|
let userContext = null;
|
||||||
if ($settings?.memory ?? false) {
|
if ($settings?.memory ?? false) {
|
||||||
@ -451,7 +466,6 @@
|
|||||||
toast.error(error);
|
toast.error(error);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
if (res.documents[0].length > 0) {
|
if (res.documents[0].length > 0) {
|
||||||
userContext = res.documents.reduce((acc, doc, index) => {
|
userContext = res.documents.reduce((acc, doc, index) => {
|
||||||
@ -477,7 +491,6 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
let _response = null;
|
let _response = null;
|
||||||
|
|
||||||
if (model?.owned_by === 'openai') {
|
if (model?.owned_by === 'openai') {
|
||||||
_response = await sendPromptOpenAI(model, prompt, responseMessageId, _chatId);
|
_response = await sendPromptOpenAI(model, prompt, responseMessageId, _chatId);
|
||||||
} else if (model) {
|
} else if (model) {
|
||||||
|
Loading…
Reference in New Issue
Block a user