Skip to content

Retrieve

labridge.func_modules.memory.chat.retrieve

labridge.func_modules.memory.chat.retrieve.ChatMemoryRetriever

Bases: LogBaseRetriever

This is a retriever that retrieve in the permanent chat history of a user or a chat group. You can use this tool when you want to obtain the historical interaction between you and the user.

PARAMETER DESCRIPTION
embed_model

The used embedding model, if not specified, will use the Settings.embed_model

TYPE: BaseEmbedding DEFAULT: None

final_use_context

Whether to add the context nodes of the retrieved log nodes to the final results. Defaults to True.

TYPE: bool DEFAULT: True

relevant_top_k

The top-k relevant nodes in retrieving will be used as the retrieved results. Defaults to CHAT_MEMORY_RELEVANT_TOP_K.

TYPE: int DEFAULT: CHAT_MEMORY_RELEVANT_TOP_K

Source code in labridge\func_modules\memory\chat\retrieve.py
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class ChatMemoryRetriever(LogBaseRetriever):
	r"""
	This is a retriever that retrieve in the permanent chat history of a user or a chat group.
	You can use this tool when you want to obtain the historical interaction between you and the user.

	Args:
		embed_model (BaseEmbedding): The used embedding model, if not specified, will use the `Settings.embed_model`
		final_use_context (bool): Whether to add the context nodes of the retrieved log nodes to the final results.
			Defaults to True.
		relevant_top_k (int): The top-k relevant nodes in retrieving will be used as the retrieved results.
			Defaults to `CHAT_MEMORY_RELEVANT_TOP_K`.
	"""
	def __init__(
		self,
		embed_model: BaseEmbedding = None,
		final_use_context: bool = True,
		relevant_top_k: int = CHAT_MEMORY_RELEVANT_TOP_K
	):
		super().__init__(
			embed_model=embed_model,
			final_use_context=final_use_context,
			relevant_top_k=relevant_top_k,
		)

	def get_memory_vector_retriever(self) -> VectorIndexRetriever:
		memory_retriever = self.memory.vector_index.as_retriever(
			similarity_top_k=self.relevant_top_k,
			filters=None,
		)
		return memory_retriever

	def reset_vector_retriever(self):
		self.memory_vector_retriever._filters = None
		self.memory_vector_retriever._node_ids = None

	def get_memory_vector_index(self) -> VectorStoreIndex:
		return self.memory.vector_index

	@dispatcher.span
	def retrieve(
		self,
		item_to_be_retrieved: str,
		memory_id: str,
		start_date: str = None,
		end_date: str = None,
		**kwargs: Any,
	) -> List[NodeWithScore]:
		r"""
		This tool is used to retrieve relevant chat history in a certain chat history memory.
		The memory_id of a chat history memory is the `user_id` of a specific user or the `chat_group_id` of a specific
		chat group.

		Additionally, you can provide the `start_date` and `end_state` to limit the retrieving range of date,
		The end date can be the same as the start date, but should not be earlier than the start date.
		If the start date or end_date is not provided, retrieving will be performed among the whole memory.

		Args:
			item_to_be_retrieved (str): Things that you want to retrieve in the chat history memory.
			memory_id (str): The memory_id of a chat history memory. It is either a `user_id` or a `chat_group_id`.
			start_date (str): The START date of the retrieving date limit. Defaults to None.
				If given, it should be given in the following FORMAT: Year-Month-Day.
				For example, 2020-12-1 means the year 2020, the 12th month, the 1rst day.
			end_date (str): The END date of the retrieving date limit. Defaults to None.
				If given, It should be given in the following FORMAT: Year-Month-Day.
				For example, 2024-6-2 means the year 2024, the 6th month, the 2nd day.

		Returns:
			Retrieved chat history.
		"""
		# This docstring is used as the tool description.
		# set self.memory_index according to the user_id.
		if self.memory is None or self.memory.memory_id != memory_id:
			self.memory = ChatVectorMemory.from_memory_id(memory_id=memory_id, embed_model=self.embed_model,
				retriever_kwargs={}, )
			self.memory_vector_retriever = self.get_memory_vector_retriever()

		# get the candidate date list.
		date_list = self._parse_date(start_date_str=start_date, end_date_str=end_date)
		metadata_filters =  MetadataFilters(
			filters=[
				self.get_date_filter(date_list=date_list),
			]
		)
		self.memory_vector_retriever._filters = metadata_filters
		chat_nodes = self.memory_vector_retriever.retrieve(item_to_be_retrieved)
		self.reset_vector_retriever()
		# get the results, add prev node and next node to it (if in a same date.).
		if self.final_use_context:
			chat_nodes = self._add_context(content_nodes=chat_nodes)
		return chat_nodes

	@dispatcher.span
	async def aretrieve(
		self,
		item_to_be_retrieved: str,
		memory_id: str,
		start_date: str = None,
		end_date: str = None,
		**kwargs: Any,
	) -> List[NodeWithScore]:
		r"""
		This method is used to asynchronously retrieve relevant chat history in a certain chat history memory.
		The memory_id of a chat history memory is the `user_id` of a specific user or the `chat_group_id` of a specific
		chat group.

		Additionally, you can provide the `start_date` and `end_state` to limit the retrieving range of date,
		The end date should not be earlier than the start date.
		If the start date or end_date is not provided, retrieving will be performed among the whole memory.

		Args:
			item_to_be_retrieved (str): Things that you want to retrieve in the chat history memory.
			memory_id (str): The memory_id of a chat history memory. It is either a `user_id` or a `chat_group_id`.
			start_date (str): The START date of the retrieving date limit. Defaults to None.
				If given, it should be given in the following FORMAT: Year-Month-Day.
				For example, 2020-12-1 means the year 2020, the 12th month, the 1rst day.
			end_date (str): The END date of the retrieving date limit. Defaults to None.
				If given, it should be given in the following FORMAT: Year-Month-Day.
				For example, 2024-6-2 means the year 2024, the 6th month, the 2nd day.

		Returns:
			Retrieved chat history.
		"""
		# set self.memory_index according to the user_id.
		if self.memory is None or self.memory.memory_id != memory_id:
			self.memory = ChatVectorMemory.from_memory_id(
				memory_id=memory_id,
				embed_model=self.embed_model,
				retriever_kwargs={},
			)
			self.memory_vector_retriever = self.get_memory_vector_retriever()

		# get the candidate date list.
		date_list = self._parse_date(start_date_str=start_date, end_date_str=end_date)
		metadata_filters = MetadataFilters(
			filters=[
				self.get_date_filter(date_list=date_list),
			]
		)
		self.memory_vector_retriever._filters = metadata_filters
		chat_nodes = await self.memory_vector_retriever.aretrieve(item_to_be_retrieved)
		# get the results, add prev node and next node to it (if in a same date.).
		if self.final_use_context:
			chat_nodes = self._add_context(content_nodes=chat_nodes)
		return chat_nodes

