cleaned file tree structure

This commit is contained in:
2025-10-13 19:23:41 -04:00
parent 29a451ab58
commit 839f0c9107
6 changed files with 0 additions and 0 deletions

16
milestone-0/README.md Normal file
View File

@@ -0,0 +1,16 @@
## Platform to connect to our DB
We used DBeaver as connector to connect to Mysql DB. First you will need to install DBeaver in your local.
- Click connect button on the top left. In "main" tab, specify the Database name as "cs348db", username: cs348, Password Cs348Group!
![](./image1.png)
- go to SSH tab. specify ssh host: mini.peisongxiao.com. user: cs348. passwd: cs348
![](./image2.png)
- click finish
You are now good to navigate our DB.
## Load table
go all the way down to the ChatHistory, click it then you will be able to view our table, which contains all prompt history the user had with the chatbot so far, and the response from the robot(hard coded for now).
![](./image3.png)
## Create a new table
We can create a new table in DBeaver and specify the columns, type of each columns, constraints, keys and so on..
![](./image4.png)

151
milestone-0/app.py Executable file
View File

@@ -0,0 +1,151 @@
#!/usr/bin/env python3
import os
import sys
import mysql.connector
from mysql.connector import errorcode
CFG = {
"host": os.getenv("DB_HOST", "127.0.0.1"),
"user": os.getenv("DB_USER", "cs348"),
"password": os.getenv("DB_PASS", "Cs348Group!"),
"database": os.getenv("DB_NAME", "cs348db"),
"charset": "utf8mb4",
"collation": "utf8mb4_unicode_ci",
"auth_plugin": "caching_sha2_password",
}
DDL = """
CREATE TABLE IF NOT EXISTS `ChatHistory` (
`text_id` INT NOT NULL,
`type` VARCHAR(64) NOT NULL,
`text` MEDIUMTEXT NOT NULL,
PRIMARY KEY (`text_id`)
) ENGINE=InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
"""
def get_connection():
try:
return mysql.connector.connect(**CFG)
except mysql.connector.Error as e:
print(f"[fatal] MySQL connect error: {e}", file=sys.stderr)
sys.exit(1)
def ensure_table(conn):
with conn.cursor() as cur:
cur.execute(DDL)
conn.commit()
def get_next_id(conn):
with conn.cursor() as cur:
cur.execute("SELECT COALESCE(MAX(text_id), 0) FROM `ChatHistory`;")
(max_id,) = cur.fetchone()
return int(max_id) + 1
def insert_two_rows(conn, user_text: str):
nxt = get_next_id(conn)
rows = [
(nxt, "user", user_text),
(nxt + 1, "assistant", "LLM answer here"),
]
with conn.cursor() as cur:
cur.executemany(
"INSERT INTO `ChatHistory` (`text_id`, `type`, `text`) VALUES (%s, %s, %s);",
rows,
)
conn.commit()
return nxt, nxt + 1
def list_history(conn, limit: int | None = None):
sql = "SELECT `text_id`, `type`, `text` FROM `ChatHistory` ORDER BY `text_id` ASC"
params = ()
if limit is not None and limit > 0:
sql += " LIMIT %s"
params = (limit,)
with conn.cursor() as cur:
cur.execute(sql, params)
rows = cur.fetchall()
if not rows:
print("(no rows)")
return
# Simple pretty print without extra deps
idw = max(len("text_id"), *(len(str(r[0])) for r in rows))
typew = max(len("type"), *(len(str(r[1])) for r in rows))
print(f"{'text_id'.ljust(idw)} {'type'.ljust(typew)} text")
print(f"{'-'*idw} {'-'*typew} {'-'*4}")
for tid, typ, txt in rows:
print(f"{str(tid).ljust(idw)} {str(typ).ljust(typew)} {txt}")
def print_help():
print(r"""
Commands:
\list Print all history (ordered by text_id)
\list N Print first N rows (ordered by text_id)
\q Quit
\help This help
Anything else is sent as a user prompt:
- Inserts (next_id, 'user', <your text>)
- Inserts (next_id+1, 'assistant', 'LLM answer here')
""".strip())
def main():
print("Connecting to MySQL…")
conn = get_connection()
try:
ensure_table(conn)
print("Ready. Type \\help for commands. Empty line quits.")
while True:
try:
line = input("Enter prompt> ")
except (EOFError, KeyboardInterrupt):
print("\nBye.")
break
if line is None:
continue
line = line.strip()
if not line:
print("Bye.")
break
# Commands
if line == r"\q":
print("Bye.")
break
if line == r"\help":
print_help()
continue
if line.startswith(r"\list"):
parts = line.split()
limit = None
if len(parts) == 2 and parts[1].isdigit():
limit = int(parts[1])
try:
list_history(conn, limit)
except mysql.connector.Error as e:
print(f"[error] list failed: {e}", file=sys.stderr)
continue
# Regular insert path
try:
first_id, second_id = insert_two_rows(conn, line)
print(f"Inserted prompt at text_id={first_id} and placeholder at text_id={second_id}.")
except mysql.connector.Error as e:
print(f"[error] insert failed: {e}", file=sys.stderr)
if e.errno in (errorcode.CR_SERVER_GONE_ERROR, errorcode.CR_SERVER_LOST):
try:
conn.close()
except Exception:
pass
conn = get_connection()
finally:
try:
conn.close()
except Exception:
pass
if __name__ == "__main__":
main()

BIN
milestone-0/image1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 527 KiB

BIN
milestone-0/image2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 416 KiB

BIN
milestone-0/image3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 653 KiB

BIN
milestone-0/image4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB