index.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 createNewFeedback = async (token: string, feedback: object) => {
  84. let error = null;
  85. const res = await fetch(`${WEBUI_API_BASE_URL}/evaluations/feedback`, {
  86. method: 'POST',
  87. headers: {
  88. Accept: 'application/json',
  89. 'Content-Type': 'application/json',
  90. authorization: `Bearer ${token}`
  91. },
  92. body: JSON.stringify({
  93. ...feedback
  94. })
  95. })
  96. .then(async (res) => {
  97. if (!res.ok) throw await res.json();
  98. return res.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 getFeedbackById = async (token: string, feedbackId: string) => {
  111. let error = null;
  112. const res = await fetch(`${WEBUI_API_BASE_URL}/evaluations/feedback/${feedbackId}`, {
  113. method: 'GET',
  114. headers: {
  115. Accept: 'application/json',
  116. 'Content-Type': 'application/json',
  117. authorization: `Bearer ${token}`
  118. }
  119. })
  120. .then(async (res) => {
  121. if (!res.ok) throw await res.json();
  122. return res.json();
  123. })
  124. .then((json) => {
  125. return 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 updateFeedbackById = async (token: string, feedbackId: string, feedback: object) => {
  138. let error = null;
  139. const res = await fetch(`${WEBUI_API_BASE_URL}/evaluations/feedback/${feedbackId}`, {
  140. method: 'POST',
  141. headers: {
  142. Accept: 'application/json',
  143. 'Content-Type': 'application/json',
  144. authorization: `Bearer ${token}`
  145. },
  146. body: JSON.stringify({
  147. ...feedback
  148. })
  149. })
  150. .then(async (res) => {
  151. if (!res.ok) throw await res.json();
  152. return res.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 deleteFeedbackById = async (token: string, feedbackId: string) => {
  165. let error = null;
  166. const res = await fetch(`${WEBUI_API_BASE_URL}/evaluations/feedback/${feedbackId}`, {
  167. method: 'DELETE',
  168. headers: {
  169. Accept: 'application/json',
  170. 'Content-Type': 'application/json',
  171. authorization: `Bearer ${token}`
  172. }
  173. })
  174. .then(async (res) => {
  175. if (!res.ok) throw await res.json();
  176. return res.json();
  177. })
  178. .catch((err) => {
  179. error = err.detail;
  180. console.log(err);
  181. return null;
  182. });
  183. if (error) {
  184. throw error;
  185. }
  186. return res;
  187. };