MCPcopy Create free account
hub / github.com/Mercury7353/PyBench / parse_code_action

Function parse_code_action

utils/output_parser.py:7–37  ·  view source on GitHub ↗

parse output from code interpreter Args: output (str): the output from code interpreter mode: the mode of the output, could be prompt, functioncall, assistant code_start_token: the token code script starts with, only used in prompt mode code_end_token: the token

(
    output: str,
    mode: str = "prompt",
    code_start_token: str = "```\npython\n",
    code_end_token: str = "```",
    tool_call_token: str = "<|tool_call|>",
)

Source from the content-addressed store, hash-verified

5
6
7def parse_code_action(
8 output: str,
9 mode: str = "prompt",
10 code_start_token: str = "```\npython\n",
11 code_end_token: str = "```",
12 tool_call_token: str = "<|tool_call|>",
13) -> Tuple[str, str]:
14 """parse output from code interpreter
15
16 Args:
17 output (str): the output from code interpreter
18 mode: the mode of the output, could be prompt, functioncall, assistant
19 code_start_token: the token code script starts with, only used in prompt mode
20 code_end_token: the token code script ends with, only used in prompt mode
21 tool_call_token: the token for tool call, only used in prompt mode
22
23 Returns:
24 Tuple[str, str]: reasoning and code action
25 """
26 if mode == "prompt":
27 return extract_code(output, code_start_token, code_end_token)
28 elif mode == "functioncall":
29 rsp = fc2dict(output, tool_call_token)
30 if "tool_calls" in rsp and len(rsp["tool_calls"]) > 0:
31 return rsp["content"], rsp["tool_calls"][0]["arguments"]["code"]
32 else:
33 return rsp["content"], ""
34 elif mode == "assistant":
35 raise NotImplementedError("assistant mode is not implemented yet")
36 else:
37 raise ValueError(f"mode {mode} is not supported")
38
39
40def extract_code(

Callers 1

process_taskFunction · 0.90

Calls 2

extract_codeFunction · 0.85
fc2dictFunction · 0.85

Tested by

no test coverage detected