index.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import { OPENAI_API_BASE_URL } from '$lib/constants';
  2. export const getOpenAIUrls = async (token: string = '') => {
  3. let error = null;
  4. const res = await fetch(`${OPENAI_API_BASE_URL}/urls`, {
  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.OPENAI_API_BASE_URLS;
  29. };
  30. export const updateOpenAIUrls = async (token: string = '', urls: string[]) => {
  31. let error = null;
  32. const res = await fetch(`${OPENAI_API_BASE_URL}/urls/update`, {
  33. method: 'POST',
  34. headers: {
  35. Accept: 'application/json',
  36. 'Content-Type': 'application/json',
  37. ...(token && { authorization: `Bearer ${token}` })
  38. },
  39. body: JSON.stringify({
  40. urls: urls
  41. })
  42. })
  43. .then(async (res) => {
  44. if (!res.ok) throw await res.json();
  45. return res.json();
  46. })
  47. .catch((err) => {
  48. console.log(err);
  49. if ('detail' in err) {
  50. error = err.detail;
  51. } else {
  52. error = 'Server connection failed';
  53. }
  54. return null;
  55. });
  56. if (error) {
  57. throw error;
  58. }
  59. return res.OPENAI_API_BASE_URLS;
  60. };
  61. export const getOpenAIKeys = async (token: string = '') => {
  62. let error = null;
  63. const res = await fetch(`${OPENAI_API_BASE_URL}/keys`, {
  64. method: 'GET',
  65. headers: {
  66. Accept: 'application/json',
  67. 'Content-Type': 'application/json',
  68. ...(token && { authorization: `Bearer ${token}` })
  69. }
  70. })
  71. .then(async (res) => {
  72. if (!res.ok) throw await res.json();
  73. return res.json();
  74. })
  75. .catch((err) => {
  76. console.log(err);
  77. if ('detail' in err) {
  78. error = err.detail;
  79. } else {
  80. error = 'Server connection failed';
  81. }
  82. return null;
  83. });
  84. if (error) {
  85. throw error;
  86. }
  87. return res.OPENAI_API_KEYS;
  88. };
  89. export const updateOpenAIKeys = async (token: string = '', keys: string[]) => {
  90. let error = null;
  91. const res = await fetch(`${OPENAI_API_BASE_URL}/keys/update`, {
  92. method: 'POST',
  93. headers: {
  94. Accept: 'application/json',
  95. 'Content-Type': 'application/json',
  96. ...(token && { authorization: `Bearer ${token}` })
  97. },
  98. body: JSON.stringify({
  99. keys: keys
  100. })
  101. })
  102. .then(async (res) => {
  103. if (!res.ok) throw await res.json();
  104. return res.json();
  105. })
  106. .catch((err) => {
  107. console.log(err);
  108. if ('detail' in err) {
  109. error = err.detail;
  110. } else {
  111. error = 'Server connection failed';
  112. }
  113. return null;
  114. });
  115. if (error) {
  116. throw error;
  117. }
  118. return res.OPENAI_API_KEYS;
  119. };
  120. export const getOpenAIModels = async (token: string = '') => {
  121. let error = null;
  122. const res = await fetch(`${OPENAI_API_BASE_URL}/models`, {
  123. method: 'GET',
  124. headers: {
  125. Accept: 'application/json',
  126. 'Content-Type': 'application/json',
  127. ...(token && { authorization: `Bearer ${token}` })
  128. }
  129. })
  130. .then(async (res) => {
  131. if (!res.ok) throw await res.json();
  132. return res.json();
  133. })
  134. .catch((err) => {
  135. error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`;
  136. return [];
  137. });
  138. if (error) {
  139. throw error;
  140. }
  141. const models = Array.isArray(res) ? res : res?.data ?? null;
  142. return models
  143. ? models
  144. .map((model) => ({ id: model.id, name: model.name ?? model.id, external: true }))
  145. .sort((a, b) => {
  146. return a.name.localeCompare(b.name);
  147. })
  148. : models;
  149. };
  150. export const getOpenAIModelsDirect = async (
  151. base_url: string = 'https://api.openai.com/v1',
  152. api_key: string = ''
  153. ) => {
  154. let error = null;
  155. const res = await fetch(`${base_url}/models`, {
  156. method: 'GET',
  157. headers: {
  158. 'Content-Type': 'application/json',
  159. Authorization: `Bearer ${api_key}`
  160. }
  161. })
  162. .then(async (res) => {
  163. if (!res.ok) throw await res.json();
  164. return res.json();
  165. })
  166. .catch((err) => {
  167. console.log(err);
  168. error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`;
  169. return null;
  170. });
  171. if (error) {
  172. throw error;
  173. }
  174. const models = Array.isArray(res) ? res : res?.data ?? null;
  175. return models
  176. .map((model) => ({ id: model.id, name: model.name ?? model.id, external: true }))
  177. .filter((model) => (base_url.includes('openai') ? model.name.includes('gpt') : true))
  178. .sort((a, b) => {
  179. return a.name.localeCompare(b.name);
  180. });
  181. };
  182. export const generateOpenAIChatCompletion = async (
  183. token: string = '',
  184. body: object,
  185. url: string = OPENAI_API_BASE_URL
  186. ) => {
  187. let error = null;
  188. const res = await fetch(`${url}/chat/completions`, {
  189. method: 'POST',
  190. headers: {
  191. Authorization: `Bearer ${token}`,
  192. 'Content-Type': 'application/json'
  193. },
  194. body: JSON.stringify(body)
  195. }).catch((err) => {
  196. console.log(err);
  197. error = err;
  198. return null;
  199. });
  200. if (error) {
  201. throw error;
  202. }
  203. return res;
  204. };
  205. export const synthesizeOpenAISpeech = async (
  206. token: string = '',
  207. speaker: string = 'alloy',
  208. text: string = ''
  209. ) => {
  210. let error = null;
  211. const res = await fetch(`${OPENAI_API_BASE_URL}/audio/speech`, {
  212. method: 'POST',
  213. headers: {
  214. Authorization: `Bearer ${token}`,
  215. 'Content-Type': 'application/json'
  216. },
  217. body: JSON.stringify({
  218. model: 'tts-1',
  219. input: text,
  220. voice: speaker
  221. })
  222. }).catch((err) => {
  223. console.log(err);
  224. error = err;
  225. return null;
  226. });
  227. if (error) {
  228. throw error;
  229. }
  230. return res;
  231. };
  232. export const generateTitle = async (
  233. token: string = '',
  234. template: string,
  235. model: string,
  236. prompt: string,
  237. url: string = OPENAI_API_BASE_URL
  238. ) => {
  239. let error = null;
  240. template = template.replace(/{{prompt}}/g, prompt);
  241. console.log(template);
  242. const res = await fetch(`${url}/chat/completions`, {
  243. method: 'POST',
  244. headers: {
  245. Accept: 'application/json',
  246. 'Content-Type': 'application/json',
  247. Authorization: `Bearer ${token}`
  248. },
  249. body: JSON.stringify({
  250. model: model,
  251. messages: [
  252. {
  253. role: 'user',
  254. content: template
  255. }
  256. ],
  257. stream: false
  258. })
  259. })
  260. .then(async (res) => {
  261. if (!res.ok) throw await res.json();
  262. return res.json();
  263. })
  264. .catch((err) => {
  265. console.log(err);
  266. if ('detail' in err) {
  267. error = err.detail;
  268. }
  269. return null;
  270. });
  271. if (error) {
  272. throw error;
  273. }
  274. return res?.choices[0]?.message?.content ?? 'New Chat';
  275. };