index.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 getQuerySettings = async (token: string) => {
  79. let error = null;
  80. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/query/settings`, {
  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;
  100. };
  101. type QuerySettings = {
  102. k: number | null;
  103. r: number | null;
  104. template: string | null;
  105. };
  106. export const updateQuerySettings = async (token: string, settings: QuerySettings) => {
  107. let error = null;
  108. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/query/settings/update`, {
  109. method: 'POST',
  110. headers: {
  111. 'Content-Type': 'application/json',
  112. Authorization: `Bearer ${token}`
  113. },
  114. body: JSON.stringify({
  115. ...settings
  116. })
  117. })
  118. .then(async (res) => {
  119. if (!res.ok) throw await res.json();
  120. return res.json();
  121. })
  122. .catch((err) => {
  123. console.log(err);
  124. error = err.detail;
  125. return null;
  126. });
  127. if (error) {
  128. throw error;
  129. }
  130. return res;
  131. };
  132. export const getEmbeddingConfig = async (token: string) => {
  133. let error = null;
  134. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/embedding`, {
  135. method: 'GET',
  136. headers: {
  137. 'Content-Type': 'application/json',
  138. Authorization: `Bearer ${token}`
  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. type OpenAIConfigForm = {
  156. key: string;
  157. url: string;
  158. };
  159. type EmbeddingModelUpdateForm = {
  160. openai_config?: OpenAIConfigForm;
  161. embedding_engine: string;
  162. embedding_model: string;
  163. embedding_batch_size?: number;
  164. };
  165. export const updateEmbeddingConfig = async (token: string, payload: EmbeddingModelUpdateForm) => {
  166. let error = null;
  167. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/embedding/update`, {
  168. method: 'POST',
  169. headers: {
  170. 'Content-Type': 'application/json',
  171. Authorization: `Bearer ${token}`
  172. },
  173. body: JSON.stringify({
  174. ...payload
  175. })
  176. })
  177. .then(async (res) => {
  178. if (!res.ok) throw await res.json();
  179. return res.json();
  180. })
  181. .catch((err) => {
  182. console.log(err);
  183. error = err.detail;
  184. return null;
  185. });
  186. if (error) {
  187. throw error;
  188. }
  189. return res;
  190. };
  191. export const getRerankingConfig = async (token: string) => {
  192. let error = null;
  193. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/reranking`, {
  194. method: 'GET',
  195. headers: {
  196. 'Content-Type': 'application/json',
  197. Authorization: `Bearer ${token}`
  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. type RerankingModelUpdateForm = {
  215. reranking_model: string;
  216. };
  217. export const updateRerankingConfig = async (token: string, payload: RerankingModelUpdateForm) => {
  218. let error = null;
  219. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/reranking/update`, {
  220. method: 'POST',
  221. headers: {
  222. 'Content-Type': 'application/json',
  223. Authorization: `Bearer ${token}`
  224. },
  225. body: JSON.stringify({
  226. ...payload
  227. })
  228. })
  229. .then(async (res) => {
  230. if (!res.ok) throw await res.json();
  231. return res.json();
  232. })
  233. .catch((err) => {
  234. console.log(err);
  235. error = err.detail;
  236. return null;
  237. });
  238. if (error) {
  239. throw error;
  240. }
  241. return res;
  242. };
  243. export interface SearchDocument {
  244. status: boolean;
  245. collection_name: string;
  246. filenames: string[];
  247. }
  248. export const processFile = async (
  249. token: string,
  250. file_id: string,
  251. collection_name: string | null = null
  252. ) => {
  253. let error = null;
  254. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/process/file`, {
  255. method: 'POST',
  256. headers: {
  257. Accept: 'application/json',
  258. 'Content-Type': 'application/json',
  259. authorization: `Bearer ${token}`
  260. },
  261. body: JSON.stringify({
  262. file_id: file_id,
  263. collection_name: collection_name ? collection_name : undefined
  264. })
  265. })
  266. .then(async (res) => {
  267. if (!res.ok) throw await res.json();
  268. return res.json();
  269. })
  270. .catch((err) => {
  271. error = err.detail;
  272. console.log(err);
  273. return null;
  274. });
  275. if (error) {
  276. throw error;
  277. }
  278. return res;
  279. };
  280. export const processYoutubeVideo = async (token: string, url: string) => {
  281. let error = null;
  282. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/process/youtube`, {
  283. method: 'POST',
  284. headers: {
  285. Accept: 'application/json',
  286. 'Content-Type': 'application/json',
  287. authorization: `Bearer ${token}`
  288. },
  289. body: JSON.stringify({
  290. url: url
  291. })
  292. })
  293. .then(async (res) => {
  294. if (!res.ok) throw await res.json();
  295. return res.json();
  296. })
  297. .catch((err) => {
  298. error = err.detail;
  299. console.log(err);
  300. return null;
  301. });
  302. if (error) {
  303. throw error;
  304. }
  305. return res;
  306. };
  307. export const processWeb = async (token: string, collection_name: string, url: string) => {
  308. let error = null;
  309. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/process/web`, {
  310. method: 'POST',
  311. headers: {
  312. Accept: 'application/json',
  313. 'Content-Type': 'application/json',
  314. authorization: `Bearer ${token}`
  315. },
  316. body: JSON.stringify({
  317. url: url,
  318. collection_name: collection_name
  319. })
  320. })
  321. .then(async (res) => {
  322. if (!res.ok) throw await res.json();
  323. return res.json();
  324. })
  325. .catch((err) => {
  326. error = err.detail;
  327. console.log(err);
  328. return null;
  329. });
  330. if (error) {
  331. throw error;
  332. }
  333. return res;
  334. };
  335. export const processWebSearch = async (
  336. token: string,
  337. query: string,
  338. collection_name?: string
  339. ): Promise<SearchDocument | null> => {
  340. let error = null;
  341. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/process/web/search`, {
  342. method: 'POST',
  343. headers: {
  344. 'Content-Type': 'application/json',
  345. Authorization: `Bearer ${token}`
  346. },
  347. body: JSON.stringify({
  348. query,
  349. collection_name: collection_name ?? ''
  350. })
  351. })
  352. .then(async (res) => {
  353. if (!res.ok) throw await res.json();
  354. return res.json();
  355. })
  356. .catch((err) => {
  357. console.log(err);
  358. error = err.detail;
  359. return null;
  360. });
  361. if (error) {
  362. throw error;
  363. }
  364. return res;
  365. };
  366. export const queryDoc = async (
  367. token: string,
  368. collection_name: string,
  369. query: string,
  370. k: number | null = null
  371. ) => {
  372. let error = null;
  373. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/query/doc`, {
  374. method: 'POST',
  375. headers: {
  376. Accept: 'application/json',
  377. 'Content-Type': 'application/json',
  378. authorization: `Bearer ${token}`
  379. },
  380. body: JSON.stringify({
  381. collection_name: collection_name,
  382. query: query,
  383. k: k
  384. })
  385. })
  386. .then(async (res) => {
  387. if (!res.ok) throw await res.json();
  388. return res.json();
  389. })
  390. .catch((err) => {
  391. error = err.detail;
  392. return null;
  393. });
  394. if (error) {
  395. throw error;
  396. }
  397. return res;
  398. };
  399. export const queryCollection = async (
  400. token: string,
  401. collection_names: string,
  402. query: string,
  403. k: number | null = null
  404. ) => {
  405. let error = null;
  406. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/query/collection`, {
  407. method: 'POST',
  408. headers: {
  409. Accept: 'application/json',
  410. 'Content-Type': 'application/json',
  411. authorization: `Bearer ${token}`
  412. },
  413. body: JSON.stringify({
  414. collection_names: collection_names,
  415. query: query,
  416. k: k
  417. })
  418. })
  419. .then(async (res) => {
  420. if (!res.ok) throw await res.json();
  421. return res.json();
  422. })
  423. .catch((err) => {
  424. error = err.detail;
  425. return null;
  426. });
  427. if (error) {
  428. throw error;
  429. }
  430. return res;
  431. };
  432. export const resetUploadDir = async (token: string) => {
  433. let error = null;
  434. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/reset/uploads`, {
  435. method: 'POST',
  436. headers: {
  437. Accept: 'application/json',
  438. authorization: `Bearer ${token}`
  439. }
  440. })
  441. .then(async (res) => {
  442. if (!res.ok) throw await res.json();
  443. return res.json();
  444. })
  445. .catch((err) => {
  446. error = err.detail;
  447. return null;
  448. });
  449. if (error) {
  450. throw error;
  451. }
  452. return res;
  453. };
  454. export const resetVectorDB = async (token: string) => {
  455. let error = null;
  456. const res = await fetch(`${RETRIEVAL_API_BASE_URL}/reset/db`, {
  457. method: 'POST',
  458. headers: {
  459. Accept: 'application/json',
  460. authorization: `Bearer ${token}`
  461. }
  462. })
  463. .then(async (res) => {
  464. if (!res.ok) throw await res.json();
  465. return res.json();
  466. })
  467. .catch((err) => {
  468. error = err.detail;
  469. return null;
  470. });
  471. if (error) {
  472. throw error;
  473. }
  474. return res;
  475. };