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

Function safe_format

opencompass/utils/prompt.py:11–35  ·  view source on GitHub ↗

Safely formats a string with the given keyword arguments. If a keyword is not found in the string, it will be ignored. Args: input_str (str): The string to be formatted. **kwargs: The keyword arguments to be used for formatting. Returns: str: The formatted strin

(input_str: str, **kwargs)

Source from the content-addressed store, hash-verified

9
10
11def safe_format(input_str: str, **kwargs) -> str:
12 """Safely formats a string with the given keyword arguments. If a keyword
13 is not found in the string, it will be ignored.
14
15 Args:
16 input_str (str): The string to be formatted.
17 **kwargs: The keyword arguments to be used for formatting.
18
19 Returns:
20 str: The formatted string.
21 """
22 # import re
23 # segs = [input_str]
24 # for k, v in kwargs.items():
25 # regex = re.compile(f'(?<={{{k}}})(?={{{k}}})|({{{k}}})')
26 # segs = [regex.split(seg) for seg in segs]
27 # segs = sum(segs, [])
28 # replace_dict = {f'{{{k}}}': str(v) for k, v in kwargs.items()}
29 # segs = [replace_dict.get(seg, seg) for seg in segs]
30 # output_str = ''.join(segs)
31 # return output_str
32
33 for k, v in kwargs.items():
34 input_str = input_str.replace(f'{{{k}}}', str(v))
35 return input_str
36
37
38def get_prompt_hash(dataset_cfg: Union[ConfigDict, List[ConfigDict]]) -> str:

Callers 4

generate_ice_itemMethod · 0.90
generate_itemMethod · 0.90
formatMethod · 0.85

Calls 1

replaceMethod · 0.80

Tested by

no test coverage detected