跳转至

Select info

labridge.interact.collect.types.select_info

labridge.interact.collect.types.select_info.CollectingSelectInfo

Bases: CollectingInfoBase

This class defines the select information to be collected from the user. The select information should be selected between several given choices.

PARAMETER DESCRIPTION
info_name

The name of the information to be collected.

TYPE: str

info_description

The description of the information to be collected.

TYPE: str

choices

The given choices.

  • key (str): The choice.
  • value (str): The description of the choice.

TYPE: Dict[str, str]

Source code in labridge\interact\collect\types\select_info.py
 20
 21
 22
 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
class CollectingSelectInfo(CollectingInfoBase):
	r"""
	This class defines the select information to be collected from the user.
	The select information should be selected between several given choices.

	Args:
		info_name (str): The name of the information to be collected.
		info_description (str): The description of the information to be collected.
		choices (Dict[str, str]):
			The given choices.

			- key (str): The choice.
			- value (str): The description of the choice.
	"""
	def __init__(
		self,
		info_name: str,
		info_description: str,
		choices: Dict[str, str],
	):
		self._choices = choices
		super().__init__(
			info_name=info_name,
			info_description=info_description,
			info_type=CollectingInfoType.SELECT,
			batch_mode=False,
		)

	def _collected(self) -> bool:
		r""" Whether all information is collected. """
		return self.info_name in self._collected_infos.keys()

	def update_collected_info(self, collected_info_dict: Dict[str, str]):
		r""" Update the collected information. """
		if self.info_name in collected_info_dict:
			self._collected_infos[self.info_name] = collected_info_dict[self.info_name]

	def _required_infos(self) -> Dict[str, str]:
		r""" Return the required information names and descriptions. """
		return {self.info_name: self.info_description}

	def _collecting_keys(self) -> List[str]:
		r""" Return the information names to be collected currently. """
		if self.collected:
			return []
		return [self.info_name]

	@property
	def candidates(self) -> List[str]:
		r""" Get the candidates """
		return list(self._choices.keys())

	def _extra_info_format(self, choice_keys: List[str]) -> str:
		r""" The prompt format for selecting. """
		contents = []
		for idx, key in enumerate(choice_keys):
			content = f"Paragraph {idx + 1}\n"
			content += f"Name: {key}\nDescription: {self._choices[key]}".strip()
			contents.append(content)
		choices_str = "\n\n".join(contents)
		return choices_str

	def info_content(self) -> Iterator[Tuple[Dict[str, str], List[str]]]:
		r""" Yield the information name, description and corresponding choices to the LLM for selection. """
		required_infos_str = json.dumps({self.info_name: self.info_description})
		candidates = self.candidates
		if not self.collected:
			for idx in range(0, len(candidates), SELECT_CHOICE_BATCH_SIZE):
				extra_info = self._extra_info_format(choice_keys=candidates[idx: idx + SELECT_CHOICE_BATCH_SIZE])
				yield {
					CollectPromptKeys.required_infos_key: required_infos_str,
					CollectPromptKeys.extra_info_key: extra_info,
				}, candidates[idx: idx + SELECT_CHOICE_BATCH_SIZE]

	def modify_info_content(self) -> Iterator[Tuple[Dict[str, str], List[str]]]:
		r""" Yield the information name, description, collected content and corresponding choices to the LLM for modification. """
		required_infos_str = json.dumps({self.info_name: self.info_description})
		candidates = self.candidates
		for idx in range(0, len(candidates), SELECT_CHOICE_BATCH_SIZE):
			extra_info = self._extra_info_format(choice_keys=candidates[idx: idx + SELECT_CHOICE_BATCH_SIZE])
			yield {
				CollectPromptKeys.required_infos_key: required_infos_str,
				ModifyPromptKeys.collected_infos_key: json.dumps(self.collected_infos),
				CollectPromptKeys.extra_info_key: extra_info,
			}, candidates[idx: idx + SELECT_CHOICE_BATCH_SIZE]

labridge.interact.collect.types.select_info.CollectingSelectInfo.candidates: List[str] property

Get the candidates

labridge.interact.collect.types.select_info.CollectingSelectInfo.info_content()

Yield the information name, description and corresponding choices to the LLM for selection.

Source code in labridge\interact\collect\types\select_info.py
82
83
84
85
86
87
88
89
90
91
92
def info_content(self) -> Iterator[Tuple[Dict[str, str], List[str]]]:
	r""" Yield the information name, description and corresponding choices to the LLM for selection. """
	required_infos_str = json.dumps({self.info_name: self.info_description})
	candidates = self.candidates
	if not self.collected:
		for idx in range(0, len(candidates), SELECT_CHOICE_BATCH_SIZE):
			extra_info = self._extra_info_format(choice_keys=candidates[idx: idx + SELECT_CHOICE_BATCH_SIZE])
			yield {
				CollectPromptKeys.required_infos_key: required_infos_str,
				CollectPromptKeys.extra_info_key: extra_info,
			}, candidates[idx: idx + SELECT_CHOICE_BATCH_SIZE]

labridge.interact.collect.types.select_info.CollectingSelectInfo.modify_info_content()

Yield the information name, description, collected content and corresponding choices to the LLM for modification.

Source code in labridge\interact\collect\types\select_info.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def modify_info_content(self) -> Iterator[Tuple[Dict[str, str], List[str]]]:
	r""" Yield the information name, description, collected content and corresponding choices to the LLM for modification. """
	required_infos_str = json.dumps({self.info_name: self.info_description})
	candidates = self.candidates
	for idx in range(0, len(candidates), SELECT_CHOICE_BATCH_SIZE):
		extra_info = self._extra_info_format(choice_keys=candidates[idx: idx + SELECT_CHOICE_BATCH_SIZE])
		yield {
			CollectPromptKeys.required_infos_key: required_infos_str,
			ModifyPromptKeys.collected_infos_key: json.dumps(self.collected_infos),
			CollectPromptKeys.extra_info_key: extra_info,
		}, candidates[idx: idx + SELECT_CHOICE_BATCH_SIZE]

labridge.interact.collect.types.select_info.CollectingSelectInfo.update_collected_info(collected_info_dict)

Update the collected information.

Source code in labridge\interact\collect\types\select_info.py
52
53
54
55
def update_collected_info(self, collected_info_dict: Dict[str, str]):
	r""" Update the collected information. """
	if self.info_name in collected_info_dict:
		self._collected_infos[self.info_name] = collected_info_dict[self.info_name]