index.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const addNewModel = async (token: string, model: object) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/models/add`, {
  5. method: 'POST',
  6. headers: {
  7. Accept: 'application/json',
  8. 'Content-Type': 'application/json',
  9. authorization: `Bearer ${token}`
  10. },
  11. body: JSON.stringify(model)
  12. })
  13. .then(async (res) => {
  14. if (!res.ok) throw await res.json();
  15. return res.json();
  16. })
  17. .catch((err) => {
  18. error = err.detail;
  19. console.log(err);
  20. return null;
  21. });
  22. if (error) {
  23. throw error;
  24. }
  25. return res;
  26. };
  27. export const getModelInfos = async (token: string = '') => {
  28. let error = null;
  29. const res = await fetch(`${WEBUI_API_BASE_URL}/models/`, {
  30. method: 'GET',
  31. headers: {
  32. Accept: 'application/json',
  33. 'Content-Type': 'application/json',
  34. authorization: `Bearer ${token}`
  35. }
  36. })
  37. .then(async (res) => {
  38. if (!res.ok) throw await res.json();
  39. return res.json();
  40. })
  41. .then((json) => {
  42. return json;
  43. })
  44. .catch((err) => {
  45. error = err;
  46. console.log(err);
  47. return null;
  48. });
  49. if (error) {
  50. throw error;
  51. }
  52. return res;
  53. };
  54. export const getModelById = async (token: string, id: string) => {
  55. let error = null;
  56. const res = await fetch(`${WEBUI_API_BASE_URL}/models/${id}`, {
  57. method: 'GET',
  58. headers: {
  59. Accept: 'application/json',
  60. 'Content-Type': 'application/json',
  61. authorization: `Bearer ${token}`
  62. }
  63. })
  64. .then(async (res) => {
  65. if (!res.ok) throw await res.json();
  66. return res.json();
  67. })
  68. .then((json) => {
  69. return json;
  70. })
  71. .catch((err) => {
  72. error = err;
  73. console.log(err);
  74. return null;
  75. });
  76. if (error) {
  77. throw error;
  78. }
  79. return res;
  80. };
  81. export const updateModelById = async (token: string, id: string, model: object) => {
  82. let error = null;
  83. const res = await fetch(`${WEBUI_API_BASE_URL}/models/${id}/update`, {
  84. method: 'POST',
  85. headers: {
  86. Accept: 'application/json',
  87. 'Content-Type': 'application/json',
  88. authorization: `Bearer ${token}`
  89. },
  90. body: JSON.stringify(model)
  91. })
  92. .then(async (res) => {
  93. if (!res.ok) throw await res.json();
  94. return res.json();
  95. })
  96. .then((json) => {
  97. return json;
  98. })
  99. .catch((err) => {
  100. error = err;
  101. console.log(err);
  102. return null;
  103. });
  104. if (error) {
  105. throw error;
  106. }
  107. return res;
  108. };
  109. export const deleteModelById = async (token: string, id: string) => {
  110. let error = null;
  111. const res = await fetch(`${WEBUI_API_BASE_URL}/models/${id}/delete`, {
  112. method: 'DELETE',
  113. headers: {
  114. Accept: 'application/json',
  115. 'Content-Type': 'application/json',
  116. authorization: `Bearer ${token}`
  117. }
  118. })
  119. .then(async (res) => {
  120. if (!res.ok) throw await res.json();
  121. return res.json();
  122. })
  123. .then((json) => {
  124. return json;
  125. })
  126. .catch((err) => {
  127. error = err;
  128. console.log(err);
  129. return null;
  130. });
  131. if (error) {
  132. throw error;
  133. }
  134. return res;
  135. };