From 889e1ee9f26c31ce5fe2db72580d1770d5b94755 Mon Sep 17 00:00:00 2001 From: Mark Liffiton Date: Sat, 20 Jul 2024 01:15:43 -0500 Subject: [PATCH] Rename tutor_chats->chats; migrate table w/ new columns. --- .../20240720--codehelp--improve-chats.sql | 42 +++++++++++++++++++ src/codehelp/schema.sql | 12 +++--- ...hat_component.html => chat_component.html} | 0 src/codehelp/templates/tutor_admin.html | 2 +- src/codehelp/templates/tutor_view.html | 2 +- src/codehelp/tutor.py | 32 +++++++------- tests/test_data.sql | 2 +- 7 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 src/codehelp/migrations/20240720--codehelp--improve-chats.sql rename src/codehelp/templates/{tutor_chat_component.html => chat_component.html} (100%) diff --git a/src/codehelp/migrations/20240720--codehelp--improve-chats.sql b/src/codehelp/migrations/20240720--codehelp--improve-chats.sql new file mode 100644 index 0000000..eee8da2 --- /dev/null +++ b/src/codehelp/migrations/20240720--codehelp--improve-chats.sql @@ -0,0 +1,42 @@ +-- SPDX-FileCopyrightText: 2024 Mark Liffiton +-- +-- SPDX-License-Identifier: AGPL-3.0-only + +BEGIN; + +-- Rename tutor_chats -> chats and improve context storage. + +CREATE TABLE chats ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + chat_started TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + topic TEXT NOT NULL, + context_name TEXT, + context_string_id INTEGER, + chat_json TEXT NOT NULL, + user_id INTEGER NOT NULL, + role_id INTEGER, + FOREIGN KEY(user_id) REFERENCES users(id), + FOREIGN KEY(role_id) REFERENCES roles(id), + FOREIGN KEY(context_string_id) REFERENCES context_strings(id) +); + +INSERT INTO chats (id, topic, context_name, chat_json, user_id, role_id) + SELECT + id, + topic, + context, + chat_json, + user_id, + role_id + FROM tutor_chats; + +DROP TABLE tutor_chats; + +DROP INDEX IF EXISTS tutor_chats_by_user; +CREATE INDEX chats_by_user ON chats(user_id); +DROP INDEX IF EXISTS tutor_chats_by_role; +CREATE INDEX chats_by_role ON chats(role_id); + +COMMIT; + +VACUUM; diff --git a/src/codehelp/schema.sql b/src/codehelp/schema.sql index 27451dd..cbbeef6 100644 --- a/src/codehelp/schema.sql +++ b/src/codehelp/schema.sql @@ -29,8 +29,8 @@ CREATE INDEX queries_by_user ON queries(user_id); DROP INDEX IF EXISTS queries_by_role; CREATE INDEX queries_by_role ON queries(role_id); -DROP TABLE IF EXISTS tutor_chats; -CREATE TABLE tutor_chats ( +DROP TABLE IF EXISTS chats; +CREATE TABLE chats ( id INTEGER PRIMARY KEY AUTOINCREMENT, chat_started TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, topic TEXT NOT NULL, @@ -43,10 +43,10 @@ CREATE TABLE tutor_chats ( FOREIGN KEY(role_id) REFERENCES roles(id), FOREIGN KEY(context_string_id) REFERENCES context_strings(id) ); -DROP INDEX IF EXISTS tutor_chats_by_user; -CREATE INDEX tutor_chats_by_user ON tutor_chats(user_id); -DROP INDEX IF EXISTS tutor_chats_by_role; -CREATE INDEX tutor_chats_by_role ON tutor_chats(role_id); +DROP INDEX IF EXISTS chats_by_user; +CREATE INDEX chats_by_user ON chats(user_id); +DROP INDEX IF EXISTS chats_by_role; +CREATE INDEX chats_by_role ON chats(role_id); DROP TABLE IF EXISTS context_strings; CREATE TABLE context_strings ( diff --git a/src/codehelp/templates/tutor_chat_component.html b/src/codehelp/templates/chat_component.html similarity index 100% rename from src/codehelp/templates/tutor_chat_component.html rename to src/codehelp/templates/chat_component.html diff --git a/src/codehelp/templates/tutor_admin.html b/src/codehelp/templates/tutor_admin.html index 1668605..bab4514 100644 --- a/src/codehelp/templates/tutor_admin.html +++ b/src/codehelp/templates/tutor_admin.html @@ -6,7 +6,7 @@ {% extends "admin_base.html" %} {% from "tables.html" import datatable %} -{% from "tutor_chat_component.html" import chat_component %} +{% from "chat_component.html" import chat_component %} {% block admin_body %}
diff --git a/src/codehelp/templates/tutor_view.html b/src/codehelp/templates/tutor_view.html index 679f4a1..e8f0379 100644 --- a/src/codehelp/templates/tutor_view.html +++ b/src/codehelp/templates/tutor_view.html @@ -5,7 +5,7 @@ #} {% extends "base.html" %} -{% from "tutor_chat_component.html" import chat_component %} +{% from "chat_component.html" import chat_component %} {% from "recent_chats.html" import recent_chats %} {% block body %} diff --git a/src/codehelp/tutor.py b/src/codehelp/tutor.py index c5bee9a..ec6398b 100644 --- a/src/codehelp/tutor.py +++ b/src/codehelp/tutor.py @@ -123,7 +123,7 @@ def create_chat(topic: str, context: CodeHelpContext) -> int: db = get_db() cur = db.execute( - "INSERT INTO tutor_chats (user_id, role_id, topic, context_name, context_string_id, chat_json) VALUES (?, ?, ?, ?, ?, ?)", + "INSERT INTO chats (user_id, role_id, topic, context_name, context_string_id, chat_json) VALUES (?, ?, ?, ?, ?, ?)", [user_id, role_id, topic, context.name, context_string_id, json.dumps([])] ) new_row_id = cur.lastrowid @@ -139,7 +139,7 @@ def get_chat_history(limit: int = 10) -> list[Row]: db = get_db() auth = get_auth() - history = db.execute("SELECT * FROM tutor_chats WHERE user_id=? ORDER BY id DESC LIMIT ?", [auth['user_id'], limit]).fetchall() + history = db.execute("SELECT * FROM chats WHERE user_id=? ORDER BY id DESC LIMIT ?", [auth['user_id'], limit]).fetchall() return history @@ -148,12 +148,12 @@ def get_chat(chat_id: int) -> tuple[list[ChatCompletionMessageParam], str, str, auth = get_auth() chat_row = db.execute( - "SELECT chat_json, topic, context_name, context_strings.ctx_str AS context_string, tutor_chats.user_id, roles.class_id " - "FROM tutor_chats " - "JOIN users ON tutor_chats.user_id=users.id " - "LEFT JOIN roles ON tutor_chats.role_id=roles.id " - "LEFT JOIN context_strings ON tutor_chats.context_string_id=context_strings.id " - "WHERE tutor_chats.id=?", + "SELECT chat_json, topic, context_name, context_strings.ctx_str AS context_string, chats.user_id, roles.class_id " + "FROM chats " + "JOIN users ON chats.user_id=users.id " + "LEFT JOIN roles ON chats.role_id=roles.id " + "LEFT JOIN context_strings ON chats.context_string_id=context_strings.id " + "WHERE chats.id=?", [chat_id] ).fetchone() @@ -200,7 +200,7 @@ def get_response(llm_dict: LLMDict, chat: list[ChatCompletionMessageParam]) -> t def save_chat(chat_id: int, chat: list[ChatCompletionMessageParam]) -> None: db = get_db() db.execute( - "UPDATE tutor_chats SET chat_json=? WHERE id=?", + "UPDATE chats SET chat_json=? WHERE id=?", [json.dumps(chat), chat_id] ) db.commit() @@ -264,27 +264,27 @@ def tutor_admin(id : int|None = None) -> str: db = get_db() chats = db.execute(""" SELECT - tutor_chats.id, + chats.id, users.display_name, - tutor_chats.topic, + chats.topic, ( SELECT COUNT(*) FROM - json_each(tutor_chats.chat_json) + json_each(chats.chat_json) WHERE json_extract(json_each.value, '$.role')='user' ) as user_msgs FROM - tutor_chats + chats JOIN - users ON tutor_chats.user_id=users.id + users ON chats.user_id=users.id ORDER BY - tutor_chats.id DESC + chats.id DESC """).fetchall() if id is not None: - chat_row = db.execute("SELECT users.display_name, topic, chat_json FROM tutor_chats JOIN users ON tutor_chats.user_id=users.id WHERE tutor_chats.id=?", [id]).fetchone() + chat_row = db.execute("SELECT users.display_name, topic, chat_json FROM chats JOIN users ON chats.user_id=users.id WHERE chats.id=?", [id]).fetchone() chat = json.loads(chat_row['chat_json']) else: chat_row = None diff --git a/tests/test_data.sql b/tests/test_data.sql index 577be58..5890d3e 100644 --- a/tests/test_data.sql +++ b/tests/test_data.sql @@ -92,7 +92,7 @@ VALUES (2, 'test_disabled', 0, '2199-12-31', 10, 0), (3, 'test_expired', 1, '2000-01-01', 10, 0); -INSERT INTO tutor_chats (id, topic, context_name, context_string_id, chat_json, user_id, role_id) +INSERT INTO chats (id, topic, context_name, context_string_id, chat_json, user_id, role_id) VALUES (1, 'topic1', 'ctx1', 1, '[{"role": "user", "content": "user_msg_1"}, {"role": "assistant", "content": "assistant_msg_1"}]', 11, NULL), (2, 'topic2', 'ctx2', 2, '[{"role": "user", "content": "user_msg_2"}, {"role": "assistant", "content": "assistant_msg_2"}]', 12, NULL);