index.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
  2. export const getModels = async (token: string = '') => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_BASE_URL}/api/models`, {
  5. method: 'GET',
  6. headers: {
  7. Accept: 'application/json',
  8. 'Content-Type': 'application/json',
  9. ...(token && { authorization: `Bearer ${token}` })
  10. }
  11. })
  12. .then(async (res) => {
  13. if (!res.ok) throw await res.json();
  14. return res.json();
  15. })
  16. .catch((err) => {
  17. console.log(err);
  18. error = err;
  19. return null;
  20. });
  21. if (error) {
  22. throw error;
  23. }
  24. let models = res?.data ?? [];
  25. models = models
  26. .filter((models) => models)
  27. // Sort the models
  28. .sort((a, b) => {
  29. // Check if models have position property
  30. const aHasPosition = a.info?.meta?.position !== undefined;
  31. const bHasPosition = b.info?.meta?.position !== undefined;
  32. // If both a and b have the position property
  33. if (aHasPosition && bHasPosition) {
  34. return a.info.meta.position - b.info.meta.position;
  35. }
  36. // If only a has the position property, it should come first
  37. if (aHasPosition) return -1;
  38. // If only b has the position property, it should come first
  39. if (bHasPosition) return 1;
  40. // Compare case-insensitively by name for models without position property
  41. const lowerA = a.name.toLowerCase();
  42. const lowerB = b.name.toLowerCase();
  43. if (lowerA < lowerB) return -1;
  44. if (lowerA > lowerB) return 1;
  45. // If same case-insensitively, sort by original strings,
  46. // lowercase will come before uppercase due to ASCII values
  47. if (a.name < b.name) return -1;
  48. if (a.name > b.name) return 1;
  49. return 0; // They are equal
  50. });
  51. console.log(models);
  52. return models;
  53. };
  54. type ChatCompletedForm = {
  55. model: string;
  56. messages: string[];
  57. chat_id: string;
  58. };
  59. export const chatCompleted = async (token: string, body: ChatCompletedForm) => {
  60. let error = null;
  61. const res = await fetch(`${WEBUI_BASE_URL}/api/chat/completed`, {
  62. method: 'POST',
  63. headers: {
  64. Accept: 'application/json',
  65. 'Content-Type': 'application/json',
  66. ...(token && { authorization: `Bearer ${token}` })
  67. },
  68. body: JSON.stringify(body)
  69. })
  70. .then(async (res) => {
  71. if (!res.ok) throw await res.json();
  72. return res.json();
  73. })
  74. .catch((err) => {
  75. console.log(err);
  76. if ('detail' in err) {
  77. error = err.detail;
  78. } else {
  79. error = err;
  80. }
  81. return null;
  82. });
  83. if (error) {
  84. throw error;
  85. }
  86. return res;
  87. };
  88. export const getPipelinesList = async (token: string = '') => {
  89. let error = null;
  90. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/list`, {
  91. method: 'GET',
  92. headers: {
  93. Accept: 'application/json',
  94. 'Content-Type': 'application/json',
  95. ...(token && { authorization: `Bearer ${token}` })
  96. }
  97. })
  98. .then(async (res) => {
  99. if (!res.ok) throw await res.json();
  100. return res.json();
  101. })
  102. .catch((err) => {
  103. console.log(err);
  104. error = err;
  105. return null;
  106. });
  107. if (error) {
  108. throw error;
  109. }
  110. let pipelines = res?.data ?? [];
  111. return pipelines;
  112. };
  113. export const downloadPipeline = async (token: string, url: string, urlIdx: string) => {
  114. let error = null;
  115. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/add`, {
  116. method: 'POST',
  117. headers: {
  118. Accept: 'application/json',
  119. 'Content-Type': 'application/json',
  120. ...(token && { authorization: `Bearer ${token}` })
  121. },
  122. body: JSON.stringify({
  123. url: url,
  124. urlIdx: urlIdx
  125. })
  126. })
  127. .then(async (res) => {
  128. if (!res.ok) throw await res.json();
  129. return res.json();
  130. })
  131. .catch((err) => {
  132. console.log(err);
  133. if ('detail' in err) {
  134. error = err.detail;
  135. } else {
  136. error = err;
  137. }
  138. return null;
  139. });
  140. if (error) {
  141. throw error;
  142. }
  143. return res;
  144. };
  145. export const deletePipeline = async (token: string, id: string, urlIdx: string) => {
  146. let error = null;
  147. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/delete`, {
  148. method: 'DELETE',
  149. headers: {
  150. Accept: 'application/json',
  151. 'Content-Type': 'application/json',
  152. ...(token && { authorization: `Bearer ${token}` })
  153. },
  154. body: JSON.stringify({
  155. id: id,
  156. urlIdx: urlIdx
  157. })
  158. })
  159. .then(async (res) => {
  160. if (!res.ok) throw await res.json();
  161. return res.json();
  162. })
  163. .catch((err) => {
  164. console.log(err);
  165. if ('detail' in err) {
  166. error = err.detail;
  167. } else {
  168. error = err;
  169. }
  170. return null;
  171. });
  172. if (error) {
  173. throw error;
  174. }
  175. return res;
  176. };
  177. export const getPipelines = async (token: string, urlIdx?: string) => {
  178. let error = null;
  179. const searchParams = new URLSearchParams();
  180. if (urlIdx !== undefined) {
  181. searchParams.append('urlIdx', urlIdx);
  182. }
  183. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines?${searchParams.toString()}`, {
  184. method: 'GET',
  185. headers: {
  186. Accept: 'application/json',
  187. 'Content-Type': 'application/json',
  188. ...(token && { authorization: `Bearer ${token}` })
  189. }
  190. })
  191. .then(async (res) => {
  192. if (!res.ok) throw await res.json();
  193. return res.json();
  194. })
  195. .catch((err) => {
  196. console.log(err);
  197. error = err;
  198. return null;
  199. });
  200. if (error) {
  201. throw error;
  202. }
  203. let pipelines = res?.data ?? [];
  204. return pipelines;
  205. };
  206. export const getPipelineValves = async (token: string, pipeline_id: string, urlIdx: string) => {
  207. let error = null;
  208. const searchParams = new URLSearchParams();
  209. if (urlIdx !== undefined) {
  210. searchParams.append('urlIdx', urlIdx);
  211. }
  212. const res = await fetch(
  213. `${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves?${searchParams.toString()}`,
  214. {
  215. method: 'GET',
  216. headers: {
  217. Accept: 'application/json',
  218. 'Content-Type': 'application/json',
  219. ...(token && { authorization: `Bearer ${token}` })
  220. }
  221. }
  222. )
  223. .then(async (res) => {
  224. if (!res.ok) throw await res.json();
  225. return res.json();
  226. })
  227. .catch((err) => {
  228. console.log(err);
  229. error = err;
  230. return null;
  231. });
  232. if (error) {
  233. throw error;
  234. }
  235. return res;
  236. };
  237. export const getPipelineValvesSpec = async (token: string, pipeline_id: string, urlIdx: string) => {
  238. let error = null;
  239. const searchParams = new URLSearchParams();
  240. if (urlIdx !== undefined) {
  241. searchParams.append('urlIdx', urlIdx);
  242. }
  243. const res = await fetch(
  244. `${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves/spec?${searchParams.toString()}`,
  245. {
  246. method: 'GET',
  247. headers: {
  248. Accept: 'application/json',
  249. 'Content-Type': 'application/json',
  250. ...(token && { authorization: `Bearer ${token}` })
  251. }
  252. }
  253. )
  254. .then(async (res) => {
  255. if (!res.ok) throw await res.json();
  256. return res.json();
  257. })
  258. .catch((err) => {
  259. console.log(err);
  260. error = err;
  261. return null;
  262. });
  263. if (error) {
  264. throw error;
  265. }
  266. return res;
  267. };
  268. export const updatePipelineValves = async (
  269. token: string = '',
  270. pipeline_id: string,
  271. valves: object,
  272. urlIdx: string
  273. ) => {
  274. let error = null;
  275. const searchParams = new URLSearchParams();
  276. if (urlIdx !== undefined) {
  277. searchParams.append('urlIdx', urlIdx);
  278. }
  279. const res = await fetch(
  280. `${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves/update?${searchParams.toString()}`,
  281. {
  282. method: 'POST',
  283. headers: {
  284. Accept: 'application/json',
  285. 'Content-Type': 'application/json',
  286. ...(token && { authorization: `Bearer ${token}` })
  287. },
  288. body: JSON.stringify(valves)
  289. }
  290. )
  291. .then(async (res) => {
  292. if (!res.ok) throw await res.json();
  293. return res.json();
  294. })
  295. .catch((err) => {
  296. console.log(err);
  297. if ('detail' in err) {
  298. error = err.detail;
  299. } else {
  300. error = err;
  301. }
  302. return null;
  303. });
  304. if (error) {
  305. throw error;
  306. }
  307. return res;
  308. };
  309. export const getBackendConfig = async () => {
  310. let error = null;
  311. const res = await fetch(`${WEBUI_BASE_URL}/api/config`, {
  312. method: 'GET',
  313. headers: {
  314. 'Content-Type': 'application/json'
  315. }
  316. })
  317. .then(async (res) => {
  318. if (!res.ok) throw await res.json();
  319. return res.json();
  320. })
  321. .catch((err) => {
  322. console.log(err);
  323. error = err;
  324. return null;
  325. });
  326. if (error) {
  327. throw error;
  328. }
  329. return res;
  330. };
  331. export const getChangelog = async () => {
  332. let error = null;
  333. const res = await fetch(`${WEBUI_BASE_URL}/api/changelog`, {
  334. method: 'GET',
  335. headers: {
  336. 'Content-Type': 'application/json'
  337. }
  338. })
  339. .then(async (res) => {
  340. if (!res.ok) throw await res.json();
  341. return res.json();
  342. })
  343. .catch((err) => {
  344. console.log(err);
  345. error = err;
  346. return null;
  347. });
  348. if (error) {
  349. throw error;
  350. }
  351. return res;
  352. };
  353. export const getVersionUpdates = async () => {
  354. let error = null;
  355. const res = await fetch(`${WEBUI_BASE_URL}/api/version/updates`, {
  356. method: 'GET',
  357. headers: {
  358. 'Content-Type': 'application/json'
  359. }
  360. })
  361. .then(async (res) => {
  362. if (!res.ok) throw await res.json();
  363. return res.json();
  364. })
  365. .catch((err) => {
  366. console.log(err);
  367. error = err;
  368. return null;
  369. });
  370. if (error) {
  371. throw error;
  372. }
  373. return res;
  374. };
  375. export const getModelFilterConfig = async (token: string) => {
  376. let error = null;
  377. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  378. method: 'GET',
  379. headers: {
  380. 'Content-Type': 'application/json',
  381. Authorization: `Bearer ${token}`
  382. }
  383. })
  384. .then(async (res) => {
  385. if (!res.ok) throw await res.json();
  386. return res.json();
  387. })
  388. .catch((err) => {
  389. console.log(err);
  390. error = err;
  391. return null;
  392. });
  393. if (error) {
  394. throw error;
  395. }
  396. return res;
  397. };
  398. export const updateModelFilterConfig = async (
  399. token: string,
  400. enabled: boolean,
  401. models: string[]
  402. ) => {
  403. let error = null;
  404. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  405. method: 'POST',
  406. headers: {
  407. 'Content-Type': 'application/json',
  408. Authorization: `Bearer ${token}`
  409. },
  410. body: JSON.stringify({
  411. enabled: enabled,
  412. models: models
  413. })
  414. })
  415. .then(async (res) => {
  416. if (!res.ok) throw await res.json();
  417. return res.json();
  418. })
  419. .catch((err) => {
  420. console.log(err);
  421. error = err;
  422. return null;
  423. });
  424. if (error) {
  425. throw error;
  426. }
  427. return res;
  428. };
  429. export const getWebhookUrl = async (token: string) => {
  430. let error = null;
  431. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  432. method: 'GET',
  433. headers: {
  434. 'Content-Type': 'application/json',
  435. Authorization: `Bearer ${token}`
  436. }
  437. })
  438. .then(async (res) => {
  439. if (!res.ok) throw await res.json();
  440. return res.json();
  441. })
  442. .catch((err) => {
  443. console.log(err);
  444. error = err;
  445. return null;
  446. });
  447. if (error) {
  448. throw error;
  449. }
  450. return res.url;
  451. };
  452. export const updateWebhookUrl = async (token: string, url: string) => {
  453. let error = null;
  454. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  455. method: 'POST',
  456. headers: {
  457. 'Content-Type': 'application/json',
  458. Authorization: `Bearer ${token}`
  459. },
  460. body: JSON.stringify({
  461. url: url
  462. })
  463. })
  464. .then(async (res) => {
  465. if (!res.ok) throw await res.json();
  466. return res.json();
  467. })
  468. .catch((err) => {
  469. console.log(err);
  470. error = err;
  471. return null;
  472. });
  473. if (error) {
  474. throw error;
  475. }
  476. return res.url;
  477. };
  478. export const getCommunitySharingEnabledStatus = async (token: string) => {
  479. let error = null;
  480. const res = await fetch(`${WEBUI_BASE_URL}/api/community_sharing`, {
  481. method: 'GET',
  482. headers: {
  483. 'Content-Type': 'application/json',
  484. Authorization: `Bearer ${token}`
  485. }
  486. })
  487. .then(async (res) => {
  488. if (!res.ok) throw await res.json();
  489. return res.json();
  490. })
  491. .catch((err) => {
  492. console.log(err);
  493. error = err;
  494. return null;
  495. });
  496. if (error) {
  497. throw error;
  498. }
  499. return res;
  500. };
  501. export const toggleCommunitySharingEnabledStatus = async (token: string) => {
  502. let error = null;
  503. const res = await fetch(`${WEBUI_BASE_URL}/api/community_sharing/toggle`, {
  504. method: 'GET',
  505. headers: {
  506. 'Content-Type': 'application/json',
  507. Authorization: `Bearer ${token}`
  508. }
  509. })
  510. .then(async (res) => {
  511. if (!res.ok) throw await res.json();
  512. return res.json();
  513. })
  514. .catch((err) => {
  515. console.log(err);
  516. error = err.detail;
  517. return null;
  518. });
  519. if (error) {
  520. throw error;
  521. }
  522. return res;
  523. };
  524. export const getModelConfig = async (token: string): Promise<GlobalModelConfig> => {
  525. let error = null;
  526. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  527. method: 'GET',
  528. headers: {
  529. 'Content-Type': 'application/json',
  530. Authorization: `Bearer ${token}`
  531. }
  532. })
  533. .then(async (res) => {
  534. if (!res.ok) throw await res.json();
  535. return res.json();
  536. })
  537. .catch((err) => {
  538. console.log(err);
  539. error = err;
  540. return null;
  541. });
  542. if (error) {
  543. throw error;
  544. }
  545. return res.models;
  546. };
  547. export interface ModelConfig {
  548. id: string;
  549. name: string;
  550. meta: ModelMeta;
  551. base_model_id?: string;
  552. params: ModelParams;
  553. }
  554. export interface ModelMeta {
  555. description?: string;
  556. capabilities?: object;
  557. }
  558. export interface ModelParams {}
  559. export type GlobalModelConfig = ModelConfig[];
  560. export const updateModelConfig = async (token: string, config: GlobalModelConfig) => {
  561. let error = null;
  562. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  563. method: 'POST',
  564. headers: {
  565. 'Content-Type': 'application/json',
  566. Authorization: `Bearer ${token}`
  567. },
  568. body: JSON.stringify({
  569. models: config
  570. })
  571. })
  572. .then(async (res) => {
  573. if (!res.ok) throw await res.json();
  574. return res.json();
  575. })
  576. .catch((err) => {
  577. console.log(err);
  578. error = err;
  579. return null;
  580. });
  581. if (error) {
  582. throw error;
  583. }
  584. return res;
  585. };