Conditionning, prefixes and suffixes are now modifiable in the configuration file

This commit is contained in:
Saifeddine ALOUI 2023-04-13 17:24:52 +02:00
parent 471d297e4c
commit 329032d213
4 changed files with 104 additions and 63 deletions

37
app.py
View File

@ -135,7 +135,7 @@ class Gpt4AllWebUI:
# Create chatbot
self.chatbot_bindings = self.create_chatbot()
# Chatbot conditionning
self.condition_chatbot()
self.condition_chatbot(self.config["personality_conditionning"])
def create_chatbot(self):
@ -145,23 +145,15 @@ class Gpt4AllWebUI:
seed=self.config['seed'],
)
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?"""
):
def condition_chatbot(self, conditionning_message):
self.full_message += conditionning_message
if self.current_discussion is None:
if self.db.does_last_discussion_have_messages():
self.current_discussion = self.db.create_discussion()
else:
self.current_discussion = self.db.load_last_discussion()
self.current_discussion = self.db.load_last_discussion()
message_id = self.current_discussion.add_message(
"conditionner", conditionning_message, DiscussionsDB.MSG_TYPE_CONDITIONNING,0
)
self.full_message_list.append(conditionning_message)
return message_id
def prepare_query(self):
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_list.append(self.current_message)
if len(self.full_message_list) > 5:
self.prompt_message = '\n'.join(self.full_message_list[-5:])
if len(self.full_message_list) > self.config["nb_messages_to_remember"]:
self.prompt_message = [self.config["personality_conditionning"]]+ '\n'.join(self.full_message_list[-self.config["nb_messages_to_remember"]:])
else:
self.prompt_message = self.full_message
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):
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)
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__":
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(
"-s", "--seed", type=int, default=None, help="Force using a specific model."
)
@ -490,7 +492,8 @@ if __name__ == "__main__":
)
parser.set_defaults(debug=False)
args = parser.parse_args()
config_file_path = "configs/default.yaml"
config_file_path = f"configs/{args.config}.yaml"
config = load_config(config_file_path)
# Override values in config with command-line arguments

View File

@ -11,3 +11,9 @@ debug: false
host: "localhost"
port: 9600
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
View File

@ -12,19 +12,26 @@ class DiscussionsDB:
"""
create database schema
"""
db_version = 2
print("Checking discussions database...")
with sqlite3.connect(self.db_path) as conn:
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
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:
cursor.execute("""
CREATE TABLE discussion (
@ -32,16 +39,17 @@ class DiscussionsDB:
title TEXT
)
""")
except:
except Exception:
discussion_table_exist=True
try:
cursor.execute("""
CREATE TABLE IF NOT EXISTS message (
CREATE TABLE message (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sender TEXT NOT NULL,
content TEXT NOT NULL,
type INT NOT NULL,
rank INT NOT NULL,
parent INT,
discussion_id INTEGER NOT NULL,
FOREIGN KEY (discussion_id) REFERENCES discussion(id)
)
@ -62,15 +70,27 @@ class DiscussionsDB:
# Upgrade the schema to 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
if message_table_exist:
cursor.execute("ALTER TABLE message ADD COLUMN type INT DEFAULT 0")
cursor.execute("ALTER TABLE message ADD COLUMN rank INT DEFAULT 0")
# Update the schema version
cursor.execute("INSERT INTO schema_version (id, version) VALUES (1, 1)")
version = 1
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") # Added in V1
cursor.execute("ALTER TABLE message ADD COLUMN parent INT DEFAULT 0") # Added in V2
# Upgrade the schema to 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()
def select(self, query, params=None, fetch_all=True):

View File

@ -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()
{
// 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.textContent = discussion.title;
discussionButton.addEventListener('click', () => {
// send query with discussion id to reveal discussion messages
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');
});
load_discussion(discussion);
console.log(`Showing messages for discussion ${discussion.id}`);
});
@ -190,6 +200,8 @@ actionBtns.appendChild(exportDiscussionButton);
const newDiscussionBtn = document.querySelector('#new-discussion-btn');
newDiscussionBtn.addEventListener('click', () => {
const chatWindow = document.getElementById('chat-window');
const discussionName = prompt('Enter a name for the new discussion:');
if (discussionName) {
const sendbtn = document.querySelector("#submit-input")