/ docs-website / reference_versioned_docs / version-2.18 / experiments-api / experimental_retrievers_api.md
experimental_retrievers_api.md
1 --- 2 title: "Retrievers" 3 id: experimental-retrievers-api 4 description: "Sweep through Document Stores and return a set of candidate documents that are relevant to the query." 5 slug: "/experimental-retrievers-api" 6 --- 7 8 <a id="haystack_experimental.components.retrievers.chat_message_retriever"></a> 9 10 ## Module haystack\_experimental.components.retrievers.chat\_message\_retriever 11 12 <a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever"></a> 13 14 ### ChatMessageRetriever 15 16 Retrieves chat messages from the underlying ChatMessageStore. 17 18 Usage example: 19 ```python 20 from haystack.dataclasses import ChatMessage 21 from haystack_experimental.components.retrievers import ChatMessageRetriever 22 from haystack_experimental.chat_message_stores.in_memory import InMemoryChatMessageStore 23 24 messages = [ 25 ChatMessage.from_assistant("Hello, how can I help you?"), 26 ChatMessage.from_user("Hi, I have a question about Python. What is a Protocol?"), 27 ] 28 29 message_store = InMemoryChatMessageStore() 30 message_store.write_messages(chat_history_id="user_456_session_123", messages=messages) 31 retriever = ChatMessageRetriever(message_store) 32 33 result = retriever.run(chat_history_id="user_456_session_123") 34 35 print(result["messages"]) 36 ``` 37 38 <a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever.__init__"></a> 39 40 #### ChatMessageRetriever.\_\_init\_\_ 41 42 ```python 43 def __init__(chat_message_store: ChatMessageStore, last_k: int | None = 10) 44 ``` 45 46 Create the ChatMessageRetriever component. 47 48 **Arguments**: 49 50 - `chat_message_store`: An instance of a ChatMessageStore. 51 - `last_k`: The number of last messages to retrieve. Defaults to 10 messages if not specified. 52 53 <a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever.to_dict"></a> 54 55 #### ChatMessageRetriever.to\_dict 56 57 ```python 58 def to_dict() -> dict[str, Any] 59 ``` 60 61 Serializes the component to a dictionary. 62 63 **Returns**: 64 65 Dictionary with serialized data. 66 67 <a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever.from_dict"></a> 68 69 #### ChatMessageRetriever.from\_dict 70 71 ```python 72 @classmethod 73 def from_dict(cls, data: dict[str, Any]) -> "ChatMessageRetriever" 74 ``` 75 76 Deserializes the component from a dictionary. 77 78 **Arguments**: 79 80 - `data`: The dictionary to deserialize from. 81 82 **Returns**: 83 84 The deserialized component. 85 86 <a id="haystack_experimental.components.retrievers.chat_message_retriever.ChatMessageRetriever.run"></a> 87 88 #### ChatMessageRetriever.run 89 90 ```python 91 @component.output_types(messages=list[ChatMessage]) 92 def run( 93 chat_history_id: str, 94 *, 95 last_k: int | None = None, 96 current_messages: list[ChatMessage] | None = None 97 ) -> dict[str, list[ChatMessage]] 98 ``` 99 100 Run the ChatMessageRetriever 101 102 **Arguments**: 103 104 - `chat_history_id`: A unique identifier for the chat session or conversation whose messages should be retrieved. 105 Each `chat_history_id` corresponds to a distinct chat history stored in the underlying ChatMessageStore. 106 For example, use a session ID or conversation ID to isolate messages from different chat sessions. 107 - `last_k`: The number of last messages to retrieve. This parameter takes precedence over the last_k 108 parameter passed to the ChatMessageRetriever constructor. If unspecified, the last_k parameter passed 109 to the constructor will be used. 110 - `current_messages`: A list of incoming chat messages to combine with the retrieved messages. System messages from this list 111 are prepended before the retrieved history, while all other messages (e.g., user messages) are appended 112 after. This is useful for including new conversational context alongside stored history so the output 113 can be directly used as input to a ChatGenerator or an Agent. If not provided, only the stored messages 114 will be returned. 115 116 **Raises**: 117 118 - `ValueError`: If last_k is not None and is less than 0. 119 120 **Returns**: 121 122 A dictionary with the following key: 123 - `messages` - The retrieved chat messages combined with any provided current messages.