Refactor MessageHistoryComponent to use get_messages function
This commit is contained in:
parent
e0e35b3245
commit
65a88cbcbd
1 changed files with 14 additions and 25 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
from langchain.schema import Document
|
|
||||||
from langflow import CustomComponent
|
from langflow import CustomComponent
|
||||||
from langflow.services.deps import get_monitor_service
|
from langflow.memory import get_messages
|
||||||
|
from langflow.schema import Record
|
||||||
|
|
||||||
|
|
||||||
class MessageHistoryComponent(CustomComponent):
|
class MessageHistoryComponent(CustomComponent):
|
||||||
|
|
@ -11,7 +11,10 @@ class MessageHistoryComponent(CustomComponent):
|
||||||
|
|
||||||
def build_config(self):
|
def build_config(self):
|
||||||
return {
|
return {
|
||||||
"sender": {"options": ["Machine", "User"], "display_name": "Sender Type"},
|
"sender": {
|
||||||
|
"options": ["Machine", "User"],
|
||||||
|
"display_name": "Sender Type",
|
||||||
|
},
|
||||||
"sender_name": {"display_name": "Sender Name"},
|
"sender_name": {"display_name": "Sender Name"},
|
||||||
"file_path": {
|
"file_path": {
|
||||||
"display_name": "File Path",
|
"display_name": "File Path",
|
||||||
|
|
@ -34,26 +37,12 @@ class MessageHistoryComponent(CustomComponent):
|
||||||
sender_name: Optional[str] = None,
|
sender_name: Optional[str] = None,
|
||||||
session_id: Optional[str] = None,
|
session_id: Optional[str] = None,
|
||||||
n_messages: int = 5,
|
n_messages: int = 5,
|
||||||
) -> List[Document]:
|
) -> List[Record]:
|
||||||
# Load the chat history df
|
messages = get_messages(
|
||||||
monitor_service = get_monitor_service()
|
sender=sender,
|
||||||
chat_history_df = monitor_service.to_df("messages")
|
sender_name=sender_name,
|
||||||
|
session_id=session_id,
|
||||||
# Filter the df
|
limit=n_messages,
|
||||||
if session_id:
|
)
|
||||||
chat_history_df = chat_history_df[chat_history_df["session_id"] == session_id]
|
self.status = messages
|
||||||
if sender:
|
|
||||||
chat_history_df = chat_history_df[chat_history_df["sender_type"] == sender]
|
|
||||||
if sender_name:
|
|
||||||
chat_history_df = chat_history_df[chat_history_df["sender_name"] == sender_name]
|
|
||||||
# Sort the df
|
|
||||||
chat_history_df = chat_history_df.sort_values(by="timestamp")
|
|
||||||
# Get the last n messages
|
|
||||||
if n_messages:
|
|
||||||
chat_history_df = chat_history_df.tail(n_messages)
|
|
||||||
# Create a list of messages
|
|
||||||
messages = []
|
|
||||||
for _, row in chat_history_df.iterrows():
|
|
||||||
messages.append(Document(page_content=f"{row['sender_name']}: {row['message']}"))
|
|
||||||
# Return the list of messages
|
|
||||||
return messages
|
return messages
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue