index.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { OPENAI_API_BASE_URL } from '$lib/constants';
  2. export const getOpenAIUrl = async (token: string = '') => {
  3. let error = null;
  4. const res = await fetch(`${OPENAI_API_BASE_URL}/url`, {
  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_URL;
  29. };
  30. export const updateOpenAIUrl = async (token: string = '', url: string) => {
  31. let error = null;
  32. const res = await fetch(`${OPENAI_API_BASE_URL}/url/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. url: url
  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_URL;
  60. };
  61. export const getOpenAIKey = async (token: string = '') => {
  62. let error = null;
  63. const res = await fetch(`${OPENAI_API_BASE_URL}/key`, {
  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_KEY;
  88. };
  89. export const updateOpenAIKey = async (token: string = '', key: string) => {
  90. let error = null;
  91. const res = await fetch(`${OPENAI_API_BASE_URL}/key/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. key: key
  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_KEY;
  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. console.log(err);
  136. error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`;
  137. return [];
  138. });
  139. if (error) {
  140. throw error;
  141. }
  142. const models = Array.isArray(res) ? res : res?.data ?? null;
  143. return models
  144. ? models
  145. .map((model) => ({ name: model.id, external: true }))
  146. .sort((a, b) => {
  147. return a.name.localeCompare(b.name);
  148. })
  149. : models;
  150. };
  151. export const getOpenAIModelsDirect = async (
  152. base_url: string = 'https://api.openai.com/v1',
  153. api_key: string = ''
  154. ) => {
  155. let error = null;
  156. const res = await fetch(`${base_url}/models`, {
  157. method: 'GET',
  158. headers: {
  159. 'Content-Type': 'application/json',
  160. Authorization: `Bearer ${api_key}`
  161. }
  162. })
  163. .then(async (res) => {
  164. if (!res.ok) throw await res.json();
  165. return res.json();
  166. })
  167. .catch((err) => {
  168. console.log(err);
  169. error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`;
  170. return null;
  171. });
  172. if (error) {
  173. throw error;
  174. }
  175. let models = Array.isArray(res) ? res : res?.data ?? null;
  176. return models
  177. .map((model) => ({ name: model.id, external: true }))
  178. .filter((model) => (base_url.includes('openai') ? model.name.includes('gpt') : true))
  179. .sort((a, b) => {
  180. return a.name.localeCompare(b.name);
  181. });
  182. };
  183. export const generateOpenAIChatCompletion = async (token: string = '', body: object) => {
  184. let error = null;
  185. const res = await fetch(`${OPENAI_API_BASE_URL}/chat/completions`, {
  186. method: 'POST',
  187. headers: {
  188. Authorization: `Bearer ${token}`,
  189. 'Content-Type': 'application/json'
  190. },
  191. body: JSON.stringify(body)
  192. }).catch((err) => {
  193. console.log(err);
  194. error = err;
  195. return null;
  196. });
  197. if (error) {
  198. throw error;
  199. }
  200. return res;
  201. };