index.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
  2. import { getOpenAIModelsDirect } from './openai';
  3. export const getModels = async (
  4. token: string = '',
  5. connections: object | null = null,
  6. base: boolean = false
  7. ) => {
  8. let error = null;
  9. const res = await fetch(`${WEBUI_BASE_URL}/api/models${base ? '/base' : ''}`, {
  10. method: 'GET',
  11. headers: {
  12. Accept: 'application/json',
  13. 'Content-Type': 'application/json',
  14. ...(token && { authorization: `Bearer ${token}` })
  15. }
  16. })
  17. .then(async (res) => {
  18. if (!res.ok) throw await res.json();
  19. return res.json();
  20. })
  21. .catch((err) => {
  22. error = err;
  23. console.log(err);
  24. return null;
  25. });
  26. if (error) {
  27. throw error;
  28. }
  29. let models = res?.data ?? [];
  30. if (connections && !base) {
  31. let localModels = [];
  32. if (connections) {
  33. const OPENAI_API_BASE_URLS = connections.OPENAI_API_BASE_URLS;
  34. const OPENAI_API_KEYS = connections.OPENAI_API_KEYS;
  35. const OPENAI_API_CONFIGS = connections.OPENAI_API_CONFIGS;
  36. const requests = [];
  37. for (const idx in OPENAI_API_BASE_URLS) {
  38. const url = OPENAI_API_BASE_URLS[idx];
  39. if (idx.toString() in OPENAI_API_CONFIGS) {
  40. const apiConfig = OPENAI_API_CONFIGS[idx.toString()] ?? {};
  41. const enable = apiConfig?.enable ?? true;
  42. const modelIds = apiConfig?.model_ids ?? [];
  43. if (enable) {
  44. if (modelIds.length > 0) {
  45. const modelList = {
  46. object: 'list',
  47. data: modelIds.map((modelId) => ({
  48. id: modelId,
  49. name: modelId,
  50. owned_by: 'openai',
  51. openai: { id: modelId },
  52. urlIdx: idx
  53. }))
  54. };
  55. requests.push(
  56. (async () => {
  57. return modelList;
  58. })()
  59. );
  60. } else {
  61. requests.push(
  62. (async () => {
  63. return await getOpenAIModelsDirect(url, OPENAI_API_KEYS[idx])
  64. .then((res) => {
  65. return res;
  66. })
  67. .catch((err) => {
  68. return {
  69. object: 'list',
  70. data: [],
  71. urlIdx: idx
  72. };
  73. });
  74. })()
  75. );
  76. }
  77. } else {
  78. requests.push(
  79. (async () => {
  80. return {
  81. object: 'list',
  82. data: [],
  83. urlIdx: idx
  84. };
  85. })()
  86. );
  87. }
  88. }
  89. }
  90. const responses = await Promise.all(requests);
  91. for (const idx in responses) {
  92. const response = responses[idx];
  93. const apiConfig = OPENAI_API_CONFIGS[idx.toString()] ?? {};
  94. let models = Array.isArray(response) ? response : (response?.data ?? []);
  95. models = models.map((model) => ({ ...model, openai: { id: model.id }, urlIdx: idx }));
  96. const prefixId = apiConfig.prefix_id;
  97. if (prefixId) {
  98. for (const model of models) {
  99. model.id = `${prefixId}.${model.id}`;
  100. }
  101. }
  102. localModels = localModels.concat(models);
  103. }
  104. }
  105. models = models.concat(
  106. localModels.map((model) => ({
  107. ...model,
  108. name: model?.name ?? model?.id,
  109. direct: true
  110. }))
  111. );
  112. // Remove duplicates
  113. const modelsMap = {};
  114. for (const model of models) {
  115. modelsMap[model.id] = model;
  116. }
  117. models = Object.values(modelsMap);
  118. }
  119. return models;
  120. };
  121. type ChatCompletedForm = {
  122. model: string;
  123. messages: string[];
  124. chat_id: string;
  125. session_id: string;
  126. };
  127. export const chatCompleted = async (token: string, body: ChatCompletedForm) => {
  128. let error = null;
  129. const res = await fetch(`${WEBUI_BASE_URL}/api/chat/completed`, {
  130. method: 'POST',
  131. headers: {
  132. Accept: 'application/json',
  133. 'Content-Type': 'application/json',
  134. ...(token && { authorization: `Bearer ${token}` })
  135. },
  136. body: JSON.stringify(body)
  137. })
  138. .then(async (res) => {
  139. if (!res.ok) throw await res.json();
  140. return res.json();
  141. })
  142. .catch((err) => {
  143. console.log(err);
  144. if ('detail' in err) {
  145. error = err.detail;
  146. } else {
  147. error = err;
  148. }
  149. return null;
  150. });
  151. if (error) {
  152. throw error;
  153. }
  154. return res;
  155. };
  156. type ChatActionForm = {
  157. model: string;
  158. messages: string[];
  159. chat_id: string;
  160. };
  161. export const chatAction = async (token: string, action_id: string, body: ChatActionForm) => {
  162. let error = null;
  163. const res = await fetch(`${WEBUI_BASE_URL}/api/chat/actions/${action_id}`, {
  164. method: 'POST',
  165. headers: {
  166. Accept: 'application/json',
  167. 'Content-Type': 'application/json',
  168. ...(token && { authorization: `Bearer ${token}` })
  169. },
  170. body: JSON.stringify(body)
  171. })
  172. .then(async (res) => {
  173. if (!res.ok) throw await res.json();
  174. return res.json();
  175. })
  176. .catch((err) => {
  177. console.log(err);
  178. if ('detail' in err) {
  179. error = err.detail;
  180. } else {
  181. error = err;
  182. }
  183. return null;
  184. });
  185. if (error) {
  186. throw error;
  187. }
  188. return res;
  189. };
  190. export const stopTask = async (token: string, id: string) => {
  191. let error = null;
  192. const res = await fetch(`${WEBUI_BASE_URL}/api/tasks/stop/${id}`, {
  193. method: 'POST',
  194. headers: {
  195. Accept: 'application/json',
  196. 'Content-Type': 'application/json',
  197. ...(token && { authorization: `Bearer ${token}` })
  198. }
  199. })
  200. .then(async (res) => {
  201. if (!res.ok) throw await res.json();
  202. return res.json();
  203. })
  204. .catch((err) => {
  205. console.log(err);
  206. if ('detail' in err) {
  207. error = err.detail;
  208. } else {
  209. error = err;
  210. }
  211. return null;
  212. });
  213. if (error) {
  214. throw error;
  215. }
  216. return res;
  217. };
  218. export const getTaskConfig = async (token: string = '') => {
  219. let error = null;
  220. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/config`, {
  221. method: 'GET',
  222. headers: {
  223. Accept: 'application/json',
  224. 'Content-Type': 'application/json',
  225. ...(token && { authorization: `Bearer ${token}` })
  226. }
  227. })
  228. .then(async (res) => {
  229. if (!res.ok) throw await res.json();
  230. return res.json();
  231. })
  232. .catch((err) => {
  233. console.log(err);
  234. error = err;
  235. return null;
  236. });
  237. if (error) {
  238. throw error;
  239. }
  240. return res;
  241. };
  242. export const updateTaskConfig = async (token: string, config: object) => {
  243. let error = null;
  244. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/config/update`, {
  245. method: 'POST',
  246. headers: {
  247. Accept: 'application/json',
  248. 'Content-Type': 'application/json',
  249. ...(token && { authorization: `Bearer ${token}` })
  250. },
  251. body: JSON.stringify(config)
  252. })
  253. .then(async (res) => {
  254. if (!res.ok) throw await res.json();
  255. return res.json();
  256. })
  257. .catch((err) => {
  258. console.log(err);
  259. if ('detail' in err) {
  260. error = err.detail;
  261. } else {
  262. error = err;
  263. }
  264. return null;
  265. });
  266. if (error) {
  267. throw error;
  268. }
  269. return res;
  270. };
  271. export const generateTitle = async (
  272. token: string = '',
  273. model: string,
  274. messages: string[],
  275. chat_id?: string
  276. ) => {
  277. let error = null;
  278. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/title/completions`, {
  279. method: 'POST',
  280. headers: {
  281. Accept: 'application/json',
  282. 'Content-Type': 'application/json',
  283. Authorization: `Bearer ${token}`
  284. },
  285. body: JSON.stringify({
  286. model: model,
  287. messages: messages,
  288. ...(chat_id && { chat_id: chat_id })
  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. }
  300. return null;
  301. });
  302. if (error) {
  303. throw error;
  304. }
  305. return res?.choices[0]?.message?.content.replace(/["']/g, '') ?? 'New Chat';
  306. };
  307. export const generateTags = async (
  308. token: string = '',
  309. model: string,
  310. messages: string,
  311. chat_id?: string
  312. ) => {
  313. let error = null;
  314. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/tags/completions`, {
  315. method: 'POST',
  316. headers: {
  317. Accept: 'application/json',
  318. 'Content-Type': 'application/json',
  319. Authorization: `Bearer ${token}`
  320. },
  321. body: JSON.stringify({
  322. model: model,
  323. messages: messages,
  324. ...(chat_id && { chat_id: chat_id })
  325. })
  326. })
  327. .then(async (res) => {
  328. if (!res.ok) throw await res.json();
  329. return res.json();
  330. })
  331. .catch((err) => {
  332. console.log(err);
  333. if ('detail' in err) {
  334. error = err.detail;
  335. }
  336. return null;
  337. });
  338. if (error) {
  339. throw error;
  340. }
  341. try {
  342. // Step 1: Safely extract the response string
  343. const response = res?.choices[0]?.message?.content ?? '';
  344. // Step 2: Attempt to fix common JSON format issues like single quotes
  345. const sanitizedResponse = response.replace(/['‘’`]/g, '"'); // Convert single quotes to double quotes for valid JSON
  346. // Step 3: Find the relevant JSON block within the response
  347. const jsonStartIndex = sanitizedResponse.indexOf('{');
  348. const jsonEndIndex = sanitizedResponse.lastIndexOf('}');
  349. // Step 4: Check if we found a valid JSON block (with both `{` and `}`)
  350. if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
  351. const jsonResponse = sanitizedResponse.substring(jsonStartIndex, jsonEndIndex + 1);
  352. // Step 5: Parse the JSON block
  353. const parsed = JSON.parse(jsonResponse);
  354. // Step 6: If there's a "tags" key, return the tags array; otherwise, return an empty array
  355. if (parsed && parsed.tags) {
  356. return Array.isArray(parsed.tags) ? parsed.tags : [];
  357. } else {
  358. return [];
  359. }
  360. }
  361. // If no valid JSON block found, return an empty array
  362. return [];
  363. } catch (e) {
  364. // Catch and safely return empty array on any parsing errors
  365. console.error('Failed to parse response: ', e);
  366. return [];
  367. }
  368. };
  369. export const generateEmoji = async (
  370. token: string = '',
  371. model: string,
  372. prompt: string,
  373. chat_id?: string
  374. ) => {
  375. let error = null;
  376. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/emoji/completions`, {
  377. method: 'POST',
  378. headers: {
  379. Accept: 'application/json',
  380. 'Content-Type': 'application/json',
  381. Authorization: `Bearer ${token}`
  382. },
  383. body: JSON.stringify({
  384. model: model,
  385. prompt: prompt,
  386. ...(chat_id && { chat_id: chat_id })
  387. })
  388. })
  389. .then(async (res) => {
  390. if (!res.ok) throw await res.json();
  391. return res.json();
  392. })
  393. .catch((err) => {
  394. console.log(err);
  395. if ('detail' in err) {
  396. error = err.detail;
  397. }
  398. return null;
  399. });
  400. if (error) {
  401. throw error;
  402. }
  403. const response = res?.choices[0]?.message?.content.replace(/["']/g, '') ?? null;
  404. if (response) {
  405. if (/\p{Extended_Pictographic}/u.test(response)) {
  406. return response.match(/\p{Extended_Pictographic}/gu)[0];
  407. }
  408. }
  409. return null;
  410. };
  411. export const generateQueries = async (
  412. token: string = '',
  413. model: string,
  414. messages: object[],
  415. prompt: string,
  416. type?: string = 'web_search'
  417. ) => {
  418. let error = null;
  419. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/queries/completions`, {
  420. method: 'POST',
  421. headers: {
  422. Accept: 'application/json',
  423. 'Content-Type': 'application/json',
  424. Authorization: `Bearer ${token}`
  425. },
  426. body: JSON.stringify({
  427. model: model,
  428. messages: messages,
  429. prompt: prompt,
  430. type: type
  431. })
  432. })
  433. .then(async (res) => {
  434. if (!res.ok) throw await res.json();
  435. return res.json();
  436. })
  437. .catch((err) => {
  438. console.log(err);
  439. if ('detail' in err) {
  440. error = err.detail;
  441. }
  442. return null;
  443. });
  444. if (error) {
  445. throw error;
  446. }
  447. // Step 1: Safely extract the response string
  448. const response = res?.choices[0]?.message?.content ?? '';
  449. try {
  450. const jsonStartIndex = response.indexOf('{');
  451. const jsonEndIndex = response.lastIndexOf('}');
  452. if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
  453. const jsonResponse = response.substring(jsonStartIndex, jsonEndIndex + 1);
  454. // Step 5: Parse the JSON block
  455. const parsed = JSON.parse(jsonResponse);
  456. // Step 6: If there's a "queries" key, return the queries array; otherwise, return an empty array
  457. if (parsed && parsed.queries) {
  458. return Array.isArray(parsed.queries) ? parsed.queries : [];
  459. } else {
  460. return [];
  461. }
  462. }
  463. // If no valid JSON block found, return response as is
  464. return [response];
  465. } catch (e) {
  466. // Catch and safely return empty array on any parsing errors
  467. console.error('Failed to parse response: ', e);
  468. return [response];
  469. }
  470. };
  471. export const generateAutoCompletion = async (
  472. token: string = '',
  473. model: string,
  474. prompt: string,
  475. messages?: object[],
  476. type: string = 'search query'
  477. ) => {
  478. const controller = new AbortController();
  479. let error = null;
  480. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/auto/completions`, {
  481. signal: controller.signal,
  482. method: 'POST',
  483. headers: {
  484. Accept: 'application/json',
  485. 'Content-Type': 'application/json',
  486. Authorization: `Bearer ${token}`
  487. },
  488. body: JSON.stringify({
  489. model: model,
  490. prompt: prompt,
  491. ...(messages && { messages: messages }),
  492. type: type,
  493. stream: false
  494. })
  495. })
  496. .then(async (res) => {
  497. if (!res.ok) throw await res.json();
  498. return res.json();
  499. })
  500. .catch((err) => {
  501. console.log(err);
  502. if ('detail' in err) {
  503. error = err.detail;
  504. }
  505. return null;
  506. });
  507. if (error) {
  508. throw error;
  509. }
  510. const response = res?.choices[0]?.message?.content ?? '';
  511. try {
  512. const jsonStartIndex = response.indexOf('{');
  513. const jsonEndIndex = response.lastIndexOf('}');
  514. if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
  515. const jsonResponse = response.substring(jsonStartIndex, jsonEndIndex + 1);
  516. // Step 5: Parse the JSON block
  517. const parsed = JSON.parse(jsonResponse);
  518. // Step 6: If there's a "queries" key, return the queries array; otherwise, return an empty array
  519. if (parsed && parsed.text) {
  520. return parsed.text;
  521. } else {
  522. return '';
  523. }
  524. }
  525. // If no valid JSON block found, return response as is
  526. return response;
  527. } catch (e) {
  528. // Catch and safely return empty array on any parsing errors
  529. console.error('Failed to parse response: ', e);
  530. return response;
  531. }
  532. };
  533. export const generateMoACompletion = async (
  534. token: string = '',
  535. model: string,
  536. prompt: string,
  537. responses: string[]
  538. ) => {
  539. const controller = new AbortController();
  540. let error = null;
  541. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/moa/completions`, {
  542. signal: controller.signal,
  543. method: 'POST',
  544. headers: {
  545. Accept: 'application/json',
  546. 'Content-Type': 'application/json',
  547. Authorization: `Bearer ${token}`
  548. },
  549. body: JSON.stringify({
  550. model: model,
  551. prompt: prompt,
  552. responses: responses,
  553. stream: true
  554. })
  555. }).catch((err) => {
  556. console.log(err);
  557. error = err;
  558. return null;
  559. });
  560. if (error) {
  561. throw error;
  562. }
  563. return [res, controller];
  564. };
  565. export const getPipelinesList = async (token: string = '') => {
  566. let error = null;
  567. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/pipelines/list`, {
  568. method: 'GET',
  569. headers: {
  570. Accept: 'application/json',
  571. 'Content-Type': 'application/json',
  572. ...(token && { authorization: `Bearer ${token}` })
  573. }
  574. })
  575. .then(async (res) => {
  576. if (!res.ok) throw await res.json();
  577. return res.json();
  578. })
  579. .catch((err) => {
  580. console.log(err);
  581. error = err;
  582. return null;
  583. });
  584. if (error) {
  585. throw error;
  586. }
  587. let pipelines = res?.data ?? [];
  588. return pipelines;
  589. };
  590. export const uploadPipeline = async (token: string, file: File, urlIdx: string) => {
  591. let error = null;
  592. // Create a new FormData object to handle the file upload
  593. const formData = new FormData();
  594. formData.append('file', file);
  595. formData.append('urlIdx', urlIdx);
  596. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/pipelines/upload`, {
  597. method: 'POST',
  598. headers: {
  599. ...(token && { authorization: `Bearer ${token}` })
  600. // 'Content-Type': 'multipart/form-data' is not needed as Fetch API will set it automatically
  601. },
  602. body: formData
  603. })
  604. .then(async (res) => {
  605. if (!res.ok) throw await res.json();
  606. return res.json();
  607. })
  608. .catch((err) => {
  609. console.log(err);
  610. if ('detail' in err) {
  611. error = err.detail;
  612. } else {
  613. error = err;
  614. }
  615. return null;
  616. });
  617. if (error) {
  618. throw error;
  619. }
  620. return res;
  621. };
  622. export const downloadPipeline = async (token: string, url: string, urlIdx: string) => {
  623. let error = null;
  624. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/pipelines/add`, {
  625. method: 'POST',
  626. headers: {
  627. Accept: 'application/json',
  628. 'Content-Type': 'application/json',
  629. ...(token && { authorization: `Bearer ${token}` })
  630. },
  631. body: JSON.stringify({
  632. url: url,
  633. urlIdx: urlIdx
  634. })
  635. })
  636. .then(async (res) => {
  637. if (!res.ok) throw await res.json();
  638. return res.json();
  639. })
  640. .catch((err) => {
  641. console.log(err);
  642. if ('detail' in err) {
  643. error = err.detail;
  644. } else {
  645. error = err;
  646. }
  647. return null;
  648. });
  649. if (error) {
  650. throw error;
  651. }
  652. return res;
  653. };
  654. export const deletePipeline = async (token: string, id: string, urlIdx: string) => {
  655. let error = null;
  656. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/pipelines/delete`, {
  657. method: 'DELETE',
  658. headers: {
  659. Accept: 'application/json',
  660. 'Content-Type': 'application/json',
  661. ...(token && { authorization: `Bearer ${token}` })
  662. },
  663. body: JSON.stringify({
  664. id: id,
  665. urlIdx: urlIdx
  666. })
  667. })
  668. .then(async (res) => {
  669. if (!res.ok) throw await res.json();
  670. return res.json();
  671. })
  672. .catch((err) => {
  673. console.log(err);
  674. if ('detail' in err) {
  675. error = err.detail;
  676. } else {
  677. error = err;
  678. }
  679. return null;
  680. });
  681. if (error) {
  682. throw error;
  683. }
  684. return res;
  685. };
  686. export const getPipelines = async (token: string, urlIdx?: string) => {
  687. let error = null;
  688. const searchParams = new URLSearchParams();
  689. if (urlIdx !== undefined) {
  690. searchParams.append('urlIdx', urlIdx);
  691. }
  692. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/pipelines/?${searchParams.toString()}`, {
  693. method: 'GET',
  694. headers: {
  695. Accept: 'application/json',
  696. 'Content-Type': 'application/json',
  697. ...(token && { authorization: `Bearer ${token}` })
  698. }
  699. })
  700. .then(async (res) => {
  701. if (!res.ok) throw await res.json();
  702. return res.json();
  703. })
  704. .catch((err) => {
  705. console.log(err);
  706. error = err;
  707. return null;
  708. });
  709. if (error) {
  710. throw error;
  711. }
  712. let pipelines = res?.data ?? [];
  713. return pipelines;
  714. };
  715. export const getPipelineValves = async (token: string, pipeline_id: string, urlIdx: string) => {
  716. let error = null;
  717. const searchParams = new URLSearchParams();
  718. if (urlIdx !== undefined) {
  719. searchParams.append('urlIdx', urlIdx);
  720. }
  721. const res = await fetch(
  722. `${WEBUI_BASE_URL}/api/v1/pipelines/${pipeline_id}/valves?${searchParams.toString()}`,
  723. {
  724. method: 'GET',
  725. headers: {
  726. Accept: 'application/json',
  727. 'Content-Type': 'application/json',
  728. ...(token && { authorization: `Bearer ${token}` })
  729. }
  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;
  745. };
  746. export const getPipelineValvesSpec = async (token: string, pipeline_id: string, urlIdx: string) => {
  747. let error = null;
  748. const searchParams = new URLSearchParams();
  749. if (urlIdx !== undefined) {
  750. searchParams.append('urlIdx', urlIdx);
  751. }
  752. const res = await fetch(
  753. `${WEBUI_BASE_URL}/api/v1/pipelines/${pipeline_id}/valves/spec?${searchParams.toString()}`,
  754. {
  755. method: 'GET',
  756. headers: {
  757. Accept: 'application/json',
  758. 'Content-Type': 'application/json',
  759. ...(token && { authorization: `Bearer ${token}` })
  760. }
  761. }
  762. )
  763. .then(async (res) => {
  764. if (!res.ok) throw await res.json();
  765. return res.json();
  766. })
  767. .catch((err) => {
  768. console.log(err);
  769. error = err;
  770. return null;
  771. });
  772. if (error) {
  773. throw error;
  774. }
  775. return res;
  776. };
  777. export const updatePipelineValves = async (
  778. token: string = '',
  779. pipeline_id: string,
  780. valves: object,
  781. urlIdx: string
  782. ) => {
  783. let error = null;
  784. const searchParams = new URLSearchParams();
  785. if (urlIdx !== undefined) {
  786. searchParams.append('urlIdx', urlIdx);
  787. }
  788. const res = await fetch(
  789. `${WEBUI_BASE_URL}/api/v1/pipelines/${pipeline_id}/valves/update?${searchParams.toString()}`,
  790. {
  791. method: 'POST',
  792. headers: {
  793. Accept: 'application/json',
  794. 'Content-Type': 'application/json',
  795. ...(token && { authorization: `Bearer ${token}` })
  796. },
  797. body: JSON.stringify(valves)
  798. }
  799. )
  800. .then(async (res) => {
  801. if (!res.ok) throw await res.json();
  802. return res.json();
  803. })
  804. .catch((err) => {
  805. console.log(err);
  806. if ('detail' in err) {
  807. error = err.detail;
  808. } else {
  809. error = err;
  810. }
  811. return null;
  812. });
  813. if (error) {
  814. throw error;
  815. }
  816. return res;
  817. };
  818. export const getBackendConfig = async () => {
  819. let error = null;
  820. const res = await fetch(`${WEBUI_BASE_URL}/api/config`, {
  821. method: 'GET',
  822. credentials: 'include',
  823. headers: {
  824. 'Content-Type': 'application/json'
  825. }
  826. })
  827. .then(async (res) => {
  828. if (!res.ok) throw await res.json();
  829. return res.json();
  830. })
  831. .catch((err) => {
  832. console.log(err);
  833. error = err;
  834. return null;
  835. });
  836. if (error) {
  837. throw error;
  838. }
  839. return res;
  840. };
  841. export const getChangelog = async () => {
  842. let error = null;
  843. const res = await fetch(`${WEBUI_BASE_URL}/api/changelog`, {
  844. method: 'GET',
  845. headers: {
  846. 'Content-Type': 'application/json'
  847. }
  848. })
  849. .then(async (res) => {
  850. if (!res.ok) throw await res.json();
  851. return res.json();
  852. })
  853. .catch((err) => {
  854. console.log(err);
  855. error = err;
  856. return null;
  857. });
  858. if (error) {
  859. throw error;
  860. }
  861. return res;
  862. };
  863. export const getVersionUpdates = async (token: string) => {
  864. let error = null;
  865. const res = await fetch(`${WEBUI_BASE_URL}/api/version/updates`, {
  866. method: 'GET',
  867. headers: {
  868. 'Content-Type': 'application/json',
  869. Authorization: `Bearer ${token}`
  870. }
  871. })
  872. .then(async (res) => {
  873. if (!res.ok) throw await res.json();
  874. return res.json();
  875. })
  876. .catch((err) => {
  877. console.log(err);
  878. error = err;
  879. return null;
  880. });
  881. if (error) {
  882. throw error;
  883. }
  884. return res;
  885. };
  886. export const getModelFilterConfig = async (token: string) => {
  887. let error = null;
  888. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  889. method: 'GET',
  890. headers: {
  891. 'Content-Type': 'application/json',
  892. Authorization: `Bearer ${token}`
  893. }
  894. })
  895. .then(async (res) => {
  896. if (!res.ok) throw await res.json();
  897. return res.json();
  898. })
  899. .catch((err) => {
  900. console.log(err);
  901. error = err;
  902. return null;
  903. });
  904. if (error) {
  905. throw error;
  906. }
  907. return res;
  908. };
  909. export const updateModelFilterConfig = async (
  910. token: string,
  911. enabled: boolean,
  912. models: string[]
  913. ) => {
  914. let error = null;
  915. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  916. method: 'POST',
  917. headers: {
  918. 'Content-Type': 'application/json',
  919. Authorization: `Bearer ${token}`
  920. },
  921. body: JSON.stringify({
  922. enabled: enabled,
  923. models: models
  924. })
  925. })
  926. .then(async (res) => {
  927. if (!res.ok) throw await res.json();
  928. return res.json();
  929. })
  930. .catch((err) => {
  931. console.log(err);
  932. error = err;
  933. return null;
  934. });
  935. if (error) {
  936. throw error;
  937. }
  938. return res;
  939. };
  940. export const getWebhookUrl = async (token: string) => {
  941. let error = null;
  942. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  943. method: 'GET',
  944. headers: {
  945. 'Content-Type': 'application/json',
  946. Authorization: `Bearer ${token}`
  947. }
  948. })
  949. .then(async (res) => {
  950. if (!res.ok) throw await res.json();
  951. return res.json();
  952. })
  953. .catch((err) => {
  954. console.log(err);
  955. error = err;
  956. return null;
  957. });
  958. if (error) {
  959. throw error;
  960. }
  961. return res.url;
  962. };
  963. export const updateWebhookUrl = async (token: string, url: string) => {
  964. let error = null;
  965. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  966. method: 'POST',
  967. headers: {
  968. 'Content-Type': 'application/json',
  969. Authorization: `Bearer ${token}`
  970. },
  971. body: JSON.stringify({
  972. url: url
  973. })
  974. })
  975. .then(async (res) => {
  976. if (!res.ok) throw await res.json();
  977. return res.json();
  978. })
  979. .catch((err) => {
  980. console.log(err);
  981. error = err;
  982. return null;
  983. });
  984. if (error) {
  985. throw error;
  986. }
  987. return res.url;
  988. };
  989. export const getCommunitySharingEnabledStatus = async (token: string) => {
  990. let error = null;
  991. const res = await fetch(`${WEBUI_BASE_URL}/api/community_sharing`, {
  992. method: 'GET',
  993. headers: {
  994. 'Content-Type': 'application/json',
  995. Authorization: `Bearer ${token}`
  996. }
  997. })
  998. .then(async (res) => {
  999. if (!res.ok) throw await res.json();
  1000. return res.json();
  1001. })
  1002. .catch((err) => {
  1003. console.log(err);
  1004. error = err;
  1005. return null;
  1006. });
  1007. if (error) {
  1008. throw error;
  1009. }
  1010. return res;
  1011. };
  1012. export const toggleCommunitySharingEnabledStatus = async (token: string) => {
  1013. let error = null;
  1014. const res = await fetch(`${WEBUI_BASE_URL}/api/community_sharing/toggle`, {
  1015. method: 'GET',
  1016. headers: {
  1017. 'Content-Type': 'application/json',
  1018. Authorization: `Bearer ${token}`
  1019. }
  1020. })
  1021. .then(async (res) => {
  1022. if (!res.ok) throw await res.json();
  1023. return res.json();
  1024. })
  1025. .catch((err) => {
  1026. console.log(err);
  1027. error = err.detail;
  1028. return null;
  1029. });
  1030. if (error) {
  1031. throw error;
  1032. }
  1033. return res;
  1034. };
  1035. export const getModelConfig = async (token: string): Promise<GlobalModelConfig> => {
  1036. let error = null;
  1037. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  1038. method: 'GET',
  1039. headers: {
  1040. 'Content-Type': 'application/json',
  1041. Authorization: `Bearer ${token}`
  1042. }
  1043. })
  1044. .then(async (res) => {
  1045. if (!res.ok) throw await res.json();
  1046. return res.json();
  1047. })
  1048. .catch((err) => {
  1049. console.log(err);
  1050. error = err;
  1051. return null;
  1052. });
  1053. if (error) {
  1054. throw error;
  1055. }
  1056. return res.models;
  1057. };
  1058. export interface ModelConfig {
  1059. id: string;
  1060. name: string;
  1061. meta: ModelMeta;
  1062. base_model_id?: string;
  1063. params: ModelParams;
  1064. }
  1065. export interface ModelMeta {
  1066. description?: string;
  1067. capabilities?: object;
  1068. profile_image_url?: string;
  1069. }
  1070. export interface ModelParams {}
  1071. export type GlobalModelConfig = ModelConfig[];
  1072. export const updateModelConfig = async (token: string, config: GlobalModelConfig) => {
  1073. let error = null;
  1074. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  1075. method: 'POST',
  1076. headers: {
  1077. 'Content-Type': 'application/json',
  1078. Authorization: `Bearer ${token}`
  1079. },
  1080. body: JSON.stringify({
  1081. models: config
  1082. })
  1083. })
  1084. .then(async (res) => {
  1085. if (!res.ok) throw await res.json();
  1086. return res.json();
  1087. })
  1088. .catch((err) => {
  1089. console.log(err);
  1090. error = err;
  1091. return null;
  1092. });
  1093. if (error) {
  1094. throw error;
  1095. }
  1096. return res;
  1097. };