Add new IO components for chat input, chat output, message history, and text input
This commit is contained in:
parent
5a7e46ba12
commit
898dfab0ef
5 changed files with 141 additions and 0 deletions
21
src/backend/langflow/components/io/ChatInput.py
Normal file
21
src/backend/langflow/components/io/ChatInput.py
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from langflow import CustomComponent
|
||||||
|
from langflow.field_typing import Text
|
||||||
|
|
||||||
|
|
||||||
|
class ChatInput(CustomComponent):
|
||||||
|
display_name = "Chat Input"
|
||||||
|
description = "Used to get user input from the chat."
|
||||||
|
|
||||||
|
field_config = {
|
||||||
|
"code": {
|
||||||
|
"show": False,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def build(self, message: Optional[str] = None) -> Text:
|
||||||
|
self.repr_value = message
|
||||||
|
if not message:
|
||||||
|
message = ""
|
||||||
|
return message
|
||||||
39
src/backend/langflow/components/io/ChatOutput.py
Normal file
39
src/backend/langflow/components/io/ChatOutput.py
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from langflow import CustomComponent
|
||||||
|
from langflow.field_typing import Text
|
||||||
|
|
||||||
|
|
||||||
|
class ChatOutput(CustomComponent):
|
||||||
|
display_name = "Chat Output"
|
||||||
|
description = "Used to send a message to the chat."
|
||||||
|
|
||||||
|
field_config = {
|
||||||
|
"code": {
|
||||||
|
"show": True,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def build_config(self):
|
||||||
|
return {
|
||||||
|
"message": {"input_types": ["Text"]},
|
||||||
|
"sender": {"options": ["Machine", "User"], "display_name": "Sender Type"},
|
||||||
|
"sender_name": {"display_name": "Sender Name"},
|
||||||
|
"session_id": {
|
||||||
|
"display_name": "Session ID",
|
||||||
|
"info": "Session ID of the chat history.",
|
||||||
|
"input_types": ["Text"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
def build(
|
||||||
|
self,
|
||||||
|
sender: Optional[str] = "Machine",
|
||||||
|
sender_name: Optional[str] = "AI",
|
||||||
|
session_id: Optional[str] = None,
|
||||||
|
message: Optional[str] = None,
|
||||||
|
) -> Text:
|
||||||
|
self.repr_value = message
|
||||||
|
if not message:
|
||||||
|
message = ""
|
||||||
|
return message
|
||||||
59
src/backend/langflow/components/io/MessageHistory.py
Normal file
59
src/backend/langflow/components/io/MessageHistory.py
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
from langchain.schema import Document
|
||||||
|
from langflow import CustomComponent
|
||||||
|
from langflow.services.deps import get_monitor_service
|
||||||
|
|
||||||
|
|
||||||
|
class MessageHistoryComponent(CustomComponent):
|
||||||
|
display_name = "Message History"
|
||||||
|
description = "Used to retrieve stored messages."
|
||||||
|
|
||||||
|
def build_config(self):
|
||||||
|
return {
|
||||||
|
"sender": {"options": ["Machine", "User"], "display_name": "Sender Type"},
|
||||||
|
"sender_name": {"display_name": "Sender Name"},
|
||||||
|
"file_path": {
|
||||||
|
"display_name": "File Path",
|
||||||
|
"info": "Path of the local JSON file to store the messages. It should be a unique path for each chat history.",
|
||||||
|
},
|
||||||
|
"n_messages": {
|
||||||
|
"display_name": "Number of Messages",
|
||||||
|
"info": "Number of messages to retrieve.",
|
||||||
|
},
|
||||||
|
"session_id": {
|
||||||
|
"display_name": "Session ID",
|
||||||
|
"info": "Session ID of the chat history.",
|
||||||
|
"input_types": ["Text"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
def build(
|
||||||
|
self,
|
||||||
|
sender: Optional[str] = None,
|
||||||
|
sender_name: Optional[str] = None,
|
||||||
|
session_id: Optional[str] = None,
|
||||||
|
n_messages: int = 5,
|
||||||
|
) -> List[Document]:
|
||||||
|
# Load the chat history df
|
||||||
|
monitor_service = get_monitor_service()
|
||||||
|
chat_history_df = monitor_service.to_df("messages")
|
||||||
|
|
||||||
|
# Filter the df
|
||||||
|
if session_id:
|
||||||
|
chat_history_df = chat_history_df[chat_history_df["session_id"] == session_id]
|
||||||
|
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
|
||||||
22
src/backend/langflow/components/io/TextInput.py
Normal file
22
src/backend/langflow/components/io/TextInput.py
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from langflow import CustomComponent
|
||||||
|
from langflow.field_typing import Text
|
||||||
|
|
||||||
|
|
||||||
|
class TextInput(CustomComponent):
|
||||||
|
display_name = "Text Input"
|
||||||
|
description = "Used to pass text input to the next component."
|
||||||
|
|
||||||
|
field_config = {
|
||||||
|
"code": {
|
||||||
|
"show": False,
|
||||||
|
},
|
||||||
|
"value": {"display_name": "Value"},
|
||||||
|
}
|
||||||
|
|
||||||
|
def build(self, value: Optional[str] = "") -> Text:
|
||||||
|
self.status = value
|
||||||
|
if not value:
|
||||||
|
value = ""
|
||||||
|
return value
|
||||||
0
src/backend/langflow/components/io/__init__.py
Normal file
0
src/backend/langflow/components/io/__init__.py
Normal file
Loading…
Add table
Add a link
Reference in a new issue