index.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. import { RAG_API_BASE_URL } from '$lib/constants';
  2. export const getRAGConfig = async (token: string) => {
  3. let error = null;
  4. const res = await fetch(`${RAG_API_BASE_URL}/config`, {
  5. method: 'GET',
  6. headers: {
  7. 'Content-Type': 'application/json',
  8. Authorization: `Bearer ${token}`
  9. }
  10. })
  11. .then(async (res) => {
  12. if (!res.ok) throw await res.json();
  13. return res.json();
  14. })
  15. .catch((err) => {
  16. console.log(err);
  17. error = err.detail;
  18. return null;
  19. });
  20. if (error) {
  21. throw error;
  22. }
  23. return res;
  24. };
  25. type ChunkConfigForm = {
  26. chunk_size: number;
  27. chunk_overlap: number;
  28. };
  29. type RAGConfigForm = {
  30. pdf_extract_images?: boolean;
  31. chunk?: ChunkConfigForm;
  32. web_loader_ssl_verification?: boolean;
  33. };
  34. export const updateRAGConfig = async (token: string, payload: RAGConfigForm) => {
  35. let error = null;
  36. const res = await fetch(`${RAG_API_BASE_URL}/config/update`, {
  37. method: 'POST',
  38. headers: {
  39. 'Content-Type': 'application/json',
  40. Authorization: `Bearer ${token}`
  41. },
  42. body: JSON.stringify({
  43. ...payload
  44. })
  45. })
  46. .then(async (res) => {
  47. if (!res.ok) throw await res.json();
  48. return res.json();
  49. })
  50. .catch((err) => {
  51. console.log(err);
  52. error = err.detail;
  53. return null;
  54. });
  55. if (error) {
  56. throw error;
  57. }
  58. return res;
  59. };
  60. export const getRAGTemplate = async (token: string) => {
  61. let error = null;
  62. const res = await fetch(`${RAG_API_BASE_URL}/template`, {
  63. method: 'GET',
  64. headers: {
  65. 'Content-Type': 'application/json',
  66. Authorization: `Bearer ${token}`
  67. }
  68. })
  69. .then(async (res) => {
  70. if (!res.ok) throw await res.json();
  71. return res.json();
  72. })
  73. .catch((err) => {
  74. console.log(err);
  75. error = err.detail;
  76. return null;
  77. });
  78. if (error) {
  79. throw error;
  80. }
  81. return res?.template ?? '';
  82. };
  83. export const getQuerySettings = async (token: string) => {
  84. let error = null;
  85. const res = await fetch(`${RAG_API_BASE_URL}/query/settings`, {
  86. method: 'GET',
  87. headers: {
  88. 'Content-Type': 'application/json',
  89. Authorization: `Bearer ${token}`
  90. }
  91. })
  92. .then(async (res) => {
  93. if (!res.ok) throw await res.json();
  94. return res.json();
  95. })
  96. .catch((err) => {
  97. console.log(err);
  98. error = err.detail;
  99. return null;
  100. });
  101. if (error) {
  102. throw error;
  103. }
  104. return res;
  105. };
  106. type QuerySettings = {
  107. k: number | null;
  108. r: number | null;
  109. template: string | null;
  110. };
  111. export const updateQuerySettings = async (token: string, settings: QuerySettings) => {
  112. let error = null;
  113. const res = await fetch(`${RAG_API_BASE_URL}/query/settings/update`, {
  114. method: 'POST',
  115. headers: {
  116. 'Content-Type': 'application/json',
  117. Authorization: `Bearer ${token}`
  118. },
  119. body: JSON.stringify({
  120. ...settings
  121. })
  122. })
  123. .then(async (res) => {
  124. if (!res.ok) throw await res.json();
  125. return res.json();
  126. })
  127. .catch((err) => {
  128. console.log(err);
  129. error = err.detail;
  130. return null;
  131. });
  132. if (error) {
  133. throw error;
  134. }
  135. return res;
  136. };
  137. export const uploadDocToVectorDB = async (token: string, collection_name: string, file: File) => {
  138. const data = new FormData();
  139. data.append('file', file);
  140. data.append('collection_name', collection_name);
  141. let error = null;
  142. const res = await fetch(`${RAG_API_BASE_URL}/doc`, {
  143. method: 'POST',
  144. headers: {
  145. Accept: 'application/json',
  146. authorization: `Bearer ${token}`
  147. },
  148. body: data
  149. })
  150. .then(async (res) => {
  151. if (!res.ok) throw await res.json();
  152. return res.json();
  153. })
  154. .catch((err) => {
  155. error = err.detail;
  156. console.log(err);
  157. return null;
  158. });
  159. if (error) {
  160. throw error;
  161. }
  162. return res;
  163. };
  164. export const uploadWebToVectorDB = async (token: string, collection_name: string, url: string) => {
  165. let error = null;
  166. const res = await fetch(`${RAG_API_BASE_URL}/web`, {
  167. method: 'POST',
  168. headers: {
  169. Accept: 'application/json',
  170. 'Content-Type': 'application/json',
  171. authorization: `Bearer ${token}`
  172. },
  173. body: JSON.stringify({
  174. url: url,
  175. collection_name: collection_name
  176. })
  177. })
  178. .then(async (res) => {
  179. if (!res.ok) throw await res.json();
  180. return res.json();
  181. })
  182. .catch((err) => {
  183. error = err.detail;
  184. console.log(err);
  185. return null;
  186. });
  187. if (error) {
  188. throw error;
  189. }
  190. return res;
  191. };
  192. export const uploadYoutubeTranscriptionToVectorDB = async (token: string, url: string) => {
  193. let error = null;
  194. const res = await fetch(`${RAG_API_BASE_URL}/youtube`, {
  195. method: 'POST',
  196. headers: {
  197. Accept: 'application/json',
  198. 'Content-Type': 'application/json',
  199. authorization: `Bearer ${token}`
  200. },
  201. body: JSON.stringify({
  202. url: url
  203. })
  204. })
  205. .then(async (res) => {
  206. if (!res.ok) throw await res.json();
  207. return res.json();
  208. })
  209. .catch((err) => {
  210. error = err.detail;
  211. console.log(err);
  212. return null;
  213. });
  214. if (error) {
  215. throw error;
  216. }
  217. return res;
  218. };
  219. export const queryDoc = async (
  220. token: string,
  221. collection_name: string,
  222. query: string,
  223. k: number | null = null
  224. ) => {
  225. let error = null;
  226. const res = await fetch(`${RAG_API_BASE_URL}/query/doc`, {
  227. method: 'POST',
  228. headers: {
  229. Accept: 'application/json',
  230. 'Content-Type': 'application/json',
  231. authorization: `Bearer ${token}`
  232. },
  233. body: JSON.stringify({
  234. collection_name: collection_name,
  235. query: query,
  236. k: k
  237. })
  238. })
  239. .then(async (res) => {
  240. if (!res.ok) throw await res.json();
  241. return res.json();
  242. })
  243. .catch((err) => {
  244. error = err.detail;
  245. return null;
  246. });
  247. if (error) {
  248. throw error;
  249. }
  250. return res;
  251. };
  252. export const queryCollection = async (
  253. token: string,
  254. collection_names: string,
  255. query: string,
  256. k: number | null = null
  257. ) => {
  258. let error = null;
  259. const res = await fetch(`${RAG_API_BASE_URL}/query/collection`, {
  260. method: 'POST',
  261. headers: {
  262. Accept: 'application/json',
  263. 'Content-Type': 'application/json',
  264. authorization: `Bearer ${token}`
  265. },
  266. body: JSON.stringify({
  267. collection_names: collection_names,
  268. query: query,
  269. k: k
  270. })
  271. })
  272. .then(async (res) => {
  273. if (!res.ok) throw await res.json();
  274. return res.json();
  275. })
  276. .catch((err) => {
  277. error = err.detail;
  278. return null;
  279. });
  280. if (error) {
  281. throw error;
  282. }
  283. return res;
  284. };
  285. export const scanDocs = async (token: string) => {
  286. let error = null;
  287. const res = await fetch(`${RAG_API_BASE_URL}/scan`, {
  288. method: 'GET',
  289. headers: {
  290. Accept: 'application/json',
  291. authorization: `Bearer ${token}`
  292. }
  293. })
  294. .then(async (res) => {
  295. if (!res.ok) throw await res.json();
  296. return res.json();
  297. })
  298. .catch((err) => {
  299. error = err.detail;
  300. return null;
  301. });
  302. if (error) {
  303. throw error;
  304. }
  305. return res;
  306. };
  307. export const resetVectorDB = async (token: string) => {
  308. let error = null;
  309. const res = await fetch(`${RAG_API_BASE_URL}/reset`, {
  310. method: 'GET',
  311. headers: {
  312. Accept: 'application/json',
  313. authorization: `Bearer ${token}`
  314. }
  315. })
  316. .then(async (res) => {
  317. if (!res.ok) throw await res.json();
  318. return res.json();
  319. })
  320. .catch((err) => {
  321. error = err.detail;
  322. return null;
  323. });
  324. if (error) {
  325. throw error;
  326. }
  327. return res;
  328. };
  329. export const getEmbeddingConfig = async (token: string) => {
  330. let error = null;
  331. const res = await fetch(`${RAG_API_BASE_URL}/embedding`, {
  332. method: 'GET',
  333. headers: {
  334. 'Content-Type': 'application/json',
  335. Authorization: `Bearer ${token}`
  336. }
  337. })
  338. .then(async (res) => {
  339. if (!res.ok) throw await res.json();
  340. return res.json();
  341. })
  342. .catch((err) => {
  343. console.log(err);
  344. error = err.detail;
  345. return null;
  346. });
  347. if (error) {
  348. throw error;
  349. }
  350. return res;
  351. };
  352. type OpenAIConfigForm = {
  353. key: string;
  354. url: string;
  355. };
  356. type EmbeddingModelUpdateForm = {
  357. openai_config?: OpenAIConfigForm;
  358. embedding_engine: string;
  359. embedding_model: string;
  360. };
  361. export const updateEmbeddingConfig = async (token: string, payload: EmbeddingModelUpdateForm) => {
  362. let error = null;
  363. const res = await fetch(`${RAG_API_BASE_URL}/embedding/update`, {
  364. method: 'POST',
  365. headers: {
  366. 'Content-Type': 'application/json',
  367. Authorization: `Bearer ${token}`
  368. },
  369. body: JSON.stringify({
  370. ...payload
  371. })
  372. })
  373. .then(async (res) => {
  374. if (!res.ok) throw await res.json();
  375. return res.json();
  376. })
  377. .catch((err) => {
  378. console.log(err);
  379. error = err.detail;
  380. return null;
  381. });
  382. if (error) {
  383. throw error;
  384. }
  385. return res;
  386. };
  387. export const getRerankingConfig = async (token: string) => {
  388. let error = null;
  389. const res = await fetch(`${RAG_API_BASE_URL}/reranking`, {
  390. method: 'GET',
  391. headers: {
  392. 'Content-Type': 'application/json',
  393. Authorization: `Bearer ${token}`
  394. }
  395. })
  396. .then(async (res) => {
  397. if (!res.ok) throw await res.json();
  398. return res.json();
  399. })
  400. .catch((err) => {
  401. console.log(err);
  402. error = err.detail;
  403. return null;
  404. });
  405. if (error) {
  406. throw error;
  407. }
  408. return res;
  409. };
  410. type RerankingModelUpdateForm = {
  411. reranking_model: string;
  412. };
  413. export const updateRerankingConfig = async (token: string, payload: RerankingModelUpdateForm) => {
  414. let error = null;
  415. const res = await fetch(`${RAG_API_BASE_URL}/reranking/update`, {
  416. method: 'POST',
  417. headers: {
  418. 'Content-Type': 'application/json',
  419. Authorization: `Bearer ${token}`
  420. },
  421. body: JSON.stringify({
  422. ...payload
  423. })
  424. })
  425. .then(async (res) => {
  426. if (!res.ok) throw await res.json();
  427. return res.json();
  428. })
  429. .catch((err) => {
  430. console.log(err);
  431. error = err.detail;
  432. return null;
  433. });
  434. if (error) {
  435. throw error;
  436. }
  437. return res;
  438. };