index.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const getGravatarUrl = async (token: string, email: string) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/utils/gravatar?email=${email}`, {
  5. method: 'GET',
  6. headers: {
  7. 'Content-Type': 'application/json',
  8. Authorization: `Bearer ${token}`
  9. }
  10. })
  11. .then(async (res) => {
  12. if (!res.ok) throw await res.json();
  13. return res.json();
  14. })
  15. .catch((err) => {
  16. console.log(err);
  17. error = err;
  18. return null;
  19. });
  20. return res;
  21. };
  22. export const executeCode = async (token: string, code: string) => {
  23. let error = null;
  24. const res = await fetch(`${WEBUI_API_BASE_URL}/utils/code/execute`, {
  25. method: 'POST',
  26. headers: {
  27. 'Content-Type': 'application/json',
  28. Authorization: `Bearer ${token}`
  29. },
  30. body: JSON.stringify({
  31. code: code
  32. })
  33. })
  34. .then(async (res) => {
  35. if (!res.ok) throw await res.json();
  36. return res.json();
  37. })
  38. .catch((err) => {
  39. console.log(err);
  40. error = err;
  41. if (err.detail) {
  42. error = err.detail;
  43. }
  44. return null;
  45. });
  46. if (error) {
  47. throw error;
  48. }
  49. return res;
  50. };
  51. export const formatPythonCode = async (token: string, code: string) => {
  52. let error = null;
  53. const res = await fetch(`${WEBUI_API_BASE_URL}/utils/code/format`, {
  54. method: 'POST',
  55. headers: {
  56. 'Content-Type': 'application/json',
  57. Authorization: `Bearer ${token}`
  58. },
  59. body: JSON.stringify({
  60. code: code
  61. })
  62. })
  63. .then(async (res) => {
  64. if (!res.ok) throw await res.json();
  65. return res.json();
  66. })
  67. .catch((err) => {
  68. console.log(err);
  69. error = err;
  70. if (err.detail) {
  71. error = err.detail;
  72. }
  73. return null;
  74. });
  75. if (error) {
  76. throw error;
  77. }
  78. return res;
  79. };
  80. export const downloadChatAsPDF = async (token: string, title: string, messages: object[]) => {
  81. let error = null;
  82. const blob = await fetch(`${WEBUI_API_BASE_URL}/utils/pdf`, {
  83. method: 'POST',
  84. headers: {
  85. 'Content-Type': 'application/json',
  86. Authorization: `Bearer ${token}`
  87. },
  88. body: JSON.stringify({
  89. title: title,
  90. messages: messages
  91. })
  92. })
  93. .then(async (res) => {
  94. if (!res.ok) throw await res.json();
  95. return res.blob();
  96. })
  97. .catch((err) => {
  98. console.log(err);
  99. error = err;
  100. return null;
  101. });
  102. return blob;
  103. };
  104. export const getHTMLFromMarkdown = async (token: string, md: string) => {
  105. let error = null;
  106. const res = await fetch(`${WEBUI_API_BASE_URL}/utils/markdown`, {
  107. method: 'POST',
  108. headers: {
  109. 'Content-Type': 'application/json',
  110. Authorization: `Bearer ${token}`
  111. },
  112. body: JSON.stringify({
  113. md: md
  114. })
  115. })
  116. .then(async (res) => {
  117. if (!res.ok) throw await res.json();
  118. return res.json();
  119. })
  120. .catch((err) => {
  121. console.log(err);
  122. error = err;
  123. return null;
  124. });
  125. return res.html;
  126. };
  127. export const downloadDatabase = async (token: string) => {
  128. let error = null;
  129. const res = await fetch(`${WEBUI_API_BASE_URL}/utils/db/download`, {
  130. method: 'GET',
  131. headers: {
  132. 'Content-Type': 'application/json',
  133. Authorization: `Bearer ${token}`
  134. }
  135. })
  136. .then(async (response) => {
  137. if (!response.ok) {
  138. throw await response.json();
  139. }
  140. return response.blob();
  141. })
  142. .then((blob) => {
  143. const url = window.URL.createObjectURL(blob);
  144. const a = document.createElement('a');
  145. a.href = url;
  146. a.download = 'webui.db';
  147. document.body.appendChild(a);
  148. a.click();
  149. window.URL.revokeObjectURL(url);
  150. })
  151. .catch((err) => {
  152. console.log(err);
  153. error = err.detail;
  154. return null;
  155. });
  156. if (error) {
  157. throw error;
  158. }
  159. };
  160. export const downloadLiteLLMConfig = async (token: string) => {
  161. let error = null;
  162. const res = await fetch(`${WEBUI_API_BASE_URL}/utils/litellm/config`, {
  163. method: 'GET',
  164. headers: {
  165. 'Content-Type': 'application/json',
  166. Authorization: `Bearer ${token}`
  167. }
  168. })
  169. .then(async (response) => {
  170. if (!response.ok) {
  171. throw await response.json();
  172. }
  173. return response.blob();
  174. })
  175. .then((blob) => {
  176. const url = window.URL.createObjectURL(blob);
  177. const a = document.createElement('a');
  178. a.href = url;
  179. a.download = 'config.yaml';
  180. document.body.appendChild(a);
  181. a.click();
  182. window.URL.revokeObjectURL(url);
  183. })
  184. .catch((err) => {
  185. console.log(err);
  186. error = err.detail;
  187. return null;
  188. });
  189. if (error) {
  190. throw error;
  191. }
  192. };