index.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import { RAG_API_BASE_URL } from '$lib/constants';
  2. export const getChunkParams = async (token: string) => {
  3. let error = null;
  4. const res = await fetch(`${RAG_API_BASE_URL}/chunk`, {
  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. export const updateChunkParams = async (token: string, size: number, overlap: number) => {
  26. let error = null;
  27. const res = await fetch(`${RAG_API_BASE_URL}/chunk/update`, {
  28. method: 'POST',
  29. headers: {
  30. 'Content-Type': 'application/json',
  31. Authorization: `Bearer ${token}`
  32. },
  33. body: JSON.stringify({
  34. chunk_size: size,
  35. chunk_overlap: overlap
  36. })
  37. })
  38. .then(async (res) => {
  39. if (!res.ok) throw await res.json();
  40. return res.json();
  41. })
  42. .catch((err) => {
  43. console.log(err);
  44. error = err.detail;
  45. return null;
  46. });
  47. if (error) {
  48. throw error;
  49. }
  50. return res;
  51. };
  52. export const getRAGTemplate = async (token: string) => {
  53. let error = null;
  54. const res = await fetch(`${RAG_API_BASE_URL}/template`, {
  55. method: 'GET',
  56. headers: {
  57. 'Content-Type': 'application/json',
  58. Authorization: `Bearer ${token}`
  59. }
  60. })
  61. .then(async (res) => {
  62. if (!res.ok) throw await res.json();
  63. return res.json();
  64. })
  65. .catch((err) => {
  66. console.log(err);
  67. error = err.detail;
  68. return null;
  69. });
  70. if (error) {
  71. throw error;
  72. }
  73. return res?.template ?? '';
  74. };
  75. export const getQuerySettings = async (token: string) => {
  76. let error = null;
  77. const res = await fetch(`${RAG_API_BASE_URL}/query/settings`, {
  78. method: 'GET',
  79. headers: {
  80. 'Content-Type': 'application/json',
  81. Authorization: `Bearer ${token}`
  82. }
  83. })
  84. .then(async (res) => {
  85. if (!res.ok) throw await res.json();
  86. return res.json();
  87. })
  88. .catch((err) => {
  89. console.log(err);
  90. error = err.detail;
  91. return null;
  92. });
  93. if (error) {
  94. throw error;
  95. }
  96. return res;
  97. };
  98. type QuerySettings = {
  99. k: number | null;
  100. template: string | null;
  101. };
  102. export const updateQuerySettings = async (token: string, settings: QuerySettings) => {
  103. let error = null;
  104. const res = await fetch(`${RAG_API_BASE_URL}/query/settings/update`, {
  105. method: 'POST',
  106. headers: {
  107. 'Content-Type': 'application/json',
  108. Authorization: `Bearer ${token}`
  109. },
  110. body: JSON.stringify({
  111. ...settings
  112. })
  113. })
  114. .then(async (res) => {
  115. if (!res.ok) throw await res.json();
  116. return res.json();
  117. })
  118. .catch((err) => {
  119. console.log(err);
  120. error = err.detail;
  121. return null;
  122. });
  123. if (error) {
  124. throw error;
  125. }
  126. return res;
  127. };
  128. export const uploadDocToVectorDB = async (token: string, collection_name: string, file: File) => {
  129. const data = new FormData();
  130. data.append('file', file);
  131. data.append('collection_name', collection_name);
  132. let error = null;
  133. const res = await fetch(`${RAG_API_BASE_URL}/doc`, {
  134. method: 'POST',
  135. headers: {
  136. Accept: 'application/json',
  137. authorization: `Bearer ${token}`
  138. },
  139. body: data
  140. })
  141. .then(async (res) => {
  142. if (!res.ok) throw await res.json();
  143. return res.json();
  144. })
  145. .catch((err) => {
  146. error = err.detail;
  147. console.log(err);
  148. return null;
  149. });
  150. if (error) {
  151. throw error;
  152. }
  153. return res;
  154. };
  155. export const uploadWebToVectorDB = async (token: string, collection_name: string, url: string) => {
  156. let error = null;
  157. const res = await fetch(`${RAG_API_BASE_URL}/web`, {
  158. method: 'POST',
  159. headers: {
  160. Accept: 'application/json',
  161. 'Content-Type': 'application/json',
  162. authorization: `Bearer ${token}`
  163. },
  164. body: JSON.stringify({
  165. url: url,
  166. collection_name: collection_name
  167. })
  168. })
  169. .then(async (res) => {
  170. if (!res.ok) throw await res.json();
  171. return res.json();
  172. })
  173. .catch((err) => {
  174. error = err.detail;
  175. console.log(err);
  176. return null;
  177. });
  178. if (error) {
  179. throw error;
  180. }
  181. return res;
  182. };
  183. export const queryDoc = async (
  184. token: string,
  185. collection_name: string,
  186. query: string,
  187. k: number | null = null
  188. ) => {
  189. let error = null;
  190. const res = await fetch(`${RAG_API_BASE_URL}/query/doc`, {
  191. method: 'POST',
  192. headers: {
  193. Accept: 'application/json',
  194. 'Content-Type': 'application/json',
  195. authorization: `Bearer ${token}`
  196. },
  197. body: JSON.stringify({
  198. collection_name: collection_name,
  199. query: query,
  200. k: k
  201. })
  202. })
  203. .then(async (res) => {
  204. if (!res.ok) throw await res.json();
  205. return res.json();
  206. })
  207. .catch((err) => {
  208. error = err.detail;
  209. return null;
  210. });
  211. if (error) {
  212. throw error;
  213. }
  214. return res;
  215. };
  216. export const queryCollection = async (
  217. token: string,
  218. collection_names: string,
  219. query: string,
  220. k: number | null = null
  221. ) => {
  222. let error = null;
  223. const res = await fetch(`${RAG_API_BASE_URL}/query/collection`, {
  224. method: 'POST',
  225. headers: {
  226. Accept: 'application/json',
  227. 'Content-Type': 'application/json',
  228. authorization: `Bearer ${token}`
  229. },
  230. body: JSON.stringify({
  231. collection_names: collection_names,
  232. query: query,
  233. k: k
  234. })
  235. })
  236. .then(async (res) => {
  237. if (!res.ok) throw await res.json();
  238. return res.json();
  239. })
  240. .catch((err) => {
  241. error = err.detail;
  242. return null;
  243. });
  244. if (error) {
  245. throw error;
  246. }
  247. return res;
  248. };
  249. export const scanDocs = async (token: string) => {
  250. let error = null;
  251. const res = await fetch(`${RAG_API_BASE_URL}/scan`, {
  252. method: 'GET',
  253. headers: {
  254. Accept: 'application/json',
  255. authorization: `Bearer ${token}`
  256. }
  257. })
  258. .then(async (res) => {
  259. if (!res.ok) throw await res.json();
  260. return res.json();
  261. })
  262. .catch((err) => {
  263. error = err.detail;
  264. return null;
  265. });
  266. if (error) {
  267. throw error;
  268. }
  269. return res;
  270. };
  271. export const resetVectorDB = async (token: string) => {
  272. let error = null;
  273. const res = await fetch(`${RAG_API_BASE_URL}/reset`, {
  274. method: 'GET',
  275. headers: {
  276. Accept: 'application/json',
  277. authorization: `Bearer ${token}`
  278. }
  279. })
  280. .then(async (res) => {
  281. if (!res.ok) throw await res.json();
  282. return res.json();
  283. })
  284. .catch((err) => {
  285. error = err.detail;
  286. return null;
  287. });
  288. if (error) {
  289. throw error;
  290. }
  291. return res;
  292. };