2024-01-07 07:52:22 +00:00
|
|
|
import { RAG_API_BASE_URL } from '$lib/constants';
|
|
|
|
|
|
|
|
export const uploadDocToVectorDB = async (token: string, collection_name: string, file: File) => {
|
|
|
|
const data = new FormData();
|
|
|
|
data.append('file', file);
|
|
|
|
data.append('collection_name', collection_name);
|
|
|
|
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${RAG_API_BASE_URL}/doc`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: data
|
|
|
|
})
|
|
|
|
.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 uploadWebToVectorDB = async (token: string, collection_name: string, url: string) => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${RAG_API_BASE_URL}/web`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
url: url,
|
|
|
|
collection_name: collection_name
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.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-02-03 06:59:36 +00:00
|
|
|
export const queryCollection = async (
|
2024-01-07 07:52:22 +00:00
|
|
|
token: string,
|
2024-02-03 06:57:18 +00:00
|
|
|
collection_name: string,
|
2024-01-07 07:52:22 +00:00
|
|
|
query: string,
|
|
|
|
k: number
|
|
|
|
) => {
|
|
|
|
let error = null;
|
|
|
|
|
2024-02-03 06:57:18 +00:00
|
|
|
const res = await fetch(`${RAG_API_BASE_URL}/query/collection`, {
|
2024-02-01 21:35:41 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2024-02-03 06:57:18 +00:00
|
|
|
collection_name: collection_name,
|
2024-02-01 21:35:41 +00:00
|
|
|
query: query,
|
|
|
|
k: k
|
|
|
|
})
|
|
|
|
})
|
2024-01-07 07:52:22 +00:00
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
error = err.detail;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
2024-01-07 17:15:45 +00:00
|
|
|
|
|
|
|
export const resetVectorDB = async (token: string) => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${RAG_API_BASE_URL}/reset`, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
authorization: `Bearer ${token}`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
error = err.detail;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|