Spaces:
Sleeping
Sleeping
Update llm_handler.py
Browse files- llm_handler.py +48 -21
llm_handler.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
| 3 |
from typing import Dict, List
|
| 4 |
from openai import OpenAI
|
| 5 |
|
|
@@ -28,8 +29,18 @@ def initialize_llm():
|
|
| 28 |
|
| 29 |
def create_chat_session() -> str:
|
| 30 |
"""Creates a new chat session and returns the session ID."""
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
chat_sessions[session_id] = []
|
|
|
|
| 33 |
return session_id
|
| 34 |
|
| 35 |
def clear_chat_session(session_id: str) -> bool:
|
|
@@ -50,6 +61,16 @@ def get_chat_history(session_id: str) -> List[Dict[str, str]]:
|
|
| 50 |
"""Gets the chat history for a specific session."""
|
| 51 |
return chat_sessions.get(session_id, [])
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
def add_to_chat_history(session_id: str, role: str, content: str):
|
| 54 |
"""Adds a message to the chat history."""
|
| 55 |
if session_id not in chat_sessions:
|
|
@@ -60,19 +81,40 @@ def add_to_chat_history(session_id: str, role: str, content: str):
|
|
| 60 |
"content": content
|
| 61 |
})
|
| 62 |
|
| 63 |
-
# Keep only the last 20 messages to prevent memory overflow
|
| 64 |
# (10 user messages + 10 assistant responses)
|
| 65 |
if len(chat_sessions[session_id]) > 20:
|
| 66 |
chat_sessions[session_id] = chat_sessions[session_id][-20:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
def get_rag_response(query: str, session_id: str = None) -> str:
|
| 69 |
"""Generates a response using Retrieval-Augmented Generation with chat memory."""
|
| 70 |
if not all([encoder, chroma_collection, openrouter_client]):
|
| 71 |
-
return "Chatbot is not ready. Models or clients are not loaded."
|
| 72 |
|
| 73 |
# Create a new session if none provided
|
| 74 |
if session_id is None:
|
| 75 |
session_id = create_chat_session()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# 1. Retrieve relevant documents from ChromaDB
|
| 78 |
query_embedding = encoder.encode([query])[0].tolist()
|
|
@@ -129,19 +171,4 @@ If the context doesn't have the answer, use your own general knowledge to provid
|
|
| 129 |
|
| 130 |
except Exception as e:
|
| 131 |
print(f"β Error calling OpenRouter API: {e}")
|
| 132 |
-
return "Sorry, I encountered an error while processing your request.", session_id
|
| 133 |
-
|
| 134 |
-
def get_chat_session_count() -> int:
|
| 135 |
-
"""Returns the number of active chat sessions."""
|
| 136 |
-
return len(chat_sessions)
|
| 137 |
-
|
| 138 |
-
def cleanup_old_sessions():
|
| 139 |
-
"""Clean up old sessions - can be called periodically."""
|
| 140 |
-
# This is a simple cleanup - in production you might want to track timestamps
|
| 141 |
-
# and clean up sessions older than a certain time
|
| 142 |
-
if len(chat_sessions) > 1000: # If too many sessions
|
| 143 |
-
# Keep only the most recent 500 sessions
|
| 144 |
-
session_items = list(chat_sessions.items())
|
| 145 |
-
chat_sessions.clear()
|
| 146 |
-
chat_sessions.update(dict(session_items[-500:]))
|
| 147 |
-
print(f"π§Ή Cleaned up old chat sessions. Current count: {len(chat_sessions)}")
|
|
|
|
| 1 |
import os
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
from typing import Dict, List
|
| 5 |
from openai import OpenAI
|
| 6 |
|
|
|
|
| 29 |
|
| 30 |
def create_chat_session() -> str:
|
| 31 |
"""Creates a new chat session and returns the session ID."""
|
| 32 |
+
# Generate a unique session ID using timestamp + random number
|
| 33 |
+
timestamp = int(time.time() * 1000) # milliseconds
|
| 34 |
+
random_num = random.randint(1000, 9999)
|
| 35 |
+
session_id = f"{timestamp}_{random_num}"
|
| 36 |
+
|
| 37 |
+
# Ensure uniqueness (very unlikely to collide, but just in case)
|
| 38 |
+
while session_id in chat_sessions:
|
| 39 |
+
random_num = random.randint(1000, 9999)
|
| 40 |
+
session_id = f"{timestamp}_{random_num}"
|
| 41 |
+
|
| 42 |
chat_sessions[session_id] = []
|
| 43 |
+
print(f"π Created new chat session: {session_id}")
|
| 44 |
return session_id
|
| 45 |
|
| 46 |
def clear_chat_session(session_id: str) -> bool:
|
|
|
|
| 61 |
"""Gets the chat history for a specific session."""
|
| 62 |
return chat_sessions.get(session_id, [])
|
| 63 |
|
| 64 |
+
def cleanup_old_sessions():
|
| 65 |
+
"""Clean up old sessions - can be called periodically."""
|
| 66 |
+
# Keep only 15 most recent sessions to save memory
|
| 67 |
+
if len(chat_sessions) > 15:
|
| 68 |
+
# Keep only the most recent 10 sessions when cleanup is triggered
|
| 69 |
+
session_items = list(chat_sessions.items())
|
| 70 |
+
chat_sessions.clear()
|
| 71 |
+
chat_sessions.update(dict(session_items[-10:]))
|
| 72 |
+
print(f"π§Ή Cleaned up old chat sessions. Current count: {len(chat_sessions)}")
|
| 73 |
+
|
| 74 |
def add_to_chat_history(session_id: str, role: str, content: str):
|
| 75 |
"""Adds a message to the chat history."""
|
| 76 |
if session_id not in chat_sessions:
|
|
|
|
| 81 |
"content": content
|
| 82 |
})
|
| 83 |
|
| 84 |
+
# Keep only the last 20 messages per session to prevent memory overflow
|
| 85 |
# (10 user messages + 10 assistant responses)
|
| 86 |
if len(chat_sessions[session_id]) > 20:
|
| 87 |
chat_sessions[session_id] = chat_sessions[session_id][-20:]
|
| 88 |
+
|
| 89 |
+
# Trigger cleanup if we have too many sessions
|
| 90 |
+
if len(chat_sessions) > 15:
|
| 91 |
+
cleanup_old_sessions()
|
| 92 |
+
|
| 93 |
+
def get_chat_session_count() -> int:
|
| 94 |
+
"""Returns the number of active chat sessions."""
|
| 95 |
+
return len(chat_sessions)
|
| 96 |
+
|
| 97 |
+
def clear_all_chat_sessions() -> int:
|
| 98 |
+
"""Clears all chat sessions and returns the count of sessions that were cleared."""
|
| 99 |
+
session_count = len(chat_sessions)
|
| 100 |
+
chat_sessions.clear()
|
| 101 |
+
print(f"π§Ή All chat sessions cleared. Removed {session_count} sessions.")
|
| 102 |
+
return session_count
|
| 103 |
|
| 104 |
+
def get_rag_response(query: str, session_id: str = None) -> tuple[str, str]:
|
| 105 |
"""Generates a response using Retrieval-Augmented Generation with chat memory."""
|
| 106 |
if not all([encoder, chroma_collection, openrouter_client]):
|
| 107 |
+
return "Chatbot is not ready. Models or clients are not loaded.", session_id or create_chat_session()
|
| 108 |
|
| 109 |
# Create a new session if none provided
|
| 110 |
if session_id is None:
|
| 111 |
session_id = create_chat_session()
|
| 112 |
+
print(f"π Created new chat session: {session_id}")
|
| 113 |
+
|
| 114 |
+
# Validate session exists, create if it doesn't
|
| 115 |
+
if session_id not in chat_sessions:
|
| 116 |
+
chat_sessions[session_id] = []
|
| 117 |
+
print(f"π Session {session_id} not found, created new one")
|
| 118 |
|
| 119 |
# 1. Retrieve relevant documents from ChromaDB
|
| 120 |
query_embedding = encoder.encode([query])[0].tolist()
|
|
|
|
| 171 |
|
| 172 |
except Exception as e:
|
| 173 |
print(f"β Error calling OpenRouter API: {e}")
|
| 174 |
+
return "Sorry, I encountered an error while processing your request.", session_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|