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

568 lines
10 KiB
TypeScript
Raw Normal View History

2024-10-05 00:03:56 +00:00
import { RETRIEVAL_API_BASE_URL } from '$lib/constants';
2024-01-07 07:52:22 +00:00
2024-03-10 20:32:34 +00:00
export const getRAGConfig = async (token: string) => {
2024-02-18 06:29:52 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/config`, {
2024-02-18 06:29:52 +00:00
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.log(err);
error = err.detail;
return null;
});
if (error) {
throw error;
}
return res;
};
2024-03-10 20:32:34 +00:00
type ChunkConfigForm = {
chunk_size: number;
chunk_overlap: number;
};
2024-07-02 00:11:09 +00:00
type ContentExtractConfigForm = {
engine: string;
tika_server_url: string | null;
};
2024-05-08 17:47:05 +00:00
type YoutubeConfigForm = {
language: string[];
translation?: string | null;
};
2024-03-10 20:32:34 +00:00
type RAGConfigForm = {
pdf_extract_images?: boolean;
chunk?: ChunkConfigForm;
2024-07-02 00:11:09 +00:00
content_extraction?: ContentExtractConfigForm;
web_loader_ssl_verification?: boolean;
2024-05-08 17:47:05 +00:00
youtube?: YoutubeConfigForm;
2024-03-10 20:32:34 +00:00
};
export const updateRAGConfig = async (token: string, payload: RAGConfigForm) => {
2024-02-18 06:29:52 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/config/update`, {
2024-02-18 06:29:52 +00:00
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
},
body: JSON.stringify({
2024-03-10 20:32:34 +00:00
...payload
2024-02-18 06:29:52 +00:00
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.log(err);
error = err.detail;
return null;
});
if (error) {
throw error;
}
return res;
};
2024-02-18 06:41:03 +00:00
export const getRAGTemplate = async (token: string) => {
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/template`, {
2024-02-18 06:41:03 +00:00
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.log(err);
error = err.detail;
return null;
});
if (error) {
throw error;
}
2024-02-18 06:42:36 +00:00
return res?.template ?? '';
2024-02-18 06:41:03 +00:00
};
2024-03-03 02:56:57 +00:00
export const getQuerySettings = async (token: string) => {
2024-02-18 06:41:03 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/query/settings`, {
2024-03-03 02:56:57 +00:00
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.log(err);
error = err.detail;
return null;
});
if (error) {
throw error;
}
return res;
};
type QuerySettings = {
k: number | null;
r: number | null;
2024-03-03 02:56:57 +00:00
template: string | null;
};
export const updateQuerySettings = async (token: string, settings: QuerySettings) => {
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/query/settings/update`, {
2024-02-18 06:41:03 +00:00
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
},
body: JSON.stringify({
2024-03-03 02:56:57 +00:00
...settings
2024-02-18 06:41:03 +00:00
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.log(err);
error = err.detail;
return null;
});
if (error) {
throw error;
}
return res;
};
2024-09-28 00:23:09 +00:00
export const getEmbeddingConfig = async (token: string) => {
2024-06-18 20:50:18 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/embedding`, {
2024-09-28 00:23:09 +00:00
method: 'GET',
2024-06-18 20:50:18 +00:00
headers: {
2024-06-18 21:15:08 +00:00
'Content-Type': 'application/json',
2024-09-28 00:23:09 +00:00
Authorization: `Bearer ${token}`
}
2024-06-18 20:50:18 +00:00
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.log(err);
2024-09-28 00:23:09 +00:00
error = err.detail;
2024-06-18 20:50:18 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-09-28 00:23:09 +00:00
type OpenAIConfigForm = {
key: string;
url: string;
};
2024-01-07 07:52:22 +00:00
2024-09-28 00:23:09 +00:00
type EmbeddingModelUpdateForm = {
openai_config?: OpenAIConfigForm;
embedding_engine: string;
embedding_model: string;
embedding_batch_size?: number;
2024-01-07 07:52:22 +00:00
};
2024-09-28 00:23:09 +00:00
export const updateEmbeddingConfig = async (token: string, payload: EmbeddingModelUpdateForm) => {
2024-01-07 07:52:22 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/embedding/update`, {
2024-01-07 07:52:22 +00:00
method: 'POST',
headers: {
'Content-Type': 'application/json',
2024-09-28 00:23:09 +00:00
Authorization: `Bearer ${token}`
2024-01-07 07:52:22 +00:00
},
body: JSON.stringify({
2024-09-28 00:23:09 +00:00
...payload
2024-01-07 07:52:22 +00:00
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.log(err);
2024-09-28 00:23:09 +00:00
error = err.detail;
2024-01-07 07:52:22 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-09-28 00:23:09 +00:00
export const getRerankingConfig = async (token: string) => {
2024-05-02 00:17:00 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/reranking`, {
2024-09-28 00:23:09 +00:00
method: 'GET',
2024-05-02 00:17:00 +00:00
headers: {
'Content-Type': 'application/json',
2024-09-28 00:23:09 +00:00
Authorization: `Bearer ${token}`
}
2024-05-02 00:17:00 +00:00
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.log(err);
2024-09-28 00:23:09 +00:00
error = err.detail;
2024-05-02 00:17:00 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-09-28 00:23:09 +00:00
type RerankingModelUpdateForm = {
reranking_model: string;
};
export const updateRerankingConfig = async (token: string, payload: RerankingModelUpdateForm) => {
2024-01-07 07:52:22 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/reranking/update`, {
2024-02-01 21:35:41 +00:00
method: 'POST',
headers: {
'Content-Type': 'application/json',
2024-09-28 00:23:09 +00:00
Authorization: `Bearer ${token}`
2024-02-01 21:35:41 +00:00
},
body: JSON.stringify({
2024-09-28 00:23:09 +00:00
...payload
2024-02-01 21:35:41 +00:00
})
})
2024-01-07 07:52:22 +00:00
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
2024-09-28 00:23:09 +00:00
console.log(err);
2024-01-07 07:52:22 +00:00
error = err.detail;
return null;
});
if (error) {
throw error;
}
return res;
};
2024-01-07 17:15:45 +00:00
2024-09-28 00:23:09 +00:00
export interface SearchDocument {
status: boolean;
collection_name: string;
filenames: string[];
}
2024-10-03 04:14:58 +00:00
export const processFile = async (
token: string,
file_id: string,
collection_name: string | null = null
) => {
2024-02-03 23:57:06 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/process/file`, {
2024-02-03 23:57:06 +00:00
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({
2024-10-03 04:14:58 +00:00
file_id: file_id,
collection_name: collection_name ? collection_name : undefined
2024-02-03 23:57:06 +00:00
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
error = err.detail;
2024-09-28 00:23:09 +00:00
console.log(err);
2024-02-03 23:57:06 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-09-28 00:23:09 +00:00
export const processYoutubeVideo = async (token: string, url: string) => {
2024-06-04 04:45:36 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/process/youtube`, {
method: 'POST',
2024-06-04 04:45:36 +00:00
headers: {
Accept: 'application/json',
2024-09-28 00:23:09 +00:00
'Content-Type': 'application/json',
2024-06-04 04:45:36 +00:00
authorization: `Bearer ${token}`
2024-09-28 00:23:09 +00:00
},
body: JSON.stringify({
url: url
})
2024-06-04 04:45:36 +00:00
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
error = err.detail;
2024-09-28 00:23:09 +00:00
console.log(err);
2024-06-04 04:45:36 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-09-28 00:23:09 +00:00
export const processWeb = async (token: string, collection_name: string, url: string) => {
2024-01-07 17:15:45 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/process/web`, {
method: 'POST',
2024-01-07 17:15:45 +00:00
headers: {
Accept: 'application/json',
2024-09-28 00:23:09 +00:00
'Content-Type': 'application/json',
2024-01-07 17:15:45 +00:00
authorization: `Bearer ${token}`
2024-09-28 00:23:09 +00:00
},
body: JSON.stringify({
url: url,
collection_name: collection_name
})
2024-01-07 17:15:45 +00:00
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
error = err.detail;
2024-09-28 00:23:09 +00:00
console.log(err);
2024-01-07 17:15:45 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-09-28 00:23:09 +00:00
export const processWebSearch = async (
token: string,
query: string,
collection_name?: string
): Promise<SearchDocument | null> => {
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/process/web/search`, {
2024-09-28 00:23:09 +00:00
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
2024-09-28 00:23:09 +00:00
},
body: JSON.stringify({
query,
collection_name: collection_name ?? ''
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.log(err);
error = err.detail;
return null;
});
if (error) {
throw error;
}
return res;
};
2024-09-28 00:23:09 +00:00
export const queryDoc = async (
token: string,
collection_name: string,
query: string,
k: number | null = null
) => {
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/query/doc`, {
method: 'POST',
headers: {
2024-09-28 00:23:09 +00:00
Accept: 'application/json',
'Content-Type': 'application/json',
2024-09-28 00:23:09 +00:00
authorization: `Bearer ${token}`
},
body: JSON.stringify({
2024-09-28 00:23:09 +00:00
collection_name: collection_name,
query: query,
k: k
})
})
.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-04-22 20:49:58 +00:00
2024-09-28 00:23:09 +00:00
export const queryCollection = async (
token: string,
collection_names: string,
query: string,
k: number | null = null
) => {
2024-04-22 20:49:58 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/query/collection`, {
2024-09-28 00:23:09 +00:00
method: 'POST',
2024-04-22 20:49:58 +00:00
headers: {
2024-09-28 00:23:09 +00:00
Accept: 'application/json',
2024-04-22 20:49:58 +00:00
'Content-Type': 'application/json',
2024-09-28 00:23:09 +00:00
authorization: `Bearer ${token}`
},
body: JSON.stringify({
collection_names: collection_names,
query: query,
k: k
})
2024-04-22 20:49:58 +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-09-28 00:23:09 +00:00
export const resetUploadDir = async (token: string) => {
2024-04-22 20:49:58 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/reset/uploads`, {
2024-04-22 20:49:58 +00:00
method: 'POST',
headers: {
2024-09-28 00:23:09 +00:00
Accept: 'application/json',
authorization: `Bearer ${token}`
}
2024-04-22 20:49:58 +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-09-28 00:23:09 +00:00
export const resetVectorDB = async (token: string) => {
2024-06-02 02:52:12 +00:00
let error = null;
2024-10-05 00:03:56 +00:00
const res = await fetch(`${RETRIEVAL_API_BASE_URL}/reset/db`, {
method: 'POST',
headers: {
2024-09-28 00:23:09 +00:00
Accept: 'application/json',
authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
2024-06-02 02:52:12 +00:00
error = err.detail;
return null;
});
2024-06-02 02:52:12 +00:00
if (error) {
throw error;
}
return res;
};