From a8c4588fb5ee8b9b588cec8213a86ba6e655721e Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 19 May 2024 08:14:04 -0700 Subject: [PATCH] feat: memory integration --- backend/apps/web/routers/memories.py | 2 +- src/lib/apis/memories/index.ts | 123 ++++++++++++++++++ .../Personalization/AddMemoryModal.svelte | 30 ++++- .../Personalization/ManageModal.svelte | 68 +++++++--- 4 files changed, 202 insertions(+), 21 deletions(-) create mode 100644 src/lib/apis/memories/index.ts diff --git a/backend/apps/web/routers/memories.py b/backend/apps/web/routers/memories.py index d234d46de..1e61bd478 100644 --- a/backend/apps/web/routers/memories.py +++ b/backend/apps/web/routers/memories.py @@ -72,7 +72,7 @@ class QueryMemoryForm(BaseModel): @router.post("/query", response_model=Optional[MemoryModel]) -async def add_memory( +async def query_memory( request: Request, form_data: QueryMemoryForm, user=Depends(get_verified_user) ): query_embedding = request.app.state.EMBEDDING_FUNCTION(form_data.content) diff --git a/src/lib/apis/memories/index.ts b/src/lib/apis/memories/index.ts new file mode 100644 index 000000000..8c2ee3635 --- /dev/null +++ b/src/lib/apis/memories/index.ts @@ -0,0 +1,123 @@ +import { WEBUI_API_BASE_URL } from '$lib/constants'; + +export const getMemories = async (token: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/memories`, { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + authorization: `Bearer ${token}` + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + error = err.detail; + console.log(err); + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + +export const addNewMemory = async (token: string, content: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/memories/add`, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + authorization: `Bearer ${token}` + }, + body: JSON.stringify({ + content: content + }) + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + error = err.detail; + console.log(err); + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + +export const queryMemory = async (token: string, content: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/memories/query`, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + authorization: `Bearer ${token}` + }, + body: JSON.stringify({ + content: content + }) + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + error = err.detail; + console.log(err); + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + +export const deleteMemoryById = async (token: string, id: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/memories/${id}`, { + method: 'DELETE', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + authorization: `Bearer ${token}` + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .then((json) => { + return json; + }) + .catch((err) => { + error = err.detail; + + console.log(err); + return null; + }); + + if (error) { + throw error; + } + + return res; +}; diff --git a/src/lib/components/chat/Settings/Personalization/AddMemoryModal.svelte b/src/lib/components/chat/Settings/Personalization/AddMemoryModal.svelte index a3a74c19b..cac86f3a0 100644 --- a/src/lib/components/chat/Settings/Personalization/AddMemoryModal.svelte +++ b/src/lib/components/chat/Settings/Personalization/AddMemoryModal.svelte @@ -1,17 +1,37 @@ @@ -48,7 +68,7 @@ >