index.ts 4.9 KB

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