MCPcopy Create free account
hub / github.com/InternScience/SciReason / _split_rounds

Method _split_rounds

opencompass/models/base_api.py:323–354  ·  view source on GitHub ↗

Split the prompt template into rounds, based on single round template. Return the index ranges of each round. Specifically, prompt_template[res[i]:res[i+1]] represents the i-th round in the template.

(
            self, prompt_template: List[Union[str, Dict]],
            single_round_template: List[Union[str, Dict]])

Source from the content-addressed store, hash-verified

321 return role_dict
322
323 def _split_rounds(
324 self, prompt_template: List[Union[str, Dict]],
325 single_round_template: List[Union[str, Dict]]) -> List[int]:
326 """Split the prompt template into rounds, based on single round
327 template.
328
329 Return the index ranges of each round. Specifically,
330 prompt_template[res[i]:res[i+1]] represents the i-th round in the
331 template.
332 """
333 role_idxs = {
334 role_cfg['role']: i
335 for i, role_cfg in enumerate(single_round_template)
336 if not isinstance(role_cfg, str)
337 }
338 last_role_idx = -1
339 cutoff_idxs = [0]
340 for idx, template in enumerate(prompt_template):
341 if isinstance(template, str):
342 continue
343 role_idx = role_idxs.get(template['role'], None)
344 if role_idx is None:
345 try:
346 role_idx = role_idxs[template['fallback_role']]
347 except KeyError:
348 raise KeyError(f'{template} neither has an appropriate '
349 'role nor a fallback role.')
350 if role_idx <= last_role_idx:
351 cutoff_idxs.append(idx)
352 last_role_idx = role_idx
353 cutoff_idxs.append(len(prompt_template))
354 return cutoff_idxs
355
356 def _prompt2api(self,
357 prompts: Union[List, str],

Callers 1

parse_templateMethod · 0.95

Calls 1

getMethod · 0.80

Tested by

no test coverage detected