Spaces:
Sleeping
Sleeping
File size: 23,565 Bytes
e679d69 |
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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
# flake8: noqa: E501
import asyncio
import base64
import io
import json
import logging
import os
import queue
import re
import signal
import sys
import tempfile
import traceback
import uuid
from typing import Optional, Tuple, Type
from jupyter_client import AsyncKernelClient, AsyncKernelManager, AsyncMultiKernelManager
from tenacity import retry, retry_if_result, stop_after_attempt, wait_fixed
from lagent.actions.base_action import AsyncActionMixin, BaseAction, tool_api
from lagent.actions.parser import BaseParser, JsonParser
from lagent.schema import ActionReturn, ActionStatusCode
logger = logging.getLogger(__name__)
START_CODE = """
def input(*args, **kwargs):
raise NotImplementedError('Python input() function is disabled.')
get_ipython().system = lambda *args: print('Assume we have this package, ! is disabled!')
{}
""" # noqa
class TimeoutError(Exception):
pass
class KernelDeath(Exception):
pass
async def async_run_code(
km: AsyncKernelManager,
code,
*,
interrupt_after=30,
iopub_timeout=40,
wait_for_ready_timeout=60,
shutdown_kernel=True,
):
assert iopub_timeout > interrupt_after
try:
async def get_iopub_msg_with_death_detection(kc: AsyncKernelClient,
*,
timeout=None):
loop = asyncio.get_running_loop()
dead_fut = loop.create_future()
def restarting():
assert (
False
), "Restart shouldn't happen because config.KernelRestarter.restart_limit is expected to be set to 0"
def dead():
logger.info("Kernel has died, will NOT restart")
dead_fut.set_result(None)
msg_task = asyncio.create_task(kc.get_iopub_msg(timeout=timeout))
km.add_restart_callback(restarting, "restart")
km.add_restart_callback(dead, "dead")
try:
done, _ = await asyncio.wait(
[dead_fut, msg_task], return_when=asyncio.FIRST_COMPLETED)
if dead_fut in done:
raise KernelDeath()
assert msg_task in done
return await msg_task
finally:
msg_task.cancel()
km.remove_restart_callback(restarting, "restart")
km.remove_restart_callback(dead, "dead")
async def send_interrupt():
await asyncio.sleep(interrupt_after)
logger.info("Sending interrupt to kernel")
await km.interrupt_kernel()
@retry(
retry=retry_if_result(lambda ret: ret[-1].strip() in [
'KeyboardInterrupt',
f"Kernel didn't respond in {wait_for_ready_timeout} seconds",
] if isinstance(ret, tuple) else False),
stop=stop_after_attempt(3),
wait=wait_fixed(1),
retry_error_callback=lambda state: state.outcome.result())
async def run():
execute_result = None
error_traceback = None
stream_text_list = []
kc = km.client()
assert isinstance(kc, AsyncKernelClient)
kc.start_channels()
try:
await kc.wait_for_ready(timeout=wait_for_ready_timeout)
msg_id = kc.execute(code)
while True:
message = await get_iopub_msg_with_death_detection(
kc, timeout=iopub_timeout)
if logger.isEnabledFor(logging.DEBUG):
logger.debug(
json.dumps(message, indent=2, default=str))
assert message["parent_header"]["msg_id"] == msg_id
msg_type = message["msg_type"]
if msg_type == "status":
if message["content"]["execution_state"] == "idle":
break
elif msg_type == "stream":
stream_name = message["content"]["name"]
stream_text = message["content"]["text"]
stream_text_list.append(stream_text)
elif msg_type == "execute_result":
execute_result = message["content"]["data"]
elif msg_type == "error":
error_traceback_lines = message["content"]["traceback"]
error_traceback = "\n".join(error_traceback_lines)
elif msg_type == "execute_input":
pass
else:
assert False, f"Unknown message_type: {msg_type}"
finally:
kc.stop_channels()
return execute_result, error_traceback, "".join(stream_text_list)
if interrupt_after:
run_task = asyncio.create_task(run())
send_interrupt_task = asyncio.create_task(send_interrupt())
done, _ = await asyncio.wait([run_task, send_interrupt_task],
return_when=asyncio.FIRST_COMPLETED)
if run_task in done:
send_interrupt_task.cancel()
else:
assert send_interrupt_task in done
result = await run_task
else:
result = await run()
return result
finally:
if shutdown_kernel:
await km.shutdown_kernel()
class IPythonInterpreter(BaseAction):
"""A IPython executor that can execute Python scripts in a jupyter manner.
Args:
timeout (int): Upper bound of waiting time for Python script execution.
Defaults to 20.
user_data_dir (str, optional): Specified the user data directory for files
loading. If set to `ENV`, use `USER_DATA_DIR` environment variable.
Defaults to `ENV`.
work_dir (str, optional): Specify which directory to save output images to.
Defaults to ``'./work_dir/tmp_dir'``.
description (dict): The description of the action. Defaults to ``None``.
parser (Type[BaseParser]): The parser class to process the
action's inputs and outputs. Defaults to :class:`JsonParser`.
"""
_KERNEL_CLIENTS = {}
def __init__(
self,
timeout: int = 20,
user_data_dir: str = 'ENV',
work_dir='./work_dir/tmp_dir',
description: Optional[dict] = None,
parser: Type[BaseParser] = JsonParser,
):
super().__init__(description, parser)
self.timeout = timeout
if user_data_dir == 'ENV':
user_data_dir = os.environ.get('USER_DATA_DIR', '')
if user_data_dir:
user_data_dir = os.path.dirname(user_data_dir)
user_data_dir = f"import os\nos.chdir('{user_data_dir}')"
self.user_data_dir = user_data_dir
self._initialized = False
self.work_dir = work_dir
if not os.path.exists(self.work_dir):
os.makedirs(self.work_dir, exist_ok=True)
@staticmethod
def start_kernel():
from jupyter_client import KernelManager
# start the kernel and manager
km = KernelManager()
km.start_kernel()
kc = km.client()
return km, kc
def initialize(self):
if self._initialized:
return
pid = os.getpid()
if pid not in self._KERNEL_CLIENTS:
self._KERNEL_CLIENTS[pid] = self.start_kernel()
self.kernel_manager, self.kernel_client = self._KERNEL_CLIENTS[pid]
self._initialized = True
self._call(START_CODE.format(self.user_data_dir), None)
def reset(self):
if not self._initialized:
self.initialize()
else:
code = "get_ipython().run_line_magic('reset', '-f')\n" + \
START_CODE.format(self.user_data_dir)
self._call(code, None)
def _call(self,
command: str,
timeout: Optional[int] = None) -> Tuple[str, bool]:
self.initialize()
command = extract_code(command)
# check previous remaining result
while True:
try:
msg = self.kernel_client.get_iopub_msg(timeout=5)
msg_type = msg['msg_type']
if msg_type == 'status':
if msg['content'].get('execution_state') == 'idle':
break
except queue.Empty:
# assume no result
break
self.kernel_client.execute(command)
def _inner_call():
result = ''
images = []
succeed = True
image_idx = 0
while True:
text = ''
image = ''
finished = False
msg_type = 'error'
try:
msg = self.kernel_client.get_iopub_msg(timeout=20)
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 = publish_image_to_local(
image_b64, self.work_dir)
image_idx += 1
image = '![fig-%03d](%s)' % (image_idx, image_url)
elif msg_type == 'display_data':
if 'image/png' in msg['content']['data']:
image_b64 = msg['content']['data']['image/png']
image_url = publish_image_to_local(
image_b64, self.work_dir)
image_idx += 1
image = '![fig-%03d](%s)' % (image_idx, 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':
succeed = False
text = escape_ansi('\n'.join(
msg['content']['traceback']))
if 'M6_CODE_INTERPRETER_TIMEOUT' in text:
text = f'Timeout. No response after {timeout} seconds.' # noqa
except queue.Empty:
# stop current task in case break next input.
self.kernel_manager.interrupt_kernel()
succeed = False
text = f'Timeout. No response after {timeout} seconds.'
finished = True
except Exception:
succeed = False
msg = ''.join(traceback.format_exception(*sys.exc_info()))
# text = 'The code interpreter encountered an unexpected error.' # noqa
text = msg
logging.warning(msg)
finished = True
if text:
# result += f'\n\n{msg_type}:\n\n```\n{text}\n```'
result += f'{text}'
if image:
images.append(image_url)
if finished:
return succeed, dict(text=result, image=images)
try:
if timeout:
def handler(signum, frame):
raise TimeoutError()
signal.signal(signal.SIGALRM, handler)
signal.alarm(timeout)
succeed, result = _inner_call()
except TimeoutError:
succeed = False
text = 'The code interpreter encountered an unexpected error.'
result = f'\n\nerror:\n\n```\n{text}\n```'
finally:
if timeout:
signal.alarm(0)
# result = result.strip('\n')
return succeed, result
@tool_api
def run(self, command: str, timeout: Optional[int] = None) -> ActionReturn:
r"""When you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. The drive at '/mnt/data' can be used to save and persist user files. Internet access for this session is disabled. Do not make external web requests or API calls as they will fail.
Args:
command (:class:`str`): Python code
timeout (:class:`Optional[int]`): Upper bound of waiting time for Python script execution.
"""
tool_return = ActionReturn(url=None, args=None, type=self.name)
tool_return.args = dict(text=command)
succeed, result = self._call(command, timeout)
if succeed:
text = result['text']
image = result.get('image', [])
resp = [dict(type='text', content=text)]
if image:
resp.extend([dict(type='image', content=im) for im in image])
tool_return.result = resp
# tool_return.result = dict(
# text=result['text'], image=result.get('image', [])[0])
tool_return.state = ActionStatusCode.SUCCESS
else:
tool_return.errmsg = result.get('text', '') if isinstance(
result, dict) else result
tool_return.state = ActionStatusCode.API_ERROR
return tool_return
class AsyncIPythonInterpreter(AsyncActionMixin, IPythonInterpreter):
"""A IPython executor that can execute Python scripts in a jupyter manner.
Args:
timeout (int): Upper bound of waiting time for Python script execution.
Defaults to 20.
user_data_dir (str, optional): Specified the user data directory for files
loading. If set to `ENV`, use `USER_DATA_DIR` environment variable.
Defaults to `ENV`.
work_dir (str, optional): Specify which directory to save output images to.
Defaults to ``'./work_dir/tmp_dir'``.
description (dict): The description of the action. Defaults to ``None``.
parser (Type[BaseParser]): The parser class to process the
action's inputs and outputs. Defaults to :class:`JsonParser`.
"""
_UNBOUND_KERNEL_CLIENTS = asyncio.Queue()
def __init__(
self,
timeout: int = 20,
user_data_dir: str = 'ENV',
work_dir=os.path.join(tempfile.gettempdir(), 'tmp_dir'),
max_kernels: Optional[int] = None,
reuse_kernel: bool = True,
startup_rate: bool = 32,
connection_dir: str = tempfile.gettempdir(),
description: Optional[dict] = None,
parser: Type[BaseParser] = JsonParser,
):
super().__init__(timeout, user_data_dir, work_dir, description, parser)
from traitlets.config import Config
c = Config()
c.KernelManager.transport = 'ipc'
self._amkm = AsyncMultiKernelManager(
config=c, connection_dir=connection_dir)
self._max_kernels = max_kernels
self._reuse_kernel = reuse_kernel
self._sem = asyncio.Semaphore(startup_rate)
self._lock = asyncio.Lock()
async def initialize(self, session_id: str):
session_id = str(session_id)
while True:
if session_id in self._KERNEL_CLIENTS:
return self._KERNEL_CLIENTS[session_id]
if self._reuse_kernel and not self._UNBOUND_KERNEL_CLIENTS.empty():
self._KERNEL_CLIENTS[
session_id] = await self._UNBOUND_KERNEL_CLIENTS.get()
return self._KERNEL_CLIENTS[session_id]
async with self._sem:
if self._max_kernels is None or len(
self._KERNEL_CLIENTS
) + self._UNBOUND_KERNEL_CLIENTS.qsize() < self._max_kernels:
kernel_id = None
try:
kernel_id = await self._amkm.start_kernel()
kernel = self._amkm.get_kernel(kernel_id)
client = kernel.client()
_, error_stacktrace, stream_text = await async_run_code(
kernel,
START_CODE.format(self.user_data_dir),
shutdown_kernel=False)
# check if the output of START_CODE meets expectations
if not (error_stacktrace is None
and stream_text == ''):
raise RuntimeError
except Exception as e:
print(f'Starting kernel error: {e}')
if kernel_id:
await self._amkm.shutdown_kernel(kernel_id)
self._amkm.remove_kernel(kernel_id)
await asyncio.sleep(1)
continue
if self._max_kernels is None:
self._KERNEL_CLIENTS[session_id] = (kernel_id, kernel,
client)
return kernel_id, kernel, client
async with self._lock:
if len(self._KERNEL_CLIENTS
) + self._UNBOUND_KERNEL_CLIENTS.qsize(
) < self._max_kernels:
self._KERNEL_CLIENTS[session_id] = (kernel_id,
kernel, client)
return kernel_id, kernel, client
await self._amkm.shutdown_kernel(kernel_id)
self._amkm.remove_kernel(kernel_id)
await asyncio.sleep(1)
async def reset(self, session_id: str):
session_id = str(session_id)
if session_id not in self._KERNEL_CLIENTS:
return
_, kernel, _ = self._KERNEL_CLIENTS[session_id]
code = "get_ipython().run_line_magic('reset', '-f')\n" + \
START_CODE.format(self.user_data_dir)
await async_run_code(kernel, code, shutdown_kernel=False)
async def shutdown(self, session_id: str):
session_id = str(session_id)
if session_id in self._KERNEL_CLIENTS:
kernel_id, _, _ = self._KERNEL_CLIENTS.get(session_id)
await self._amkm.shutdown_kernel(kernel_id)
self._amkm.remove_kernel(kernel_id)
del self._KERNEL_CLIENTS[session_id]
async def close_session(self, session_id: str):
session_id = str(session_id)
if self._reuse_kernel:
if session_id in self._KERNEL_CLIENTS:
await self.reset(session_id)
await self._UNBOUND_KERNEL_CLIENTS.put(
self._KERNEL_CLIENTS.pop(session_id))
else:
await self.shutdown(session_id)
async def _call(self, command, timeout=None, session_id=None):
_, kernel, _ = await self.initialize(str(session_id))
result = await async_run_code(
kernel,
extract_code(command),
interrupt_after=timeout or self.timeout,
shutdown_kernel=False)
execute_result, error_stacktrace, stream_text = result
if error_stacktrace is not None:
ret = re.sub('^-*\n', '', escape_ansi(error_stacktrace))
if ret.endswith('KeyboardInterrupt: '):
ret = 'The code interpreter encountered a timeout error.'
status, ret = False, ret.strip()
elif execute_result is not None:
status, ret = True, dict(text=execute_result.get('text/plain', ''))
else:
status, ret = True, dict(text=stream_text.strip())
return status, ret
@tool_api
async def run(self,
command: str,
timeout: Optional[int] = None,
session_id: Optional[str] = None) -> ActionReturn:
r"""When you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 60.0 seconds. The drive at '/mnt/data' can be used to save and persist user files. Internet access for this session is disabled. Do not make external web requests or API calls as they will fail.
Args:
command (:class:`str`): Python code
timeout (:class:`Optional[int]`): Upper bound of waiting time for Python script execution.
"""
tool_return = ActionReturn(url=None, args=None, type=self.name)
tool_return.args = dict(text=command)
succeed, result = await self._call(command, timeout, session_id)
if succeed:
text = result['text']
image = result.get('image', [])
resp = [dict(type='text', content=text)]
if image:
resp.extend([dict(type='image', content=im) for im in image])
tool_return.result = resp
# tool_return.result = dict(
# text=result['text'], image=result.get('image', [])[0])
tool_return.state = ActionStatusCode.SUCCESS
else:
tool_return.errmsg = result.get('text', '') if isinstance(
result, dict) else result
tool_return.state = ActionStatusCode.API_ERROR
return tool_return
def extract_code(text):
import json5
# Match triple backtick blocks first
triple_match = re.search(r'```[^\n]*\n(.+?)```', text, re.DOTALL)
# Match single backtick blocks second
single_match = re.search(r'`([^`]*)`', text, re.DOTALL)
if triple_match:
text = triple_match.group(1)
elif single_match:
text = single_match.group(1)
else:
try:
text = json5.loads(text)['code']
except Exception:
pass
# If no code blocks found, return original text
return text
def escape_ansi(line):
ansi_escape = re.compile(r'(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]')
return ansi_escape.sub('', line)
def publish_image_to_local(image_base64: str, work_dir='./work_dir/tmp_dir'):
import PIL.Image
image_file = str(uuid.uuid4()) + '.png'
local_image_file = os.path.join(work_dir, image_file)
png_bytes = base64.b64decode(image_base64)
assert isinstance(png_bytes, bytes)
bytes_io = io.BytesIO(png_bytes)
PIL.Image.open(bytes_io).save(local_image_file, 'png')
return local_image_file
# local test for code interpreter
def get_multiline_input(hint):
print(hint)
print('// Press ENTER to make a new line. Press CTRL-D to end input.')
lines = []
while True:
try:
line = input()
except EOFError: # CTRL-D
break
lines.append(line)
print('// Input received.')
if lines:
return '\n'.join(lines)
else:
return ''
if __name__ == '__main__':
code_interpreter = IPythonInterpreter()
while True:
print(code_interpreter(get_multiline_input('Enter python code:')))
|