index.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const createNewModelfile = async (token: string, modelfile: object) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/modelfiles/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. modelfile: modelfile
  13. })
  14. })
  15. .then(async (res) => {
  16. if (!res.ok) throw await res.json();
  17. return res.json();
  18. })
  19. .catch((err) => {
  20. error = err.detail;
  21. console.log(err);
  22. return null;
  23. });
  24. if (error) {
  25. throw error;
  26. }
  27. return res;
  28. };
  29. export const getModelfiles = async (token: string = '') => {
  30. let error = null;
  31. const res = await fetch(`${WEBUI_API_BASE_URL}/modelfiles/`, {
  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.map((modelfile) => modelfile.modelfile);
  55. };
  56. export const getModelfileByTagName = async (token: string, tagName: string) => {
  57. let error = null;
  58. const res = await fetch(`${WEBUI_API_BASE_URL}/modelfiles/`, {
  59. method: 'POST',
  60. headers: {
  61. Accept: 'application/json',
  62. 'Content-Type': 'application/json',
  63. authorization: `Bearer ${token}`
  64. },
  65. body: JSON.stringify({
  66. tag_name: tagName
  67. })
  68. })
  69. .then(async (res) => {
  70. if (!res.ok) throw await res.json();
  71. return res.json();
  72. })
  73. .then((json) => {
  74. return json;
  75. })
  76. .catch((err) => {
  77. error = err;
  78. console.log(err);
  79. return null;
  80. });
  81. if (error) {
  82. throw error;
  83. }
  84. return res.modelfile;
  85. };
  86. export const updateModelfileByTagName = async (
  87. token: string,
  88. tagName: string,
  89. modelfile: object
  90. ) => {
  91. let error = null;
  92. const res = await fetch(`${WEBUI_API_BASE_URL}/modelfiles/update`, {
  93. method: 'POST',
  94. headers: {
  95. Accept: 'application/json',
  96. 'Content-Type': 'application/json',
  97. authorization: `Bearer ${token}`
  98. },
  99. body: JSON.stringify({
  100. tag_name: tagName,
  101. modelfile: modelfile
  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;
  113. console.log(err);
  114. return null;
  115. });
  116. if (error) {
  117. throw error;
  118. }
  119. return res;
  120. };
  121. export const deleteModelfileByTagName = async (token: string, tagName: string) => {
  122. let error = null;
  123. const res = await fetch(`${WEBUI_API_BASE_URL}/modelfiles/delete`, {
  124. method: 'DELETE',
  125. headers: {
  126. Accept: 'application/json',
  127. 'Content-Type': 'application/json',
  128. authorization: `Bearer ${token}`
  129. },
  130. body: JSON.stringify({
  131. tag_name: tagName
  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;
  143. console.log(err);
  144. return null;
  145. });
  146. if (error) {
  147. throw error;
  148. }
  149. return res;
  150. };