浏览代码

feat: rag apis added to frontend

Timothy J. Baek 1 年之前
父节点
当前提交
3229ec116c
共有 2 个文件被更改,包括 109 次插入0 次删除
  1. 108 0
      src/lib/apis/rag/index.ts
  2. 1 0
      src/lib/constants.ts

+ 108 - 0
src/lib/apis/rag/index.ts

@@ -0,0 +1,108 @@
+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',
+			'Content-Type': '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;
+};
+
+export const queryVectorDB = async (
+	token: string,
+	collection_name: string,
+	query: string,
+	k: number
+) => {
+	let error = null;
+	const searchParams = new URLSearchParams();
+
+	searchParams.set('query', query);
+	if (k) {
+		searchParams.set('k', k.toString());
+	}
+
+	const res = await fetch(
+		`${RAG_API_BASE_URL}/query/${collection_name}/?${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();
+		})
+		.catch((err) => {
+			error = err.detail;
+			console.log(err);
+			return null;
+		});
+
+	if (error) {
+		throw error;
+	}
+
+	return res;
+};

+ 1 - 0
src/lib/constants.ts

@@ -5,6 +5,7 @@ export const WEBUI_BASE_URL = dev ? `http://${location.hostname}:8080` : ``;
 export const WEBUI_API_BASE_URL = `${WEBUI_BASE_URL}/api/v1`;
 export const OLLAMA_API_BASE_URL = `${WEBUI_BASE_URL}/ollama/api`;
 export const OPENAI_API_BASE_URL = `${WEBUI_BASE_URL}/openai/api`;
+export const RAG_API_BASE_URL = `${WEBUI_BASE_URL}/rag/api/v1`;
 
 export const WEB_UI_VERSION = 'v1.0.0-alpha-static';