了解history的结构
使大模型融入history,通过浏览下面swift库中的文件可以发现,在编码时,会将query加入到history的末尾,并将query置为空,如果history不传值就会默认为None;所以为了能够利用history,我们只需要传入history的值即可。
def encode(self, example: Dict[str, Any]) -> Tuple[Dict[str, Any], Dict[str, Any]]:
"""return: inputs, tokenizer_kwargs"""
if not self._is_init:
raise ValueError(
'Template is not initialized, please use the `get_template` function to obtain the template.')
query: Optional[str] = example.get('query', None)
response: Optional[str] = example.get('response', None)
history: Optional[History] = example.get('history', None)
system: Optional[str] = example.get('system', None)
if history is None:
history = []
if len(history) > 0:
assert self.support_multi_round, 'The template does not support multi-round chat.'
if system is None:
if self.use_default_system:
system = self.default_system
elif system == '':
system = None
else:
assert self.prefix_has_system is not None, 'The template does not support `system`.'
if query is None:
query = ''
inputs, tokenizer_kwargs = self._encode(
query, response, history, system, self.truncation_strategy, auto_add_bos=self.auto_add_bos)
if inputs.get('labels') is None:
inputs.pop('loss_scale', None)
return inputs, tokenizer_kwargs
def _encode(self,
query: str,
response: Optional[str],
history: History,
system: Optional[str],
truncation_strategy: str,
auto_add_bos: bool = False) -> Tuple[Dict[str, Any], Dict[str, Any]]:
"""
return: inputs, tokenizer_kwargs
"""
history = history.copy()
res_context_list: List[Context] = []
loss_scale_list: List[float] = []
if auto_add_bos:
bos_token_id = self.tokenizer.bos_token_id
if isinstance(bos_token_id, int) and bos_token_id in self.tokenizer.encode(''):
res_context_list.append([bos_token_id])
loss_scale_list.append(0.)
if system is None:
prefix = self.prefix
else:
prefix = self.prefix_has_system
self._concat_context_list(prefix, res_context_list, loss_scale_list, system=system)
history.append([query, response])
for i, (q, r) in enumerate(history):
context_list = self.prompt.copy()
if i < len(history) - 1:
context_list.append('{{RESPONSE}}')
context_list += self.chat_sep
elif r is not None:
# last response
context_list.append('{{RESPONSE}}')
context_list += self.suffix
if q or r:
self._concat_context_list(
context_list, res_context_list, loss_scale_list, query=q, response=r, round0=i)