MCPcopy
hub / github.com/open-compass/opencompass / replace

Method replace

opencompass/utils/prompt.py:107–147  ·  view source on GitHub ↗

Replaces all instances of 'src' in the PromptList with 'dst'. Args: src (str): The string to be replaced. dst (PromptType): The string or PromptList to replace with. Returns: PromptList: A new PromptList with 'src' replaced by 'dst'. Rai

(self, src: str, dst: Union[str, PromptList])

Source from the content-addressed store, hash-verified

105 return new_list
106
107 def replace(self, src: str, dst: Union[str, PromptList]) -> PromptList:
108 """Replaces all instances of 'src' in the PromptList with 'dst'.
109
110 Args:
111 src (str): The string to be replaced.
112 dst (PromptType): The string or PromptList to replace with.
113
114 Returns:
115 PromptList: A new PromptList with 'src' replaced by 'dst'.
116
117 Raises:
118 TypeError: If 'dst' is a PromptList and 'src' is in a dictionary's
119 'prompt' key.
120 """
121 new_list = PromptList()
122 for item in self:
123 if isinstance(item, str):
124 if isinstance(dst, str):
125 new_list.append(item.replace(src, dst))
126 elif isinstance(dst, PromptList):
127 split_str = item.split(src)
128 for i, split_item in enumerate(split_str):
129 if split_item:
130 new_list.append(split_item)
131 if i < len(split_str) - 1:
132 new_list += dst
133 elif isinstance(item, Dict):
134 new_item = deepcopy(item)
135 if 'prompt' in item:
136 if src in item['prompt']:
137 if isinstance(dst, PromptList):
138 raise TypeError(
139 f'Found keyword {src} in a dictionary\'s &#x27;
140 'prompt key. Cannot replace with a '
141 'PromptList.')
142 new_item['prompt'] = new_item['prompt'].replace(
143 src, dst)
144 new_list.append(new_item)
145 else:
146 new_list.append(item.replace(src, dst))
147 return new_list
148
149 def __add__(self, other: Union[str, PromptList]) -> PromptList:
150 """Adds a string or another PromptList to this PromptList.

Calls 1

PromptListClass · 0.85

Tested by 5

test_replaceMethod · 0.76
run_testFunction · 0.64