mirror of
https://github.com/ParisNeo/lollms-webui.git
synced 2025-02-21 17:36:39 +00:00
Conditionning, prefixes and suffixes are now modifiable in the configuration file
This commit is contained in:
parent
471d297e4c
commit
329032d213
37
app.py
37
app.py
@ -135,7 +135,7 @@ class Gpt4AllWebUI:
|
|||||||
# Create chatbot
|
# Create chatbot
|
||||||
self.chatbot_bindings = self.create_chatbot()
|
self.chatbot_bindings = self.create_chatbot()
|
||||||
# Chatbot conditionning
|
# Chatbot conditionning
|
||||||
self.condition_chatbot()
|
self.condition_chatbot(self.config["personality_conditionning"])
|
||||||
|
|
||||||
|
|
||||||
def create_chatbot(self):
|
def create_chatbot(self):
|
||||||
@ -145,23 +145,15 @@ class Gpt4AllWebUI:
|
|||||||
seed=self.config['seed'],
|
seed=self.config['seed'],
|
||||||
)
|
)
|
||||||
|
|
||||||
def condition_chatbot(self, conditionning_message = """
|
def condition_chatbot(self, conditionning_message):
|
||||||
Instruction: Act as GPT4All. A kind and helpful AI bot built to help users solve problems.
|
|
||||||
GPT4All:Welcome! I'm here to assist you with anything you need. What can I do for you today?"""
|
|
||||||
):
|
|
||||||
self.full_message += conditionning_message
|
self.full_message += conditionning_message
|
||||||
if self.current_discussion is None:
|
if self.current_discussion is None:
|
||||||
if self.db.does_last_discussion_have_messages():
|
self.current_discussion = self.db.load_last_discussion()
|
||||||
self.current_discussion = self.db.create_discussion()
|
|
||||||
else:
|
|
||||||
self.current_discussion = self.db.load_last_discussion()
|
|
||||||
|
|
||||||
message_id = self.current_discussion.add_message(
|
message_id = self.current_discussion.add_message(
|
||||||
"conditionner", conditionning_message, DiscussionsDB.MSG_TYPE_CONDITIONNING,0
|
"conditionner", conditionning_message, DiscussionsDB.MSG_TYPE_CONDITIONNING,0
|
||||||
)
|
)
|
||||||
|
return message_id
|
||||||
self.full_message_list.append(conditionning_message)
|
|
||||||
|
|
||||||
|
|
||||||
def prepare_query(self):
|
def prepare_query(self):
|
||||||
self.bot_says = ""
|
self.bot_says = ""
|
||||||
@ -257,12 +249,12 @@ GPT4All:Welcome! I'm here to assist you with anything you need. What can I do fo
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.current_message = "\nUser: " + message + "\nGPT4All: "
|
self.current_message = self.config["message_prefix"] + message + self.config["message_postfix"]
|
||||||
self.full_message += self.current_message
|
self.full_message += self.current_message
|
||||||
self.full_message_list.append(self.current_message)
|
self.full_message_list.append(self.current_message)
|
||||||
|
|
||||||
if len(self.full_message_list) > 5:
|
if len(self.full_message_list) > self.config["nb_messages_to_remember"]:
|
||||||
self.prompt_message = '\n'.join(self.full_message_list[-5:])
|
self.prompt_message = [self.config["personality_conditionning"]]+ '\n'.join(self.full_message_list[-self.config["nb_messages_to_remember"]:])
|
||||||
else:
|
else:
|
||||||
self.prompt_message = self.full_message
|
self.prompt_message = self.full_message
|
||||||
self.prepare_query()
|
self.prepare_query()
|
||||||
@ -332,7 +324,13 @@ GPT4All:Welcome! I'm here to assist you with anything you need. What can I do fo
|
|||||||
|
|
||||||
def load_discussion(self):
|
def load_discussion(self):
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
discussion_id = data["id"]
|
if "id" in data:
|
||||||
|
discussion_id = data["id"]
|
||||||
|
else:
|
||||||
|
if self.current_discussion is not None:
|
||||||
|
discussion_id = self.current_discussion.discussion_id
|
||||||
|
else:
|
||||||
|
discussion_id = self.db.create_discussion()
|
||||||
self.current_discussion = Discussion(discussion_id, self.db)
|
self.current_discussion = Discussion(discussion_id, self.db)
|
||||||
messages = self.current_discussion.get_messages()
|
messages = self.current_discussion.get_messages()
|
||||||
|
|
||||||
@ -438,6 +436,10 @@ GPT4All:Welcome! I'm here to assist you with anything you need. What can I do fo
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description="Start the chatbot Flask app.")
|
parser = argparse.ArgumentParser(description="Start the chatbot Flask app.")
|
||||||
|
parser.add_argument(
|
||||||
|
"-c", "--config", type=str, default="default", help="Sets the configuration file to be used."
|
||||||
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-s", "--seed", type=int, default=None, help="Force using a specific model."
|
"-s", "--seed", type=int, default=None, help="Force using a specific model."
|
||||||
)
|
)
|
||||||
@ -490,7 +492,8 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
parser.set_defaults(debug=False)
|
parser.set_defaults(debug=False)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
config_file_path = "configs/default.yaml"
|
|
||||||
|
config_file_path = f"configs/{args.config}.yaml"
|
||||||
config = load_config(config_file_path)
|
config = load_config(config_file_path)
|
||||||
|
|
||||||
# Override values in config with command-line arguments
|
# Override values in config with command-line arguments
|
||||||
|
@ -11,3 +11,9 @@ debug: false
|
|||||||
host: "localhost"
|
host: "localhost"
|
||||||
port: 9600
|
port: 9600
|
||||||
db_path: "database.db"
|
db_path: "database.db"
|
||||||
|
nb_messages_to_remember: 5
|
||||||
|
message_prefix: "\nuser:"
|
||||||
|
message_postfix: "\ngpt4all:"
|
||||||
|
personality_conditionning: |
|
||||||
|
Instruction: Act as gpt4all. A kind and helpful AI bot built to help users solve problems.
|
||||||
|
gpt4all:Welcome! I'm here to assist you with anything you need. What can I do for you today?
|
||||||
|
52
db.py
52
db.py
@ -12,19 +12,26 @@ class DiscussionsDB:
|
|||||||
"""
|
"""
|
||||||
create database schema
|
create database schema
|
||||||
"""
|
"""
|
||||||
|
db_version = 2
|
||||||
|
|
||||||
print("Checking discussions database...")
|
print("Checking discussions database...")
|
||||||
with sqlite3.connect(self.db_path) as conn:
|
with sqlite3.connect(self.db_path) as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# Check if the 'schema_version' table exists
|
|
||||||
cursor.execute("""
|
|
||||||
CREATE TABLE IF NOT EXISTS schema_version (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
version INTEGER NOT NULL
|
|
||||||
)
|
|
||||||
""")
|
|
||||||
discussion_table_exist=False
|
discussion_table_exist=False
|
||||||
message_table_exist=False
|
message_table_exist=False
|
||||||
|
schema_table_exist=False
|
||||||
|
|
||||||
|
# Check if the 'schema_version' table exists
|
||||||
|
try:
|
||||||
|
cursor.execute("""
|
||||||
|
CREATE TABLE schema_version (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
version INTEGER NOT NULL
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
except:
|
||||||
|
schema_table_exist = True
|
||||||
try:
|
try:
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
CREATE TABLE discussion (
|
CREATE TABLE discussion (
|
||||||
@ -32,16 +39,17 @@ class DiscussionsDB:
|
|||||||
title TEXT
|
title TEXT
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
except:
|
except Exception:
|
||||||
discussion_table_exist=True
|
discussion_table_exist=True
|
||||||
try:
|
try:
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
CREATE TABLE IF NOT EXISTS message (
|
CREATE TABLE message (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
sender TEXT NOT NULL,
|
sender TEXT NOT NULL,
|
||||||
content TEXT NOT NULL,
|
content TEXT NOT NULL,
|
||||||
type INT NOT NULL,
|
type INT NOT NULL,
|
||||||
rank INT NOT NULL,
|
rank INT NOT NULL,
|
||||||
|
parent INT,
|
||||||
discussion_id INTEGER NOT NULL,
|
discussion_id INTEGER NOT NULL,
|
||||||
FOREIGN KEY (discussion_id) REFERENCES discussion(id)
|
FOREIGN KEY (discussion_id) REFERENCES discussion(id)
|
||||||
)
|
)
|
||||||
@ -62,15 +70,27 @@ class DiscussionsDB:
|
|||||||
|
|
||||||
# Upgrade the schema to version 1
|
# Upgrade the schema to version 1
|
||||||
if version < 1:
|
if version < 1:
|
||||||
print("Upgrading schema to version 1...")
|
print(f"Upgrading schema to version {db_version}...")
|
||||||
# Add the 'created_at' column to the 'message' table
|
# Add the 'created_at' column to the 'message' table
|
||||||
if message_table_exist:
|
if message_table_exist:
|
||||||
cursor.execute("ALTER TABLE message ADD COLUMN type INT DEFAULT 0")
|
cursor.execute("ALTER TABLE message ADD COLUMN type INT DEFAULT 0") # Added in V1
|
||||||
cursor.execute("ALTER TABLE message ADD COLUMN rank INT DEFAULT 0")
|
cursor.execute("ALTER TABLE message ADD COLUMN rank INT DEFAULT 0") # Added in V1
|
||||||
# Update the schema version
|
cursor.execute("ALTER TABLE message ADD COLUMN parent INT DEFAULT 0") # Added in V2
|
||||||
cursor.execute("INSERT INTO schema_version (id, version) VALUES (1, 1)")
|
# Upgrade the schema to version 1
|
||||||
version = 1
|
elif version < 2:
|
||||||
|
print(f"Upgrading schema to version {db_version}...")
|
||||||
|
# Add the 'created_at' column to the 'message' table
|
||||||
|
if message_table_exist:
|
||||||
|
try:
|
||||||
|
cursor.execute("ALTER TABLE message ADD COLUMN parent INT DEFAULT 0") # Added in V2
|
||||||
|
except :
|
||||||
|
pass
|
||||||
|
# Update the schema version
|
||||||
|
if not schema_table_exist:
|
||||||
|
cursor.execute(f"INSERT INTO schema_version (id, version) VALUES (1, {db_version})")
|
||||||
|
else:
|
||||||
|
cursor.execute(f"UPDATE schema_version SET version=? WHERE id=?",(db_version,1))
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
def select(self, query, params=None, fetch_all=True):
|
def select(self, query, params=None, fetch_all=True):
|
||||||
|
@ -1,4 +1,43 @@
|
|||||||
|
|
||||||
|
function load_discussion(discussion=0){
|
||||||
|
if(discussion)
|
||||||
|
body = { id: discussion.id }
|
||||||
|
else
|
||||||
|
body = { }
|
||||||
|
// send query with discussion id to reveal discussion messages
|
||||||
|
fetch('/load_discussion', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(body)
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
if (response.ok) {
|
||||||
|
response.text().then(data => {
|
||||||
|
const messages = JSON.parse(data);
|
||||||
|
console.log(messages)
|
||||||
|
// process messages
|
||||||
|
var container = document.getElementById('chat-window');
|
||||||
|
container.innerHTML = '';
|
||||||
|
messages.forEach(message => {
|
||||||
|
if(message.type==0){
|
||||||
|
addMessage(message.sender, message.content, message.id, message.rank, true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
alert('Failed to query the discussion');
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('Failed to get messages:', error);
|
||||||
|
alert('Failed to get messages');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
load_discussion();
|
||||||
|
|
||||||
function populate_discussions_list()
|
function populate_discussions_list()
|
||||||
{
|
{
|
||||||
// Populate discussions list
|
// Populate discussions list
|
||||||
@ -113,36 +152,7 @@ function populate_discussions_list()
|
|||||||
discussionButton.classList.add('bg-green-500', 'hover:bg-green-700', 'text-white', 'font-bold', 'py-2', 'px-4', 'rounded', 'ml-2', 'w-full');
|
discussionButton.classList.add('bg-green-500', 'hover:bg-green-700', 'text-white', 'font-bold', 'py-2', 'px-4', 'rounded', 'ml-2', 'w-full');
|
||||||
discussionButton.textContent = discussion.title;
|
discussionButton.textContent = discussion.title;
|
||||||
discussionButton.addEventListener('click', () => {
|
discussionButton.addEventListener('click', () => {
|
||||||
// send query with discussion id to reveal discussion messages
|
load_discussion(discussion);
|
||||||
fetch('/load_discussion', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ id: discussion.id })
|
|
||||||
})
|
|
||||||
.then(response => {
|
|
||||||
if (response.ok) {
|
|
||||||
response.text().then(data => {
|
|
||||||
const messages = JSON.parse(data);
|
|
||||||
console.log(messages)
|
|
||||||
// process messages
|
|
||||||
var container = document.getElementById('chat-window');
|
|
||||||
container.innerHTML = '';
|
|
||||||
messages.forEach(message => {
|
|
||||||
if(message.type==0){
|
|
||||||
addMessage(message.sender, message.content, message.id, message.rank, true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
alert('Failed to query the discussion');
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
console.error('Failed to get messages:', error);
|
|
||||||
alert('Failed to get messages');
|
|
||||||
});
|
|
||||||
console.log(`Showing messages for discussion ${discussion.id}`);
|
console.log(`Showing messages for discussion ${discussion.id}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -190,6 +200,8 @@ actionBtns.appendChild(exportDiscussionButton);
|
|||||||
const newDiscussionBtn = document.querySelector('#new-discussion-btn');
|
const newDiscussionBtn = document.querySelector('#new-discussion-btn');
|
||||||
|
|
||||||
newDiscussionBtn.addEventListener('click', () => {
|
newDiscussionBtn.addEventListener('click', () => {
|
||||||
|
const chatWindow = document.getElementById('chat-window');
|
||||||
|
|
||||||
const discussionName = prompt('Enter a name for the new discussion:');
|
const discussionName = prompt('Enter a name for the new discussion:');
|
||||||
if (discussionName) {
|
if (discussionName) {
|
||||||
const sendbtn = document.querySelector("#submit-input")
|
const sendbtn = document.querySelector("#submit-input")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user