Skip to content

Commit

Permalink
multi users
Browse files Browse the repository at this point in the history
  • Loading branch information
BlazzByte committed Oct 14, 2023
1 parent c258972 commit fd2c090
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
12 changes: 9 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
You are capable of almost *any* task, but you can't run code that show *UI* from a python file then that's why you always review the code in the file, you're told to run.
"""

#INITIAL SESSION STATE
if 'user_id' not in st.session_state:
user_id = str(uuid.uuid4())
st.session_state.user_id = user_id

# INITIAL STATES
if 'openai_key' in st.session_state and 'temperature' in st.session_state:
interpreter.reset()
Expand All @@ -82,14 +87,14 @@

#DATABASE
create_tables()
conversations = list(reversed(get_all_conversations()))
conversations = list(reversed(get_all_conversations(st.session_state.user_id)))
current_conversation = None

if conversations:
current_conversation = conversations[0]
else:
conversation_id = str(uuid.uuid4())
new_conversation = Conversation(conversation_id, f"Conversation {len(conversations)}")
new_conversation = Conversation(conversation_id, st.session_state.user_id, f"Conversation {len(conversations)}")
save_conversation(new_conversation)
st.rerun()

Expand Down Expand Up @@ -152,7 +157,8 @@

if st.button("Add New Conversation"):
conversation_id = str(uuid.uuid4())
new_conversation = Conversation(conversation_id, f"Conversation {len(conversations)}")
user_id = st.session_state.user_id
new_conversation = Conversation(conversation_id, user_id, f"Conversation {len(conversations)}")
save_conversation(new_conversation)
st.rerun()

Expand Down
Binary file added chats.db
Binary file not shown.
9 changes: 5 additions & 4 deletions src/data/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from contextlib import contextmanager
from src.data.models import Conversation, Chat

DATABASE_PATH = "chats_db.db"
DATABASE_PATH = "chats.db"

@contextmanager
def create_connection():
Expand All @@ -21,6 +21,7 @@ def create_tables():
cursor.execute('''
CREATE TABLE IF NOT EXISTS conversations (
id TEXT PRIMARY KEY,
user_id TEXT,
name TEXT
)
''')
Expand All @@ -37,15 +38,15 @@ def create_tables():

def save_conversation(conversation):
with create_connection() as cursor:
cursor.execute("INSERT INTO conversations (id, name) VALUES (?, ?)", (conversation.id, conversation.name))
cursor.execute("INSERT INTO conversations (id, user_id, name) VALUES (?, ?, ?)", (conversation.id, conversation.user_id, conversation.name))

def save_chat(chat):
with create_connection() as cursor:
cursor.execute("INSERT INTO chats (id, conversation_id, role, content) VALUES (?, ?, ?, ?)", (str(uuid.uuid4()), chat.conversation_id, chat.role, chat.content))

def get_all_conversations():
def get_all_conversations(user_id):
with create_connection() as cursor:
cursor.execute("SELECT id, name FROM conversations")
cursor.execute("SELECT id, user_id, name FROM conversations WHERE user_id=?", (user_id,))
result = cursor.fetchall()
conversations = [Conversation(*row) for row in result]
return [conversation.to_dict() for conversation in conversations]
Expand Down
6 changes: 4 additions & 2 deletions src/data/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import json


class Conversation:
def __init__(self, id, name):
def __init__(self, id, user_id, name = 'Conversation'):
self.id = id
self.user_id = user_id
self.name = name

def to_dict(self):
return {"id": self.id, "name": self.name}
return {"id": self.id, "user_id": self.user_id, "name": self.name}

def to_json(self):
return json.dumps(self.to_dict())
Expand Down

0 comments on commit fd2c090

Please sign in to comment.