MCPcopy Create free account
hub / github.com/AMAP-ML/Thinking-with-Map / call

Method call

demo/qwen_agent/tools/code_interpreter.py:105–147  ·  view source on GitHub ↗
(self, params: Union[str, dict], files: List[str] = None, timeout: Optional[int] = 30, **kwargs)

Source from the content-addressed store, hash-verified

103 return fmt
104
105 def call(self, params: Union[str, dict], files: List[str] = None, timeout: Optional[int] = 30, **kwargs) -> str:
106 super().call(params=params, files=files) # copy remote files to work_dir
107
108 try:
109 params = json5.loads(params)
110 code = params['code']
111 except Exception:
112 code = extract_code(params)
113
114 if not code.strip():
115 return ''
116
117 kernel_id: str = f'{self.instance_id}_{os.getpid()}'
118 if kernel_id in _KERNEL_CLIENTS:
119 kc = _KERNEL_CLIENTS[kernel_id]
120 else:
121 _fix_matplotlib_cjk_font_issue()
122 self._fix_secure_write_for_code_interpreter()
123 kc, subproc = self._start_kernel(kernel_id)
124 with open(INIT_CODE_FILE) as fin:
125 start_code = fin.read()
126 start_code = start_code.replace('{{M6_FONT_PATH}}', repr(ALIB_FONT_FILE)[1:-1])
127 start_code += '\n%xmode Minimal'
128 logger.info(self._execute_code(kc, start_code))
129 _KERNEL_CLIENTS[kernel_id] = kc
130 _MISC_SUBPROCESSES[kernel_id] = subproc
131
132 if timeout:
133 code = f'_M6CountdownTimer.start({timeout})\n{code}'
134
135 fixed_code = []
136 for line in code.split('\n'):
137 fixed_code.append(line)
138 if line.startswith('sns.set_theme('):
139 fixed_code.append('plt.rcParams["font.family"] = _m6_font_prop.get_name()')
140 fixed_code = '\n'.join(fixed_code)
141 fixed_code += '\n\n' # Prevent code not executing in notebook due to no line breaks at the end
142 result = self._execute_code(kc, fixed_code)
143
144 if timeout:
145 self._execute_code(kc, '_M6CountdownTimer.cancel()')
146
147 return result if result.strip() else 'Finished execution.'
148
149 def __del__(self):
150 # Recycle the jupyter subprocess:

Callers

nothing calls this directly

Calls 6

_start_kernelMethod · 0.95
_execute_codeMethod · 0.95
extract_codeFunction · 0.90
splitMethod · 0.80

Tested by

no test coverage detected