index.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import { OLLAMA_API_BASE_URL } from '$lib/constants';
  2. export const getOllamaAPIUrl = async (token: string = '') => {
  3. let error = null;
  4. const res = await fetch(`${OLLAMA_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.OLLAMA_API_BASE_URL;
  29. };
  30. export const updateOllamaAPIUrl = async (token: string = '', url: string) => {
  31. let error = null;
  32. const res = await fetch(`${OLLAMA_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.OLLAMA_API_BASE_URL;
  60. };
  61. export const getOllamaVersion = async (token: string = '') => {
  62. let error = null;
  63. const res = await fetch(`${OLLAMA_API_BASE_URL}/version`, {
  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?.version ?? '';
  88. };
  89. export const getOllamaModels = async (token: string = '') => {
  90. let error = null;
  91. const res = await fetch(`${OLLAMA_API_BASE_URL}/tags`, {
  92. method: 'GET',
  93. headers: {
  94. Accept: 'application/json',
  95. 'Content-Type': 'application/json',
  96. ...(token && { authorization: `Bearer ${token}` })
  97. }
  98. })
  99. .then(async (res) => {
  100. if (!res.ok) throw await res.json();
  101. return res.json();
  102. })
  103. .catch((err) => {
  104. console.log(err);
  105. if ('detail' in err) {
  106. error = err.detail;
  107. } else {
  108. error = 'Server connection failed';
  109. }
  110. return null;
  111. });
  112. if (error) {
  113. throw error;
  114. }
  115. return (res?.models ?? []).sort((a, b) => {
  116. return a.name.localeCompare(b.name);
  117. });
  118. };
  119. export const generateTitle = async (token: string = '', model: string, prompt: string) => {
  120. let error = null;
  121. const res = await fetch(`${OLLAMA_API_BASE_URL}/generate`, {
  122. method: 'POST',
  123. headers: {
  124. 'Content-Type': 'text/event-stream',
  125. Authorization: `Bearer ${token}`
  126. },
  127. body: JSON.stringify({
  128. model: model,
  129. prompt: `Generate a brief 3-5 word title for this question, excluding the term 'title.' Then, please reply with only the title: ${prompt}`,
  130. stream: false
  131. })
  132. })
  133. .then(async (res) => {
  134. if (!res.ok) throw await res.json();
  135. return res.json();
  136. })
  137. .catch((err) => {
  138. console.log(err);
  139. if ('detail' in err) {
  140. error = err.detail;
  141. }
  142. return null;
  143. });
  144. if (error) {
  145. throw error;
  146. }
  147. return res?.response ?? 'New Chat';
  148. };
  149. export const generateChatCompletion = async (token: string = '', body: object) => {
  150. let error = null;
  151. const res = await fetch(`${OLLAMA_API_BASE_URL}/chat`, {
  152. method: 'POST',
  153. headers: {
  154. 'Content-Type': 'text/event-stream',
  155. Authorization: `Bearer ${token}`
  156. },
  157. body: JSON.stringify(body)
  158. }).catch((err) => {
  159. error = err;
  160. return null;
  161. });
  162. if (error) {
  163. throw error;
  164. }
  165. return res;
  166. };
  167. export const createModel = async (token: string, tagName: string, content: string) => {
  168. let error = null;
  169. const res = await fetch(`${OLLAMA_API_BASE_URL}/create`, {
  170. method: 'POST',
  171. headers: {
  172. 'Content-Type': 'text/event-stream',
  173. Authorization: `Bearer ${token}`
  174. },
  175. body: JSON.stringify({
  176. name: tagName,
  177. modelfile: content
  178. })
  179. }).catch((err) => {
  180. error = err;
  181. return null;
  182. });
  183. if (error) {
  184. throw error;
  185. }
  186. return res;
  187. };
  188. export const deleteModel = async (token: string, tagName: string) => {
  189. let error = null;
  190. const res = await fetch(`${OLLAMA_API_BASE_URL}/delete`, {
  191. method: 'DELETE',
  192. headers: {
  193. 'Content-Type': 'text/event-stream',
  194. Authorization: `Bearer ${token}`
  195. },
  196. body: JSON.stringify({
  197. name: tagName
  198. })
  199. })
  200. .then(async (res) => {
  201. if (!res.ok) throw await res.json();
  202. return res.json();
  203. })
  204. .then((json) => {
  205. console.log(json);
  206. return true;
  207. })
  208. .catch((err) => {
  209. console.log(err);
  210. error = err.error;
  211. return null;
  212. });
  213. if (error) {
  214. throw error;
  215. }
  216. return res;
  217. };
  218. export const pullModel = async (token: string, tagName: string) => {
  219. let error = null;
  220. const res = await fetch(`${OLLAMA_API_BASE_URL}/pull`, {
  221. method: 'POST',
  222. headers: {
  223. 'Content-Type': 'text/event-stream',
  224. Authorization: `Bearer ${token}`
  225. },
  226. body: JSON.stringify({
  227. name: tagName
  228. })
  229. }).catch((err) => {
  230. error = err;
  231. return null;
  232. });
  233. if (error) {
  234. throw error;
  235. }
  236. return res;
  237. };