Skip to content

Commit

Permalink
Rename tutor_chats->chats; migrate table w/ new columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
liffiton committed Jul 20, 2024
1 parent 5512bef commit 889e1ee
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 25 deletions.
42 changes: 42 additions & 0 deletions src/codehelp/migrations/20240720--codehelp--improve-chats.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- SPDX-FileCopyrightText: 2024 Mark Liffiton <[email protected]>
--
-- 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;
12 changes: 6 additions & 6 deletions src/codehelp/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/codehelp/templates/tutor_admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
<div class="columns is-multiline is-desktop">
Expand Down
2 changes: 1 addition & 1 deletion src/codehelp/templates/tutor_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
32 changes: 16 additions & 16 deletions src/codehelp/tutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand All @@ -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()

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 889e1ee

Please sign in to comment.