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,33 +367,20 @@
|
|||||||
|
|
||||||
const sendPrompt = async (prompt, parentId, modelId = null) => {
|
const sendPrompt = async (prompt, parentId, modelId = null) => {
|
||||||
let _responses = [];
|
let _responses = [];
|
||||||
const _chatId = JSON.parse(JSON.stringify($chatId));
|
|
||||||
|
|
||||||
await Promise.all(
|
// If modelId is provided, use it, else use selected model
|
||||||
(modelId
|
let selectedModelIds = modelId
|
||||||
? [modelId]
|
? [modelId]
|
||||||
: atSelectedModel !== undefined
|
: atSelectedModel !== undefined
|
||||||
? [atSelectedModel.id]
|
? [atSelectedModel.id]
|
||||||
: selectedModels
|
: selectedModels;
|
||||||
).map(async (modelId) => {
|
|
||||||
console.log('modelId', modelId);
|
// Create response messages for each selected model
|
||||||
|
const responseMessageIds = {};
|
||||||
|
for (const modelId of selectedModelIds) {
|
||||||
const model = $models.filter((m) => m.id === modelId).at(0);
|
const model = $models.filter((m) => m.id === modelId).at(0);
|
||||||
|
|
||||||
if (model) {
|
if (model) {
|
||||||
// If there are image files, check if model is vision capable
|
|
||||||
const hasImages = messages.some((message) =>
|
|
||||||
message.files?.some((file) => file.type === 'image')
|
|
||||||
);
|
|
||||||
|
|
||||||
if (hasImages && !(model.info?.meta?.capabilities?.vision ?? true)) {
|
|
||||||
toast.error(
|
|
||||||
$i18n.t('Model {{modelName}} is not vision capable', {
|
|
||||||
modelName: model.name ?? model.id
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create response message
|
|
||||||
let responseMessageId = uuidv4();
|
let responseMessageId = uuidv4();
|
||||||
let responseMessage = {
|
let responseMessage = {
|
||||||
parentId: parentId,
|
parentId: parentId,
|
||||||
@ -442,8 +406,59 @@
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
responseMessageIds[modelId] = responseMessageId;
|
||||||
|
}
|
||||||
|
}
|
||||||
await tick();
|
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));
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
selectedModelIds.map(async (modelId) => {
|
||||||
|
console.log('modelId', modelId);
|
||||||
|
const model = $models.filter((m) => m.id === modelId).at(0);
|
||||||
|
|
||||||
|
if (model) {
|
||||||
|
// If there are image files, check if model is vision capable
|
||||||
|
const hasImages = messages.some((message) =>
|
||||||
|
message.files?.some((file) => file.type === 'image')
|
||||||
|
);
|
||||||
|
|
||||||
|
if (hasImages && !(model.info?.meta?.capabilities?.vision ?? true)) {
|
||||||
|
toast.error(
|
||||||
|
$i18n.t('Model {{modelName}} is not vision capable', {
|
||||||
|
modelName: model.name ?? model.id
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let responseMessageId = responseMessageIds[modelId];
|
||||||
|
let responseMessage = history.messages[responseMessageId];
|
||||||
|
|
||||||
let userContext = null;
|
let userContext = null;
|
||||||
if ($settings?.memory ?? false) {
|
if ($settings?.memory ?? false) {
|
||||||
if (userContext === null) {
|
if (userContext === null) {
|
||||||
@ -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