Sweep through Document Stores and return a set of candidate documents that are relevant to the query.
Module haystack_experimental.components.retrievers.chat_message_retriever
ChatMessageRetriever
Retrieves chat messages from the underlying ChatMessageStore.
Usage example:
from haystack.dataclasses import ChatMessage
from haystack_experimental.components.retrievers import ChatMessageRetriever
from haystack_experimental.chat_message_stores.in_memory import InMemoryChatMessageStore
messages = [
ChatMessage.from_assistant("Hello, how can I help you?"),
ChatMessage.from_user("Hi, I have a question about Python. What is a Protocol?"),
]
message_store = InMemoryChatMessageStore()
message_store.write_messages(messages)
retriever = ChatMessageRetriever(message_store)
result = retriever.run()
print(result["messages"])
ChatMessageRetriever.__init__
def __init__(message_store: ChatMessageStore, last_k: int = 10)
Create the ChatMessageRetriever component.
Arguments:
message_store
: An instance of a ChatMessageStore.last_k
: The number of last messages to retrieve. Defaults to 10 messages if not specified.
ChatMessageRetriever.to_dict
def to_dict() -> Dict[str, Any]
Serializes the component to a dictionary.
Returns:
Dictionary with serialized data.
ChatMessageRetriever.from_dict
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "ChatMessageRetriever"
Deserializes the component from a dictionary.
Arguments:
data
: The dictionary to deserialize from.
Returns:
The deserialized component.
ChatMessageRetriever.run
@component.output_types(messages=List[ChatMessage])
def run(last_k: Optional[int] = None)
Run the ChatMessageRetriever
Arguments:
last_k
: The number of last messages to retrieve. This parameter takes precedence over the last_k parameter passed to the ChatMessageRetriever constructor. If unspecified, the last_k parameter passed to the constructor will be used.
Raises:
ValueError
: If last_k is not None and is less than 1
Returns:
messages
- The retrieved chat messages.