labridge.func_modules.memory.chat.retrieve.ChatMemoryRetriever.aretrieve(item_to_be_retrieved, memory_id, start_date=None, end_date=None, **kwargs) async

This method is used to asynchronously retrieve relevant chat history in a certain chat history memory. The memory_id of a chat history memory is the user_id of a specific user or the chat_group_id of a specific chat group.

Additionally, you can provide the start_date and end_state to limit the retrieving range of date, The end date should not be earlier than the start date. If the start date or end_date is not provided, retrieving will be performed among the whole memory.

PARAMETER DESCRIPTION
item_to_be_retrieved

Things that you want to retrieve in the chat history memory.

TYPE: str

memory_id

The memory_id of a chat history memory. It is either a user_id or a chat_group_id.

TYPE: str

start_date

The START date of the retrieving date limit. Defaults to None. If given, it should be given in the following FORMAT: Year-Month-Day. For example, 2020-12-1 means the year 2020, the 12th month, the 1rst day.

TYPE: str DEFAULT: None

end_date

The END date of the retrieving date limit. Defaults to None. If given, it should be given in the following FORMAT: Year-Month-Day. For example, 2024-6-2 means the year 2024, the 6th month, the 2nd day.

TYPE: str DEFAULT: None

RETURNS DESCRIPTION
List[NodeWithScore]

Retrieved chat history.

Source code in labridge\func_modules\memory\chat\retrieve.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
@dispatcher.span
async def aretrieve(
	self,
	item_to_be_retrieved: str,
	memory_id: str,
	start_date: str = None,
	end_date: str = None,
	**kwargs: Any,
) -> List[NodeWithScore]:
	r"""
	This method is used to asynchronously retrieve relevant chat history in a certain chat history memory.
	The memory_id of a chat history memory is the `user_id` of a specific user or the `chat_group_id` of a specific
	chat group.

	Additionally, you can provide the `start_date` and `end_state` to limit the retrieving range of date,
	The end date should not be earlier than the start date.
	If the start date or end_date is not provided, retrieving will be performed among the whole memory.

	Args:
		item_to_be_retrieved (str): Things that you want to retrieve in the chat history memory.
		memory_id (str): The memory_id of a chat history memory. It is either a `user_id` or a `chat_group_id`.
		start_date (str): The START date of the retrieving date limit. Defaults to None.
			If given, it should be given in the following FORMAT: Year-Month-Day.
			For example, 2020-12-1 means the year 2020, the 12th month, the 1rst day.
		end_date (str): The END date of the retrieving date limit. Defaults to None.
			If given, it should be given in the following FORMAT: Year-Month-Day.
			For example, 2024-6-2 means the year 2024, the 6th month, the 2nd day.

	Returns:
		Retrieved chat history.
	"""
	# set self.memory_index according to the user_id.
	if self.memory is None or self.memory.memory_id != memory_id:
		self.memory = ChatVectorMemory.from_memory_id(
			memory_id=memory_id,
			embed_model=self.embed_model,
			retriever_kwargs={},
		)
		self.memory_vector_retriever = self.get_memory_vector_retriever()

	# get the candidate date list.
	date_list = self._parse_date(start_date_str=start_date, end_date_str=end_date)
	metadata_filters = MetadataFilters(
		filters=[
			self.get_date_filter(date_list=date_list),
		]
	)
	self.memory_vector_retriever._filters = metadata_filters
	chat_nodes = await self.memory_vector_retriever.aretrieve(item_to_be_retrieved)
	# get the results, add prev node and next node to it (if in a same date.).
	if self.final_use_context:
		chat_nodes = self._add_context(content_nodes=chat_nodes)
	return chat_nodes

