Update langflow_history_service.py
This commit is contained in:
parent
8672c89743
commit
8390653861
1 changed files with 13 additions and 11 deletions
|
|
@ -7,7 +7,9 @@ import httpx
|
||||||
from typing import List, Dict, Optional, Any
|
from typing import List, Dict, Optional, Any
|
||||||
|
|
||||||
from config.settings import LANGFLOW_URL, LANGFLOW_SUPERUSER, LANGFLOW_SUPERUSER_PASSWORD
|
from config.settings import LANGFLOW_URL, LANGFLOW_SUPERUSER, LANGFLOW_SUPERUSER_PASSWORD
|
||||||
|
from utils.logging_config import get_logger
|
||||||
|
|
||||||
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
class LangflowHistoryService:
|
class LangflowHistoryService:
|
||||||
"""Simplified service to retrieve message history from Langflow"""
|
"""Simplified service to retrieve message history from Langflow"""
|
||||||
|
|
@ -22,7 +24,7 @@ class LangflowHistoryService:
|
||||||
return self.auth_token
|
return self.auth_token
|
||||||
|
|
||||||
if not all([LANGFLOW_SUPERUSER, LANGFLOW_SUPERUSER_PASSWORD]):
|
if not all([LANGFLOW_SUPERUSER, LANGFLOW_SUPERUSER_PASSWORD]):
|
||||||
print("Missing Langflow credentials")
|
logger.error("Missing Langflow credentials")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -41,14 +43,14 @@ class LangflowHistoryService:
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
result = response.json()
|
result = response.json()
|
||||||
self.auth_token = result.get('access_token')
|
self.auth_token = result.get('access_token')
|
||||||
print(f"Successfully authenticated with Langflow for history retrieval")
|
logger.debug(f"Successfully authenticated with Langflow for history retrieval")
|
||||||
return self.auth_token
|
return self.auth_token
|
||||||
else:
|
else:
|
||||||
print(f"Langflow authentication failed: {response.status_code}")
|
logger.error(f"Langflow authentication failed: {response.status_code}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error authenticating with Langflow: {e}")
|
logger.error(f"Error authenticating with Langflow: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def get_user_sessions(self, user_id: str, flow_id: Optional[str] = None) -> List[str]:
|
async def get_user_sessions(self, user_id: str, flow_id: Optional[str] = None) -> List[str]:
|
||||||
|
|
@ -76,17 +78,17 @@ class LangflowHistoryService:
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
session_ids = response.json()
|
session_ids = response.json()
|
||||||
print(f"Found {len(session_ids)} total sessions from Langflow")
|
logger.debug(f"Found {len(session_ids)} total sessions from Langflow")
|
||||||
|
|
||||||
# Since we use a single Langflow instance, return all sessions
|
# Since we use a single Langflow instance, return all sessions
|
||||||
# Session filtering is handled by user_id at the application level
|
# Session filtering is handled by user_id at the application level
|
||||||
return session_ids
|
return session_ids
|
||||||
else:
|
else:
|
||||||
print(f"Failed to get sessions: {response.status_code} - {response.text}")
|
logger.error(f"Failed to get sessions: {response.status_code} - {response.text}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error getting user sessions: {e}")
|
logger.error(f"Error getting user sessions: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
async def get_session_messages(self, user_id: str, session_id: str) -> List[Dict[str, Any]]:
|
async def get_session_messages(self, user_id: str, session_id: str) -> List[Dict[str, Any]]:
|
||||||
|
|
@ -113,11 +115,11 @@ class LangflowHistoryService:
|
||||||
# Convert to OpenRAG format
|
# Convert to OpenRAG format
|
||||||
return self._convert_langflow_messages(messages)
|
return self._convert_langflow_messages(messages)
|
||||||
else:
|
else:
|
||||||
print(f"Failed to get messages for session {session_id}: {response.status_code}")
|
logger.error(f"Failed to get messages for session {session_id}: {response.status_code}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error getting session messages: {e}")
|
logger.error(f"Error getting session messages: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def _convert_langflow_messages(self, langflow_messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
def _convert_langflow_messages(self, langflow_messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||||
|
|
@ -171,7 +173,7 @@ class LangflowHistoryService:
|
||||||
converted_messages.append(converted_msg)
|
converted_messages.append(converted_msg)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error converting message: {e}")
|
logger.error(f"Error converting message: {e}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
return converted_messages
|
return converted_messages
|
||||||
|
|
@ -216,7 +218,7 @@ class LangflowHistoryService:
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error getting user conversation history: {e}")
|
logger.error(f"Error getting user conversation history: {e}")
|
||||||
return {
|
return {
|
||||||
"error": str(e),
|
"error": str(e),
|
||||||
"conversations": []
|
"conversations": []
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue