open-webui/src/lib/apis/knowledge/index.ts

172 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-10-02 00:35:35 +00:00
import { WEBUI_API_BASE_URL } from '$lib/constants';
2024-10-02 05:45:04 +00:00
export const createNewKnowledge = async (token: string, name: string, description: 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/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,
description: description
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;
console.log(err);
return null;
});
if (error) {
throw error;
}
return res;
};
2024-10-02 05:45:04 +00:00
export const getKnowledgeItems = async (token: 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/`, {
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;
console.log(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;
console.log(err);
return null;
});
if (error) {
throw error;
}
return res;
};
2024-10-02 05:45:04 +00:00
type KnowledgeForm = {
2024-10-02 00:35:35 +00:00
name: string;
2024-10-02 05:45:04 +00:00
description: string;
data: object;
2024-10-02 00:35:35 +00:00
};
2024-10-02 05:45:04 +00:00
export const updateKnowledgeById = async (token: string, id: string, form: KnowledgeForm) => {
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-02 05:45:04 +00:00
name: form.name,
description: form.description,
data: form.data
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;
console.log(err);
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;
console.log(err);
return null;
});
if (error) {
throw error;
}
return res;
};