index.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. import { OLLAMA_API_BASE_URL } from '$lib/constants';
  2. export const getOllamaUrls = async (token: string = '') => {
  3. let error = null;
  4. const res = await fetch(`${OLLAMA_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.OLLAMA_BASE_URLS;
  29. };
  30. export const updateOllamaUrls = async (token: string = '', urls: string[]) => {
  31. let error = null;
  32. const res = await fetch(`${OLLAMA_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.OLLAMA_BASE_URLS;
  60. };
  61. export const getOllamaVersion = async (token: string = '') => {
  62. let error = null;
  63. const res = await fetch(`${OLLAMA_API_BASE_URL}/api/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}/api/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 ?? [])
  116. .map((model) => ({ id: model.model, name: model.name ?? model.model, ...model }))
  117. .sort((a, b) => {
  118. return a.name.localeCompare(b.name);
  119. });
  120. };
  121. // TODO: migrate to backend
  122. export const generateTitle = async (
  123. token: string = '',
  124. template: string,
  125. model: string,
  126. prompt: string
  127. ) => {
  128. let error = null;
  129. template = template.replace(/{{prompt}}/g, prompt);
  130. console.log(template);
  131. const res = await fetch(`${OLLAMA_API_BASE_URL}/api/generate`, {
  132. method: 'POST',
  133. headers: {
  134. Accept: 'application/json',
  135. 'Content-Type': 'application/json',
  136. Authorization: `Bearer ${token}`
  137. },
  138. body: JSON.stringify({
  139. model: model,
  140. prompt: template,
  141. stream: false
  142. })
  143. })
  144. .then(async (res) => {
  145. if (!res.ok) throw await res.json();
  146. return res.json();
  147. })
  148. .catch((err) => {
  149. console.log(err);
  150. if ('detail' in err) {
  151. error = err.detail;
  152. }
  153. return null;
  154. });
  155. if (error) {
  156. throw error;
  157. }
  158. return res?.response ?? 'New Chat';
  159. };
  160. export const generatePrompt = async (token: string = '', model: string, conversation: string) => {
  161. let error = null;
  162. if (conversation === '') {
  163. conversation = '[no existing conversation]';
  164. }
  165. const res = await fetch(`${OLLAMA_API_BASE_URL}/api/generate`, {
  166. method: 'POST',
  167. headers: {
  168. Accept: 'application/json',
  169. 'Content-Type': 'application/json',
  170. Authorization: `Bearer ${token}`
  171. },
  172. body: JSON.stringify({
  173. model: model,
  174. prompt: `Conversation:
  175. ${conversation}
  176. As USER in the conversation above, your task is to continue the conversation. Remember, Your responses should be crafted as if you're a human conversing in a natural, realistic manner, keeping in mind the context and flow of the dialogue. Please generate a fitting response to the last message in the conversation, or if there is no existing conversation, initiate one as a normal person would.
  177. Response:
  178. `
  179. })
  180. }).catch((err) => {
  181. console.log(err);
  182. if ('detail' in err) {
  183. error = err.detail;
  184. }
  185. return null;
  186. });
  187. if (error) {
  188. throw error;
  189. }
  190. return res;
  191. };
  192. export const generateTextCompletion = async (token: string = '', model: string, text: string) => {
  193. let error = null;
  194. const res = await fetch(`${OLLAMA_API_BASE_URL}/api/generate`, {
  195. method: 'POST',
  196. headers: {
  197. Accept: 'application/json',
  198. 'Content-Type': 'application/json',
  199. Authorization: `Bearer ${token}`
  200. },
  201. body: JSON.stringify({
  202. model: model,
  203. prompt: text,
  204. stream: true
  205. })
  206. }).catch((err) => {
  207. error = err;
  208. return null;
  209. });
  210. if (error) {
  211. throw error;
  212. }
  213. return res;
  214. };
  215. export const generateChatCompletion = async (token: string = '', body: object) => {
  216. let controller = new AbortController();
  217. let error = null;
  218. const res = await fetch(`${OLLAMA_API_BASE_URL}/api/chat`, {
  219. signal: controller.signal,
  220. method: 'POST',
  221. headers: {
  222. Accept: 'application/json',
  223. 'Content-Type': 'application/json',
  224. Authorization: `Bearer ${token}`
  225. },
  226. body: JSON.stringify(body)
  227. }).catch((err) => {
  228. error = err;
  229. return null;
  230. });
  231. if (error) {
  232. throw error;
  233. }
  234. return [res, controller];
  235. };
  236. export const cancelChatCompletion = async (token: string = '', requestId: string) => {
  237. let error = null;
  238. const res = await fetch(`${OLLAMA_API_BASE_URL}/cancel/${requestId}`, {
  239. method: 'GET',
  240. headers: {
  241. 'Content-Type': 'text/event-stream',
  242. Authorization: `Bearer ${token}`
  243. }
  244. }).catch((err) => {
  245. error = err;
  246. return null;
  247. });
  248. if (error) {
  249. throw error;
  250. }
  251. return res;
  252. };
  253. export const createModel = async (token: string, tagName: string, content: string) => {
  254. let error = null;
  255. const res = await fetch(`${OLLAMA_API_BASE_URL}/api/create`, {
  256. method: 'POST',
  257. headers: {
  258. Accept: 'application/json',
  259. 'Content-Type': 'application/json',
  260. Authorization: `Bearer ${token}`
  261. },
  262. body: JSON.stringify({
  263. name: tagName,
  264. modelfile: content
  265. })
  266. }).catch((err) => {
  267. error = err;
  268. return null;
  269. });
  270. if (error) {
  271. throw error;
  272. }
  273. return res;
  274. };
  275. export const deleteModel = async (token: string, tagName: string, urlIdx: string | null = null) => {
  276. let error = null;
  277. const res = await fetch(
  278. `${OLLAMA_API_BASE_URL}/api/delete${urlIdx !== null ? `/${urlIdx}` : ''}`,
  279. {
  280. method: 'DELETE',
  281. headers: {
  282. Accept: 'application/json',
  283. 'Content-Type': 'application/json',
  284. Authorization: `Bearer ${token}`
  285. },
  286. body: JSON.stringify({
  287. name: tagName
  288. })
  289. }
  290. )
  291. .then(async (res) => {
  292. if (!res.ok) throw await res.json();
  293. return res.json();
  294. })
  295. .then((json) => {
  296. console.log(json);
  297. return true;
  298. })
  299. .catch((err) => {
  300. console.log(err);
  301. error = err;
  302. if ('detail' in err) {
  303. error = err.detail;
  304. }
  305. return null;
  306. });
  307. if (error) {
  308. throw error;
  309. }
  310. return res;
  311. };
  312. export const pullModel = async (token: string, tagName: string, urlIdx: string | null = null) => {
  313. let error = null;
  314. const res = await fetch(`${OLLAMA_API_BASE_URL}/api/pull${urlIdx !== null ? `/${urlIdx}` : ''}`, {
  315. method: 'POST',
  316. headers: {
  317. Accept: 'application/json',
  318. 'Content-Type': 'application/json',
  319. Authorization: `Bearer ${token}`
  320. },
  321. body: JSON.stringify({
  322. name: tagName
  323. })
  324. }).catch((err) => {
  325. console.log(err);
  326. error = err;
  327. if ('detail' in err) {
  328. error = err.detail;
  329. }
  330. return null;
  331. });
  332. if (error) {
  333. throw error;
  334. }
  335. return res;
  336. };
  337. export const downloadModel = async (
  338. token: string,
  339. download_url: string,
  340. urlIdx: string | null = null
  341. ) => {
  342. let error = null;
  343. const res = await fetch(
  344. `${OLLAMA_API_BASE_URL}/models/download${urlIdx !== null ? `/${urlIdx}` : ''}`,
  345. {
  346. method: 'POST',
  347. headers: {
  348. Accept: 'application/json',
  349. 'Content-Type': 'application/json',
  350. Authorization: `Bearer ${token}`
  351. },
  352. body: JSON.stringify({
  353. url: download_url
  354. })
  355. }
  356. ).catch((err) => {
  357. console.log(err);
  358. error = err;
  359. if ('detail' in err) {
  360. error = err.detail;
  361. }
  362. return null;
  363. });
  364. if (error) {
  365. throw error;
  366. }
  367. return res;
  368. };
  369. export const uploadModel = async (token: string, file: File, urlIdx: string | null = null) => {
  370. let error = null;
  371. const formData = new FormData();
  372. formData.append('file', file);
  373. const res = await fetch(
  374. `${OLLAMA_API_BASE_URL}/models/upload${urlIdx !== null ? `/${urlIdx}` : ''}`,
  375. {
  376. method: 'POST',
  377. headers: {
  378. Authorization: `Bearer ${token}`
  379. },
  380. body: formData
  381. }
  382. ).catch((err) => {
  383. console.log(err);
  384. error = err;
  385. if ('detail' in err) {
  386. error = err.detail;
  387. }
  388. return null;
  389. });
  390. if (error) {
  391. throw error;
  392. }
  393. return res;
  394. };
  395. // export const pullModel = async (token: string, tagName: string) => {
  396. // return await fetch(`${OLLAMA_API_BASE_URL}/pull`, {
  397. // method: 'POST',
  398. // headers: {
  399. // 'Content-Type': 'text/event-stream',
  400. // Authorization: `Bearer ${token}`
  401. // },
  402. // body: JSON.stringify({
  403. // name: tagName
  404. // })
  405. // });
  406. // };