labridge.func_modules.memory.chat.retrieve.ChatMemoryRetriever.retrieve(item_to_be_retrieved, memory_id, start_date=None, end_date=None, **kwargs)

This tool is used to retrieve relevant chat history in a certain chat history memory. The memory_id of a chat history memory is the user_id of a specific user or the chat_group_id of a specific chat group.

Additionally, you can provide the start_date and end_state to limit the retrieving range of date, The end date can be the same as the start date, but should not be earlier than the start date. If the start date or end_date is not provided, retrieving will be performed among the whole memory.

PARAMETER DESCRIPTION
item_to_be_retrieved

Things that you want to retrieve in the chat history memory.

TYPE: str

memory_id

The memory_id of a chat history memory. It is either a user_id or a chat_group_id.

TYPE: str

start_date

The START date of the retrieving date limit. Defaults to None. If given, it should be given in the following FORMAT: Year-Month-Day. For example, 2020-12-1 means the year 2020, the 12th month, the 1rst day.

TYPE: str DEFAULT: None

end_date

The END date of the retrieving date limit. Defaults to None. If given, It should be given in the following FORMAT: Year-Month-Day. For example, 2024-6-2 means the year 2024, the 6th month, the 2nd day.

TYPE: str DEFAULT: None

RETURNS DESCRIPTION
List[NodeWithScore]

Retrieved chat history.

Source code in labridge\func_modules\memory\chat\retrieve.py
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
@dispatcher.span
def retrieve(
	self,
	item_to_be_retrieved: str,
	memory_id: str,
	start_date: str = None,
	end_date: str = None,
	**kwargs: Any,
) -> List[NodeWithScore]:
	r"""
	This tool is used to retrieve relevant chat history in a certain chat history memory.
	The memory_id of a chat history memory is the `user_id` of a specific user or the `chat_group_id` of a specific
	chat group.

	Additionally, you can provide the `start_date` and `end_state` to limit the retrieving range of date,
	The end date can be the same as the start date, but should not be earlier than the start date.
	If the start date or end_date is not provided, retrieving will be performed among the whole memory.

	Args:
		item_to_be_retrieved (str): Things that you want to retrieve in the chat history memory.
		memory_id (str): The memory_id of a chat history memory. It is either a `user_id` or a `chat_group_id`.
		start_date (str): The START date of the retrieving date limit. Defaults to None.
			If given, it should be given in the following FORMAT: Year-Month-Day.
			For example, 2020-12-1 means the year 2020, the 12th month, the 1rst day.
		end_date (str): The END date of the retrieving date limit. Defaults to None.
			If given, It should be given in the following FORMAT: Year-Month-Day.
			For example, 2024-6-2 means the year 2024, the 6th month, the 2nd day.

	Returns:
		Retrieved chat history.
	"""
	# This docstring is used as the tool description.
	# set self.memory_index according to the user_id.
	if self.memory is None or self.memory.memory_id != memory_id:
		self.memory = ChatVectorMemory.from_memory_id(memory_id=memory_id, embed_model=self.embed_model,
			retriever_kwargs={}, )
		self.memory_vector_retriever = self.get_memory_vector_retriever()

	# get the candidate date list.
	date_list = self._parse_date(start_date_str=start_date, end_date_str=end_date)
	metadata_filters =  MetadataFilters(
		filters=[
			self.get_date_filter(date_list=date_list),
		]
	)
	self.memory_vector_retriever._filters = metadata_filters
	chat_nodes = self.memory_vector_retriever.retrieve(item_to_be_retrieved)
	self.reset_vector_retriever()
	# get the results, add prev node and next node to it (if in a same date.).
	if self.final_use_context:
		chat_nodes = self._add_context(content_nodes=chat_nodes)
	return chat_nodes