(sequence: str, spliter="<|tool_call|>")
| 95 | return "" |
| 96 | |
| 97 | def fc2dict(sequence: str, spliter="<|tool_call|>"): |
| 98 | if spliter in sequence: |
| 99 | content, tool_call_string = sequence.split(spliter, 1) |
| 100 | try: |
| 101 | # 找到第一个 { 和最后一个 } |
| 102 | start_idx = tool_call_string.find('{') |
| 103 | end_idx = tool_call_string.rfind('}') |
| 104 | if start_idx != -1 and end_idx != -1: |
| 105 | arguments_str = tool_call_string[start_idx:end_idx + 1] |
| 106 | print("Arg:",arguments_str) |
| 107 | arguments_str=arguments_str.replace("\n","\\n") |
| 108 | #code_content = extract_code_from_arguments(arguments_str) |
| 109 | tool_call_dict = { |
| 110 | "name": "execute_python", |
| 111 | "arguments": json.loads(arguments_str) |
| 112 | } |
| 113 | tool_calls = [tool_call_dict] |
| 114 | else: |
| 115 | tool_calls = [] |
| 116 | return { |
| 117 | "content": content.strip(), |
| 118 | "tool_calls": tool_calls, |
| 119 | "role": "assistant", |
| 120 | } |
| 121 | except Exception as e: |
| 122 | print(f"Error: {e}") |
| 123 | return {"content": content.strip(), "role": "assistant"} |
| 124 | else: |
| 125 | return {"content": sequence.strip(), "role": "assistant"} |
| 126 | |
| 127 | # 示例用法 |
| 128 | sequence = '''To fulfill your request, I will perform the following steps: |
no outgoing calls
no test coverage detected