index.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. };
  33. export const updateRAGConfig = async (token: string, payload: RAGConfigForm) => {
  34. let error = null;
  35. const res = await fetch(`${RAG_API_BASE_URL}/config/update`, {
  36. method: 'POST',
  37. headers: {
  38. 'Content-Type': 'application/json',
  39. Authorization: `Bearer ${token}`
  40. },
  41. body: JSON.stringify({
  42. ...payload
  43. })
  44. })
  45. .then(async (res) => {
  46. if (!res.ok) throw await res.json();
  47. return res.json();
  48. })
  49. .catch((err) => {
  50. console.log(err);
  51. error = err.detail;
  52. return null;
  53. });
  54. if (error) {
  55. throw error;
  56. }
  57. return res;
  58. };
  59. export const getRAGTemplate = async (token: string) => {
  60. let error = null;
  61. const res = await fetch(`${RAG_API_BASE_URL}/template`, {
  62. method: 'GET',
  63. headers: {
  64. 'Content-Type': 'application/json',
  65. Authorization: `Bearer ${token}`
  66. }
  67. })
  68. .then(async (res) => {
  69. if (!res.ok) throw await res.json();
  70. return res.json();
  71. })
  72. .catch((err) => {
  73. console.log(err);
  74. error = err.detail;
  75. return null;
  76. });
  77. if (error) {
  78. throw error;
  79. }
  80. return res?.template ?? '';
  81. };
  82. export const getQuerySettings = async (token: string) => {
  83. let error = null;
  84. const res = await fetch(`${RAG_API_BASE_URL}/query/settings`, {
  85. method: 'GET',
  86. headers: {
  87. 'Content-Type': 'application/json',
  88. Authorization: `Bearer ${token}`
  89. }
  90. })
  91. .then(async (res) => {
  92. if (!res.ok) throw await res.json();
  93. return res.json();
  94. })
  95. .catch((err) => {
  96. console.log(err);
  97. error = err.detail;
  98. return null;
  99. });
  100. if (error) {
  101. throw error;
  102. }
  103. return res;
  104. };
  105. type QuerySettings = {
  106. k: number | null;
  107. template: string | null;
  108. };
  109. export const updateQuerySettings = async (token: string, settings: QuerySettings) => {
  110. let error = null;
  111. const res = await fetch(`${RAG_API_BASE_URL}/query/settings/update`, {
  112. method: 'POST',
  113. headers: {
  114. 'Content-Type': 'application/json',
  115. Authorization: `Bearer ${token}`
  116. },
  117. body: JSON.stringify({
  118. ...settings
  119. })
  120. })
  121. .then(async (res) => {
  122. if (!res.ok) throw await res.json();
  123. return res.json();
  124. })
  125. .catch((err) => {
  126. console.log(err);
  127. error = err.detail;
  128. return null;
  129. });
  130. if (error) {
  131. throw error;
  132. }
  133. return res;
  134. };
  135. export const uploadDocToVectorDB = async (token: string, collection_name: string, file: File) => {
  136. const data = new FormData();
  137. data.append('file', file);
  138. data.append('collection_name', collection_name);
  139. let error = null;
  140. const res = await fetch(`${RAG_API_BASE_URL}/doc`, {
  141. method: 'POST',
  142. headers: {
  143. Accept: 'application/json',
  144. authorization: `Bearer ${token}`
  145. },
  146. body: data
  147. })
  148. .then(async (res) => {
  149. if (!res.ok) throw await res.json();
  150. return res.json();
  151. })
  152. .catch((err) => {
  153. error = err.detail;
  154. console.log(err);
  155. return null;
  156. });
  157. if (error) {
  158. throw error;
  159. }
  160. return res;
  161. };
  162. export const uploadWebToVectorDB = async (token: string, collection_name: string, url: string) => {
  163. let error = null;
  164. const res = await fetch(`${RAG_API_BASE_URL}/web`, {
  165. method: 'POST',
  166. headers: {
  167. Accept: 'application/json',
  168. 'Content-Type': 'application/json',
  169. authorization: `Bearer ${token}`
  170. },
  171. body: JSON.stringify({
  172. url: url,
  173. collection_name: collection_name
  174. })
  175. })
  176. .then(async (res) => {
  177. if (!res.ok) throw await res.json();
  178. return res.json();
  179. })
  180. .catch((err) => {
  181. error = err.detail;
  182. console.log(err);
  183. return null;
  184. });
  185. if (error) {
  186. throw error;
  187. }
  188. return res;
  189. };
  190. export const queryDoc = async (
  191. token: string,
  192. collection_name: string,
  193. query: string,
  194. k: number | null = null
  195. ) => {
  196. let error = null;
  197. const res = await fetch(`${RAG_API_BASE_URL}/query/doc`, {
  198. method: 'POST',
  199. headers: {
  200. Accept: 'application/json',
  201. 'Content-Type': 'application/json',
  202. authorization: `Bearer ${token}`
  203. },
  204. body: JSON.stringify({
  205. collection_name: collection_name,
  206. query: query,
  207. k: k
  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. return null;
  217. });
  218. if (error) {
  219. throw error;
  220. }
  221. return res;
  222. };
  223. export const queryCollection = async (
  224. token: string,
  225. collection_names: string,
  226. query: string,
  227. k: number | null = null
  228. ) => {
  229. let error = null;
  230. const res = await fetch(`${RAG_API_BASE_URL}/query/collection`, {
  231. method: 'POST',
  232. headers: {
  233. Accept: 'application/json',
  234. 'Content-Type': 'application/json',
  235. authorization: `Bearer ${token}`
  236. },
  237. body: JSON.stringify({
  238. collection_names: collection_names,
  239. query: query,
  240. k: k
  241. })
  242. })
  243. .then(async (res) => {
  244. if (!res.ok) throw await res.json();
  245. return res.json();
  246. })
  247. .catch((err) => {
  248. error = err.detail;
  249. return null;
  250. });
  251. if (error) {
  252. throw error;
  253. }
  254. return res;
  255. };
  256. export const scanDocs = async (token: string) => {
  257. let error = null;
  258. const res = await fetch(`${RAG_API_BASE_URL}/scan`, {
  259. method: 'GET',
  260. headers: {
  261. Accept: 'application/json',
  262. authorization: `Bearer ${token}`
  263. }
  264. })
  265. .then(async (res) => {
  266. if (!res.ok) throw await res.json();
  267. return res.json();
  268. })
  269. .catch((err) => {
  270. error = err.detail;
  271. return null;
  272. });
  273. if (error) {
  274. throw error;
  275. }
  276. return res;
  277. };
  278. export const resetVectorDB = async (token: string) => {
  279. let error = null;
  280. const res = await fetch(`${RAG_API_BASE_URL}/reset`, {
  281. method: 'GET',
  282. headers: {
  283. Accept: 'application/json',
  284. authorization: `Bearer ${token}`
  285. }
  286. })
  287. .then(async (res) => {
  288. if (!res.ok) throw await res.json();
  289. return res.json();
  290. })
  291. .catch((err) => {
  292. error = err.detail;
  293. return null;
  294. });
  295. if (error) {
  296. throw error;
  297. }
  298. return res;
  299. };
  300. export const getEmbeddingModel = async (token: string) => {
  301. let error = null;
  302. const res = await fetch(`${RAG_API_BASE_URL}/embedding/model`, {
  303. method: 'GET',
  304. headers: {
  305. 'Content-Type': 'application/json',
  306. Authorization: `Bearer ${token}`
  307. }
  308. })
  309. .then(async (res) => {
  310. if (!res.ok) throw await res.json();
  311. return res.json();
  312. })
  313. .catch((err) => {
  314. console.log(err);
  315. error = err.detail;
  316. return null;
  317. });
  318. if (error) {
  319. throw error;
  320. }
  321. return res;
  322. };
  323. type EmbeddingModelUpdateForm = {
  324. embedding_model: string;
  325. };
  326. export const updateEmbeddingModel = async (token: string, payload: EmbeddingModelUpdateForm) => {
  327. let error = null;
  328. const res = await fetch(`${RAG_API_BASE_URL}/embedding/model/update`, {
  329. method: 'POST',
  330. headers: {
  331. 'Content-Type': 'application/json',
  332. Authorization: `Bearer ${token}`
  333. },
  334. body: JSON.stringify({
  335. ...payload
  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. };