index.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. // TODO: migrate to backend
  120. export const generateTitle = async (
  121. token: string = '',
  122. template: string,
  123. model: string,
  124. prompt: string
  125. ) => {
  126. let error = null;
  127. template = template.replace(/{{prompt}}/g, prompt);
  128. console.log(template);
  129. const res = await fetch(`${OLLAMA_API_BASE_URL}/generate`, {
  130. method: 'POST',
  131. headers: {
  132. 'Content-Type': 'text/event-stream',
  133. Authorization: `Bearer ${token}`
  134. },
  135. body: JSON.stringify({
  136. model: model,
  137. prompt: template,
  138. stream: false
  139. })
  140. })
  141. .then(async (res) => {
  142. if (!res.ok) throw await res.json();
  143. return res.json();
  144. })
  145. .catch((err) => {
  146. console.log(err);
  147. if ('detail' in err) {
  148. error = err.detail;
  149. }
  150. return null;
  151. });
  152. if (error) {
  153. throw error;
  154. }
  155. return res?.response ?? 'New Chat';
  156. };
  157. export const generatePrompt = async (token: string = '', model: string, conversation: string) => {
  158. let error = null;
  159. if (conversation === '') {
  160. conversation = '[no existing conversation]';
  161. }
  162. const res = await fetch(`${OLLAMA_API_BASE_URL}/generate`, {
  163. method: 'POST',
  164. headers: {
  165. 'Content-Type': 'text/event-stream',
  166. Authorization: `Bearer ${token}`
  167. },
  168. body: JSON.stringify({
  169. model: model,
  170. prompt: `Conversation:
  171. ${conversation}
  172. 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.
  173. Response:
  174. `
  175. })
  176. }).catch((err) => {
  177. console.log(err);
  178. if ('detail' in err) {
  179. error = err.detail;
  180. }
  181. return null;
  182. });
  183. if (error) {
  184. throw error;
  185. }
  186. return res;
  187. };
  188. export const generateChatCompletion = async (token: string = '', body: object) => {
  189. let controller = new AbortController();
  190. let error = null;
  191. const res = await fetch(`${OLLAMA_API_BASE_URL}/chat`, {
  192. signal: controller.signal,
  193. method: 'POST',
  194. headers: {
  195. 'Content-Type': 'text/event-stream',
  196. Authorization: `Bearer ${token}`
  197. },
  198. body: JSON.stringify(body)
  199. }).catch((err) => {
  200. error = err;
  201. return null;
  202. });
  203. if (error) {
  204. throw error;
  205. }
  206. return [res, controller];
  207. };
  208. export const cancelChatCompletion = async (token: string = '', requestId: string) => {
  209. let error = null;
  210. const res = await fetch(`${OLLAMA_API_BASE_URL}/cancel/${requestId}`, {
  211. method: 'GET',
  212. headers: {
  213. 'Content-Type': 'text/event-stream',
  214. Authorization: `Bearer ${token}`
  215. }
  216. }).catch((err) => {
  217. error = err;
  218. return null;
  219. });
  220. if (error) {
  221. throw error;
  222. }
  223. return res;
  224. };
  225. export const createModel = async (token: string, tagName: string, content: string) => {
  226. let error = null;
  227. const res = await fetch(`${OLLAMA_API_BASE_URL}/create`, {
  228. method: 'POST',
  229. headers: {
  230. 'Content-Type': 'text/event-stream',
  231. Authorization: `Bearer ${token}`
  232. },
  233. body: JSON.stringify({
  234. name: tagName,
  235. modelfile: content
  236. })
  237. }).catch((err) => {
  238. error = err;
  239. return null;
  240. });
  241. if (error) {
  242. throw error;
  243. }
  244. return res;
  245. };
  246. export const deleteModel = async (token: string, tagName: string) => {
  247. let error = null;
  248. const res = await fetch(`${OLLAMA_API_BASE_URL}/delete`, {
  249. method: 'DELETE',
  250. headers: {
  251. 'Content-Type': 'text/event-stream',
  252. Authorization: `Bearer ${token}`
  253. },
  254. body: JSON.stringify({
  255. name: tagName
  256. })
  257. })
  258. .then(async (res) => {
  259. if (!res.ok) throw await res.json();
  260. return res.json();
  261. })
  262. .then((json) => {
  263. console.log(json);
  264. return true;
  265. })
  266. .catch((err) => {
  267. console.log(err);
  268. error = err.error;
  269. return null;
  270. });
  271. if (error) {
  272. throw error;
  273. }
  274. return res;
  275. };
  276. export const pullModel = async (token: string, tagName: string) => {
  277. let error = null;
  278. const res = await fetch(`${OLLAMA_API_BASE_URL}/pull`, {
  279. method: 'POST',
  280. headers: {
  281. 'Content-Type': 'text/event-stream',
  282. Authorization: `Bearer ${token}`
  283. },
  284. body: JSON.stringify({
  285. name: tagName
  286. })
  287. }).catch((err) => {
  288. console.log(err);
  289. error = err;
  290. if ('detail' in err) {
  291. error = err.detail;
  292. }
  293. return null;
  294. });
  295. if (error) {
  296. throw error;
  297. }
  298. return res;
  299. };
  300. // export const pullModel = async (token: string, tagName: string) => {
  301. // return await fetch(`${OLLAMA_API_BASE_URL}/pull`, {
  302. // method: 'POST',
  303. // headers: {
  304. // 'Content-Type': 'text/event-stream',
  305. // Authorization: `Bearer ${token}`
  306. // },
  307. // body: JSON.stringify({
  308. // name: tagName
  309. // })
  310. // });
  311. // };