index.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 ContentExtractConfigForm = {
  30. engine: string;
  31. tika_server_url: string | null;
  32. };
  33. type YoutubeConfigForm = {
  34. language: string[];
  35. translation?: string | null;
  36. };
  37. type RAGConfigForm = {
  38. pdf_extract_images?: boolean;
  39. chunk?: ChunkConfigForm;
  40. content_extraction?: ContentExtractConfigForm;
  41. web_loader_ssl_verification?: boolean;
  42. youtube?: YoutubeConfigForm;
  43. };
  44. export const updateRAGConfig = async (token: string, payload: RAGConfigForm) => {
  45. let error = null;
  46. const res = await fetch(`${RAG_API_BASE_URL}/config/update`, {
  47. method: 'POST',
  48. headers: {
  49. 'Content-Type': 'application/json',
  50. Authorization: `Bearer ${token}`
  51. },
  52. body: JSON.stringify({
  53. ...payload
  54. })
  55. })
  56. .then(async (res) => {
  57. if (!res.ok) throw await res.json();
  58. return res.json();
  59. })
  60. .catch((err) => {
  61. console.log(err);
  62. error = err.detail;
  63. return null;
  64. });
  65. if (error) {
  66. throw error;
  67. }
  68. return res;
  69. };
  70. export const getRAGTemplate = async (token: string) => {
  71. let error = null;
  72. const res = await fetch(`${RAG_API_BASE_URL}/template`, {
  73. method: 'GET',
  74. headers: {
  75. 'Content-Type': 'application/json',
  76. Authorization: `Bearer ${token}`
  77. }
  78. })
  79. .then(async (res) => {
  80. if (!res.ok) throw await res.json();
  81. return res.json();
  82. })
  83. .catch((err) => {
  84. console.log(err);
  85. error = err.detail;
  86. return null;
  87. });
  88. if (error) {
  89. throw error;
  90. }
  91. return res?.template ?? '';
  92. };
  93. export const getQuerySettings = async (token: string) => {
  94. let error = null;
  95. const res = await fetch(`${RAG_API_BASE_URL}/query/settings`, {
  96. method: 'GET',
  97. headers: {
  98. 'Content-Type': 'application/json',
  99. Authorization: `Bearer ${token}`
  100. }
  101. })
  102. .then(async (res) => {
  103. if (!res.ok) throw await res.json();
  104. return res.json();
  105. })
  106. .catch((err) => {
  107. console.log(err);
  108. error = err.detail;
  109. return null;
  110. });
  111. if (error) {
  112. throw error;
  113. }
  114. return res;
  115. };
  116. type QuerySettings = {
  117. k: number | null;
  118. r: number | null;
  119. template: string | null;
  120. };
  121. export const updateQuerySettings = async (token: string, settings: QuerySettings) => {
  122. let error = null;
  123. const res = await fetch(`${RAG_API_BASE_URL}/query/settings/update`, {
  124. method: 'POST',
  125. headers: {
  126. 'Content-Type': 'application/json',
  127. Authorization: `Bearer ${token}`
  128. },
  129. body: JSON.stringify({
  130. ...settings
  131. })
  132. })
  133. .then(async (res) => {
  134. if (!res.ok) throw await res.json();
  135. return res.json();
  136. })
  137. .catch((err) => {
  138. console.log(err);
  139. error = err.detail;
  140. return null;
  141. });
  142. if (error) {
  143. throw error;
  144. }
  145. return res;
  146. };
  147. export const processDocToVectorDB = async (token: string, file_id: string) => {
  148. let error = null;
  149. const res = await fetch(`${RAG_API_BASE_URL}/process/doc`, {
  150. method: 'POST',
  151. headers: {
  152. Accept: 'application/json',
  153. 'Content-Type': 'application/json',
  154. authorization: `Bearer ${token}`
  155. },
  156. body: JSON.stringify({
  157. file_id: file_id
  158. })
  159. })
  160. .then(async (res) => {
  161. if (!res.ok) throw await res.json();
  162. return res.json();
  163. })
  164. .catch((err) => {
  165. error = err.detail;
  166. console.log(err);
  167. return null;
  168. });
  169. if (error) {
  170. throw error;
  171. }
  172. return res;
  173. };
  174. export const uploadDocToVectorDB = async (token: string, collection_name: string, file: File) => {
  175. const data = new FormData();
  176. data.append('file', file);
  177. data.append('collection_name', collection_name);
  178. let error = null;
  179. const res = await fetch(`${RAG_API_BASE_URL}/doc`, {
  180. method: 'POST',
  181. headers: {
  182. Accept: 'application/json',
  183. authorization: `Bearer ${token}`
  184. },
  185. body: data
  186. })
  187. .then(async (res) => {
  188. if (!res.ok) throw await res.json();
  189. return res.json();
  190. })
  191. .catch((err) => {
  192. error = err.detail;
  193. console.log(err);
  194. return null;
  195. });
  196. if (error) {
  197. throw error;
  198. }
  199. return res;
  200. };
  201. export const uploadWebToVectorDB = async (token: string, collection_name: string, url: string) => {
  202. let error = null;
  203. const res = await fetch(`${RAG_API_BASE_URL}/web`, {
  204. method: 'POST',
  205. headers: {
  206. Accept: 'application/json',
  207. 'Content-Type': 'application/json',
  208. authorization: `Bearer ${token}`
  209. },
  210. body: JSON.stringify({
  211. url: url,
  212. collection_name: collection_name
  213. })
  214. })
  215. .then(async (res) => {
  216. if (!res.ok) throw await res.json();
  217. return res.json();
  218. })
  219. .catch((err) => {
  220. error = err.detail;
  221. console.log(err);
  222. return null;
  223. });
  224. if (error) {
  225. throw error;
  226. }
  227. return res;
  228. };
  229. export const uploadYoutubeTranscriptionToVectorDB = async (token: string, url: string) => {
  230. let error = null;
  231. const res = await fetch(`${RAG_API_BASE_URL}/youtube`, {
  232. method: 'POST',
  233. headers: {
  234. Accept: 'application/json',
  235. 'Content-Type': 'application/json',
  236. authorization: `Bearer ${token}`
  237. },
  238. body: JSON.stringify({
  239. url: url
  240. })
  241. })
  242. .then(async (res) => {
  243. if (!res.ok) throw await res.json();
  244. return res.json();
  245. })
  246. .catch((err) => {
  247. error = err.detail;
  248. console.log(err);
  249. return null;
  250. });
  251. if (error) {
  252. throw error;
  253. }
  254. return res;
  255. };
  256. export const queryDoc = async (
  257. token: string,
  258. collection_name: string,
  259. query: string,
  260. k: number | null = null
  261. ) => {
  262. let error = null;
  263. const res = await fetch(`${RAG_API_BASE_URL}/query/doc`, {
  264. method: 'POST',
  265. headers: {
  266. Accept: 'application/json',
  267. 'Content-Type': 'application/json',
  268. authorization: `Bearer ${token}`
  269. },
  270. body: JSON.stringify({
  271. collection_name: collection_name,
  272. query: query,
  273. k: k
  274. })
  275. })
  276. .then(async (res) => {
  277. if (!res.ok) throw await res.json();
  278. return res.json();
  279. })
  280. .catch((err) => {
  281. error = err.detail;
  282. return null;
  283. });
  284. if (error) {
  285. throw error;
  286. }
  287. return res;
  288. };
  289. export const queryCollection = async (
  290. token: string,
  291. collection_names: string,
  292. query: string,
  293. k: number | null = null
  294. ) => {
  295. let error = null;
  296. const res = await fetch(`${RAG_API_BASE_URL}/query/collection`, {
  297. method: 'POST',
  298. headers: {
  299. Accept: 'application/json',
  300. 'Content-Type': 'application/json',
  301. authorization: `Bearer ${token}`
  302. },
  303. body: JSON.stringify({
  304. collection_names: collection_names,
  305. query: query,
  306. k: k
  307. })
  308. })
  309. .then(async (res) => {
  310. if (!res.ok) throw await res.json();
  311. return res.json();
  312. })
  313. .catch((err) => {
  314. error = err.detail;
  315. return null;
  316. });
  317. if (error) {
  318. throw error;
  319. }
  320. return res;
  321. };
  322. export const scanDocs = async (token: string) => {
  323. let error = null;
  324. const res = await fetch(`${RAG_API_BASE_URL}/scan`, {
  325. method: 'GET',
  326. headers: {
  327. Accept: 'application/json',
  328. authorization: `Bearer ${token}`
  329. }
  330. })
  331. .then(async (res) => {
  332. if (!res.ok) throw await res.json();
  333. return res.json();
  334. })
  335. .catch((err) => {
  336. error = err.detail;
  337. return null;
  338. });
  339. if (error) {
  340. throw error;
  341. }
  342. return res;
  343. };
  344. export const resetUploadDir = async (token: string) => {
  345. let error = null;
  346. const res = await fetch(`${RAG_API_BASE_URL}/reset/uploads`, {
  347. method: 'GET',
  348. headers: {
  349. Accept: 'application/json',
  350. authorization: `Bearer ${token}`
  351. }
  352. })
  353. .then(async (res) => {
  354. if (!res.ok) throw await res.json();
  355. return res.json();
  356. })
  357. .catch((err) => {
  358. error = err.detail;
  359. return null;
  360. });
  361. if (error) {
  362. throw error;
  363. }
  364. return res;
  365. };
  366. export const resetVectorDB = async (token: string) => {
  367. let error = null;
  368. const res = await fetch(`${RAG_API_BASE_URL}/reset/db`, {
  369. method: 'GET',
  370. headers: {
  371. Accept: 'application/json',
  372. authorization: `Bearer ${token}`
  373. }
  374. })
  375. .then(async (res) => {
  376. if (!res.ok) throw await res.json();
  377. return res.json();
  378. })
  379. .catch((err) => {
  380. error = err.detail;
  381. return null;
  382. });
  383. if (error) {
  384. throw error;
  385. }
  386. return res;
  387. };
  388. export const getEmbeddingConfig = async (token: string) => {
  389. let error = null;
  390. const res = await fetch(`${RAG_API_BASE_URL}/embedding`, {
  391. method: 'GET',
  392. headers: {
  393. 'Content-Type': 'application/json',
  394. Authorization: `Bearer ${token}`
  395. }
  396. })
  397. .then(async (res) => {
  398. if (!res.ok) throw await res.json();
  399. return res.json();
  400. })
  401. .catch((err) => {
  402. console.log(err);
  403. error = err.detail;
  404. return null;
  405. });
  406. if (error) {
  407. throw error;
  408. }
  409. return res;
  410. };
  411. type OpenAIConfigForm = {
  412. key: string;
  413. url: string;
  414. batch_size: number;
  415. };
  416. type EmbeddingModelUpdateForm = {
  417. openai_config?: OpenAIConfigForm;
  418. embedding_engine: string;
  419. embedding_model: string;
  420. };
  421. export const updateEmbeddingConfig = async (token: string, payload: EmbeddingModelUpdateForm) => {
  422. let error = null;
  423. const res = await fetch(`${RAG_API_BASE_URL}/embedding/update`, {
  424. method: 'POST',
  425. headers: {
  426. 'Content-Type': 'application/json',
  427. Authorization: `Bearer ${token}`
  428. },
  429. body: JSON.stringify({
  430. ...payload
  431. })
  432. })
  433. .then(async (res) => {
  434. if (!res.ok) throw await res.json();
  435. return res.json();
  436. })
  437. .catch((err) => {
  438. console.log(err);
  439. error = err.detail;
  440. return null;
  441. });
  442. if (error) {
  443. throw error;
  444. }
  445. return res;
  446. };
  447. export const getRerankingConfig = async (token: string) => {
  448. let error = null;
  449. const res = await fetch(`${RAG_API_BASE_URL}/reranking`, {
  450. method: 'GET',
  451. headers: {
  452. 'Content-Type': 'application/json',
  453. Authorization: `Bearer ${token}`
  454. }
  455. })
  456. .then(async (res) => {
  457. if (!res.ok) throw await res.json();
  458. return res.json();
  459. })
  460. .catch((err) => {
  461. console.log(err);
  462. error = err.detail;
  463. return null;
  464. });
  465. if (error) {
  466. throw error;
  467. }
  468. return res;
  469. };
  470. type RerankingModelUpdateForm = {
  471. reranking_model: string;
  472. };
  473. export const updateRerankingConfig = async (token: string, payload: RerankingModelUpdateForm) => {
  474. let error = null;
  475. const res = await fetch(`${RAG_API_BASE_URL}/reranking/update`, {
  476. method: 'POST',
  477. headers: {
  478. 'Content-Type': 'application/json',
  479. Authorization: `Bearer ${token}`
  480. },
  481. body: JSON.stringify({
  482. ...payload
  483. })
  484. })
  485. .then(async (res) => {
  486. if (!res.ok) throw await res.json();
  487. return res.json();
  488. })
  489. .catch((err) => {
  490. console.log(err);
  491. error = err.detail;
  492. return null;
  493. });
  494. if (error) {
  495. throw error;
  496. }
  497. return res;
  498. };
  499. export const runWebSearch = async (
  500. token: string,
  501. query: string,
  502. collection_name?: string
  503. ): Promise<SearchDocument | null> => {
  504. let error = null;
  505. const res = await fetch(`${RAG_API_BASE_URL}/web/search`, {
  506. method: 'POST',
  507. headers: {
  508. 'Content-Type': 'application/json',
  509. Authorization: `Bearer ${token}`
  510. },
  511. body: JSON.stringify({
  512. query,
  513. collection_name: collection_name ?? ''
  514. })
  515. })
  516. .then(async (res) => {
  517. if (!res.ok) throw await res.json();
  518. return res.json();
  519. })
  520. .catch((err) => {
  521. console.log(err);
  522. error = err.detail;
  523. return null;
  524. });
  525. if (error) {
  526. throw error;
  527. }
  528. return res;
  529. };
  530. export interface SearchDocument {
  531. status: boolean;
  532. collection_name: string;
  533. filenames: string[];
  534. }