index.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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 getTaskConfig = async (token: string = '') => {
  89. let error = null;
  90. const res = await fetch(`${WEBUI_BASE_URL}/api/task/config`, {
  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. return res;
  111. };
  112. export const updateTaskConfig = async (token: string, config: object) => {
  113. let error = null;
  114. const res = await fetch(`${WEBUI_BASE_URL}/api/task/config/update`, {
  115. method: 'POST',
  116. headers: {
  117. Accept: 'application/json',
  118. 'Content-Type': 'application/json',
  119. ...(token && { authorization: `Bearer ${token}` })
  120. },
  121. body: JSON.stringify(config)
  122. })
  123. .then(async (res) => {
  124. if (!res.ok) throw await res.json();
  125. return res.json();
  126. })
  127. .catch((err) => {
  128. console.log(err);
  129. if ('detail' in err) {
  130. error = err.detail;
  131. } else {
  132. error = err;
  133. }
  134. return null;
  135. });
  136. if (error) {
  137. throw error;
  138. }
  139. return res;
  140. };
  141. export const generateTitle = async (
  142. token: string = '',
  143. model: string,
  144. prompt: string,
  145. chat_id?: string
  146. ) => {
  147. let error = null;
  148. const res = await fetch(`${WEBUI_BASE_URL}/api/task/title/completions`, {
  149. method: 'POST',
  150. headers: {
  151. Accept: 'application/json',
  152. 'Content-Type': 'application/json',
  153. Authorization: `Bearer ${token}`
  154. },
  155. body: JSON.stringify({
  156. model: model,
  157. prompt: prompt,
  158. ...(chat_id && { chat_id: chat_id })
  159. })
  160. })
  161. .then(async (res) => {
  162. if (!res.ok) throw await res.json();
  163. return res.json();
  164. })
  165. .catch((err) => {
  166. console.log(err);
  167. if ('detail' in err) {
  168. error = err.detail;
  169. }
  170. return null;
  171. });
  172. if (error) {
  173. throw error;
  174. }
  175. return res?.choices[0]?.message?.content.replace(/["']/g, '') ?? 'New Chat';
  176. };
  177. export const generateEmoji = async (
  178. token: string = '',
  179. model: string,
  180. prompt: string,
  181. chat_id?: string
  182. ) => {
  183. let error = null;
  184. const res = await fetch(`${WEBUI_BASE_URL}/api/task/emoji/completions`, {
  185. method: 'POST',
  186. headers: {
  187. Accept: 'application/json',
  188. 'Content-Type': 'application/json',
  189. Authorization: `Bearer ${token}`
  190. },
  191. body: JSON.stringify({
  192. model: model,
  193. prompt: prompt,
  194. ...(chat_id && { chat_id: chat_id })
  195. })
  196. })
  197. .then(async (res) => {
  198. if (!res.ok) throw await res.json();
  199. return res.json();
  200. })
  201. .catch((err) => {
  202. console.log(err);
  203. if ('detail' in err) {
  204. error = err.detail;
  205. }
  206. return null;
  207. });
  208. if (error) {
  209. throw error;
  210. }
  211. const response = res?.choices[0]?.message?.content.replace(/["']/g, '') ?? null;
  212. if (response) {
  213. if (/\p{Extended_Pictographic}/u.test(response)) {
  214. return response.match(/\p{Extended_Pictographic}/gu)[0];
  215. }
  216. }
  217. return null;
  218. };
  219. export const generateSearchQuery = async (
  220. token: string = '',
  221. model: string,
  222. messages: object[],
  223. prompt: string
  224. ) => {
  225. let error = null;
  226. const res = await fetch(`${WEBUI_BASE_URL}/api/task/query/completions`, {
  227. method: 'POST',
  228. headers: {
  229. Accept: 'application/json',
  230. 'Content-Type': 'application/json',
  231. Authorization: `Bearer ${token}`
  232. },
  233. body: JSON.stringify({
  234. model: model,
  235. messages: messages,
  236. prompt: prompt
  237. })
  238. })
  239. .then(async (res) => {
  240. if (!res.ok) throw await res.json();
  241. return res.json();
  242. })
  243. .catch((err) => {
  244. console.log(err);
  245. if ('detail' in err) {
  246. error = err.detail;
  247. }
  248. return null;
  249. });
  250. if (error) {
  251. throw error;
  252. }
  253. return res?.choices[0]?.message?.content.replace(/["']/g, '') ?? prompt;
  254. };
  255. export const getPipelinesList = async (token: string = '') => {
  256. let error = null;
  257. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/list`, {
  258. method: 'GET',
  259. headers: {
  260. Accept: 'application/json',
  261. 'Content-Type': 'application/json',
  262. ...(token && { authorization: `Bearer ${token}` })
  263. }
  264. })
  265. .then(async (res) => {
  266. if (!res.ok) throw await res.json();
  267. return res.json();
  268. })
  269. .catch((err) => {
  270. console.log(err);
  271. error = err;
  272. return null;
  273. });
  274. if (error) {
  275. throw error;
  276. }
  277. let pipelines = res?.data ?? [];
  278. return pipelines;
  279. };
  280. export const uploadPipeline = async (token: string, file: File, urlIdx: string) => {
  281. let error = null;
  282. // Create a new FormData object to handle the file upload
  283. const formData = new FormData();
  284. formData.append('file', file);
  285. formData.append('urlIdx', urlIdx);
  286. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/upload`, {
  287. method: 'POST',
  288. headers: {
  289. ...(token && { authorization: `Bearer ${token}` })
  290. // 'Content-Type': 'multipart/form-data' is not needed as Fetch API will set it automatically
  291. },
  292. body: formData
  293. })
  294. .then(async (res) => {
  295. if (!res.ok) throw await res.json();
  296. return res.json();
  297. })
  298. .catch((err) => {
  299. console.log(err);
  300. if ('detail' in err) {
  301. error = err.detail;
  302. } else {
  303. error = err;
  304. }
  305. return null;
  306. });
  307. if (error) {
  308. throw error;
  309. }
  310. return res;
  311. };
  312. export const downloadPipeline = async (token: string, url: string, urlIdx: string) => {
  313. let error = null;
  314. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/add`, {
  315. method: 'POST',
  316. headers: {
  317. Accept: 'application/json',
  318. 'Content-Type': 'application/json',
  319. ...(token && { authorization: `Bearer ${token}` })
  320. },
  321. body: JSON.stringify({
  322. url: url,
  323. urlIdx: urlIdx
  324. })
  325. })
  326. .then(async (res) => {
  327. if (!res.ok) throw await res.json();
  328. return res.json();
  329. })
  330. .catch((err) => {
  331. console.log(err);
  332. if ('detail' in err) {
  333. error = err.detail;
  334. } else {
  335. error = err;
  336. }
  337. return null;
  338. });
  339. if (error) {
  340. throw error;
  341. }
  342. return res;
  343. };
  344. export const deletePipeline = async (token: string, id: string, urlIdx: string) => {
  345. let error = null;
  346. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/delete`, {
  347. method: 'DELETE',
  348. headers: {
  349. Accept: 'application/json',
  350. 'Content-Type': 'application/json',
  351. ...(token && { authorization: `Bearer ${token}` })
  352. },
  353. body: JSON.stringify({
  354. id: id,
  355. urlIdx: urlIdx
  356. })
  357. })
  358. .then(async (res) => {
  359. if (!res.ok) throw await res.json();
  360. return res.json();
  361. })
  362. .catch((err) => {
  363. console.log(err);
  364. if ('detail' in err) {
  365. error = err.detail;
  366. } else {
  367. error = err;
  368. }
  369. return null;
  370. });
  371. if (error) {
  372. throw error;
  373. }
  374. return res;
  375. };
  376. export const getPipelines = async (token: string, urlIdx?: string) => {
  377. let error = null;
  378. const searchParams = new URLSearchParams();
  379. if (urlIdx !== undefined) {
  380. searchParams.append('urlIdx', urlIdx);
  381. }
  382. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines?${searchParams.toString()}`, {
  383. method: 'GET',
  384. headers: {
  385. Accept: 'application/json',
  386. 'Content-Type': 'application/json',
  387. ...(token && { authorization: `Bearer ${token}` })
  388. }
  389. })
  390. .then(async (res) => {
  391. if (!res.ok) throw await res.json();
  392. return res.json();
  393. })
  394. .catch((err) => {
  395. console.log(err);
  396. error = err;
  397. return null;
  398. });
  399. if (error) {
  400. throw error;
  401. }
  402. let pipelines = res?.data ?? [];
  403. return pipelines;
  404. };
  405. export const getPipelineValves = async (token: string, pipeline_id: string, urlIdx: string) => {
  406. let error = null;
  407. const searchParams = new URLSearchParams();
  408. if (urlIdx !== undefined) {
  409. searchParams.append('urlIdx', urlIdx);
  410. }
  411. const res = await fetch(
  412. `${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves?${searchParams.toString()}`,
  413. {
  414. method: 'GET',
  415. headers: {
  416. Accept: 'application/json',
  417. 'Content-Type': 'application/json',
  418. ...(token && { authorization: `Bearer ${token}` })
  419. }
  420. }
  421. )
  422. .then(async (res) => {
  423. if (!res.ok) throw await res.json();
  424. return res.json();
  425. })
  426. .catch((err) => {
  427. console.log(err);
  428. error = err;
  429. return null;
  430. });
  431. if (error) {
  432. throw error;
  433. }
  434. return res;
  435. };
  436. export const getPipelineValvesSpec = async (token: string, pipeline_id: string, urlIdx: string) => {
  437. let error = null;
  438. const searchParams = new URLSearchParams();
  439. if (urlIdx !== undefined) {
  440. searchParams.append('urlIdx', urlIdx);
  441. }
  442. const res = await fetch(
  443. `${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves/spec?${searchParams.toString()}`,
  444. {
  445. method: 'GET',
  446. headers: {
  447. Accept: 'application/json',
  448. 'Content-Type': 'application/json',
  449. ...(token && { authorization: `Bearer ${token}` })
  450. }
  451. }
  452. )
  453. .then(async (res) => {
  454. if (!res.ok) throw await res.json();
  455. return res.json();
  456. })
  457. .catch((err) => {
  458. console.log(err);
  459. error = err;
  460. return null;
  461. });
  462. if (error) {
  463. throw error;
  464. }
  465. return res;
  466. };
  467. export const updatePipelineValves = async (
  468. token: string = '',
  469. pipeline_id: string,
  470. valves: object,
  471. urlIdx: string
  472. ) => {
  473. let error = null;
  474. const searchParams = new URLSearchParams();
  475. if (urlIdx !== undefined) {
  476. searchParams.append('urlIdx', urlIdx);
  477. }
  478. const res = await fetch(
  479. `${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves/update?${searchParams.toString()}`,
  480. {
  481. method: 'POST',
  482. headers: {
  483. Accept: 'application/json',
  484. 'Content-Type': 'application/json',
  485. ...(token && { authorization: `Bearer ${token}` })
  486. },
  487. body: JSON.stringify(valves)
  488. }
  489. )
  490. .then(async (res) => {
  491. if (!res.ok) throw await res.json();
  492. return res.json();
  493. })
  494. .catch((err) => {
  495. console.log(err);
  496. if ('detail' in err) {
  497. error = err.detail;
  498. } else {
  499. error = err;
  500. }
  501. return null;
  502. });
  503. if (error) {
  504. throw error;
  505. }
  506. return res;
  507. };
  508. export const getBackendConfig = async () => {
  509. let error = null;
  510. const res = await fetch(`${WEBUI_BASE_URL}/api/config`, {
  511. method: 'GET',
  512. headers: {
  513. 'Content-Type': 'application/json'
  514. }
  515. })
  516. .then(async (res) => {
  517. if (!res.ok) throw await res.json();
  518. return res.json();
  519. })
  520. .catch((err) => {
  521. console.log(err);
  522. error = err;
  523. return null;
  524. });
  525. if (error) {
  526. throw error;
  527. }
  528. return res;
  529. };
  530. export const getChangelog = async () => {
  531. let error = null;
  532. const res = await fetch(`${WEBUI_BASE_URL}/api/changelog`, {
  533. method: 'GET',
  534. headers: {
  535. 'Content-Type': 'application/json'
  536. }
  537. })
  538. .then(async (res) => {
  539. if (!res.ok) throw await res.json();
  540. return res.json();
  541. })
  542. .catch((err) => {
  543. console.log(err);
  544. error = err;
  545. return null;
  546. });
  547. if (error) {
  548. throw error;
  549. }
  550. return res;
  551. };
  552. export const getVersionUpdates = async () => {
  553. let error = null;
  554. const res = await fetch(`${WEBUI_BASE_URL}/api/version/updates`, {
  555. method: 'GET',
  556. headers: {
  557. 'Content-Type': 'application/json'
  558. }
  559. })
  560. .then(async (res) => {
  561. if (!res.ok) throw await res.json();
  562. return res.json();
  563. })
  564. .catch((err) => {
  565. console.log(err);
  566. error = err;
  567. return null;
  568. });
  569. if (error) {
  570. throw error;
  571. }
  572. return res;
  573. };
  574. export const getModelFilterConfig = async (token: string) => {
  575. let error = null;
  576. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  577. method: 'GET',
  578. headers: {
  579. 'Content-Type': 'application/json',
  580. Authorization: `Bearer ${token}`
  581. }
  582. })
  583. .then(async (res) => {
  584. if (!res.ok) throw await res.json();
  585. return res.json();
  586. })
  587. .catch((err) => {
  588. console.log(err);
  589. error = err;
  590. return null;
  591. });
  592. if (error) {
  593. throw error;
  594. }
  595. return res;
  596. };
  597. export const updateModelFilterConfig = async (
  598. token: string,
  599. enabled: boolean,
  600. models: string[]
  601. ) => {
  602. let error = null;
  603. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  604. method: 'POST',
  605. headers: {
  606. 'Content-Type': 'application/json',
  607. Authorization: `Bearer ${token}`
  608. },
  609. body: JSON.stringify({
  610. enabled: enabled,
  611. models: models
  612. })
  613. })
  614. .then(async (res) => {
  615. if (!res.ok) throw await res.json();
  616. return res.json();
  617. })
  618. .catch((err) => {
  619. console.log(err);
  620. error = err;
  621. return null;
  622. });
  623. if (error) {
  624. throw error;
  625. }
  626. return res;
  627. };
  628. export const getWebhookUrl = async (token: string) => {
  629. let error = null;
  630. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  631. method: 'GET',
  632. headers: {
  633. 'Content-Type': 'application/json',
  634. Authorization: `Bearer ${token}`
  635. }
  636. })
  637. .then(async (res) => {
  638. if (!res.ok) throw await res.json();
  639. return res.json();
  640. })
  641. .catch((err) => {
  642. console.log(err);
  643. error = err;
  644. return null;
  645. });
  646. if (error) {
  647. throw error;
  648. }
  649. return res.url;
  650. };
  651. export const updateWebhookUrl = async (token: string, url: string) => {
  652. let error = null;
  653. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  654. method: 'POST',
  655. headers: {
  656. 'Content-Type': 'application/json',
  657. Authorization: `Bearer ${token}`
  658. },
  659. body: JSON.stringify({
  660. url: url
  661. })
  662. })
  663. .then(async (res) => {
  664. if (!res.ok) throw await res.json();
  665. return res.json();
  666. })
  667. .catch((err) => {
  668. console.log(err);
  669. error = err;
  670. return null;
  671. });
  672. if (error) {
  673. throw error;
  674. }
  675. return res.url;
  676. };
  677. export const getCommunitySharingEnabledStatus = async (token: string) => {
  678. let error = null;
  679. const res = await fetch(`${WEBUI_BASE_URL}/api/community_sharing`, {
  680. method: 'GET',
  681. headers: {
  682. 'Content-Type': 'application/json',
  683. Authorization: `Bearer ${token}`
  684. }
  685. })
  686. .then(async (res) => {
  687. if (!res.ok) throw await res.json();
  688. return res.json();
  689. })
  690. .catch((err) => {
  691. console.log(err);
  692. error = err;
  693. return null;
  694. });
  695. if (error) {
  696. throw error;
  697. }
  698. return res;
  699. };
  700. export const toggleCommunitySharingEnabledStatus = async (token: string) => {
  701. let error = null;
  702. const res = await fetch(`${WEBUI_BASE_URL}/api/community_sharing/toggle`, {
  703. method: 'GET',
  704. headers: {
  705. 'Content-Type': 'application/json',
  706. Authorization: `Bearer ${token}`
  707. }
  708. })
  709. .then(async (res) => {
  710. if (!res.ok) throw await res.json();
  711. return res.json();
  712. })
  713. .catch((err) => {
  714. console.log(err);
  715. error = err.detail;
  716. return null;
  717. });
  718. if (error) {
  719. throw error;
  720. }
  721. return res;
  722. };
  723. export const getModelConfig = async (token: string): Promise<GlobalModelConfig> => {
  724. let error = null;
  725. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  726. method: 'GET',
  727. headers: {
  728. 'Content-Type': 'application/json',
  729. Authorization: `Bearer ${token}`
  730. }
  731. })
  732. .then(async (res) => {
  733. if (!res.ok) throw await res.json();
  734. return res.json();
  735. })
  736. .catch((err) => {
  737. console.log(err);
  738. error = err;
  739. return null;
  740. });
  741. if (error) {
  742. throw error;
  743. }
  744. return res.models;
  745. };
  746. export interface ModelConfig {
  747. id: string;
  748. name: string;
  749. meta: ModelMeta;
  750. base_model_id?: string;
  751. params: ModelParams;
  752. }
  753. export interface ModelMeta {
  754. description?: string;
  755. capabilities?: object;
  756. }
  757. export interface ModelParams {}
  758. export type GlobalModelConfig = ModelConfig[];
  759. export const updateModelConfig = async (token: string, config: GlobalModelConfig) => {
  760. let error = null;
  761. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  762. method: 'POST',
  763. headers: {
  764. 'Content-Type': 'application/json',
  765. Authorization: `Bearer ${token}`
  766. },
  767. body: JSON.stringify({
  768. models: config
  769. })
  770. })
  771. .then(async (res) => {
  772. if (!res.ok) throw await res.json();
  773. return res.json();
  774. })
  775. .catch((err) => {
  776. console.log(err);
  777. error = err;
  778. return null;
  779. });
  780. if (error) {
  781. throw error;
  782. }
  783. return res;
  784. };