Container Executor
Classes:
CodeBlock
Bases: BaseModel
A class that represents a code block.
Source code in tapeagents/tools/container_executor.py
307 308 309 310 311 |
|
CodeResult
Bases: BaseModel
A class that represents the result of a code execution.
Source code in tapeagents/tools/container_executor.py
314 315 316 317 318 319 |
|
CommandLineCodeResult
Bases: CodeResult
(Experimental) A code result class for command line code executor.
Source code in tapeagents/tools/container_executor.py
322 323 324 325 326 327 328 |
|
ContainerExecutor
Methods:
-
__init__
–(Experimental) A code executor class that executes code through
-
execute_code_blocks
–(Experimental) Execute the code blocks and return the result.
-
restart
–(Experimental) Restart the code executor.
-
stop
–(Experimental) Stop the code executor.
Attributes:
-
bind_dir
(Path
) –(Experimental) The binding directory for the code execution container.
-
timeout
(int
) –(Experimental) The timeout for code execution.
-
work_dir
(Path
) –(Experimental) The working directory for the code execution.
Source code in tapeagents/tools/container_executor.py
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 |
|
bind_dir: Path
property
(Experimental) The binding directory for the code execution container.
timeout: int
property
(Experimental) The timeout for code execution.
work_dir: Path
property
(Experimental) The working directory for the code execution.
__init__(image='python:3-slim', container_name=None, timeout=60, work_dir=Path('.'), bind_dir=None, auto_remove=True, stop_container=True, execution_policies=None)
(Experimental) A code executor class that executes code through a command line environment in a Docker container.
The executor first saves each code block in a file in the working directory, and then executes the code file in the container. The executor executes the code blocks in the order they are received. Currently, the executor only supports Python and shell scripts. For Python code, use the language "python" for the code block. For shell scripts, use the language "bash", "shell", or "sh" for the code block.
Parameters:
-
image
(_type_
, default:'python:3-slim'
) –Docker image to use for code execution. Defaults to "python:3-slim".
-
container_name
(Optional[str]
, default:None
) –Name of the Docker container which is created. If None, will autogenerate a name. Defaults to None.
-
timeout
(int
, default:60
) –The timeout for code execution. Defaults to 60.
-
work_dir
(Union[Path, str]
, default:Path('.')
) –The working directory for the code execution. Defaults to Path(".").
-
bind_dir
(Union[Path, str]
, default:None
) –The directory that will be bound to the code executor container. Useful for cases where you want to spawn the container from within a container. Defaults to work_dir.
-
auto_remove
(bool
, default:True
) –If true, will automatically remove the Docker container when it is stopped. Defaults to True.
-
stop_container
(bool
, default:True
) –If true, will automatically stop the container when stop is called, when the context manager exits or when the Python process exits with atext. Defaults to True.
Raises:
-
ValueError
–On argument error, or if the container fails to start.
Source code in tapeagents/tools/container_executor.py
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 |
|
execute_code_blocks(code_blocks)
(Experimental) Execute the code blocks and return the result.
Parameters:
-
code_blocks
(List[CodeBlock]
) –The code blocks to execute.
Returns:
-
CommandlineCodeResult
(CommandLineCodeResult
) –The result of the code execution.
Source code in tapeagents/tools/container_executor.py
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 |
|
restart()
(Experimental) Restart the code executor.
Source code in tapeagents/tools/container_executor.py
266 267 268 269 270 |
|
stop()
(Experimental) Stop the code executor.
Source code in tapeagents/tools/container_executor.py
272 273 274 |
|
extract_code_blocks(message)
(Experimental) Extract code blocks from a message. If no code blocks are found, return an empty list.
Parameters:
-
message
(str
) –The message to extract code blocks from.
Returns:
-
List[CodeBlock]
–List[CodeBlock]: The extracted code blocks or an empty list.
Source code in tapeagents/tools/container_executor.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
|
infer_lang(code)
infer the language for the code. TODO: make it robust.
Source code in tapeagents/tools/container_executor.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
|
silence_pip(code, lang)
Apply -qqq flag to pip install commands.
Source code in tapeagents/tools/container_executor.py
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
|