2024-10-02 00:35:35 +00:00
|
|
|
import { WEBUI_API_BASE_URL } from '$lib/constants';
|
|
|
|
|
|
2024-11-17 07:46:12 +00:00
|
|
|
export const createNewKnowledge = async (
|
|
|
|
|
token: string,
|
|
|
|
|
name: string,
|
|
|
|
|
description: string,
|
|
|
|
|
accessControl: null | object
|
|
|
|
|
) => {
|
2024-10-02 00:35:35 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
2024-10-02 05:45:04 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/create`, {
|
2024-10-02 00:35:35 +00:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
2024-10-02 04:32:59 +00:00
|
|
|
name: name,
|
2024-11-17 04:47:45 +00:00
|
|
|
description: description,
|
|
|
|
|
access_control: accessControl
|
2024-10-02 00:35:35 +00:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err.detail;
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-10-02 00:35:35 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-11 04:19:19 +00:00
|
|
|
export const getKnowledgeBases = async (token: string = '', page: number | null = null) => {
|
2024-10-02 00:35:35 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
2025-12-11 04:19:19 +00:00
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
|
if (page) searchParams.append('page', page.toString());
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/?${searchParams.toString()}`, {
|
2024-10-02 00:35:35 +00:00
|
|
|
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();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err.detail;
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-10-02 00:35:35 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-11 04:19:19 +00:00
|
|
|
export const searchKnowledgeBases = async (
|
|
|
|
|
token: string = '',
|
|
|
|
|
query: string | null = null,
|
|
|
|
|
viewOption: string | null = null,
|
|
|
|
|
page: number | null = null
|
|
|
|
|
) => {
|
2024-11-17 00:51:55 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
2025-12-11 04:19:19 +00:00
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
|
if (query) searchParams.append('query', query);
|
|
|
|
|
if (viewOption) searchParams.append('view_option', viewOption);
|
|
|
|
|
if (page) searchParams.append('page', page.toString());
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/search?${searchParams.toString()}`, {
|
2024-11-17 00:51:55 +00:00
|
|
|
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();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err.detail;
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-11-17 00:51:55 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-11 04:19:19 +00:00
|
|
|
export const searchKnowledgeFiles = async (
|
|
|
|
|
token: string,
|
|
|
|
|
query?: string | null = null,
|
|
|
|
|
viewOption?: string | null = null,
|
|
|
|
|
orderBy?: string | null = null,
|
|
|
|
|
direction?: string | null = null,
|
|
|
|
|
page: number = 1
|
|
|
|
|
) => {
|
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
|
if (query) searchParams.append('query', query);
|
|
|
|
|
if (viewOption) searchParams.append('view_option', viewOption);
|
|
|
|
|
if (orderBy) searchParams.append('order_by', orderBy);
|
|
|
|
|
if (direction) searchParams.append('direction', direction);
|
|
|
|
|
searchParams.append('page', page.toString());
|
|
|
|
|
|
|
|
|
|
const res = await fetch(
|
|
|
|
|
`${WEBUI_API_BASE_URL}/knowledge/search/files?${searchParams.toString()}`,
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err.detail;
|
|
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-02 05:45:04 +00:00
|
|
|
export const getKnowledgeById = async (token: string, id: string) => {
|
2024-10-02 00:35:35 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
2024-10-02 05:45:04 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}`, {
|
2024-10-02 00:35:35 +00:00
|
|
|
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();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err.detail;
|
|
|
|
|
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-10-02 00:35:35 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-10 05:53:41 +00:00
|
|
|
export const searchKnowledgeFilesById = async (
|
|
|
|
|
token: string,
|
|
|
|
|
id: string,
|
|
|
|
|
query?: string | null = null,
|
|
|
|
|
viewOption?: string | null = null,
|
|
|
|
|
orderBy?: string | null = null,
|
|
|
|
|
direction?: string | null = null,
|
|
|
|
|
page: number = 1
|
|
|
|
|
) => {
|
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
|
if (query) searchParams.append('query', query);
|
|
|
|
|
if (viewOption) searchParams.append('view_option', viewOption);
|
|
|
|
|
if (orderBy) searchParams.append('order_by', orderBy);
|
|
|
|
|
if (direction) searchParams.append('direction', direction);
|
|
|
|
|
searchParams.append('page', page.toString());
|
|
|
|
|
|
|
|
|
|
const res = await fetch(
|
|
|
|
|
`${WEBUI_API_BASE_URL}/knowledge/${id}/files?${searchParams.toString()}`,
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err.detail;
|
|
|
|
|
|
|
|
|
|
console.error(err);
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-03 03:42:10 +00:00
|
|
|
type KnowledgeUpdateForm = {
|
|
|
|
|
name?: string;
|
|
|
|
|
description?: string;
|
|
|
|
|
data?: object;
|
2024-11-17 07:46:12 +00:00
|
|
|
access_control?: null | object;
|
2024-10-02 00:35:35 +00:00
|
|
|
};
|
|
|
|
|
|
2024-10-03 03:42:10 +00:00
|
|
|
export const updateKnowledgeById = async (token: string, id: string, form: KnowledgeUpdateForm) => {
|
2024-10-02 00:35:35 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
2024-10-02 05:45:04 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/update`, {
|
2024-10-02 00:35:35 +00:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
2024-10-03 03:42:10 +00:00
|
|
|
name: form?.name ? form.name : undefined,
|
|
|
|
|
description: form?.description ? form.description : undefined,
|
2024-11-17 04:47:45 +00:00
|
|
|
data: form?.data ? form.data : undefined,
|
|
|
|
|
access_control: form.access_control
|
2024-10-02 00:35:35 +00:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err.detail;
|
|
|
|
|
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-10-02 00:35:35 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-04 05:22:22 +00:00
|
|
|
export const addFileToKnowledgeById = async (token: string, id: string, fileId: string) => {
|
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/file/add`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
file_id: fileId
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err.detail;
|
|
|
|
|
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-10-04 05:22:22 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-04 07:46:32 +00:00
|
|
|
export const updateFileFromKnowledgeById = async (token: string, id: string, fileId: string) => {
|
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/file/update`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
file_id: fileId
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err.detail;
|
|
|
|
|
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-10-04 07:46:32 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-04 05:22:22 +00:00
|
|
|
export const removeFileFromKnowledgeById = async (token: string, id: string, fileId: string) => {
|
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/file/remove`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
file_id: fileId
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
|
return res.json();
|
|
|
|
|
})
|
|
|
|
|
.then((json) => {
|
|
|
|
|
return json;
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
error = err.detail;
|
|
|
|
|
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-10-04 05:22:22 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-05 01:44:57 +00:00
|
|
|
export const resetKnowledgeById = async (token: string, id: string) => {
|
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/reset`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
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;
|
|
|
|
|
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-10-05 01:44:57 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-02 05:45:04 +00:00
|
|
|
export const deleteKnowledgeById = async (token: string, id: string) => {
|
2024-10-02 00:35:35 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
2024-10-02 05:45:04 +00:00
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/delete`, {
|
2024-10-02 00:35:35 +00:00
|
|
|
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;
|
|
|
|
|
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2024-10-02 00:35:35 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
};
|
2025-04-07 15:44:10 +00:00
|
|
|
|
|
|
|
|
export const reindexKnowledgeFiles = async (token: string) => {
|
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
|
|
const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/reindex`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
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;
|
2025-05-15 19:35:07 +00:00
|
|
|
console.error(err);
|
2025-04-07 15:44:10 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
2025-04-12 23:35:11 +00:00
|
|
|
};
|