index.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 getOpenAIModels = async (token: string, urlIdx?: number) => {
  186. let error = null;
  187. const res = await fetch(
  188. `${OPENAI_API_BASE_URL}/models${typeof urlIdx === 'number' ? `/${urlIdx}` : ''}`,
  189. {
  190. method: 'GET',
  191. headers: {
  192. Accept: 'application/json',
  193. 'Content-Type': 'application/json',
  194. ...(token && { authorization: `Bearer ${token}` })
  195. }
  196. }
  197. )
  198. .then(async (res) => {
  199. if (!res.ok) throw await res.json();
  200. return res.json();
  201. })
  202. .catch((err) => {
  203. error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`;
  204. return [];
  205. });
  206. if (error) {
  207. throw error;
  208. }
  209. return res;
  210. };
  211. export const verifyOpenAIConnection = async (
  212. token: string = '',
  213. url: string = 'https://api.openai.com/v1',
  214. key: string = ''
  215. ) => {
  216. let error = null;
  217. const res = await fetch(`${OPENAI_API_BASE_URL}/verify`, {
  218. method: 'POST',
  219. headers: {
  220. Accept: 'application/json',
  221. Authorization: `Bearer ${token}`,
  222. 'Content-Type': 'application/json'
  223. },
  224. body: JSON.stringify({
  225. url,
  226. key
  227. })
  228. })
  229. .then(async (res) => {
  230. if (!res.ok) throw await res.json();
  231. return res.json();
  232. })
  233. .catch((err) => {
  234. error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`;
  235. return [];
  236. });
  237. if (error) {
  238. throw error;
  239. }
  240. return res;
  241. };
  242. export const chatCompletion = async (
  243. token: string = '',
  244. body: object,
  245. url: string = `${WEBUI_BASE_URL}/api`
  246. ): Promise<[Response | null, AbortController]> => {
  247. const controller = new AbortController();
  248. let error = null;
  249. const res = await fetch(`${url}/chat/completions`, {
  250. signal: controller.signal,
  251. method: 'POST',
  252. headers: {
  253. Authorization: `Bearer ${token}`,
  254. 'Content-Type': 'application/json'
  255. },
  256. body: JSON.stringify(body)
  257. }).catch((err) => {
  258. console.log(err);
  259. error = err;
  260. return null;
  261. });
  262. if (error) {
  263. throw error;
  264. }
  265. return [res, controller];
  266. };
  267. export const generateOpenAIChatCompletion = async (
  268. token: string = '',
  269. body: object,
  270. url: string = `${WEBUI_BASE_URL}/api`
  271. ) => {
  272. let error = null;
  273. const res = await fetch(`${url}/chat/completions`, {
  274. method: 'POST',
  275. headers: {
  276. Authorization: `Bearer ${token}`,
  277. 'Content-Type': 'application/json'
  278. },
  279. body: JSON.stringify(body)
  280. })
  281. .then(async (res) => {
  282. if (!res.ok) throw await res.json();
  283. return res.json();
  284. })
  285. .catch((err) => {
  286. error = `${err?.detail ?? 'Network Problem'}`;
  287. return null;
  288. });
  289. if (error) {
  290. throw error;
  291. }
  292. return res;
  293. };
  294. export const synthesizeOpenAISpeech = async (
  295. token: string = '',
  296. speaker: string = 'alloy',
  297. text: string = '',
  298. model: string = 'tts-1'
  299. ) => {
  300. let error = null;
  301. const res = await fetch(`${OPENAI_API_BASE_URL}/audio/speech`, {
  302. method: 'POST',
  303. headers: {
  304. Authorization: `Bearer ${token}`,
  305. 'Content-Type': 'application/json'
  306. },
  307. body: JSON.stringify({
  308. model: model,
  309. input: text,
  310. voice: speaker
  311. })
  312. }).catch((err) => {
  313. console.log(err);
  314. error = err;
  315. return null;
  316. });
  317. if (error) {
  318. throw error;
  319. }
  320. return res;
  321. };