Skip to content

Commit

Permalink
Further code refactoring and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fingerthief committed Mar 9, 2024
1 parent b76e02f commit 607f9bc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# [Try MinimalGPT/MinimalPaLM/MinimalClaude](https://minimalgpt.app/)

<p><img src="https://img.shields.io/badge/build-passing-brightgreen" alt="Build Status">
<img src="https://img.shields.io/badge/version-4.1.2-blue" alt="Version">
<img src="https://img.shields.io/badge/version-4.1.6-blue" alt="Version">
<img src="https://img.shields.io/badge/license-MIT-green" alt="License"></p>

**MinimalGPT** is an open-source LLM chat web app designed to be as self-contained as possible. All conversations are stored locally on the client's device, with the only information being sent to the server being API calls to GPT, PaLM or Claude chat when the user sends a message and when a user saves a conversation to generate a conversation title.
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<div class="settings-header">
<h2 data-bind="click: () => { window.location.reload(true); }">
<span class="fa-solid fa-arrows-rotate"></span>
Settings | V4.1.4</h2>
Settings | V4.1.6</h2>
</div>
<div class="sidebar-content-container">
<div class="api-key">
Expand Down
87 changes: 42 additions & 45 deletions js/app-view-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ export function AppViewModel() {

if (!self.showingSearchField()) {
floatingSearchField.style.zIndex = '9999';
//userSearchInput.focus();
}

self.showingSearchField(!self.showingSearchField());
Expand All @@ -395,27 +394,30 @@ export function AppViewModel() {
return;
}


self.storedConversations(loadStoredConversations());

self.isProcessing(true);

const conversationIndex = self.storedConversations().findIndex(conversation => {
return conversation.id === parseInt(self.lastLoadedConversationId());
});
const storedConversations = loadStoredConversations();
const conversationIndex = storedConversations.findIndex(
(conversation) => conversation.id === parseInt(self.lastLoadedConversationId())
);

self.storedConversations().splice(conversationIndex, 1);
self.storedConversations.valueHasMutated();
if (conversationIndex !== -1) {
storedConversations.splice(conversationIndex, 1);
localStorage.setItem("gpt-conversations", JSON.stringify(storedConversations));
}

localStorage.setItem("gpt-conversations", JSON.stringify(self.storedConversations()));
self.storedConversations(loadStoredConversations());
self.storedConversations(storedConversations);
self.messages([]);
self.palmMessages = [];
self.claudeMessages = [];
self.conversationTitles(loadConversationTitles());
self.conversations(loadConversationTitles());

const conversationTitles = loadConversationTitles();
self.conversationTitles(conversationTitles);
self.conversations(conversationTitles);

self.lastLoadedConversationId(null);
localStorage.setItem("lastConversationId", self.lastLoadedConversationId());
localStorage.setItem("lastConversationId", null);

self.isProcessing(false);
};

Expand Down Expand Up @@ -465,6 +467,18 @@ function getStoredApiKey() {
return storedApiKey;
}

function getStoredClaudeApiKey() {
const storedApiKey = localStorage.getItem("claudeKey");
const trimmedApiKey = claudeApiKey.value.trim();

if (storedApiKey !== trimmedApiKey) {
localStorage.setItem("claudeKey", trimmedApiKey);
return trimmedApiKey;
}

return storedApiKey;
}

function saveAttitude(attitude) {
if (!localStorage.getItem("gpt-attitude") || localStorage.getItem("gpt-attitude") !== attitude) {
localStorage.setItem("gpt-attitude", attitude);
Expand Down Expand Up @@ -511,9 +525,9 @@ async function retryFetchGPTResponseStream(conversation, attitude, model, retryC
if (retryCount < 50) {
console.log("Retry Number: " + (retryCount + 1));
return await fetchGPTResponseStream(conversation, attitude, model);
} else {
return "An error occurred while fetching GPT response stream.";
}
}

return "An error occurred while fetching GPT response stream.";
}

// Step 1: Add the encodeImage function
Expand All @@ -537,7 +551,7 @@ async function retryFetchGPTResponseStream(conversation, attitude, model, retryC
self.userInput("");
userInput.style.height = '30px';

const storedApiKey = getStoredApiKey();
const storedApiKey = getStoredClaudeApiKey();
const visionFormattedMessages = formatMessagesForVision(gptMessagesOnly);

if (self.selectedModel().indexOf("gpt") !== -1) {
Expand All @@ -560,7 +574,7 @@ async function retryFetchGPTResponseStream(conversation, attitude, model, retryC
}
});

const response = await fetchClaudeVisionResponse(visionFormattedMessages, storedApiKey, self.selectedModel());
const response = await fetchClaudeVisionResponse(visionFormattedMessages, getStoredClaudeApiKey(), self.selectedModel());
addMessage("assistant", response);
self.claudeMessages.push({ role: "assistant", content: response });
self.saveMessages();
Expand All @@ -571,15 +585,6 @@ async function retryFetchGPTResponseStream(conversation, attitude, model, retryC
}
}

function getStoredApiKey() {
let storedApiKey = localStorage.getItem("claudeKey");
if (storedApiKey !== claudeApiKey.value.trim()) {
localStorage.setItem("claudeKey", claudeApiKey.value.trim());
storedApiKey = claudeApiKey.value.trim();
}
return storedApiKey;
}

function formatMessagesForVision(messages) {
return messages.map(message => ({
type: "text",
Expand Down Expand Up @@ -744,29 +749,21 @@ async function retryFetchGPTResponseStream(conversation, attitude, model, retryC
}

self.saveMessages = async function () {
const savedMessages = self.messages().map(message => ({
role: message.role,
content: message.content
}));

let conversationIndex = -1;
const savedMessages = self.messages().map(({ role, content }) => ({ role, content }));

if (self.selectedConversation()) {
// Find the index of the selected conversation in storedConversations
conversationIndex = self.storedConversations().findIndex(conversation => conversation.conversation.title === self.selectedConversation().title);
}
const selectedConversation = self.selectedConversation();
const conversationIndex = selectedConversation
? self.storedConversations().findIndex(
(conversation) => conversation.conversation.title === selectedConversation.title
)
: -1;

if (conversationIndex !== -1) {
// Update the message history of the selected conversation
self.storedConversations()[conversationIndex].conversation.messageHistory = savedMessages;
}
else {
if (JSON.parse(self.selectedAutoSaveOption())) {
await this.saveNewConversations();
}
} else if (JSON.parse(self.selectedAutoSaveOption())) {
await this.saveNewConversations();
}

// Save the updated conversations to localStorage
localStorage.setItem("gpt-conversations", JSON.stringify(self.storedConversations()));
};

Expand Down

0 comments on commit 607f9bc

Please sign in to comment.