index.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const getConfig = async (token: string = '') => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/evaluations/config`, {
  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.detail;
  21. console.log(err);
  22. return null;
  23. });
  24. if (error) {
  25. throw error;
  26. }
  27. return res;
  28. };
  29. export const updateConfig = async (token: string, config: object) => {
  30. let error = null;
  31. const res = await fetch(`${WEBUI_API_BASE_URL}/evaluations/config`, {
  32. method: 'POST',
  33. headers: {
  34. Accept: 'application/json',
  35. 'Content-Type': 'application/json',
  36. authorization: `Bearer ${token}`
  37. },
  38. body: JSON.stringify({
  39. ...config
  40. })
  41. })
  42. .then(async (res) => {
  43. if (!res.ok) throw await res.json();
  44. return res.json();
  45. })
  46. .catch((err) => {
  47. error = err.detail;
  48. console.log(err);
  49. return null;
  50. });
  51. if (error) {
  52. throw error;
  53. }
  54. return res;
  55. };
  56. export const getAllFeedbacks = async (token: string = '') => {
  57. let error = null;
  58. const res = await fetch(`${WEBUI_API_BASE_URL}/evaluations/feedbacks/all`, {
  59. method: 'GET',
  60. headers: {
  61. Accept: 'application/json',
  62. 'Content-Type': 'application/json',
  63. authorization: `Bearer ${token}`
  64. }
  65. })
  66. .then(async (res) => {
  67. if (!res.ok) throw await res.json();
  68. return res.json();
  69. })
  70. .then((json) => {
  71. return json;
  72. })
  73. .catch((err) => {
  74. error = err.detail;
  75. console.log(err);
  76. return null;
  77. });
  78. if (error) {
  79. throw error;
  80. }
  81. return res;
  82. };
  83. export const exportAllFeedbacks = async (token: string = '') => {
  84. let error = null;
  85. const res = await fetch(`${WEBUI_API_BASE_URL}/evaluations/feedbacks/all/export`, {
  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.detail;
  102. console.log(err);
  103. return null;
  104. });
  105. if (error) {
  106. throw error;
  107. }
  108. return res;
  109. };
  110. export const createNewFeedback = async (token: string, feedback: object) => {
  111. let error = null;
  112. const res = await fetch(`${WEBUI_API_BASE_URL}/evaluations/feedback`, {
  113. method: 'POST',
  114. headers: {
  115. Accept: 'application/json',
  116. 'Content-Type': 'application/json',
  117. authorization: `Bearer ${token}`
  118. },
  119. body: JSON.stringify({
  120. ...feedback
  121. })
  122. })
  123. .then(async (res) => {
  124. if (!res.ok) throw await res.json();
  125. return res.json();
  126. })
  127. .catch((err) => {
  128. error = err.detail;
  129. console.log(err);
  130. return null;
  131. });
  132. if (error) {
  133. throw error;
  134. }
  135. return res;
  136. };
  137. export const getFeedbackById = async (token: string, feedbackId: string) => {
  138. let error = null;
  139. const res = await fetch(`${WEBUI_API_BASE_URL}/evaluations/feedback/${feedbackId}`, {
  140. method: 'GET',
  141. headers: {
  142. Accept: 'application/json',
  143. 'Content-Type': 'application/json',
  144. authorization: `Bearer ${token}`
  145. }
  146. })
  147. .then(async (res) => {
  148. if (!res.ok) throw await res.json();
  149. return res.json();
  150. })
  151. .then((json) => {
  152. return json;
  153. })
  154. .catch((err) => {
  155. error = err.detail;
  156. console.log(err);
  157. return null;
  158. });
  159. if (error) {
  160. throw error;
  161. }
  162. return res;
  163. };
  164. export const updateFeedbackById = async (token: string, feedbackId: string, feedback: object) => {
  165. let error = null;
  166. const res = await fetch(`${WEBUI_API_BASE_URL}/evaluations/feedback/${feedbackId}`, {
  167. method: 'POST',
  168. headers: {
  169. Accept: 'application/json',
  170. 'Content-Type': 'application/json',
  171. authorization: `Bearer ${token}`
  172. },
  173. body: JSON.stringify({
  174. ...feedback
  175. })
  176. })
  177. .then(async (res) => {
  178. if (!res.ok) throw await res.json();
  179. return res.json();
  180. })
  181. .catch((err) => {
  182. error = err.detail;
  183. console.log(err);
  184. return null;
  185. });
  186. if (error) {
  187. throw error;
  188. }
  189. return res;
  190. };
  191. export const deleteFeedbackById = async (token: string, feedbackId: string) => {
  192. let error = null;
  193. const res = await fetch(`${WEBUI_API_BASE_URL}/evaluations/feedback/${feedbackId}`, {
  194. method: 'DELETE',
  195. headers: {
  196. Accept: 'application/json',
  197. 'Content-Type': 'application/json',
  198. authorization: `Bearer ${token}`
  199. }
  200. })
  201. .then(async (res) => {
  202. if (!res.ok) throw await res.json();
  203. return res.json();
  204. })
  205. .catch((err) => {
  206. error = err.detail;
  207. console.log(err);
  208. return null;
  209. });
  210. if (error) {
  211. throw error;
  212. }
  213. return res;
  214. };