index.ts 9.2 KB

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