index.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. import { OPENAI_API_BASE_URL, WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
  2. export const getOpenAIConfig = async (token: string = '') => {
  3. let error = null;
  4. const res = await fetch(`${OPENAI_API_BASE_URL}/config`, {
  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. if ('detail' in err) {
  19. error = err.detail;
  20. } else {
  21. error = 'Server connection failed';
  22. }
  23. return null;
  24. });
  25. if (error) {
  26. throw error;
  27. }
  28. return res;
  29. };
  30. type OpenAIConfig = {
  31. ENABLE_OPENAI_API: boolean;
  32. OPENAI_API_BASE_URLS: string[];
  33. OPENAI_API_KEYS: string[];
  34. OPENAI_API_CONFIGS: object;
  35. };
  36. export const updateOpenAIConfig = async (token: string = '', config: OpenAIConfig) => {
  37. let error = null;
  38. const res = await fetch(`${OPENAI_API_BASE_URL}/config/update`, {
  39. method: 'POST',
  40. headers: {
  41. Accept: 'application/json',
  42. 'Content-Type': 'application/json',
  43. ...(token && { authorization: `Bearer ${token}` })
  44. },
  45. body: JSON.stringify({
  46. ...config
  47. })
  48. })
  49. .then(async (res) => {
  50. if (!res.ok) throw await res.json();
  51. return res.json();
  52. })
  53. .catch((err) => {
  54. console.log(err);
  55. if ('detail' in err) {
  56. error = err.detail;
  57. } else {
  58. error = 'Server connection failed';
  59. }
  60. return null;
  61. });
  62. if (error) {
  63. throw error;
  64. }
  65. return res;
  66. };
  67. export const getOpenAIUrls = async (token: string = '') => {
  68. let error = null;
  69. const res = await fetch(`${OPENAI_API_BASE_URL}/urls`, {
  70. method: 'GET',
  71. headers: {
  72. Accept: 'application/json',
  73. 'Content-Type': 'application/json',
  74. ...(token && { authorization: `Bearer ${token}` })
  75. }
  76. })
  77. .then(async (res) => {
  78. if (!res.ok) throw await res.json();
  79. return res.json();
  80. })
  81. .catch((err) => {
  82. console.log(err);
  83. if ('detail' in err) {
  84. error = err.detail;
  85. } else {
  86. error = 'Server connection failed';
  87. }
  88. return null;
  89. });
  90. if (error) {
  91. throw error;
  92. }
  93. return res.OPENAI_API_BASE_URLS;
  94. };
  95. export const updateOpenAIUrls = async (token: string = '', urls: string[]) => {
  96. let error = null;
  97. const res = await fetch(`${OPENAI_API_BASE_URL}/urls/update`, {
  98. method: 'POST',
  99. headers: {
  100. Accept: 'application/json',
  101. 'Content-Type': 'application/json',
  102. ...(token && { authorization: `Bearer ${token}` })
  103. },
  104. body: JSON.stringify({
  105. urls: urls
  106. })
  107. })
  108. .then(async (res) => {
  109. if (!res.ok) throw await res.json();
  110. return res.json();
  111. })
  112. .catch((err) => {
  113. console.log(err);
  114. if ('detail' in err) {
  115. error = err.detail;
  116. } else {
  117. error = 'Server connection failed';
  118. }
  119. return null;
  120. });
  121. if (error) {
  122. throw error;
  123. }
  124. return res.OPENAI_API_BASE_URLS;
  125. };
  126. export const getOpenAIKeys = async (token: string = '') => {
  127. let error = null;
  128. const res = await fetch(`${OPENAI_API_BASE_URL}/keys`, {
  129. method: 'GET',
  130. headers: {
  131. Accept: 'application/json',
  132. 'Content-Type': 'application/json',
  133. ...(token && { authorization: `Bearer ${token}` })
  134. }
  135. })
  136. .then(async (res) => {
  137. if (!res.ok) throw await res.json();
  138. return res.json();
  139. })
  140. .catch((err) => {
  141. console.log(err);
  142. if ('detail' in err) {
  143. error = err.detail;
  144. } else {
  145. error = 'Server connection failed';
  146. }
  147. return null;
  148. });
  149. if (error) {
  150. throw error;
  151. }
  152. return res.OPENAI_API_KEYS;
  153. };
  154. export const updateOpenAIKeys = async (token: string = '', keys: string[]) => {
  155. let error = null;
  156. const res = await fetch(`${OPENAI_API_BASE_URL}/keys/update`, {
  157. method: 'POST',
  158. headers: {
  159. Accept: 'application/json',
  160. 'Content-Type': 'application/json',
  161. ...(token && { authorization: `Bearer ${token}` })
  162. },
  163. body: JSON.stringify({
  164. keys: keys
  165. })
  166. })
  167. .then(async (res) => {
  168. if (!res.ok) throw await res.json();
  169. return res.json();
  170. })
  171. .catch((err) => {
  172. console.log(err);
  173. if ('detail' in err) {
  174. error = err.detail;
  175. } else {
  176. error = 'Server connection failed';
  177. }
  178. return null;
  179. });
  180. if (error) {
  181. throw error;
  182. }
  183. return res.OPENAI_API_KEYS;
  184. };
  185. export const getOpenAIModelsDirect = async (url: string, key: string) => {
  186. let error = null;
  187. const res = await fetch(`${url}/models`, {
  188. method: 'GET',
  189. headers: {
  190. Accept: 'application/json',
  191. 'Content-Type': 'application/json',
  192. ...(key && { authorization: `Bearer ${key}` })
  193. }
  194. })
  195. .then(async (res) => {
  196. if (!res.ok) throw await res.json();
  197. return res.json();
  198. })
  199. .catch((err) => {
  200. error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`;
  201. return [];
  202. });
  203. if (error) {
  204. throw error;
  205. }
  206. return res;
  207. };
  208. export const getOpenAIModels = async (token: string, urlIdx?: number) => {
  209. let error = null;
  210. const res = await fetch(
  211. `${OPENAI_API_BASE_URL}/models${typeof urlIdx === 'number' ? `/${urlIdx}` : ''}`,
  212. {
  213. method: 'GET',
  214. headers: {
  215. Accept: 'application/json',
  216. 'Content-Type': 'application/json',
  217. ...(token && { authorization: `Bearer ${token}` })
  218. }
  219. }
  220. )
  221. .then(async (res) => {
  222. if (!res.ok) throw await res.json();
  223. return res.json();
  224. })
  225. .catch((err) => {
  226. error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`;
  227. return [];
  228. });
  229. if (error) {
  230. throw error;
  231. }
  232. return res;
  233. };
  234. export const verifyOpenAIConnection = async (
  235. token: string = '',
  236. url: string = 'https://api.openai.com/v1',
  237. key: string = '',
  238. direct: boolean = false
  239. ) => {
  240. if (!url) {
  241. throw 'OpenAI: URL is required';
  242. }
  243. let error = null;
  244. let res = null;
  245. if (direct) {
  246. res = await fetch(`${url}/models`, {
  247. method: 'GET',
  248. headers: {
  249. Accept: 'application/json',
  250. Authorization: `Bearer ${key}`,
  251. 'Content-Type': 'application/json'
  252. }
  253. })
  254. .then(async (res) => {
  255. if (!res.ok) throw await res.json();
  256. return res.json();
  257. })
  258. .catch((err) => {
  259. error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`;
  260. return [];
  261. });
  262. if (error) {
  263. throw error;
  264. }
  265. } else {
  266. res = await fetch(`${OPENAI_API_BASE_URL}/verify`, {
  267. method: 'POST',
  268. headers: {
  269. Accept: 'application/json',
  270. Authorization: `Bearer ${token}`,
  271. 'Content-Type': 'application/json'
  272. },
  273. body: JSON.stringify({
  274. url,
  275. key
  276. })
  277. })
  278. .then(async (res) => {
  279. if (!res.ok) throw await res.json();
  280. return res.json();
  281. })
  282. .catch((err) => {
  283. error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`;
  284. return [];
  285. });
  286. if (error) {
  287. throw error;
  288. }
  289. }
  290. return res;
  291. };
  292. export const chatCompletion = async (
  293. token: string = '',
  294. body: object,
  295. url: string = `${WEBUI_BASE_URL}/api`
  296. ): Promise<[Response | null, AbortController]> => {
  297. const controller = new AbortController();
  298. let error = null;
  299. const res = await fetch(`${url}/chat/completions`, {
  300. signal: controller.signal,
  301. method: 'POST',
  302. headers: {
  303. Authorization: `Bearer ${token}`,
  304. 'Content-Type': 'application/json'
  305. },
  306. body: JSON.stringify(body)
  307. }).catch((err) => {
  308. console.log(err);
  309. error = err;
  310. return null;
  311. });
  312. if (error) {
  313. throw error;
  314. }
  315. return [res, controller];
  316. };
  317. export const generateOpenAIChatCompletion = async (
  318. token: string = '',
  319. body: object,
  320. url: string = `${WEBUI_BASE_URL}/api`
  321. ) => {
  322. let error = null;
  323. const res = await fetch(`${url}/chat/completions`, {
  324. method: 'POST',
  325. headers: {
  326. Authorization: `Bearer ${token}`,
  327. 'Content-Type': 'application/json'
  328. },
  329. body: JSON.stringify(body)
  330. })
  331. .then(async (res) => {
  332. if (!res.ok) throw await res.json();
  333. return res.json();
  334. })
  335. .catch((err) => {
  336. error = `${err?.detail ?? err}`;
  337. return null;
  338. });
  339. if (error) {
  340. throw error;
  341. }
  342. return res;
  343. };
  344. export const synthesizeOpenAISpeech = async (
  345. token: string = '',
  346. speaker: string = 'alloy',
  347. text: string = '',
  348. model: string = 'tts-1'
  349. ) => {
  350. let error = null;
  351. const res = await fetch(`${OPENAI_API_BASE_URL}/audio/speech`, {
  352. method: 'POST',
  353. headers: {
  354. Authorization: `Bearer ${token}`,
  355. 'Content-Type': 'application/json'
  356. },
  357. body: JSON.stringify({
  358. model: model,
  359. input: text,
  360. voice: speaker
  361. })
  362. }).catch((err) => {
  363. console.log(err);
  364. error = err;
  365. return null;
  366. });
  367. if (error) {
  368. throw error;
  369. }
  370. return res;
  371. };