2023-12-26 11:28:30 +00:00
|
|
|
import { OLLAMA_API_BASE_URL } from '$lib/constants';
|
2024-04-14 21:04:24 +00:00
|
|
|
import { promptTemplate } from '$lib/utils';
|
2023-12-26 11:28:30 +00:00
|
|
|
|
2024-03-05 08:59:35 +00:00
|
|
|
export const getOllamaUrls = async (token: string = '') => {
|
2023-12-26 11:28:30 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-03-05 08:59:35 +00:00
|
|
|
const res = await fetch(`${OLLAMA_API_BASE_URL}/urls`, {
|
2024-01-04 21:06:31 +00:00
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
} else {
|
|
|
|
error = 'Server connection failed';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:59:35 +00:00
|
|
|
return res.OLLAMA_BASE_URLS;
|
2024-01-04 21:06:31 +00:00
|
|
|
};
|
|
|
|
|
2024-03-05 08:59:35 +00:00
|
|
|
export const updateOllamaUrls = async (token: string = '', urls: string[]) => {
|
2024-01-04 21:06:31 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-03-05 08:59:35 +00:00
|
|
|
const res = await fetch(`${OLLAMA_API_BASE_URL}/urls/update`, {
|
2024-01-04 21:06:31 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2024-03-05 08:59:35 +00:00
|
|
|
urls: urls
|
2024-01-04 21:06:31 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
} else {
|
|
|
|
error = 'Server connection failed';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:59:35 +00:00
|
|
|
return res.OLLAMA_BASE_URLS;
|
2024-01-04 21:06:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getOllamaVersion = async (token: string = '') => {
|
|
|
|
let error = null;
|
|
|
|
|
2024-03-03 02:16:02 +00:00
|
|
|
const res = await fetch(`${OLLAMA_API_BASE_URL}/api/version`, {
|
2023-12-26 11:28:30 +00:00
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
2023-12-26 19:32:22 +00:00
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
2023-12-26 11:28:30 +00:00
|
|
|
} else {
|
|
|
|
error = 'Server connection failed';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2023-12-26 21:10:50 +00:00
|
|
|
return res?.version ?? '';
|
2023-12-26 11:28:30 +00:00
|
|
|
};
|
|
|
|
|
2024-01-04 21:06:31 +00:00
|
|
|
export const getOllamaModels = async (token: string = '') => {
|
2023-12-26 11:28:30 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-03-03 02:16:02 +00:00
|
|
|
const res = await fetch(`${OLLAMA_API_BASE_URL}/api/tags`, {
|
2023-12-26 11:28:30 +00:00
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
2023-12-26 19:32:22 +00:00
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
2023-12-26 11:28:30 +00:00
|
|
|
} else {
|
|
|
|
error = 'Server connection failed';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-02-22 12:12:26 +00:00
|
|
|
return (res?.models ?? [])
|
|
|
|
.map((model) => ({ id: model.model, name: model.name ?? model.model, ...model }))
|
|
|
|
.sort((a, b) => {
|
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
});
|
2023-12-26 11:28:30 +00:00
|
|
|
};
|
2023-12-26 20:50:52 +00:00
|
|
|
|
2024-02-21 20:51:52 +00:00
|
|
|
// TODO: migrate to backend
|
|
|
|
export const generateTitle = async (
|
|
|
|
token: string = '',
|
|
|
|
template: string,
|
|
|
|
model: string,
|
|
|
|
prompt: string
|
|
|
|
) => {
|
2023-12-26 20:50:52 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-04-14 21:04:24 +00:00
|
|
|
template = promptTemplate(template, prompt);
|
2024-02-21 20:51:52 +00:00
|
|
|
|
|
|
|
console.log(template);
|
|
|
|
|
2024-03-03 02:16:02 +00:00
|
|
|
const res = await fetch(`${OLLAMA_API_BASE_URL}/api/generate`, {
|
2023-12-26 20:50:52 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2024-03-05 08:59:35 +00:00
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2023-12-26 20:50:52 +00:00
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
model: model,
|
2024-02-21 20:51:52 +00:00
|
|
|
prompt: template,
|
2023-12-26 20:50:52 +00:00
|
|
|
stream: false
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res?.response ?? 'New Chat';
|
|
|
|
};
|
|
|
|
|
2024-01-10 06:47:31 +00:00
|
|
|
export const generatePrompt = async (token: string = '', model: string, conversation: string) => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
if (conversation === '') {
|
2024-01-10 07:06:33 +00:00
|
|
|
conversation = '[no existing conversation]';
|
2024-01-10 06:47:31 +00:00
|
|
|
}
|
|
|
|
|
2024-03-03 02:16:02 +00:00
|
|
|
const res = await fetch(`${OLLAMA_API_BASE_URL}/api/generate`, {
|
2024-01-10 06:47:31 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2024-03-05 08:59:35 +00:00
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2024-01-10 06:47:31 +00:00
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
model: model,
|
2024-01-10 07:06:33 +00:00
|
|
|
prompt: `Conversation:
|
2024-01-10 06:47:31 +00:00
|
|
|
${conversation}
|
|
|
|
|
2024-01-10 07:06:33 +00:00
|
|
|
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.
|
|
|
|
|
2024-01-10 06:47:31 +00:00
|
|
|
Response:
|
|
|
|
`
|
|
|
|
})
|
|
|
|
}).catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2024-03-02 11:01:44 +00:00
|
|
|
export const generateTextCompletion = async (token: string = '', model: string, text: string) => {
|
|
|
|
let error = null;
|
|
|
|
|
2024-03-03 02:16:02 +00:00
|
|
|
const res = await fetch(`${OLLAMA_API_BASE_URL}/api/generate`, {
|
2024-03-02 11:01:44 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2024-03-05 08:59:35 +00:00
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2024-03-02 11:01:44 +00:00
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
model: model,
|
|
|
|
prompt: text,
|
|
|
|
stream: true
|
|
|
|
})
|
|
|
|
}).catch((err) => {
|
|
|
|
error = err;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2024-01-04 21:06:31 +00:00
|
|
|
export const generateChatCompletion = async (token: string = '', body: object) => {
|
2024-01-18 03:19:44 +00:00
|
|
|
let controller = new AbortController();
|
2023-12-26 20:50:52 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-03-03 02:16:02 +00:00
|
|
|
const res = await fetch(`${OLLAMA_API_BASE_URL}/api/chat`, {
|
2024-01-18 03:19:44 +00:00
|
|
|
signal: controller.signal,
|
2023-12-26 20:50:52 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2024-03-05 08:59:35 +00:00
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2023-12-26 20:50:52 +00:00
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body)
|
|
|
|
}).catch((err) => {
|
|
|
|
error = err;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-01-18 03:19:44 +00:00
|
|
|
return [res, controller];
|
|
|
|
};
|
|
|
|
|
2024-03-23 20:12:23 +00:00
|
|
|
export const cancelOllamaRequest = async (token: string = '', requestId: string) => {
|
2024-01-18 03:19:44 +00:00
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${OLLAMA_API_BASE_URL}/cancel/${requestId}`, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'text/event-stream',
|
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
error = err;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2023-12-26 20:50:52 +00:00
|
|
|
return res;
|
|
|
|
};
|
2023-12-27 06:51:52 +00:00
|
|
|
|
2024-01-04 21:06:31 +00:00
|
|
|
export const createModel = async (token: string, tagName: string, content: string) => {
|
2023-12-27 06:51:52 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-03-03 02:16:02 +00:00
|
|
|
const res = await fetch(`${OLLAMA_API_BASE_URL}/api/create`, {
|
2023-12-27 06:51:52 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2024-03-05 08:59:35 +00:00
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2023-12-27 06:51:52 +00:00
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
name: tagName,
|
|
|
|
modelfile: content
|
|
|
|
})
|
|
|
|
}).catch((err) => {
|
|
|
|
error = err;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2024-03-05 10:12:55 +00:00
|
|
|
export const deleteModel = async (token: string, tagName: string, urlIdx: string | null = null) => {
|
2023-12-27 06:51:52 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-03-05 10:12:55 +00:00
|
|
|
const res = await fetch(
|
|
|
|
`${OLLAMA_API_BASE_URL}/api/delete${urlIdx !== null ? `/${urlIdx}` : ''}`,
|
|
|
|
{
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
name: tagName
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2023-12-27 06:51:52 +00:00
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.then((json) => {
|
|
|
|
console.log(json);
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
2024-03-05 08:59:35 +00:00
|
|
|
error = err;
|
|
|
|
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
}
|
|
|
|
|
2023-12-27 06:51:52 +00:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
2024-01-04 21:06:31 +00:00
|
|
|
|
2024-03-05 10:12:55 +00:00
|
|
|
export const pullModel = async (token: string, tagName: string, urlIdx: string | null = null) => {
|
2024-01-04 21:06:31 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-03-05 10:12:55 +00:00
|
|
|
const res = await fetch(`${OLLAMA_API_BASE_URL}/api/pull${urlIdx !== null ? `/${urlIdx}` : ''}`, {
|
2024-01-04 21:06:31 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2024-03-05 08:59:35 +00:00
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2024-01-04 21:06:31 +00:00
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
name: tagName
|
|
|
|
})
|
|
|
|
}).catch((err) => {
|
2024-01-06 21:02:09 +00:00
|
|
|
console.log(err);
|
2024-01-04 21:06:31 +00:00
|
|
|
error = err;
|
2024-01-06 21:02:09 +00:00
|
|
|
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
}
|
|
|
|
|
2024-01-04 21:06:31 +00:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
2024-01-06 21:02:09 +00:00
|
|
|
|
2024-03-22 06:45:00 +00:00
|
|
|
export const downloadModel = async (
|
|
|
|
token: string,
|
|
|
|
download_url: string,
|
|
|
|
urlIdx: string | null = null
|
|
|
|
) => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(
|
|
|
|
`${OLLAMA_API_BASE_URL}/models/download${urlIdx !== null ? `/${urlIdx}` : ''}`,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2024-03-22 07:10:55 +00:00
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2024-03-22 06:45:00 +00:00
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
url: download_url
|
|
|
|
})
|
|
|
|
}
|
|
|
|
).catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
error = err;
|
|
|
|
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const uploadModel = async (token: string, file: File, urlIdx: string | null = null) => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('file', file);
|
|
|
|
|
|
|
|
const res = await fetch(
|
|
|
|
`${OLLAMA_API_BASE_URL}/models/upload${urlIdx !== null ? `/${urlIdx}` : ''}`,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: formData
|
|
|
|
}
|
|
|
|
).catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
error = err;
|
|
|
|
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2024-01-06 21:02:09 +00:00
|
|
|
// export const pullModel = async (token: string, tagName: string) => {
|
|
|
|
// return await fetch(`${OLLAMA_API_BASE_URL}/pull`, {
|
|
|
|
// method: 'POST',
|
|
|
|
// headers: {
|
|
|
|
// 'Content-Type': 'text/event-stream',
|
|
|
|
// Authorization: `Bearer ${token}`
|
|
|
|
// },
|
|
|
|
// body: JSON.stringify({
|
|
|
|
// name: tagName
|
|
|
|
// })
|
|
|
|
// });
|
|
|
|
// };
|