extract code from assistant content Args: rsp (str): the response content from assistant code_start_token (str, optional): the token code script starts with. Defaults to "```\npython". code_end_token (str, optional): the token code script ends with. Defaults to "```".
(
rsp: str, code_start_token: str = "```\npython\n", code_end_token: str = "```"
)
| 38 | |
| 39 | |
| 40 | def extract_code( |
| 41 | rsp: str, code_start_token: str = "```\npython\n", code_end_token: str = "```" |
| 42 | ) -> Tuple[str, str]: |
| 43 | """extract code from assistant content |
| 44 | |
| 45 | Args: |
| 46 | rsp (str): the response content from assistant |
| 47 | code_start_token (str, optional): the token code script starts with. Defaults to "```\npython". |
| 48 | code_end_token (str, optional): the token code script ends with. Defaults to "```". |
| 49 | |
| 50 | Returns: |
| 51 | Tuple[str, str]: reasoning and code action |
| 52 | """ |
| 53 | # TODO: implement the code extraction logic using different code_start_token and code_end_token |
| 54 | rsp = str(rsp) |
| 55 | |
| 56 | start_index = rsp.find(code_start_token) |
| 57 | if start_index == -1: |
| 58 | return rsp, "" |
| 59 | |
| 60 | start_index += len(code_start_token) |
| 61 | end_index = rsp.find(code_end_token, start_index) |
| 62 | if end_index == -1: |
| 63 | return rsp, "" |
| 64 | |
| 65 | return rsp[:start_index].replace(code_start_token, "").strip(), rsp[ |
| 66 | start_index:end_index |
| 67 | ].strip() |
| 68 | |
| 69 | |
| 70 | import ast |