Skip to content

Utils

Various utility functions.

Functions:

sanitize_json_completion(completion)

Return only content inside the first pair of triple backticks if they are present.

Source code in tapeagents/utils.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def sanitize_json_completion(completion: str) -> str:
    """
    Return only content inside the first pair of triple backticks if they are present.
    """
    tiks_counter = 0
    lines = completion.strip().split("\n")
    clean_lines = []
    for line in lines:
        if line.startswith("```"):
            tiks_counter += 1
            if tiks_counter == 1:
                clean_lines = []
            elif tiks_counter == 2:
                break
            continue
        clean_lines.append(line)
    return "\n".join(clean_lines)