index.ts 11 KB

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