|
@@ -41,6 +41,7 @@
|
|
|
import { LITELLM_API_BASE_URL, OLLAMA_API_BASE_URL, OPENAI_API_BASE_URL } from '$lib/constants';
|
|
|
import { WEBUI_BASE_URL } from '$lib/constants';
|
|
|
import { createOpenAITextStream } from '$lib/apis/streaming';
|
|
|
+ import { queryMemory } from '$lib/apis/memories';
|
|
|
|
|
|
const i18n = getContext('i18n');
|
|
|
|
|
@@ -254,6 +255,26 @@
|
|
|
const sendPrompt = async (prompt, parentId, modelId = null) => {
|
|
|
const _chatId = JSON.parse(JSON.stringify($chatId));
|
|
|
|
|
|
+ let userContext = null;
|
|
|
+
|
|
|
+ if ($settings?.memory ?? false) {
|
|
|
+ const res = await queryMemory(localStorage.token, prompt).catch((error) => {
|
|
|
+ toast.error(error);
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+
|
|
|
+ if (res) {
|
|
|
+ userContext = res.documents.reduce((acc, doc, index) => {
|
|
|
+ const createdAtTimestamp = res.metadatas[index][0].created_at;
|
|
|
+ const createdAtDate = new Date(createdAtTimestamp * 1000).toISOString().split('T')[0];
|
|
|
+ acc.push(`${index + 1}. [${createdAtDate}]. ${doc[0]}`);
|
|
|
+ return acc;
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ console.log(userContext);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
await Promise.all(
|
|
|
(modelId ? [modelId] : atSelectedModel !== '' ? [atSelectedModel.id] : selectedModels).map(
|
|
|
async (modelId) => {
|
|
@@ -270,6 +291,7 @@
|
|
|
role: 'assistant',
|
|
|
content: '',
|
|
|
model: model.id,
|
|
|
+ userContext: userContext,
|
|
|
timestamp: Math.floor(Date.now() / 1000) // Unix epoch
|
|
|
};
|
|
|
|
|
@@ -311,10 +333,13 @@
|
|
|
scrollToBottom();
|
|
|
|
|
|
const messagesBody = [
|
|
|
- $settings.system
|
|
|
+ $settings.system || responseMessage?.userContext
|
|
|
? {
|
|
|
role: 'system',
|
|
|
- content: $settings.system
|
|
|
+ content:
|
|
|
+ $settings.system + (responseMessage?.userContext ?? null)
|
|
|
+ ? `\n\nUser Context:\n${responseMessage.userContext.join('\n')}`
|
|
|
+ : ''
|
|
|
}
|
|
|
: undefined,
|
|
|
...messages
|
|
@@ -567,10 +592,13 @@
|
|
|
model: model.id,
|
|
|
stream: true,
|
|
|
messages: [
|
|
|
- $settings.system
|
|
|
+ $settings.system || responseMessage?.userContext
|
|
|
? {
|
|
|
role: 'system',
|
|
|
- content: $settings.system
|
|
|
+ content:
|
|
|
+ $settings.system + (responseMessage?.userContext ?? null)
|
|
|
+ ? `\n\nUser Context:\n${responseMessage.userContext.join('\n')}`
|
|
|
+ : ''
|
|
|
}
|
|
|
: undefined,
|
|
|
...messages
|