T-K-O-H commited on
Commit
10c3afe
·
1 Parent(s): fbaa829

Update session handling with unique session IDs

Browse files
Files changed (2) hide show
  1. app.py +24 -5
  2. push_to_hf.bat +4 -0
app.py CHANGED
@@ -3,6 +3,7 @@ import chainlit as cl
3
  from agent_graph import agent_node
4
  from dotenv import load_dotenv # Import dotenv
5
  from typing import List, Dict
 
6
 
7
  # Load environment variables from .env file
8
  load_dotenv()
@@ -13,13 +14,24 @@ openai_api_key = os.getenv("OPENAI_API_KEY")
13
  if not openai_api_key:
14
  raise ValueError("OpenAI API key is missing in the .env file")
15
 
16
- # Store chat history
17
  chat_histories: Dict[str, List[Dict[str, str]]] = {}
18
 
 
 
 
 
 
 
19
  @cl.on_chat_start
20
  async def start_chat():
 
 
 
 
 
21
  # Initialize empty chat history for this session
22
- chat_histories[cl.user_session.get("id")] = []
23
 
24
  welcome_message = """👋 Welcome to the Stock Price Calculator!
25
 
@@ -38,8 +50,15 @@ What would you like to know?"""
38
  @cl.on_message
39
  async def handle_message(message: cl.Message):
40
  try:
 
 
 
 
 
 
 
 
41
  # Get chat history for this session
42
- session_id = cl.user_session.get("id")
43
  history = chat_histories.get(session_id, [])
44
 
45
  # Add current message to history
@@ -72,6 +91,6 @@ async def handle_message(message: cl.Message):
72
  @cl.on_chat_end
73
  async def end_chat():
74
  # Clean up chat history when session ends
75
- session_id = cl.user_session.get("id")
76
- if session_id in chat_histories:
77
  del chat_histories[session_id]
 
3
  from agent_graph import agent_node
4
  from dotenv import load_dotenv # Import dotenv
5
  from typing import List, Dict
6
+ import time
7
 
8
  # Load environment variables from .env file
9
  load_dotenv()
 
14
  if not openai_api_key:
15
  raise ValueError("OpenAI API key is missing in the .env file")
16
 
17
+ # Store chat history with unique session IDs
18
  chat_histories: Dict[str, List[Dict[str, str]]] = {}
19
 
20
+ def get_unique_session_id():
21
+ """Generate a unique session ID combining user session ID and timestamp."""
22
+ user_id = cl.user_session.get("id", "unknown")
23
+ timestamp = int(time.time() * 1000) # Current time in milliseconds
24
+ return f"{user_id}_{timestamp}"
25
+
26
  @cl.on_chat_start
27
  async def start_chat():
28
+ # Generate a unique session ID
29
+ session_id = get_unique_session_id()
30
+ # Store the session ID in user session for later use
31
+ cl.user_session.set("session_id", session_id)
32
+
33
  # Initialize empty chat history for this session
34
+ chat_histories[session_id] = []
35
 
36
  welcome_message = """👋 Welcome to the Stock Price Calculator!
37
 
 
50
  @cl.on_message
51
  async def handle_message(message: cl.Message):
52
  try:
53
+ # Get the unique session ID
54
+ session_id = cl.user_session.get("session_id")
55
+ if not session_id:
56
+ # If session ID is missing, generate a new one
57
+ session_id = get_unique_session_id()
58
+ cl.user_session.set("session_id", session_id)
59
+ chat_histories[session_id] = []
60
+
61
  # Get chat history for this session
 
62
  history = chat_histories.get(session_id, [])
63
 
64
  # Add current message to history
 
91
  @cl.on_chat_end
92
  async def end_chat():
93
  # Clean up chat history when session ends
94
+ session_id = cl.user_session.get("session_id")
95
+ if session_id and session_id in chat_histories:
96
  del chat_histories[session_id]
push_to_hf.bat ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ @echo off
2
+ git add .
3
+ git commit -m "Update session handling with unique session IDs"
4
+ git push origin main