extract_code

extract_code(
    text: str | list,
    pattern: str = '```[ \\t]*(\\w+)?[ \\t]*\\r?\\n(.*?)\\r?\\n[ \\t]*```',
    detect_single_line_code: bool = False
) -> list[tuple[str, str]]

Extract code from a text.

Parameters:
NameDescription
textThe content to extract code from.

The content can be a string or a list, as returned by standard GPT or multimodal GPT.

Type: str | list
patternThe regular expression pattern for finding the code block.

Defaults to CODE_BLOCK_PATTERN.

Type: str

Default:[ \\t]*(\\w+)?[ \\t]*\\r?\\n(.*?)\\r?\\n[ \\t]*
detect_single_line_codeEnable the new feature for extracting single line code.

Defaults to False.

Type: bool

Default: False
Returns:
TypeDescription
list[tuple[str, str]]list: A list of tuples, each containing the language and the code. If there is no code block in the input text, the language would be “unknown”. If there is code block but the language is not specified, the language would be "".