index.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const createNewKnowledge = async (token: string, name: string, description: string) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/create`, {
  5. method: 'POST',
  6. headers: {
  7. Accept: 'application/json',
  8. 'Content-Type': 'application/json',
  9. authorization: `Bearer ${token}`
  10. },
  11. body: JSON.stringify({
  12. name: name,
  13. description: description
  14. })
  15. })
  16. .then(async (res) => {
  17. if (!res.ok) throw await res.json();
  18. return res.json();
  19. })
  20. .catch((err) => {
  21. error = err.detail;
  22. console.log(err);
  23. return null;
  24. });
  25. if (error) {
  26. throw error;
  27. }
  28. return res;
  29. };
  30. export const getKnowledgeItems = async (token: string = '') => {
  31. let error = null;
  32. const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/`, {
  33. method: 'GET',
  34. headers: {
  35. Accept: 'application/json',
  36. 'Content-Type': 'application/json',
  37. authorization: `Bearer ${token}`
  38. }
  39. })
  40. .then(async (res) => {
  41. if (!res.ok) throw await res.json();
  42. return res.json();
  43. })
  44. .then((json) => {
  45. return json;
  46. })
  47. .catch((err) => {
  48. error = err.detail;
  49. console.log(err);
  50. return null;
  51. });
  52. if (error) {
  53. throw error;
  54. }
  55. return res;
  56. };
  57. export const getKnowledgeById = async (token: string, id: string) => {
  58. let error = null;
  59. const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}`, {
  60. method: 'GET',
  61. headers: {
  62. Accept: 'application/json',
  63. 'Content-Type': 'application/json',
  64. authorization: `Bearer ${token}`
  65. }
  66. })
  67. .then(async (res) => {
  68. if (!res.ok) throw await res.json();
  69. return res.json();
  70. })
  71. .then((json) => {
  72. return json;
  73. })
  74. .catch((err) => {
  75. error = err.detail;
  76. console.log(err);
  77. return null;
  78. });
  79. if (error) {
  80. throw error;
  81. }
  82. return res;
  83. };
  84. type KnowledgeUpdateForm = {
  85. name?: string;
  86. description?: string;
  87. data?: object;
  88. };
  89. export const updateKnowledgeById = async (token: string, id: string, form: KnowledgeUpdateForm) => {
  90. let error = null;
  91. const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/update`, {
  92. method: 'POST',
  93. headers: {
  94. Accept: 'application/json',
  95. 'Content-Type': 'application/json',
  96. authorization: `Bearer ${token}`
  97. },
  98. body: JSON.stringify({
  99. name: form?.name ? form.name : undefined,
  100. description: form?.description ? form.description : undefined,
  101. data: form?.data ? form.data : undefined
  102. })
  103. })
  104. .then(async (res) => {
  105. if (!res.ok) throw await res.json();
  106. return res.json();
  107. })
  108. .then((json) => {
  109. return json;
  110. })
  111. .catch((err) => {
  112. error = err.detail;
  113. console.log(err);
  114. return null;
  115. });
  116. if (error) {
  117. throw error;
  118. }
  119. return res;
  120. };
  121. export const addFileToKnowledgeById = async (token: string, id: string, fileId: string) => {
  122. let error = null;
  123. const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/file/add`, {
  124. method: 'POST',
  125. headers: {
  126. Accept: 'application/json',
  127. 'Content-Type': 'application/json',
  128. authorization: `Bearer ${token}`
  129. },
  130. body: JSON.stringify({
  131. file_id: fileId
  132. })
  133. })
  134. .then(async (res) => {
  135. if (!res.ok) throw await res.json();
  136. return res.json();
  137. })
  138. .then((json) => {
  139. return json;
  140. })
  141. .catch((err) => {
  142. error = err.detail;
  143. console.log(err);
  144. return null;
  145. });
  146. if (error) {
  147. throw error;
  148. }
  149. return res;
  150. };
  151. export const updateFileFromKnowledgeById = async (token: string, id: string, fileId: string) => {
  152. let error = null;
  153. const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/file/update`, {
  154. method: 'POST',
  155. headers: {
  156. Accept: 'application/json',
  157. 'Content-Type': 'application/json',
  158. authorization: `Bearer ${token}`
  159. },
  160. body: JSON.stringify({
  161. file_id: fileId
  162. })
  163. })
  164. .then(async (res) => {
  165. if (!res.ok) throw await res.json();
  166. return res.json();
  167. })
  168. .then((json) => {
  169. return json;
  170. })
  171. .catch((err) => {
  172. error = err.detail;
  173. console.log(err);
  174. return null;
  175. });
  176. if (error) {
  177. throw error;
  178. }
  179. return res;
  180. };
  181. export const removeFileFromKnowledgeById = async (token: string, id: string, fileId: string) => {
  182. let error = null;
  183. const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/file/remove`, {
  184. method: 'POST',
  185. headers: {
  186. Accept: 'application/json',
  187. 'Content-Type': 'application/json',
  188. authorization: `Bearer ${token}`
  189. },
  190. body: JSON.stringify({
  191. file_id: fileId
  192. })
  193. })
  194. .then(async (res) => {
  195. if (!res.ok) throw await res.json();
  196. return res.json();
  197. })
  198. .then((json) => {
  199. return json;
  200. })
  201. .catch((err) => {
  202. error = err.detail;
  203. console.log(err);
  204. return null;
  205. });
  206. if (error) {
  207. throw error;
  208. }
  209. return res;
  210. };
  211. export const deleteKnowledgeById = async (token: string, id: string) => {
  212. let error = null;
  213. const res = await fetch(`${WEBUI_API_BASE_URL}/knowledge/${id}/delete`, {
  214. method: 'DELETE',
  215. headers: {
  216. Accept: 'application/json',
  217. 'Content-Type': 'application/json',
  218. authorization: `Bearer ${token}`
  219. }
  220. })
  221. .then(async (res) => {
  222. if (!res.ok) throw await res.json();
  223. return res.json();
  224. })
  225. .then((json) => {
  226. return json;
  227. })
  228. .catch((err) => {
  229. error = err.detail;
  230. console.log(err);
  231. return null;
  232. });
  233. if (error) {
  234. throw error;
  235. }
  236. return res;
  237. };