MCPcopy Create free account
hub / github.com/Agora-Lab-AI/Bing-Chat-API / code_blocks

Method code_blocks

src/bing_chat/BingUtils.py:269–301  ·  view source on GitHub ↗

Return a list of code blocks (```) or snippets (`) as strings. If the response contains a mix of snippets and code blocks, return the code blocks only. This method is not suitable if the main text response includes either of the delimiters but not as part o

(self)

Source from the content-addressed store, hash-verified

267
268 @property
269 def code_blocks(self) -> list[str]:
270 """
271 Return a list of code blocks (```) or snippets (`) as strings.
272
273 If the response contains a mix of snippets and code blocks, return the
274 code blocks only.
275
276 This method is not suitable if the main text response includes either of
277 the delimiters but not as part of an actual snippet or code block.
278
279 For example:
280 'In Markdown, the back-tick (`) is used to denote a code snippet'
281
282 """
283
284 final_blocks = []
285 if isinstance(self.output, str): # I.e. simplify_response is True
286 separator = "```" if "```" in self.output else "`"
287 code_blocks = self.output.split(separator)[1:-1:2]
288 if separator == "`":
289 return code_blocks
290 else:
291 code_blocks = []
292 for response in self.output:
293 separator = "```" if "```" in response else "`"
294 code_blocks.extend(response.split(separator)[1:-1:2])
295 code_blocks = [x for x in code_blocks if x]
296 # Remove language name if present:
297 for block in code_blocks:
298 lines = block.splitlines()
299 code = lines[1:] if re.match(" *\\w+ *", lines[0]) else lines
300 final_blocks += ["\n".join(code).removeprefix(separator)]
301 return [x for x in final_blocks if x]
302
303 @property
304 def code(self) -> str:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected