From 6e3f1f7a6098a623b913b3ec539291295a329d62 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Fri, 10 Nov 2023 13:24:55 -0500 Subject: [PATCH] gpt-4 title creation modified: app.py modified: templates/chat.html --- app.py | 87 ++++++++++++++++++++++++++++++++------------- templates/chat.html | 7 ++++ 2 files changed, 69 insertions(+), 25 deletions(-) diff --git a/app.py b/app.py index a3ff210..79fbde4 100644 --- a/app.py +++ b/app.py @@ -26,30 +26,35 @@ 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 + 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_id = db.Column(db.Integer, db.ForeignKey('room.id'), nullable=False) + room_id = db.Column(db.Integer, db.ForeignKey("room.id"), nullable=False) def __init__(self, username, content, room_id): self.username = username self.content = content 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() @@ -62,8 +67,9 @@ def get_room(room_name): db.session.commit() return new_room + # Create the database and tables -#with app.app_context(): +# with app.app_context(): # db.create_all() from flask_migrate import Migrate @@ -94,6 +100,10 @@ def on_join(data): # this makes the client start listening for new events for this room. join_room(room_name) + # update the title bar with the proper room title, if it exists for just this new client. + if room.title: + socketio.emit("update_room_title", {"title": room.title}, room=request.sid) + # Fetch previous messages from the database previous_messages = Message.query.filter_by(room_id=room.id).all() @@ -298,7 +308,6 @@ def chat_claude(username, room_name, message, model_name="anthropic.claude-v1"): def chat_gpt(username, room_name, message, model_name="gpt-3.5-turbo"): - limit = 15 if model_name == "gpt-4-1106-preview": limit = 1000 @@ -311,24 +320,29 @@ def chat_gpt(username, room_name, message, model_name="gpt-3.5-turbo"): .limit(limit) .all() ) + if len(last_messages) % 6 == 0 or room.title is None: + room.title = gpt_generate_room_title(last_messages, model_name) + db.session.add(room) + db.session.commit() + socketio.emit("update_room_title", {"title": room.title}, room=room.name) - chat_history = [ - { - "role": "system" - if ( - msg.username == "gpt-3.5-turbo" - or msg.username == "anthropic.claude-v1" - or msg.username == "anthropic.claude-v2" - or msg.username == "gpt-4" - or msg.username == "gpt-4-1106-preview" - ) - else "user", - "content": f"{msg.username}: {msg.content}", - } - for msg in reversed(last_messages) - ] + chat_history = [ + { + "role": "system" + if ( + msg.username == "gpt-3.5-turbo" + or msg.username == "anthropic.claude-v1" + or msg.username == "anthropic.claude-v2" + or msg.username == "gpt-4" + or msg.username == "gpt-4-1106-preview" + ) + else "user", + "content": f"{msg.username}: {msg.content}", + } + for msg in reversed(last_messages) + ] - chat_history.append({"role": "user", "content": f"{message}\n\n{model_name}: "}) + chat_history.append({"role": "user", "content": f"{message}\n\n{model_name}: "}) buffer = "" # Content buffer for accumulating the chunks @@ -382,5 +396,28 @@ def chat_gpt(username, room_name, message, model_name="gpt-3.5-turbo"): socketio.emit("delete_processing_message", msg_id, room=room.name) +def gpt_generate_room_title(messages, model_name): + """ + Generate a title for the room based on a list of messages. + """ + conversation = " ".join([msg.content for msg in messages]) + + # Interaction with LLM to generate summary + # For example, using OpenAI's GPT model + response = openai.ChatCompletion.create( + messages=[ + { + "role": "system", + "content": f"return a short title for the title bar for this conversation: {conversation}", + } + ], + model=model_name, # or any appropriate model + max_tokens=20, # Adjust as needed + ) + + title = response.choices[0]["message"]["content"] + return title + + if __name__ == "__main__": socketio.run(app, host="0.0.0.0", port=5001) diff --git a/templates/chat.html b/templates/chat.html index fc23f8d..300373d 100644 --- a/templates/chat.html +++ b/templates/chat.html @@ -127,6 +127,13 @@ document.getElementById('message').addEventListener('keydown', function(e) { } }); + +socket.on('update_room_title', (data) => { + // Update the window's title + document.title = data.title; +}); + + socket.on('connect', () => { socket.emit('join', {username: username, room: room}); });