database improvements
This commit is contained in:
parent
ea477269c9
commit
b7419a6e58
@ -1,13 +1,13 @@
|
|||||||
--- Create a commands table that contains all the already executed commandes and the simplified version by ChatGPT
|
--- Create a commands table that contains all the already executed commandes and the simplified version by ChatGPT
|
||||||
DROP TABLE IF EXISTS queries;
|
DROP TABLE IF EXISTS commands;
|
||||||
CREATE TABLE queries (
|
CREATE TABLE commands (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
original_command VARCHAR NOT NULL,
|
original_command VARCHAR NOT NULL,
|
||||||
simplified_command VARCHAR NOT NULL,
|
simplified_command VARCHAR NOT NULL,
|
||||||
response INT NOT NULL
|
response INT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
DROP TABLE IF EXISTS responses:
|
DROP TABLE IF EXISTS responses;
|
||||||
CREATE TABLE responses (
|
CREATE TABLE responses (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
response VARCHAR NOT NULL
|
response VARCHAR NOT NULL
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import logging
|
||||||
|
import os.path
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@ -5,6 +7,11 @@ from pathlib import Path
|
|||||||
def create_database():
|
def create_database():
|
||||||
"""Creates the database."""
|
"""Creates the database."""
|
||||||
|
|
||||||
|
# Check if the database already exists
|
||||||
|
if os.path.exists(Path(__file__).parent / "jarvis-commands-memory.sqlite"):
|
||||||
|
logging.debug("Database already exists, skipping creation")
|
||||||
|
return
|
||||||
|
|
||||||
with open(Path(__file__).parent / "create_empty_database.sql", "r") as create_database_script:
|
with open(Path(__file__).parent / "create_empty_database.sql", "r") as create_database_script:
|
||||||
create_database_script = create_database_script.read()
|
create_database_script = create_database_script.read()
|
||||||
|
|
||||||
@ -14,6 +21,8 @@ def create_database():
|
|||||||
db.commit()
|
db.commit()
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
logging.info("Database created")
|
||||||
|
|
||||||
|
|
||||||
def do_request(request, args=None):
|
def do_request(request, args=None):
|
||||||
"""Execute SQL request and return result"""
|
"""Execute SQL request and return result"""
|
||||||
@ -32,8 +41,27 @@ def do_request(request, args=None):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def add_query(original_query, simplified_query, response):
|
def add_response(response):
|
||||||
"""Add a query to the database"""
|
"""Add a response to the database"""
|
||||||
|
|
||||||
do_request("INSERT INTO queries (original_query, simplified_query, response) VALUES (?, ?, ?)",
|
# Check if the response is already in the database
|
||||||
(original_query, simplified_query, response))
|
request = do_request("SELECT * FROM responses WHERE response=?", (response,))
|
||||||
|
if request:
|
||||||
|
response_id = request[0][0]
|
||||||
|
logging.debug("Response already in database (id: " + str(response_id) + ")")
|
||||||
|
|
||||||
|
# If not, add it
|
||||||
|
else:
|
||||||
|
do_request("INSERT INTO responses (response) VALUES (?)", (response,))
|
||||||
|
response_id = do_request("SELECT * FROM responses WHERE response=?", (response,))[0][0]
|
||||||
|
logging.debug("Response added to database with id " + str(response_id) + ": " + str(response))
|
||||||
|
|
||||||
|
return response_id
|
||||||
|
|
||||||
|
|
||||||
|
def add_command(original_command, simplified_command, response):
|
||||||
|
"""Add a command to the database"""
|
||||||
|
|
||||||
|
do_request("INSERT INTO commands (original_command, simplified_command, response) VALUES (?, ?, ?)",
|
||||||
|
(original_command, simplified_command, add_response(response)))
|
||||||
|
logging.debug("Query added to database: " + original_command + " -> " + response)
|
||||||
|
Loading…
Reference in New Issue
Block a user