room table so we can dedupe the name and title
modified: app.py new file: migrations/README new file: migrations/alembic.ini new file: migrations/env.py new file: migrations/script.py.mako new file: migrations/versions/a9e886c56482_create_room_table.py
This commit is contained in:
parent
4e893dc4d2
commit
2639a2f99c
6 changed files with 363 additions and 33 deletions
95
app.py
95
app.py
|
|
@ -26,29 +26,49 @@ db = SQLAlchemy(app)
|
|||
|
||||
|
||||
# Create an argument parser for aws profile.
|
||||
import argparse
|
||||
#import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--profile", help="AWS profile name")
|
||||
args = parser.parse_args()
|
||||
profile_name = args.profile
|
||||
#parser = argparse.ArgumentParser()
|
||||
#parser.add_argument("--profile", help="AWS profile name")
|
||||
#args = parser.parse_args()
|
||||
#profile_name = args.profile
|
||||
profile_name = None
|
||||
|
||||
class Room(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(128), nullable=False, unique=True)
|
||||
title = db.Column(db.String(128), nullable=True) # Initially, there might be no title
|
||||
|
||||
class Message(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(128), nullable=False)
|
||||
content = db.Column(db.String(1024), nullable=False)
|
||||
room = db.Column(db.String(128), nullable=False)
|
||||
room_id = db.Column(db.Integer, db.ForeignKey('room.id'), nullable=False)
|
||||
|
||||
def __init__(self, username, content, room):
|
||||
def __init__(self, username, content, room_id):
|
||||
self.username = username
|
||||
self.content = content
|
||||
self.room = room
|
||||
self.room_id = room_id
|
||||
|
||||
def get_room(room_name):
|
||||
"""Utility function to get room from room name."""
|
||||
room = Room.query.filter_by(name=room_name).first()
|
||||
if room:
|
||||
return room
|
||||
else:
|
||||
# Create a new room since it doesn't exist
|
||||
new_room = Room(name=room_name)
|
||||
db.session.add(new_room)
|
||||
db.session.commit()
|
||||
return new_room
|
||||
|
||||
# Create the database and tables
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
#with app.app_context():
|
||||
# db.create_all()
|
||||
|
||||
from flask_migrate import Migrate
|
||||
|
||||
migrate = Migrate(app, db)
|
||||
|
||||
|
||||
@app.route("/favicon.ico")
|
||||
|
|
@ -68,11 +88,14 @@ def chat(room):
|
|||
|
||||
@socketio.on("join")
|
||||
def on_join(data):
|
||||
room = data["room"]
|
||||
join_room(room)
|
||||
room_name = data["room"]
|
||||
room = get_room(room_name)
|
||||
|
||||
# this makes the client start listening for new events for this room.
|
||||
join_room(room_name)
|
||||
|
||||
# Fetch previous messages from the database
|
||||
previous_messages = Message.query.filter_by(room=room).all()
|
||||
previous_messages = Message.query.filter_by(room_id=room.id).all()
|
||||
|
||||
# Send the history of messages only to the newly connected client.
|
||||
# The reason for using `request.sid` here is to target the specific session (or client) that
|
||||
|
|
@ -94,17 +117,20 @@ def on_join(data):
|
|||
emit(
|
||||
"message",
|
||||
{"id": None, "content": f"{data['username']} has joined the room."},
|
||||
room=room,
|
||||
room=room.name,
|
||||
)
|
||||
|
||||
|
||||
@socketio.on("message")
|
||||
def handle_message(data):
|
||||
room_name = data["room"]
|
||||
room = get_room(room_name)
|
||||
|
||||
# Save the message to the database
|
||||
new_message = Message(
|
||||
username=data["username"],
|
||||
content=data["message"],
|
||||
room=data["room"],
|
||||
room_id=room.id,
|
||||
)
|
||||
db.session.add(new_message)
|
||||
db.session.commit()
|
||||
|
|
@ -115,7 +141,7 @@ def handle_message(data):
|
|||
"id": new_message.id,
|
||||
"content": f"**{data['username']}:**\n\n{data['message']}",
|
||||
},
|
||||
room=data["room"],
|
||||
room=room.name,
|
||||
)
|
||||
|
||||
if (
|
||||
|
|
@ -128,26 +154,26 @@ def handle_message(data):
|
|||
emit(
|
||||
"message",
|
||||
{"id": None, "content": f"<span id='processing'>Processing...</span>"},
|
||||
room=data["room"],
|
||||
room=room.name,
|
||||
)
|
||||
|
||||
if "claude-v1" in data["message"]:
|
||||
eventlet.spawn(chat_claude, data["username"], data["room"], data["message"])
|
||||
eventlet.spawn(chat_claude, data["username"], room.name, data["message"])
|
||||
if "claude-v2" in data["message"]:
|
||||
eventlet.spawn(
|
||||
chat_claude,
|
||||
data["username"],
|
||||
data["room"],
|
||||
room.name,
|
||||
data["message"],
|
||||
model_name="anthropic.claude-v2",
|
||||
)
|
||||
if "gpt-3" in data["message"]:
|
||||
eventlet.spawn(chat_gpt, data["username"], data["room"], data["message"])
|
||||
eventlet.spawn(chat_gpt, data["username"], room.name, data["message"])
|
||||
if "gpt-4" in data["message"]:
|
||||
eventlet.spawn(
|
||||
chat_gpt,
|
||||
data["username"],
|
||||
data["room"],
|
||||
room.name,
|
||||
data["message"],
|
||||
model_name="gpt-4-1106-preview",
|
||||
)
|
||||
|
|
@ -166,11 +192,12 @@ def handle_delete_message(data):
|
|||
emit("message_deleted", {"message_id": msg_id}, room=data["room"])
|
||||
|
||||
|
||||
def chat_claude(username, room, message, model_name="anthropic.claude-v1"):
|
||||
def chat_claude(username, room_name, message, model_name="anthropic.claude-v1"):
|
||||
with app.app_context():
|
||||
room = get_room(room_name)
|
||||
# claude has a 100,000 token context window for prompts.
|
||||
all_messages = (
|
||||
Message.query.filter_by(room=room).order_by(Message.id.desc()).all()
|
||||
Message.query.filter_by(room_id=room.id).order_by(Message.id.desc()).all()
|
||||
)
|
||||
|
||||
chat_history = ""
|
||||
|
|
@ -223,7 +250,7 @@ def chat_claude(username, room, message, model_name="anthropic.claude-v1"):
|
|||
|
||||
# save empty message, we need the ID when we chunk the response.
|
||||
with app.app_context():
|
||||
new_message = Message(username=model_name, content=buffer, room=room)
|
||||
new_message = Message(username=model_name, content=buffer, room_id=room.id)
|
||||
db.session.add(new_message)
|
||||
db.session.commit()
|
||||
msg_id = new_message.id
|
||||
|
|
@ -246,14 +273,14 @@ def chat_claude(username, room, message, model_name="anthropic.claude-v1"):
|
|||
"id": msg_id,
|
||||
"content": f"**{username} ({model_name}):**\n\n{content}",
|
||||
},
|
||||
room=room,
|
||||
room=room.name,
|
||||
)
|
||||
first_chunk = False
|
||||
else:
|
||||
socketio.emit(
|
||||
"message_chunk",
|
||||
{"id": msg_id, "content": content},
|
||||
room=room,
|
||||
room=room.name,
|
||||
)
|
||||
socketio.sleep(0) # Force immediate handling
|
||||
|
||||
|
|
@ -267,17 +294,19 @@ def chat_claude(username, room, message, model_name="anthropic.claude-v1"):
|
|||
db.session.add(new_message)
|
||||
db.session.commit()
|
||||
|
||||
socketio.emit("delete_processing_message", msg_id, room=room)
|
||||
socketio.emit("delete_processing_message", msg_id, room=room.name)
|
||||
|
||||
|
||||
def chat_gpt(username, room, message, model_name="gpt-3.5-turbo"):
|
||||
def chat_gpt(username, room_name, message, model_name="gpt-3.5-turbo"):
|
||||
|
||||
limit = 15
|
||||
if model_name == "gpt-4-1106-preview":
|
||||
limit = 1000
|
||||
|
||||
with app.app_context():
|
||||
room = get_room(room_name)
|
||||
last_messages = (
|
||||
Message.query.filter_by(room=room)
|
||||
Message.query.filter_by(room_id=room.id)
|
||||
.order_by(Message.id.desc())
|
||||
.limit(limit)
|
||||
.all()
|
||||
|
|
@ -305,7 +334,7 @@ def chat_gpt(username, room, message, model_name="gpt-3.5-turbo"):
|
|||
|
||||
# save empty message, we need the ID when we chunk the response.
|
||||
with app.app_context():
|
||||
new_message = Message(username=model_name, content=buffer, room=room)
|
||||
new_message = Message(username=model_name, content=buffer, room_id=room.id)
|
||||
db.session.add(new_message)
|
||||
db.session.commit()
|
||||
msg_id = new_message.id
|
||||
|
|
@ -329,14 +358,14 @@ def chat_gpt(username, room, message, model_name="gpt-3.5-turbo"):
|
|||
"id": msg_id,
|
||||
"content": f"**{username} ({model_name}):**\n\n{content}",
|
||||
},
|
||||
room=room,
|
||||
room=room.name,
|
||||
)
|
||||
first_chunk = False
|
||||
else:
|
||||
socketio.emit(
|
||||
"message_chunk",
|
||||
{"id": msg_id, "content": content},
|
||||
room=room,
|
||||
room=room.name,
|
||||
)
|
||||
socketio.sleep(0) # Force immediate handling
|
||||
|
||||
|
|
@ -350,7 +379,7 @@ def chat_gpt(username, room, message, model_name="gpt-3.5-turbo"):
|
|||
db.session.add(new_message)
|
||||
db.session.commit()
|
||||
|
||||
socketio.emit("delete_processing_message", msg_id, room=room)
|
||||
socketio.emit("delete_processing_message", msg_id, room=room.name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
1
migrations/README
Normal file
1
migrations/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Single-database configuration for Flask.
|
||||
50
migrations/alembic.ini
Normal file
50
migrations/alembic.ini
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# template used to generate migration files
|
||||
# file_template = %%(rev)s_%%(slug)s
|
||||
|
||||
# set to 'true' to run the environment during
|
||||
# the 'revision' command, regardless of autogenerate
|
||||
# revision_environment = false
|
||||
|
||||
|
||||
# Logging configuration
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic,flask_migrate
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[logger_flask_migrate]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = flask_migrate
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
||||
113
migrations/env.py
Normal file
113
migrations/env.py
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
import logging
|
||||
from logging.config import fileConfig
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from alembic import context
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name)
|
||||
logger = logging.getLogger('alembic.env')
|
||||
|
||||
|
||||
def get_engine():
|
||||
try:
|
||||
# this works with Flask-SQLAlchemy<3 and Alchemical
|
||||
return current_app.extensions['migrate'].db.get_engine()
|
||||
except (TypeError, AttributeError):
|
||||
# this works with Flask-SQLAlchemy>=3
|
||||
return current_app.extensions['migrate'].db.engine
|
||||
|
||||
|
||||
def get_engine_url():
|
||||
try:
|
||||
return get_engine().url.render_as_string(hide_password=False).replace(
|
||||
'%', '%%')
|
||||
except AttributeError:
|
||||
return str(get_engine().url).replace('%', '%%')
|
||||
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
config.set_main_option('sqlalchemy.url', get_engine_url())
|
||||
target_db = current_app.extensions['migrate'].db
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def get_metadata():
|
||||
if hasattr(target_db, 'metadatas'):
|
||||
return target_db.metadatas[None]
|
||||
return target_db.metadata
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url, target_metadata=get_metadata(), literal_binds=True
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online():
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
|
||||
# this callback is used to prevent an auto-migration from being generated
|
||||
# when there are no changes to the schema
|
||||
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
|
||||
def process_revision_directives(context, revision, directives):
|
||||
if getattr(config.cmd_opts, 'autogenerate', False):
|
||||
script = directives[0]
|
||||
if script.upgrade_ops.is_empty():
|
||||
directives[:] = []
|
||||
logger.info('No changes in schema detected.')
|
||||
|
||||
conf_args = current_app.extensions['migrate'].configure_args
|
||||
if conf_args.get("process_revision_directives") is None:
|
||||
conf_args["process_revision_directives"] = process_revision_directives
|
||||
|
||||
connectable = get_engine()
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=get_metadata(),
|
||||
**conf_args
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
24
migrations/script.py.mako
Normal file
24
migrations/script.py.mako
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade():
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade():
|
||||
${downgrades if downgrades else "pass"}
|
||||
113
migrations/versions/a9e886c56482_create_room_table.py
Normal file
113
migrations/versions/a9e886c56482_create_room_table.py
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.sql import table, column, select
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'a9e886c56482'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
def upgrade():
|
||||
# Create room table
|
||||
op.create_table('room',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(length=128), nullable=False),
|
||||
sa.Column('title', sa.String(length=128), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('name')
|
||||
)
|
||||
|
||||
# Add room_id column to message table
|
||||
op.add_column('message', sa.Column('room_id', sa.Integer(), nullable=True))
|
||||
|
||||
# Temporary table objects
|
||||
message_table = table('message',
|
||||
column('id', sa.Integer),
|
||||
column('username', sa.String),
|
||||
column('content', sa.String),
|
||||
column('room', sa.String),
|
||||
column('room_id', sa.Integer),
|
||||
)
|
||||
room_table = table('room',
|
||||
column('id', sa.Integer),
|
||||
column('name', sa.String)
|
||||
)
|
||||
|
||||
# Execution context
|
||||
conn = op.get_bind()
|
||||
|
||||
# Insert distinct rooms into room table and create mapping
|
||||
distinct_rooms = conn.execute(select(message_table.c.room).distinct())
|
||||
room_name_to_id = {}
|
||||
for room_name, in distinct_rooms:
|
||||
conn.execute(room_table.insert().values(name=room_name))
|
||||
room_id = conn.execute(select(room_table.c.id).where(room_table.c.name == room_name)).scalar()
|
||||
room_name_to_id[room_name] = room_id
|
||||
|
||||
# Update message table with room_id
|
||||
for room_name, room_id in room_name_to_id.items():
|
||||
conn.execute(message_table.update().where(message_table.c.room == room_name).values(room_id=room_id))
|
||||
|
||||
# Create new_message table
|
||||
new_message_table = op.create_table('new_message',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('username', sa.String(length=128), nullable=False),
|
||||
sa.Column('content', sa.String(length=1024), nullable=False),
|
||||
sa.Column('room_id', sa.Integer(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['room_id'], ['room.id']),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
# Copy data from old message table to new_message table
|
||||
old_messages = conn.execute(sa.select(message_table)).fetchall()
|
||||
for old_message in old_messages:
|
||||
conn.execute(new_message_table.insert().values(
|
||||
id=old_message.id,
|
||||
username=old_message.username,
|
||||
content=old_message.content,
|
||||
room_id=old_message.room_id
|
||||
))
|
||||
|
||||
# Drop old message table and rename new_message to message
|
||||
op.drop_table('message')
|
||||
op.rename_table('new_message', 'message')
|
||||
|
||||
def downgrade():
|
||||
# Recreate old_message table with 'room' column
|
||||
old_message_table = op.create_table('old_message',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('username', sa.String(length=128), nullable=False),
|
||||
sa.Column('content', sa.String(length=1024), nullable=False),
|
||||
sa.Column('room', sa.String(length=128), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
# Copy data back from message to old_message
|
||||
message_table = table('message',
|
||||
column('id', sa.Integer),
|
||||
column('username', sa.String),
|
||||
column('content', sa.String),
|
||||
column('room_id', sa.Integer)
|
||||
)
|
||||
room_table = table('room',
|
||||
column('id', sa.Integer),
|
||||
column('name', sa.String)
|
||||
)
|
||||
|
||||
conn = op.get_bind()
|
||||
messages = conn.execute(select(message_table)).fetchall()
|
||||
for message in messages:
|
||||
room_name = conn.execute(select(room_table.c.name).where(room_table.c.id == message.room_id)).scalar()
|
||||
conn.execute(old_message_table.insert().values(
|
||||
id=message.id,
|
||||
username=message.username,
|
||||
content=message.content,
|
||||
room=room_name
|
||||
))
|
||||
|
||||
# Drop current message table and rename old_message to message
|
||||
op.drop_table('message')
|
||||
op.rename_table('old_message', 'message')
|
||||
op.drop_table('room')
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue