跳转至

Instrument retriever

labridge.func_modules.instrument.retrieve.instrument_retriever

labridge.func_modules.instrument.retrieve.instrument_retriever.InstrumentRetriever

This is a retriever retrieving in the instrument docs. Hybrid retrieving is used.

PARAMETER DESCRIPTION
llm

The used large language model.

TYPE: LLM DEFAULT: None

embed_model

The used embedding model.

TYPE: BaseEmbedding DEFAULT: None

similarity_top_k

When retrieving in the vector store, the top-k relevant nodes will be selected.

TYPE: int DEFAULT: 3

instrument_top_k

When choosing among the instruments based on their descriptions, the top-k instruments will be used.

TYPE: int DEFAULT: 2

final_top_k

Finally, retrieving is conducted among the nodes belong to the corresponding instruments that are chose in the former content-based retrieving and instrument selection. The top-k nodes will be used as the finally retrieved nodes.

TYPE: int DEFAULT: 3

Source code in labridge\func_modules\instrument\retrieve\instrument_retriever.py
 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
class InstrumentRetriever:
	r"""
	This is a retriever retrieving in the instrument docs.
	Hybrid retrieving is used.

	Args:
		llm (LLM): The used large language model.
		embed_model (BaseEmbedding): The used embedding model.
		similarity_top_k (int): When retrieving in the vector store, the top-k relevant nodes will be selected.
		instrument_top_k (int): When choosing among the instruments based on their descriptions, the top-k instruments
			will be used.
		final_top_k (int): Finally, retrieving is conducted among the nodes belong to the corresponding instruments
			that are chose in the former content-based retrieving and instrument selection. The top-k nodes will be
			used as the finally retrieved nodes.
	"""
	def __init__(
		self,
		llm: LLM = None,
		embed_model: BaseEmbedding = None,
		similarity_top_k: int = 3,
		instrument_top_k: int = 2,
		final_top_k: int = 3,
		choice_batch_size: int = 8,
	):
		self.llm = llm or Settings.llm
		embed_model = embed_model or Settings.embed_model
		self.instrument_store = InstrumentStorage.from_default(embed_model=embed_model)
		self.vector_index_retriever = self.instrument_store.vector_index.as_retriever(
			similarity_top_k=similarity_top_k,
		)
		self._similarity_top_k = similarity_top_k
		self._instrument_top_k = instrument_top_k
		self._choice_batch_size = choice_batch_size
		self._final_top_k = final_top_k
		self._format_node_batch_fn = format_instrument_node_batch_fn
		self._choice_select_prompt = INSTRUMENT_CHOICE_SELECT_PROMPT
		self._parse_choice_select_answer_fn = default_parse_choice_select_answer_fn

	def _retrieve_proper_instrument(self, retrieve_items: str) -> List[str]:
		r"""
		Use LLM to select the proper instruments based on their description.

		Args:
			retrieve_items (str): The things to be retrieved.

		Returns:
			List[str]:
				The retrieved node_ids.
		"""
		instrument_ids = self.instrument_store.get_all_instruments()
		dsc_nodes = self.instrument_store.get_nodes(node_ids=instrument_ids)

		all_nodes: List[BaseNode] = []
		all_relevances: List[float] = []

		for idx in range(0, len(dsc_nodes), self._choice_batch_size):
			nodes = dsc_nodes[idx: idx + self._choice_batch_size]
			fmt_batch_str = self._format_node_batch_fn(nodes)
			llm_response = self.llm.predict(
				self._choice_select_prompt,
				context_str=fmt_batch_str,
				query_str=retrieve_items,
			)

			answer_lines = llm_response.split("\n")
			valid_lines = []
			for answer_line in answer_lines:
				if len(answer_line) > 4:
					valid_lines.append(answer_line.strip())
			valid_response = "\n".join(valid_lines)

			choices, relevances = self._parse_choice_select_answer_fn(valid_response, len(nodes), raise_error=True)
			choice_indices = [c - 1 for c in choices]

			choice_instruments = [nodes[ci] for ci in choice_indices]

			all_nodes.extend(choice_instruments)
			all_relevances.extend(relevances)

		zipped_list = list(zip(all_nodes, all_relevances))
		sorted_list = sorted(zipped_list, key=lambda x: x[1], reverse=True)
		top_k_list = sorted_list[: self._instrument_top_k]

		select_instrument_ids = [node.node_id for node, relevance in top_k_list]
		return select_instrument_ids

	async def _aretrieve_proper_instrument(self, retrieve_items: str):
		r"""
		Asynchronously select the proper instruments based on their description.

		Args:
			retrieve_items (str): The things to be retrieved.

		Returns:
			List[str]:
				The retrieved node_ids.
		"""
		instrument_ids = self.instrument_store.get_all_instruments()
		dsc_nodes = self.instrument_store.get_nodes(node_ids=instrument_ids)

		all_nodes: List[BaseNode] = []
		all_relevances: List[float] = []

		for idx in range(0, len(dsc_nodes), self._choice_batch_size):
			nodes = dsc_nodes[idx: idx + self._choice_batch_size]
			fmt_batch_str = self._format_node_batch_fn(nodes)
			llm_response = await self.llm.apredict(
				self._choice_select_prompt,
				context_str=fmt_batch_str,
				query_str=retrieve_items,
			)
			choices, relevances = self._parse_choice_select_answer_fn(llm_response, len(nodes))
			choice_indices = [c - 1 for c in choices]

			choice_instruments = [nodes[ci] for ci in choice_indices]

			all_nodes.extend(choice_instruments)
			all_relevances.extend(relevances)

		zipped_list = list(zip(all_nodes, all_relevances))
		sorted_list = sorted(zipped_list, key=lambda x: x[1], reverse=True)
		top_k_list = sorted_list[: self._instrument_top_k]

		select_instrument_ids = [node.node_id for node, relevance in top_k_list]
		return select_instrument_ids

	def set_retriever_top_k(self, similarity_top_k: int):
		r""" Set the top-k of the first content-based retrieving. """
		self.vector_index_retriever._similarity_top_k = similarity_top_k

	def set_retriever_node_ids(self, node_ids: Optional[List[str]] = None):
		r""" Confine the range of node_ids in retrieving. """
		self.vector_index_retriever._node_ids = node_ids

	def _retrieve_instrument_content_based(self, retrieve_items: str) -> List[str]:
		r"""
		Content-based retrieving.

		Args:
			retrieve_items (str): Item to be retrieved.

		Returns:
			List[str]: The ids of the instruments that the retrieved docs belong to.
		"""
		self.set_retriever_top_k(self._similarity_top_k)
		self.set_retriever_node_ids()
		content_nodes = self.vector_index_retriever.retrieve(retrieve_items)

		instrument_ids = set()
		# TODO: To be modified
		for node in content_nodes:
			instrument_node = node.node.parent_node
			if instrument_node is not None:
				instrument_ids.add(instrument_node.node_id)
			else:
				instrument_ids.add(node.node_id)
		return list(instrument_ids)

	async def _aretrieve_instrument_content_based(self, retrieve_items: str) -> List[str]:
		r"""
		Asynchronously content-based retrieving.

		Args:
			retrieve_items (str): Item to be retrieved.

		Returns:
			List[str]: The ids of the instruments that the retrieved docs belong to.
		"""
		self.set_retriever_top_k(self._similarity_top_k)
		self.set_retriever_node_ids()
		content_nodes = await self.vector_index_retriever.aretrieve(retrieve_items)

		instrument_ids = set()
		# TODO: To be modified
		for node in content_nodes:
			instrument_node = node.node.parent_node
			if instrument_node is not None:
				instrument_ids.add(instrument_node.node_id)
			else:
				instrument_ids.add(node.node_id)
		return list(instrument_ids)

	@dispatcher.span
	def retrieve(
		self,
		item_to_be_retrieved: str,
	) -> List[NodeWithScore]:
		r"""
		This tool is used to help the laboratory member to solve their encountered difficulties in their experiment,
		by retrieving in the documents of the lab's scientific instruments.

		you could use this tool to suggest proper instruments to help him/her to overcome the difficulties,
		and provide comprehensive information about these instruments from the instrument documents
		including instruction manuals, operation specifications of scientific instruments.

		Args:
			item_to_be_retrieved (str): The string to be retrieved relevant to the scientific instruments

		Returns:
			List[NodeWithScore]: The retrieved nodes.
				The contents of these retrieved nodes will be presented as the output.
		"""
		# This docstring will be used as the tool description.
		dsc_instruments = self._retrieve_proper_instrument(retrieve_items=item_to_be_retrieved)

		# content_instruments = self._retrieve_instrument_content_based(retrieve_items=item_to_be_retrieved)
		# instrument_ids = list(set(dsc_instruments + content_instruments))

		instrument_ids = dsc_instruments

		instruments = self.instrument_store.get_nodes(node_ids=instrument_ids)
		retrieve_range = []

		retrieve_range.extend(instrument_ids)
		for ins in instruments:
			doc_nodes = ins.child_nodes
			if doc_nodes is not None:
				retrieve_range.extend([node.node_id for node in doc_nodes])

		self.set_retriever_top_k(self._final_top_k)
		self.set_retriever_node_ids(node_ids=retrieve_range)
		retrieved_nodes = self.vector_index_retriever.retrieve(item_to_be_retrieved)
		final_nodes = [NodeWithScore(node=n) for n in instruments] + retrieved_nodes
		return final_nodes

	async def aretrieve(
		self,
		item_to_be_retrieved: str,
	) -> List[NodeWithScore]:
		r"""
		This tool is used to retrieve in the documents of the lab's scientific instruments.
		These documents include the instruction manuals, operation specifications of scientific instruments.

		Args:
			item_to_be_retrieved (str): The string to be retrieved relevant to the scientific instruments

		Returns:
			List[NodeWithScore]: The retrieved nodes.
				The contents of these retrieved nodes will be presented as the output.
		"""
		# This docstring will be used as the tool description.
		dsc_instruments = await self._aretrieve_proper_instrument(retrieve_items=item_to_be_retrieved)
		# content_instruments = await self._aretrieve_instrument_content_based(retrieve_items=item_to_be_retrieved)
		# instrument_ids = list(set(dsc_instruments + content_instruments))

		instrument_ids = dsc_instruments

		instruments = self.instrument_store.get_nodes(node_ids=instrument_ids)
		retrieve_range = []

		retrieve_range.extend(instrument_ids)
		for ins in instruments:
			doc_nodes = ins.child_nodes
			if doc_nodes is not None:
				retrieve_range.extend([node.node_id for node in doc_nodes])

		self.set_retriever_top_k(self._final_top_k)
		self.set_retriever_node_ids(node_ids=retrieve_range)
		retrieved_nodes = await self.vector_index_retriever.aretrieve(item_to_be_retrieved)
		final_nodes = [NodeWithScore(node=n) for n in instruments] + retrieved_nodes
		return final_nodes

labridge.func_modules.instrument.retrieve.instrument_retriever.InstrumentRetriever.aretrieve(item_to_be_retrieved) async

This tool is used to retrieve in the documents of the lab's scientific instruments. These documents include the instruction manuals, operation specifications of scientific instruments.

PARAMETER DESCRIPTION
item_to_be_retrieved

The string to be retrieved relevant to the scientific instruments

TYPE: str

RETURNS DESCRIPTION
List[NodeWithScore]

List[NodeWithScore]: The retrieved nodes. The contents of these retrieved nodes will be presented as the output.

Source code in labridge\func_modules\instrument\retrieve\instrument_retriever.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
async def aretrieve(
	self,
	item_to_be_retrieved: str,
) -> List[NodeWithScore]:
	r"""
	This tool is used to retrieve in the documents of the lab's scientific instruments.
	These documents include the instruction manuals, operation specifications of scientific instruments.

	Args:
		item_to_be_retrieved (str): The string to be retrieved relevant to the scientific instruments

	Returns:
		List[NodeWithScore]: The retrieved nodes.
			The contents of these retrieved nodes will be presented as the output.
	"""
	# This docstring will be used as the tool description.
	dsc_instruments = await self._aretrieve_proper_instrument(retrieve_items=item_to_be_retrieved)
	# content_instruments = await self._aretrieve_instrument_content_based(retrieve_items=item_to_be_retrieved)
	# instrument_ids = list(set(dsc_instruments + content_instruments))

	instrument_ids = dsc_instruments

	instruments = self.instrument_store.get_nodes(node_ids=instrument_ids)
	retrieve_range = []

	retrieve_range.extend(instrument_ids)
	for ins in instruments:
		doc_nodes = ins.child_nodes
		if doc_nodes is not None:
			retrieve_range.extend([node.node_id for node in doc_nodes])

	self.set_retriever_top_k(self._final_top_k)
	self.set_retriever_node_ids(node_ids=retrieve_range)
	retrieved_nodes = await self.vector_index_retriever.aretrieve(item_to_be_retrieved)
	final_nodes = [NodeWithScore(node=n) for n in instruments] + retrieved_nodes
	return final_nodes

labridge.func_modules.instrument.retrieve.instrument_retriever.InstrumentRetriever.retrieve(item_to_be_retrieved)

This tool is used to help the laboratory member to solve their encountered difficulties in their experiment, by retrieving in the documents of the lab's scientific instruments.

you could use this tool to suggest proper instruments to help him/her to overcome the difficulties, and provide comprehensive information about these instruments from the instrument documents including instruction manuals, operation specifications of scientific instruments.

PARAMETER DESCRIPTION
item_to_be_retrieved

The string to be retrieved relevant to the scientific instruments

TYPE: str

RETURNS DESCRIPTION
List[NodeWithScore]

List[NodeWithScore]: The retrieved nodes. The contents of these retrieved nodes will be presented as the output.

Source code in labridge\func_modules\instrument\retrieve\instrument_retriever.py
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
@dispatcher.span
def retrieve(
	self,
	item_to_be_retrieved: str,
) -> List[NodeWithScore]:
	r"""
	This tool is used to help the laboratory member to solve their encountered difficulties in their experiment,
	by retrieving in the documents of the lab's scientific instruments.

	you could use this tool to suggest proper instruments to help him/her to overcome the difficulties,
	and provide comprehensive information about these instruments from the instrument documents
	including instruction manuals, operation specifications of scientific instruments.

	Args:
		item_to_be_retrieved (str): The string to be retrieved relevant to the scientific instruments

	Returns:
		List[NodeWithScore]: The retrieved nodes.
			The contents of these retrieved nodes will be presented as the output.
	"""
	# This docstring will be used as the tool description.
	dsc_instruments = self._retrieve_proper_instrument(retrieve_items=item_to_be_retrieved)

	# content_instruments = self._retrieve_instrument_content_based(retrieve_items=item_to_be_retrieved)
	# instrument_ids = list(set(dsc_instruments + content_instruments))

	instrument_ids = dsc_instruments

	instruments = self.instrument_store.get_nodes(node_ids=instrument_ids)
	retrieve_range = []

	retrieve_range.extend(instrument_ids)
	for ins in instruments:
		doc_nodes = ins.child_nodes
		if doc_nodes is not None:
			retrieve_range.extend([node.node_id for node in doc_nodes])

	self.set_retriever_top_k(self._final_top_k)
	self.set_retriever_node_ids(node_ids=retrieve_range)
	retrieved_nodes = self.vector_index_retriever.retrieve(item_to_be_retrieved)
	final_nodes = [NodeWithScore(node=n) for n in instruments] + retrieved_nodes
	return final_nodes

labridge.func_modules.instrument.retrieve.instrument_retriever.InstrumentRetriever.set_retriever_node_ids(node_ids=None)

Confine the range of node_ids in retrieving.

Source code in labridge\func_modules\instrument\retrieve\instrument_retriever.py
175
176
177
def set_retriever_node_ids(self, node_ids: Optional[List[str]] = None):
	r""" Confine the range of node_ids in retrieving. """
	self.vector_index_retriever._node_ids = node_ids

labridge.func_modules.instrument.retrieve.instrument_retriever.InstrumentRetriever.set_retriever_top_k(similarity_top_k)

Set the top-k of the first content-based retrieving.

Source code in labridge\func_modules\instrument\retrieve\instrument_retriever.py
171
172
173
def set_retriever_top_k(self, similarity_top_k: int):
	r""" Set the top-k of the first content-based retrieving. """
	self.vector_index_retriever._similarity_top_k = similarity_top_k

labridge.func_modules.instrument.retrieve.instrument_retriever.format_instrument_node_batch_fn(instrument_nodes)

This function returns a text containing the indices and descriptions of a batch of instruments. LLM will select from these instruments according to this text.

PARAMETER DESCRIPTION
instrument_nodes

The nodes stored in the InstrumentStorage, containing the instrument name and description.

TYPE: List[BaseNode]

RETURNS DESCRIPTION
str

The generated text.

TYPE: str

Source code in labridge\func_modules\instrument\retrieve\instrument_retriever.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def format_instrument_node_batch_fn(instrument_nodes: List[BaseNode]) -> str:
	r"""
	This function returns a text containing the indices and descriptions of a batch of instruments.
	LLM will select from these instruments according to this text.

	Args:
		instrument_nodes (List[BaseNode]): The nodes stored in the `InstrumentStorage`,
			containing the instrument name and description.

	Returns:
		str: The generated text.
	"""
	texts = []
	for idx in range(len(instrument_nodes)):
		number = idx + 1
		texts.append(
			f"Instrument {number}:\n"
			f"{instrument_nodes[idx].get_content(metadata_mode=MetadataMode.LLM)}"
		)
	return "\n\n".join(texts)