Spaces:
Runtime error
Runtime error
File size: 1,275 Bytes
06cb2a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
"""
Utility functions for the Gradio-based chatbot application.
"""
import uuid
# Global state for session and user IDs
_session_id = None
_user_id = None
def get_session_id():
"""
Get the current session ID.
Creates a new ID if one doesn't exist.
"""
global _session_id
if _session_id is None:
_session_id = str(uuid.uuid4())
return _session_id
def get_user_id():
"""
Get the current user ID.
Creates a new ID if one doesn't exist.
"""
global _user_id
if _user_id is None:
_user_id = str(uuid.uuid4())
return _user_id
def reset_ids():
"""
Reset both session and user IDs.
Useful for testing or when starting a new session.
"""
global _session_id, _user_id
_session_id = None
_user_id = None
def format_source_documents(source_documents):
"""
Format source documents for display.
"""
if not source_documents:
return None
formatted_docs = []
for i, doc in enumerate(source_documents):
if hasattr(doc, 'metadata') and doc.metadata:
source = doc.metadata.get('source', 'Unknown')
formatted_docs.append(f"Source {i+1}: {source}")
return "\n".join(formatted_docs) if formatted_docs else None
|