to:sync
This commit is contained in:
parent
5e1711c128
commit
d3ec570683
@ -234,9 +234,9 @@
|
|||||||
// 初始化
|
// 初始化
|
||||||
function init() {
|
function init() {
|
||||||
addAIMessage("你好,请输入你想问的问题。");
|
addAIMessage("你好,请输入你想问的问题。");
|
||||||
var submit = $("#submit");
|
let submit = $("#submit");
|
||||||
var userInput = $("#user-input");
|
let userInput = $("#user-input");
|
||||||
var focus = false;
|
let focus = false;
|
||||||
// 监听输入框焦点
|
// 监听输入框焦点
|
||||||
userInput.focus(function () {
|
userInput.focus(function () {
|
||||||
focus = true;
|
focus = true;
|
||||||
|
@ -48,30 +48,30 @@
|
|||||||
<label for="system-prompt" class="me-2" style="font-size: normal;">System Prompt</label>
|
<label for="system-prompt" class="me-2" style="font-size: normal;">System Prompt</label>
|
||||||
<input id="system-prompt" class="form-control" type="text" placeholder="You are a helpful assistant"
|
<input id="system-prompt" class="form-control" type="text" placeholder="You are a helpful assistant"
|
||||||
style="width: auto;"></select>
|
style="width: auto;"></select>
|
||||||
<!-- <label for="host-address" class="me-2" style="font-size: normal;">Hostname</label>-->
|
<label for="host-address" class="me-2" style="font-size: normal;display: none;">Hostname</label>
|
||||||
<!-- <input id="host-address" class="form-control" type="text" placeholder="http://localhost:11434"-->
|
<input id="host-address" class="form-control" type="text" placeholder="http://localhost:11434"
|
||||||
<!-- style="width: auto;"></select>-->
|
style="width: auto;display: none;"></select>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<!-- <div class="d-flex align-items-center m-2">-->
|
<div class="d-flex align-items-center m-2">
|
||||||
<!-- <div class="p-2 flex-grow-1 bd-highlight">-->
|
<div class="p-2 flex-grow-1 bd-highlight">
|
||||||
<!-- <label for="model-select" class="me-2" style="font-size: normal;">Model:</label>-->
|
<label for="model-select" class="me-2" style="font-size: normal; display: none;">Model:</label>
|
||||||
<!-- <select class="form-select me-5" id="model-select" style="width: auto;"></select>-->
|
<select class="form-select me-5" id="model-select" style="width: auto; display: none;"></select>
|
||||||
<!-- <label for="chat-select" class="me-2" style="font-size: normal;">History:</label>-->
|
<label for="chat-select" class="me-2" style="font-size: normal; display: none;">History:</label>
|
||||||
<!-- <select id="chat-select" class="form-select me-2" style="width: auto;">-->
|
<select id="chat-select" class="form-select me-2" style="width: auto; display: none;">
|
||||||
<!-- <option value="" disabled selected>Select a chat</option>-->
|
<option value="" disabled selected>Select a chat</option>
|
||||||
<!-- </select>-->
|
</select>
|
||||||
<!-- </div>-->
|
</div>
|
||||||
<!-- <div class="p-auto flex-grow-1 bd-highlight">-->
|
<div class="p-auto flex-grow-1 bd-highlight">
|
||||||
<!-- <div class="d-flex flex-column">-->
|
<div class="d-flex flex-column" >
|
||||||
<!-- <button id="new-chat" class="btn btn-dark mb-2" type="button">Reset</button>-->
|
<button id="new-chat" class="btn btn-dark mb-2" type="button" style="display: none">Reset</button>
|
||||||
<!-- <button id="save-chat" class="btn btn-secondary mb-2" type="button" data-bs-toggle="modal"-->
|
<button id="save-chat" class="btn btn-secondary mb-2" type="button" data-bs-toggle="modal"
|
||||||
<!-- data-bs-target="#nameModal">Save-->
|
data-bs-target="#nameModal" style="display: none">Save
|
||||||
<!-- </button>-->
|
</button>
|
||||||
<!-- <button id="delete-chat" class="btn btn-danger" type="button">Delete</button>-->
|
<button id="delete-chat" class="btn btn-danger" type="button" style="display: none">Delete</button>
|
||||||
<!-- </div>-->
|
</div>
|
||||||
<!-- </div>-->
|
</div>
|
||||||
<!-- </div>-->
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -58,9 +58,10 @@ function setSystemPrompt(){
|
|||||||
|
|
||||||
|
|
||||||
async function getModels(){
|
async function getModels(){
|
||||||
const response = await fetch(`${ollama_host}/api/tags`);
|
// const response = await fetch(`${ollama_host}/api/tags`);
|
||||||
const data = await response.json();
|
// const data = await response.json();
|
||||||
return data;
|
// return data;
|
||||||
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -68,8 +69,8 @@ async function getModels(){
|
|||||||
function postRequest(data, signal) {
|
function postRequest(data, signal) {
|
||||||
// const URL = `${ollama_host}/api/generate`;
|
// const URL = `${ollama_host}/api/generate`;
|
||||||
const URL = `https://openrouter.ai/api/v1/chat/completions`;
|
const URL = `https://openrouter.ai/api/v1/chat/completions`;
|
||||||
const body = JSON.stringify({model: "google/gemma-7b-it:free", messages: [{role: "user", content: data}]});
|
const body = JSON.stringify({model: "google/gemma-7b-it:free", messages: [{role: "user", content: data.prompt}]});
|
||||||
alert(body);
|
// alert(body)
|
||||||
return fetch(URL, {
|
return fetch(URL, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -13,7 +13,6 @@ Also see: https://github.com/jmorganca/ollama/blob/main/docs/faq.md
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const clipboardIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-clipboard" viewBox="0 0 16 16">
|
const clipboardIcon = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-clipboard" viewBox="0 0 16 16">
|
||||||
<path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"/>
|
<path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z"/>
|
||||||
<path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"/>
|
<path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z"/>
|
||||||
@ -23,13 +22,13 @@ const textBoxBaseHeight = 40; // This should match the default height set in CS
|
|||||||
// change settings of marked from default to remove deprecation warnings
|
// change settings of marked from default to remove deprecation warnings
|
||||||
// see conversation here: https://github.com/markedjs/marked/issues/2793
|
// see conversation here: https://github.com/markedjs/marked/issues/2793
|
||||||
marked.use({
|
marked.use({
|
||||||
mangle: false,
|
mangle: false,
|
||||||
headerIds: false
|
headerIds: false
|
||||||
});
|
});
|
||||||
|
|
||||||
function autoFocusInput() {
|
function autoFocusInput() {
|
||||||
const userInput = document.getElementById('user-input');
|
const userInput = document.getElementById('user-input');
|
||||||
userInput.focus();
|
userInput.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -37,77 +36,78 @@ takes in model as a string
|
|||||||
updates the query parameters of page url to include model name
|
updates the query parameters of page url to include model name
|
||||||
*/
|
*/
|
||||||
function updateModelInQueryString(model) {
|
function updateModelInQueryString(model) {
|
||||||
// make sure browser supports features
|
// make sure browser supports features
|
||||||
if (window.history.replaceState && 'URLSearchParams' in window) {
|
if (window.history.replaceState && 'URLSearchParams' in window) {
|
||||||
const searchParams = new URLSearchParams(window.location.search);
|
const searchParams = new URLSearchParams(window.location.search);
|
||||||
searchParams.set("model", model);
|
searchParams.set("model", model);
|
||||||
// replace current url without reload
|
// replace current url without reload
|
||||||
const newPathWithQuery = `${window.location.pathname}?${searchParams.toString()}`
|
const newPathWithQuery = `${window.location.pathname}?${searchParams.toString()}`
|
||||||
window.history.replaceState(null, '', newPathWithQuery);
|
window.history.replaceState(null, '', newPathWithQuery);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch available models and populate the dropdown
|
// Fetch available models and populate the dropdown
|
||||||
async function populateModels() {
|
async function populateModels() {
|
||||||
document.getElementById('send-button').addEventListener('click', submitRequest);
|
document.getElementById('send-button').addEventListener('click', submitRequest);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await getModels();
|
const data = await getModels();
|
||||||
|
|
||||||
const selectElement = document.getElementById('model-select');
|
const selectElement = document.getElementById('model-select');
|
||||||
|
|
||||||
// set up handler for selection
|
// set up handler for selection
|
||||||
selectElement.onchange = (() => updateModelInQueryString(selectElement.value));
|
selectElement.onchange = (() => updateModelInQueryString(selectElement.value));
|
||||||
|
|
||||||
data.models.forEach((model) => {
|
if (data !== undefined) {
|
||||||
const option = document.createElement('option');
|
data.models.forEach((model) => {
|
||||||
option.value = model.name;
|
const option = document.createElement('option');
|
||||||
option.innerText = model.name;
|
option.value = model.name;
|
||||||
selectElement.appendChild(option);
|
option.innerText = model.name;
|
||||||
});
|
selectElement.appendChild(option);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// select option present in url parameter if present
|
|
||||||
const queryParams = new URLSearchParams(window.location.search);
|
// select option present in url parameter if present
|
||||||
const requestedModel = queryParams.get('model');
|
const queryParams = new URLSearchParams(window.location.search);
|
||||||
// update the selection based on if requestedModel is a value in options
|
const requestedModel = queryParams.get('model');
|
||||||
if ([...selectElement.options].map(o => o.value).includes(requestedModel)) {
|
// update the selection based on if requestedModel is a value in options
|
||||||
selectElement.value = requestedModel;
|
if ([...selectElement.options].map(o => o.value).includes(requestedModel)) {
|
||||||
|
selectElement.value = requestedModel;
|
||||||
|
}
|
||||||
|
// otherwise set to the first element if exists and update URL accordingly
|
||||||
|
else if (selectElement.options.length) {
|
||||||
|
selectElement.value = selectElement.options[0].value;
|
||||||
|
updateModelInQueryString(selectElement.value);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
document.getElementById('errorText').innerHTML =
|
||||||
|
DOMPurify.sanitize(marked.parse(
|
||||||
|
`Ollama-ui was unable to communitcate with Ollama due to the following error:\n\n`
|
||||||
|
+ `\`\`\`${error.message}\`\`\`\n\n---------------------\n`
|
||||||
|
+ faqString));
|
||||||
|
let modal = new bootstrap.Modal(document.getElementById('errorModal'));
|
||||||
|
modal.show();
|
||||||
}
|
}
|
||||||
// otherwise set to the first element if exists and update URL accordingly
|
|
||||||
else if (selectElement.options.length) {
|
|
||||||
selectElement.value = selectElement.options[0].value;
|
|
||||||
updateModelInQueryString(selectElement.value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
document.getElementById('errorText').innerHTML =
|
|
||||||
DOMPurify.sanitize(marked.parse(
|
|
||||||
`Ollama-ui was unable to communitcate with Ollama due to the following error:\n\n`
|
|
||||||
+ `\`\`\`${error.message}\`\`\`\n\n---------------------\n`
|
|
||||||
+ faqString));
|
|
||||||
let modal = new bootstrap.Modal(document.getElementById('errorModal'));
|
|
||||||
modal.show();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// adjusts the padding at the bottom of scrollWrapper to be the height of the input box
|
// adjusts the padding at the bottom of scrollWrapper to be the height of the input box
|
||||||
function adjustPadding() {
|
function adjustPadding() {
|
||||||
const inputBoxHeight = document.getElementById('input-area').offsetHeight;
|
const inputBoxHeight = document.getElementById('input-area').offsetHeight;
|
||||||
const scrollWrapper = document.getElementById('scroll-wrapper');
|
const scrollWrapper = document.getElementById('scroll-wrapper');
|
||||||
scrollWrapper.style.paddingBottom = `${inputBoxHeight + 15}px`;
|
scrollWrapper.style.paddingBottom = `${inputBoxHeight + 15}px`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// sets up padding resize whenever input box has its height changed
|
// sets up padding resize whenever input box has its height changed
|
||||||
const autoResizePadding = new ResizeObserver(() => {
|
const autoResizePadding = new ResizeObserver(() => {
|
||||||
adjustPadding();
|
adjustPadding();
|
||||||
});
|
});
|
||||||
autoResizePadding.observe(document.getElementById('input-area'));
|
autoResizePadding.observe(document.getElementById('input-area'));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Function to get the selected model
|
// Function to get the selected model
|
||||||
function getSelectedModel() {
|
function getSelectedModel() {
|
||||||
return document.getElementById('model-select').value;
|
return document.getElementById('model-select').value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// variables to handle auto-scroll
|
// variables to handle auto-scroll
|
||||||
@ -117,182 +117,191 @@ const scrollWrapper = document.getElementById('scroll-wrapper');
|
|||||||
let isAutoScrollOn = true;
|
let isAutoScrollOn = true;
|
||||||
// autoscroll when new line is added
|
// autoscroll when new line is added
|
||||||
const autoScroller = new ResizeObserver(() => {
|
const autoScroller = new ResizeObserver(() => {
|
||||||
if (isAutoScrollOn) {
|
if (isAutoScrollOn) {
|
||||||
scrollWrapper.scrollIntoView({behavior: "smooth", block: "end"});
|
scrollWrapper.scrollIntoView({behavior: "smooth", block: "end"});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// event listener for scrolling
|
// event listener for scrolling
|
||||||
let lastKnownScrollPosition = 0;
|
let lastKnownScrollPosition = 0;
|
||||||
let ticking = false;
|
let ticking = false;
|
||||||
document.addEventListener("scroll", (event) => {
|
document.addEventListener("scroll", (event) => {
|
||||||
// if user has scrolled up and autoScroll is on we turn it off
|
// if user has scrolled up and autoScroll is on we turn it off
|
||||||
if (!ticking && isAutoScrollOn && window.scrollY < lastKnownScrollPosition) {
|
if (!ticking && isAutoScrollOn && window.scrollY < lastKnownScrollPosition) {
|
||||||
window.requestAnimationFrame(() => {
|
window.requestAnimationFrame(() => {
|
||||||
isAutoScrollOn = false;
|
isAutoScrollOn = false;
|
||||||
ticking = false;
|
ticking = false;
|
||||||
});
|
});
|
||||||
ticking = true;
|
ticking = true;
|
||||||
}
|
}
|
||||||
// if user has scrolled nearly all the way down and autoScroll is disabled, re-enable
|
// if user has scrolled nearly all the way down and autoScroll is disabled, re-enable
|
||||||
else if (!ticking && !isAutoScrollOn &&
|
else if (!ticking && !isAutoScrollOn &&
|
||||||
window.scrollY > lastKnownScrollPosition && // make sure scroll direction is down
|
window.scrollY > lastKnownScrollPosition && // make sure scroll direction is down
|
||||||
window.scrollY >= document.documentElement.scrollHeight - window.innerHeight - 30 // add 30px of space--no need to scroll all the way down, just most of the way
|
window.scrollY >= document.documentElement.scrollHeight - window.innerHeight - 30 // add 30px of space--no need to scroll all the way down, just most of the way
|
||||||
) {
|
) {
|
||||||
window.requestAnimationFrame(() => {
|
window.requestAnimationFrame(() => {
|
||||||
isAutoScrollOn = true;
|
isAutoScrollOn = true;
|
||||||
ticking = false;
|
ticking = false;
|
||||||
});
|
});
|
||||||
ticking = true;
|
ticking = true;
|
||||||
}
|
}
|
||||||
lastKnownScrollPosition = window.scrollY;
|
lastKnownScrollPosition = window.scrollY;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Function to handle the user input and call the API functions
|
// Function to handle the user input and call the API functions
|
||||||
async function submitRequest() {
|
async function submitRequest() {
|
||||||
document.getElementById('chat-container').style.display = 'block';
|
document.getElementById('chat-container').style.display = 'block';
|
||||||
|
|
||||||
const input = document.getElementById('user-input').value;
|
const input = document.getElementById('user-input').value;
|
||||||
const selectedModel = getSelectedModel();
|
const selectedModel = getSelectedModel();
|
||||||
const context = document.getElementById('chat-history').context;
|
const context = document.getElementById('chat-history').context;
|
||||||
const systemPrompt = document.getElementById('system-prompt').value;
|
const systemPrompt = document.getElementById('system-prompt').value;
|
||||||
const data = { model: selectedModel, prompt: input, context: context, system: systemPrompt };
|
const data = {model: selectedModel, prompt: input, context: context, system: systemPrompt};
|
||||||
|
|
||||||
// Create user message element and append to chat history
|
// Create user message element and append to chat history
|
||||||
let chatHistory = document.getElementById('chat-history');
|
let chatHistory = document.getElementById('chat-history');
|
||||||
let userMessageDiv = document.createElement('div');
|
let userMessageDiv = document.createElement('div');
|
||||||
userMessageDiv.className = 'mb-2 user-message';
|
userMessageDiv.className = 'mb-2 user-message';
|
||||||
userMessageDiv.innerText = input;
|
userMessageDiv.innerText = input;
|
||||||
chatHistory.appendChild(userMessageDiv);
|
chatHistory.appendChild(userMessageDiv);
|
||||||
|
|
||||||
// Create response container
|
// Create response container
|
||||||
let responseDiv = document.createElement('div');
|
let responseDiv = document.createElement('div');
|
||||||
responseDiv.className = 'response-message mb-2 text-start';
|
responseDiv.className = 'response-message mb-2 text-start';
|
||||||
responseDiv.style.minHeight = '3em'; // make sure div does not shrink if we cancel the request when no text has been generated yet
|
responseDiv.style.minHeight = '3em'; // make sure div does not shrink if we cancel the request when no text has been generated yet
|
||||||
spinner = document.createElement('div');
|
spinner = document.createElement('div');
|
||||||
spinner.className = 'spinner-border text-light';
|
spinner.className = 'spinner-border text-light';
|
||||||
spinner.setAttribute('role', 'status');
|
spinner.setAttribute('role', 'status');
|
||||||
responseDiv.appendChild(spinner);
|
responseDiv.appendChild(spinner);
|
||||||
chatHistory.appendChild(responseDiv);
|
chatHistory.appendChild(responseDiv);
|
||||||
|
|
||||||
// create button to stop text generation
|
// create button to stop text generation
|
||||||
let interrupt = new AbortController();
|
let interrupt = new AbortController();
|
||||||
let stopButton = document.createElement('button');
|
let stopButton = document.createElement('button');
|
||||||
stopButton.className = 'btn btn-danger';
|
stopButton.className = 'btn btn-danger';
|
||||||
stopButton.innerHTML = 'Stop';
|
stopButton.innerHTML = 'Stop';
|
||||||
stopButton.onclick = (e) => {
|
stopButton.onclick = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
interrupt.abort('Stop button pressed');
|
interrupt.abort('Stop button pressed');
|
||||||
}
|
}
|
||||||
// add button after sendButton
|
// add button after sendButton
|
||||||
const sendButton = document.getElementById('send-button');
|
const sendButton = document.getElementById('send-button');
|
||||||
sendButton.insertAdjacentElement('beforebegin', stopButton);
|
sendButton.insertAdjacentElement('beforebegin', stopButton);
|
||||||
|
|
||||||
// change autoScroller to keep track of our new responseDiv
|
// change autoScroller to keep track of our new responseDiv
|
||||||
autoScroller.observe(responseDiv);
|
autoScroller.observe(responseDiv);
|
||||||
|
|
||||||
postRequest(data, interrupt.signal)
|
postRequest(data, interrupt.signal)
|
||||||
.then(async response => {
|
.then(async response => {
|
||||||
await getResponse(response, parsedResponse => {
|
await getResponse(response, parsedResponse => {
|
||||||
let word = parsedResponse.response;
|
// let word = parsedResponse.response;
|
||||||
if (parsedResponse.done) {
|
console.log(response);
|
||||||
chatHistory.context = parsedResponse.context;
|
console.log(parsedResponse);
|
||||||
// Copy button
|
let word = parsedResponse?.choices[0]?.message?.content;
|
||||||
let copyButton = document.createElement('button');
|
console.log(word);
|
||||||
copyButton.className = 'btn btn-secondary copy-button';
|
if (parsedResponse.done) {
|
||||||
copyButton.innerHTML = clipboardIcon;
|
chatHistory.context = parsedResponse.context;
|
||||||
copyButton.onclick = () => {
|
// Copy button
|
||||||
navigator.clipboard.writeText(responseDiv.hidden_text).then(() => {
|
let copyButton = document.createElement('button');
|
||||||
console.log('Text copied to clipboard');
|
copyButton.className = 'btn btn-secondary copy-button';
|
||||||
}).catch(err => {
|
copyButton.innerHTML = clipboardIcon;
|
||||||
console.error('Failed to copy text:', err);
|
copyButton.onclick = () => {
|
||||||
|
navigator.clipboard.writeText(responseDiv.hidden_text).then(() => {
|
||||||
|
console.log('Text copied to clipboard');
|
||||||
|
}).catch(err => {
|
||||||
|
console.error('Failed to copy text:', err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
responseDiv.appendChild(copyButton);
|
||||||
|
}
|
||||||
|
// add word to response
|
||||||
|
if (word != undefined && word != "") {
|
||||||
|
if (responseDiv.hidden_text == undefined) {
|
||||||
|
responseDiv.hidden_text = "";
|
||||||
|
}
|
||||||
|
responseDiv.hidden_text += word;
|
||||||
|
responseDiv.innerHTML = DOMPurify.sanitize(marked.parse(responseDiv.hidden_text)); // Append word to response container
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
})
|
||||||
responseDiv.appendChild(copyButton);
|
.then(() => {
|
||||||
}
|
stopButton.remove(); // Remove stop button from DOM now that all text has been generated
|
||||||
// add word to response
|
spinner.remove();
|
||||||
if (word != undefined && word != "") {
|
})
|
||||||
if (responseDiv.hidden_text == undefined){
|
.catch(error => {
|
||||||
responseDiv.hidden_text = "";
|
if (error !== 'Stop button pressed') {
|
||||||
}
|
console.error(error);
|
||||||
responseDiv.hidden_text += word;
|
}
|
||||||
responseDiv.innerHTML = DOMPurify.sanitize(marked.parse(responseDiv.hidden_text)); // Append word to response container
|
stopButton.remove();
|
||||||
}
|
spinner.remove();
|
||||||
});
|
});
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
stopButton.remove(); // Remove stop button from DOM now that all text has been generated
|
|
||||||
spinner.remove();
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
if (error !== 'Stop button pressed') {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
stopButton.remove();
|
|
||||||
spinner.remove();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Clear user input
|
// Clear user input
|
||||||
const element = document.getElementById('user-input');
|
const element = document.getElementById('user-input');
|
||||||
element.value = '';
|
element.value = '';
|
||||||
$(element).css("height", textBoxBaseHeight + "px");
|
$(element).css("height", textBoxBaseHeight + "px");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Event listener for Ctrl + Enter or CMD + Enter
|
// Event listener for Ctrl + Enter or CMD + Enter
|
||||||
document.getElementById('user-input').addEventListener('keydown', function (e) {
|
document.getElementById('user-input').addEventListener('keydown', function (e) {
|
||||||
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
|
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
|
||||||
submitRequest();
|
submitRequest();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
window.onload = () => {
|
window.onload = () => {
|
||||||
updateChatList();
|
updateChatList();
|
||||||
populateModels();
|
populateModels();
|
||||||
adjustPadding();
|
adjustPadding();
|
||||||
autoFocusInput();
|
autoFocusInput();
|
||||||
|
|
||||||
document.getElementById("delete-chat").addEventListener("click", deleteChat);
|
document.getElementById("delete-chat").addEventListener("click", deleteChat);
|
||||||
document.getElementById("new-chat").addEventListener("click", startNewChat);
|
document.getElementById("new-chat").addEventListener("click", startNewChat);
|
||||||
document.getElementById("saveName").addEventListener("click", saveChat);
|
document.getElementById("saveName").addEventListener("click", saveChat);
|
||||||
document.getElementById("chat-select").addEventListener("change", loadSelectedChat);
|
document.getElementById("chat-select").addEventListener("change", loadSelectedChat);
|
||||||
document.getElementById("host-address").addEventListener("change", setHostAddress);
|
document.getElementById("host-address").addEventListener("change", setHostAddress);
|
||||||
document.getElementById("system-prompt").addEventListener("change", setSystemPrompt);
|
document.getElementById("system-prompt").addEventListener("change", setSystemPrompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteChat() {
|
function deleteChat() {
|
||||||
const selectedChat = document.getElementById("chat-select").value;
|
const selectedChat = document.getElementById("chat-select").value;
|
||||||
localStorage.removeItem(selectedChat);
|
localStorage.removeItem(selectedChat);
|
||||||
updateChatList();
|
updateChatList();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to save chat with a unique name
|
// Function to save chat with a unique name
|
||||||
function saveChat() {
|
function saveChat() {
|
||||||
const chatName = document.getElementById('userName').value;
|
const chatName = document.getElementById('userName').value;
|
||||||
|
|
||||||
// Close the modal
|
// Close the modal
|
||||||
const bootstrapModal = bootstrap.Modal.getInstance(document.getElementById('nameModal'));
|
const bootstrapModal = bootstrap.Modal.getInstance(document.getElementById('nameModal'));
|
||||||
bootstrapModal.hide();
|
bootstrapModal.hide();
|
||||||
|
|
||||||
if (chatName === null || chatName.trim() === "") return;
|
if (chatName === null || chatName.trim() === "") return;
|
||||||
const history = document.getElementById("chat-history").innerHTML;
|
const history = document.getElementById("chat-history").innerHTML;
|
||||||
const context = document.getElementById('chat-history').context;
|
const context = document.getElementById('chat-history').context;
|
||||||
const systemPrompt = document.getElementById('system-prompt').value;
|
const systemPrompt = document.getElementById('system-prompt').value;
|
||||||
const model = getSelectedModel();
|
const model = getSelectedModel();
|
||||||
localStorage.setItem(chatName, JSON.stringify({"history":history, "context":context, system: systemPrompt, "model": model}));
|
localStorage.setItem(chatName, JSON.stringify({
|
||||||
updateChatList();
|
"history": history,
|
||||||
|
"context": context,
|
||||||
|
system: systemPrompt,
|
||||||
|
"model": model
|
||||||
|
}));
|
||||||
|
updateChatList();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to load selected chat from dropdown
|
// Function to load selected chat from dropdown
|
||||||
function loadSelectedChat() {
|
function loadSelectedChat() {
|
||||||
const selectedChat = document.getElementById("chat-select").value;
|
const selectedChat = document.getElementById("chat-select").value;
|
||||||
const obj = JSON.parse(localStorage.getItem(selectedChat));
|
const obj = JSON.parse(localStorage.getItem(selectedChat));
|
||||||
document.getElementById("chat-history").innerHTML = obj.history;
|
document.getElementById("chat-history").innerHTML = obj.history;
|
||||||
document.getElementById("chat-history").context = obj.context;
|
document.getElementById("chat-history").context = obj.context;
|
||||||
document.getElementById("system-prompt").value = obj.system;
|
document.getElementById("system-prompt").value = obj.system;
|
||||||
updateModelInQueryString(obj.model)
|
updateModelInQueryString(obj.model)
|
||||||
document.getElementById('chat-container').style.display = 'block';
|
document.getElementById('chat-container').style.display = 'block';
|
||||||
}
|
}
|
||||||
|
|
||||||
function startNewChat() {
|
function startNewChat() {
|
||||||
@ -304,16 +313,16 @@ function startNewChat() {
|
|||||||
|
|
||||||
// Function to update chat list dropdown
|
// Function to update chat list dropdown
|
||||||
function updateChatList() {
|
function updateChatList() {
|
||||||
const chatList = document.getElementById("chat-select");
|
const chatList = document.getElementById("chat-select");
|
||||||
chatList.innerHTML = '<option value="" disabled selected>Select a chat</option>';
|
chatList.innerHTML = '<option value="" disabled selected>Select a chat</option>';
|
||||||
for (let i = 0; i < localStorage.length; i++) {
|
for (let i = 0; i < localStorage.length; i++) {
|
||||||
const key = localStorage.key(i);
|
const key = localStorage.key(i);
|
||||||
if (key === "host-address" || key == "system-prompt") continue;
|
if (key === "host-address" || key == "system-prompt") continue;
|
||||||
const option = document.createElement("option");
|
const option = document.createElement("option");
|
||||||
option.value = key;
|
option.value = key;
|
||||||
option.text = key;
|
option.text = key;
|
||||||
chatList.add(option);
|
chatList.add(option);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function autoGrow(element) {
|
function autoGrow(element) {
|
||||||
|
@ -39,7 +39,7 @@ func Chat2(c echo.Context) error {
|
|||||||
|
|
||||||
func Chat3(c echo.Context) error {
|
func Chat3(c echo.Context) error {
|
||||||
c.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
|
c.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
c.Response().Header().Set("Cache-Control", "max-age=3600")
|
c.Response().Header().Set("Cache-Control", "max-age=1")
|
||||||
|
|
||||||
return c.Blob(http.StatusOK, "text/html; charset=utf-8", pageMap["index_ollama.html"])
|
return c.Blob(http.StatusOK, "text/html; charset=utf-8", pageMap["index_ollama.html"])
|
||||||
}
|
}
|
||||||
@ -55,7 +55,7 @@ func InitStaticGroup(g *echo.Group) {
|
|||||||
|
|
||||||
g.GET("css/:name", func(c echo.Context) error {
|
g.GET("css/:name", func(c echo.Context) error {
|
||||||
cssName := c.Param("name")
|
cssName := c.Param("name")
|
||||||
c.Response().Header().Set("Cache-Control", "max-age=3600")
|
c.Response().Header().Set("Cache-Control", "max-age=1")
|
||||||
data, _ := cssMap[cssName]
|
data, _ := cssMap[cssName]
|
||||||
return c.Blob(http.StatusOK, "text/css; charset=utf-8", data)
|
return c.Blob(http.StatusOK, "text/css; charset=utf-8", data)
|
||||||
})
|
})
|
||||||
@ -68,7 +68,7 @@ func InitStaticGroup(g *echo.Group) {
|
|||||||
|
|
||||||
g.GET("js/:name", func(c echo.Context) error {
|
g.GET("js/:name", func(c echo.Context) error {
|
||||||
jsName := c.Param("name")
|
jsName := c.Param("name")
|
||||||
c.Response().Header().Set("Cache-Control", "max-age=3600")
|
c.Response().Header().Set("Cache-Control", "max-age=1")
|
||||||
return c.Blob(http.StatusOK, "text/javaScript", jsMap[jsName])
|
return c.Blob(http.StatusOK, "text/javaScript", jsMap[jsName])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user