This commit is contained in:
shaoyongjun 2024-05-21 18:31:32 +08:00
parent 5e1711c128
commit d3ec570683
5 changed files with 240 additions and 230 deletions

View File

@ -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;

View File

@ -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>

View File

@ -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: {

View File

@ -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"/>
@ -59,12 +58,15 @@ async function populateModels() {
// set up handler for selection // set up handler for selection
selectElement.onchange = (() => updateModelInQueryString(selectElement.value)); selectElement.onchange = (() => updateModelInQueryString(selectElement.value));
if (data !== undefined) {
data.models.forEach((model) => { data.models.forEach((model) => {
const option = document.createElement('option'); const option = document.createElement('option');
option.value = model.name; option.value = model.name;
option.innerText = model.name; option.innerText = model.name;
selectElement.appendChild(option); selectElement.appendChild(option);
}); });
}
// select option present in url parameter if present // select option present in url parameter if present
const queryParams = new URLSearchParams(window.location.search); const queryParams = new URLSearchParams(window.location.search);
@ -78,8 +80,7 @@ async function populateModels() {
selectElement.value = selectElement.options[0].value; selectElement.value = selectElement.options[0].value;
updateModelInQueryString(selectElement.value); updateModelInQueryString(selectElement.value);
} }
} } catch (error) {
catch (error) {
document.getElementById('errorText').innerHTML = document.getElementById('errorText').innerHTML =
DOMPurify.sanitize(marked.parse( DOMPurify.sanitize(marked.parse(
`Ollama-ui was unable to communitcate with Ollama due to the following error:\n\n` `Ollama-ui was unable to communitcate with Ollama due to the following error:\n\n`
@ -104,7 +105,6 @@ const autoResizePadding = new ResizeObserver(() => {
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;
@ -157,7 +157,7 @@ async function submitRequest() {
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');
@ -195,7 +195,11 @@ async function submitRequest() {
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;
console.log(response);
console.log(parsedResponse);
let word = parsedResponse?.choices[0]?.message?.content;
console.log(word);
if (parsedResponse.done) { if (parsedResponse.done) {
chatHistory.context = parsedResponse.context; chatHistory.context = parsedResponse.context;
// Copy button // Copy button
@ -213,7 +217,7 @@ async function submitRequest() {
} }
// add word to response // add word to response
if (word != undefined && word != "") { if (word != undefined && word != "") {
if (responseDiv.hidden_text == undefined){ if (responseDiv.hidden_text == undefined) {
responseDiv.hidden_text = ""; responseDiv.hidden_text = "";
} }
responseDiv.hidden_text += word; responseDiv.hidden_text += word;
@ -280,7 +284,12 @@ function saveChat() {
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({
"history": history,
"context": context,
system: systemPrompt,
"model": model
}));
updateChatList(); updateChatList();
} }

View File

@ -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])
}) })