2025-12-03 21:12:43 +01:00
|
|
|
---
|
|
|
|
|
title: "Retrievers"
|
|
|
|
|
id: experimental-retrievers-api
|
|
|
|
|
description: "Sweep through Document Stores and return a set of candidate documents that are relevant to the query."
|
|
|
|
|
slug: "/experimental-retrievers-api"
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
<a id="haystack_experimental.components.retrievers.chat_message_retriever"></a>
|
|
|
|
|
|
|
|
|
|
## Module haystack\_experimental.components.retrievers.chat\_message\_retriever
|
|
|
|
|
|
|
|
|
|
<a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever"></a>
|
|
|
|
|
|
|
|
|
|
### ChatMessageRetriever
|
|
|
|
|
|
|
|
|
|
Retrieves chat messages from the underlying ChatMessageStore.
|
|
|
|
|
|
|
|
|
|
Usage example:
|
|
|
|
|
```python
|
|
|
|
|
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()
|
2025-12-30 14:54:43 +01:00
|
|
|
message_store.write_messages(chat_history_id="user_456_session_123", messages=messages)
|
2025-12-03 21:12:43 +01:00
|
|
|
retriever = ChatMessageRetriever(message_store)
|
|
|
|
|
|
2025-12-30 14:54:43 +01:00
|
|
|
result = retriever.run(chat_history_id="user_456_session_123")
|
2025-12-03 21:12:43 +01:00
|
|
|
|
|
|
|
|
print(result["messages"])
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
<a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever.__init__"></a>
|
|
|
|
|
|
|
|
|
|
#### ChatMessageRetriever.\_\_init\_\_
|
|
|
|
|
|
|
|
|
|
```python
|
2025-12-30 14:54:43 +01:00
|
|
|
def __init__(chat_message_store: ChatMessageStore, last_k: Optional[int] = 10)
|
2025-12-03 21:12:43 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Create the ChatMessageRetriever component.
|
|
|
|
|
|
|
|
|
|
**Arguments**:
|
|
|
|
|
|
2025-12-30 14:54:43 +01:00
|
|
|
- `chat_message_store`: An instance of a ChatMessageStore.
|
2025-12-03 21:12:43 +01:00
|
|
|
- `last_k`: The number of last messages to retrieve. Defaults to 10 messages if not specified.
|
|
|
|
|
|
|
|
|
|
<a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever.to_dict"></a>
|
|
|
|
|
|
|
|
|
|
#### ChatMessageRetriever.to\_dict
|
|
|
|
|
|
|
|
|
|
```python
|
2025-12-30 14:54:43 +01:00
|
|
|
def to_dict() -> dict[str, Any]
|
2025-12-03 21:12:43 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Serializes the component to a dictionary.
|
|
|
|
|
|
|
|
|
|
**Returns**:
|
|
|
|
|
|
|
|
|
|
Dictionary with serialized data.
|
|
|
|
|
|
|
|
|
|
<a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever.from_dict"></a>
|
|
|
|
|
|
|
|
|
|
#### ChatMessageRetriever.from\_dict
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
@classmethod
|
2025-12-30 14:54:43 +01:00
|
|
|
def from_dict(cls, data: dict[str, Any]) -> "ChatMessageRetriever"
|
2025-12-03 21:12:43 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Deserializes the component from a dictionary.
|
|
|
|
|
|
|
|
|
|
**Arguments**:
|
|
|
|
|
|
|
|
|
|
- `data`: The dictionary to deserialize from.
|
|
|
|
|
|
|
|
|
|
**Returns**:
|
|
|
|
|
|
|
|
|
|
The deserialized component.
|
|
|
|
|
|
|
|
|
|
<a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever.run"></a>
|
|
|
|
|
|
|
|
|
|
#### ChatMessageRetriever.run
|
|
|
|
|
|
|
|
|
|
```python
|
2025-12-30 14:54:43 +01:00
|
|
|
@component.output_types(messages=list[ChatMessage])
|
|
|
|
|
def run(
|
|
|
|
|
chat_history_id: str,
|
|
|
|
|
*,
|
|
|
|
|
last_k: Optional[int] = None,
|
|
|
|
|
current_messages: Optional[list[ChatMessage]] = None
|
|
|
|
|
) -> dict[str, list[ChatMessage]]
|
2025-12-03 21:12:43 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Run the ChatMessageRetriever
|
|
|
|
|
|
|
|
|
|
**Arguments**:
|
|
|
|
|
|
2025-12-30 14:54:43 +01:00
|
|
|
- `chat_history_id`: A unique identifier for the chat session or conversation whose messages should be retrieved.
|
|
|
|
|
Each `chat_history_id` corresponds to a distinct chat history stored in the underlying ChatMessageStore.
|
|
|
|
|
For example, use a session ID or conversation ID to isolate messages from different chat sessions.
|
2025-12-03 21:12:43 +01:00
|
|
|
- `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.
|
2025-12-30 14:54:43 +01:00
|
|
|
- `current_messages`: A list of incoming chat messages to combine with the retrieved messages. System messages from this list
|
|
|
|
|
are prepended before the retrieved history, while all other messages (e.g., user messages) are appended
|
|
|
|
|
after. This is useful for including new conversational context alongside stored history so the output
|
|
|
|
|
can be directly used as input to a ChatGenerator or an Agent. If not provided, only the stored messages
|
|
|
|
|
will be returned.
|
2025-12-03 21:12:43 +01:00
|
|
|
|
|
|
|
|
**Raises**:
|
|
|
|
|
|
2025-12-30 14:54:43 +01:00
|
|
|
- `ValueError`: If last_k is not None and is less than 0.
|
2025-12-03 21:12:43 +01:00
|
|
|
|
|
|
|
|
**Returns**:
|
|
|
|
|
|
2025-12-30 14:54:43 +01:00
|
|
|
A dictionary with the following key:
|
|
|
|
|
- `messages` - The retrieved chat messages combined with any provided current messages.
|
2025-12-03 21:12:43 +01:00
|
|
|
|