File size: 11,709 Bytes
09321b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import asyncio
import atexit
import base64
import glob
import io
import os
import queue
import re
import shutil
import signal
import subprocess
import sys
import time
import traceback
import uuid
from pathlib import Path
from typing import Dict, Optional

import json
import matplotlib
import PIL.Image
from jupyter_client import BlockingKernelClient

from .tool import Tool

WORK_DIR = os.getenv('CODE_INTERPRETER_WORK_DIR', '/tmp/ci_workspace')

STATIC_URL = os.getenv('CODE_INTERPRETER_STATIC_URL',
                       'http://127.0.0.1:7866/static')

LAUNCH_KERNEL_PY = """
from ipykernel import kernelapp as app
app.launch_new_instance()
"""

INIT_CODE_FILE = str(
    Path(__file__).absolute().parent / 'code_interpreter_utils'
    / 'code_interpreter_init_kernel.py')

ALIB_FONT_FILE = str(
    Path(__file__).absolute().parent / 'code_interpreter_utils'
    / 'AlibabaPuHuiTi-3-45-Light.ttf')

_KERNEL_CLIENTS: Dict[int, BlockingKernelClient] = {}


class CodeInterpreterJupyter(Tool):
    """
        using jupyter kernel client to interpret python code,
        should not be used the other code interpreter tool at the same time
    """
    description = '代码解释器,可用于执行Python代码。'
    name = 'code_interpreter'
    parameters: list = [{
        'name': 'code',
        'description': '待执行的代码',
        'required': True
    }]

    def __init__(self, cfg={}):
        super().__init__(cfg)
        self.timeout = self.cfg.get('timeout', 30)
        self.image_server = self.cfg.get('image_server', False)
        self.kernel_clients: Dict[int, BlockingKernelClient] = {}
        atexit.register(self._kill_kernels)

        pid: int = os.getpid()
        if pid in self.kernel_clients:
            kc = self.kernel_clients[pid]
        else:
            self._fix_matplotlib_cjk_font_issue()
            kc = self._start_kernel(pid)
            with open(INIT_CODE_FILE) as fin:
                start_code = fin.read()
                start_code = start_code.replace('{{M6_FONT_PATH}}',
                                                repr(ALIB_FONT_FILE)[1:-1])
            print(self._execute_code(kc, start_code))
            self.kernel_clients[pid] = kc

        self.kc = kc

    def __del__(self):
        # make sure all the kernels are killed during __del__
        signal.signal(signal.SIGTERM, self._kill_kernels)
        signal.signal(signal.SIGINT, self._kill_kernels)

    def _start_kernel(self, pid) -> BlockingKernelClient:
        connection_file = os.path.join(WORK_DIR,
                                       f'kernel_connection_file_{pid}.json')
        launch_kernel_script = os.path.join(WORK_DIR,
                                            f'launch_kernel_{pid}.py')
        for f in [connection_file, launch_kernel_script]:
            if os.path.exists(f):
                print(f'WARNING: {f} already exists')
                os.remove(f)

        os.makedirs(WORK_DIR, exist_ok=True)

        with open(launch_kernel_script, 'w') as fout:
            fout.write(LAUNCH_KERNEL_PY)

        available_envs = ['PATH', 'PYTHONPATH', 'LD_LIBRARY_PATH']
        envs = {}
        for k in available_envs:
            if os.getenv(k) is not None:
                envs[k] = os.getenv(k)

        args = (
            sys.executable,
            launch_kernel_script,
            '--IPKernelApp.connection_file',
            connection_file,
            '--matplotlib=inline',
            '--quiet',
        )
        kernel_process = subprocess.Popen([*args], env=envs,
                                          cwd=WORK_DIR)  # noqa E126
        print(f"INFO: kernel process's PID = {kernel_process.pid}")

        # Wait for kernel connection file to be written
        while True:
            if not os.path.isfile(connection_file):
                time.sleep(0.1)
            else:
                # Keep looping if JSON parsing fails, file may be partially written
                try:
                    with open(connection_file, 'r') as fp:
                        json.load(fp)
                    break
                except json.JSONDecodeError:
                    pass

        # Client
        kc = BlockingKernelClient(connection_file=connection_file)
        asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
        kc.load_connection_file()
        kc.start_channels()
        kc.wait_for_ready()
        return kc

    def _kill_kernels(self):
        for v in self.kernel_clients.values():
            v.shutdown()
        for k in list(self.kernel_clients.keys()):
            del self.kernel_clients[k]

    def _serve_image(self, image_base64: str, image_type: str) -> str:
        image_file = f'{uuid.uuid4()}.{image_type}'
        local_image_file = os.path.join(WORK_DIR, image_file)

        png_bytes = base64.b64decode(image_base64)
        assert isinstance(png_bytes, bytes)

        if image_type == 'gif':
            with open(local_image_file, 'wb') as file:
                file.write(png_bytes)
        else:
            bytes_io = io.BytesIO(png_bytes)
            PIL.Image.open(bytes_io).save(local_image_file, image_type)

        if self.image_server:
            image_url = f'{STATIC_URL}/{image_file}'
            return image_url
        else:
            return local_image_file

    def _escape_ansi(self, line: str) -> str:
        ansi_escape = re.compile(r'(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]')
        return ansi_escape.sub('', line)

    def _fix_matplotlib_cjk_font_issue(self):
        ttf_name = os.path.basename(ALIB_FONT_FILE)
        local_ttf = os.path.join(
            os.path.abspath(
                os.path.join(matplotlib.matplotlib_fname(), os.path.pardir)),
            'fonts', 'ttf', ttf_name)
        if not os.path.exists(local_ttf):
            try:
                shutil.copy(ALIB_FONT_FILE, local_ttf)
                font_list_cache = os.path.join(matplotlib.get_cachedir(),
                                               'fontlist-*.json')
                for cache_file in glob.glob(font_list_cache):
                    with open(cache_file) as fin:
                        cache_content = fin.read()
                    if ttf_name not in cache_content:
                        os.remove(cache_file)
            except Exception:
                traceback.format_exc()

    def _execute_code(self, kc: BlockingKernelClient, code: str) -> str:
        kc.wait_for_ready()
        kc.execute(code)
        result = ''
        image_idx = 0
        while True:
            text = ''
            image = ''
            finished = False
            msg_type = 'error'
            try:
                msg = kc.get_iopub_msg()
                msg_type = msg['msg_type']
                if msg_type == 'status':
                    if msg['content'].get('execution_state') == 'idle':
                        finished = True
                elif msg_type == 'execute_result':
                    text = msg['content']['data'].get('text/plain', '')
                    if 'image/png' in msg['content']['data']:
                        image_b64 = msg['content']['data']['image/png']
                        image_url = self._serve_image(image_b64, 'png')
                        image_idx += 1
                        image = '![IMAGEGEN](%s)' % (image_url)
                    elif 'text/html' in msg['content']['data']:
                        text += '\n' + msg['content']['data']['text/html']
                    elif 'image/gif' in msg['content']['data']:
                        image_b64 = msg['content']['data']['image/gif']
                        image_url = self._serve_image(image_b64, 'gif')
                        image_idx += 1
                        image = '![IMAGEGEN](%s)' % (image_url)
                elif msg_type == 'display_data':
                    if 'image/png' in msg['content']['data']:
                        image_b64 = msg['content']['data']['image/png']
                        image_url = self._serve_image(image_b64, 'png')
                        image_idx += 1
                        image = '![IMAGEGEN](%s)' % (image_url)
                    else:
                        text = msg['content']['data'].get('text/plain', '')
                elif msg_type == 'stream':
                    msg_type = msg['content']['name']  # stdout, stderr
                    text = msg['content']['text']
                elif msg_type == 'error':
                    text = self._escape_ansi('\n'.join(
                        msg['content']['traceback']))
                    if 'M6_CODE_INTERPRETER_TIMEOUT' in text:
                        text = 'Timeout: Code execution exceeded the time limit.'
            except queue.Empty:
                text = 'Timeout: Code execution exceeded the time limit.'
                finished = True
            except Exception:
                text = 'The code interpreter encountered an unexpected error.'
                traceback.format_exc()
                finished = True
            if text:
                result += f'\n{text}'
            if image:
                result += f'\n\n{image}'
            if finished:
                break
        result = result.lstrip('\n')
        if not result:
            result += 'The code executed successfully.'
        return result

    def _local_call(self, *args, **kwargs):
        code = self._handle_input_fallback(**kwargs)
        if not code.strip():
            return ''

        if self.timeout:
            code = f'_M6CountdownTimer.start({self.timeout})\n{code}'

        fixed_code = []
        for line in code.split('\n'):
            fixed_code.append(line)
            if line.startswith('sns.set_theme('):
                fixed_code.append(
                    'plt.rcParams["font.family"] = _m6_font_prop.get_name()')
        fixed_code = '\n'.join(fixed_code)
        result = self._execute_code(self.kc, fixed_code)

        if self.timeout:
            self._execute_code(self.kc, '_M6CountdownTimer.cancel()')

        return {'result': result}

    def _handle_input_fallback(self, **kwargs):
        """
        an alternative method is to parse code in content not from function call
        such as:
            text = response['content']
            code_block = re.search(r'```([\s\S]+)```', text)  # noqa W^05
            if code_block:
                result = code_block.group(1)
                language = result.split('\n')[0]
                code = '\n'.join(result.split('\n')[1:])

        :param fallback_text:
        :return: language, cocde
        """

        code = kwargs.get('code', None)
        fallback = kwargs.get('fallback', None)

        if code:
            return code
        elif fallback:
            try:
                text = fallback
                code_block = re.search(r'```([\s\S]+)```', text)  # noqa W^05
                if code_block:
                    result = code_block.group(1)
                    language = result.split('\n')[0]
                    if language == 'py' or language == 'python':
                        # handle py case
                        # ```py code ```
                        language = 'python'
                        code = '\n'.join(result.split('\n')[1:])
                        return code

                    if language == 'json':
                        # handle json case
                        # ```json {language,code}```
                        parameters = json.loads('\n'.join(
                            result.split('\n')[1:]).replace('\n', ''))
                        return parameters['code']
            except ValueError:
                return code
        else:
            return code