mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-07-25 12:35:29 -04:00
93a091cfb8
This allows the LLM to correct its previous command by looking at the error message output to the shell. Additionally, this uses subprocess.run because that is now recommended over subprocess.check_output: https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module Co-authored-by: Amos Ng <me@amos.ng>
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
"""Wrapper around subprocess to run commands."""
|
|
import subprocess
|
|
from typing import List, Union
|
|
|
|
|
|
class BashProcess:
|
|
"""Executes bash commands and returns the output."""
|
|
|
|
def __init__(self, strip_newlines: bool = False, return_err_output: bool = False):
|
|
"""Initialize with stripping newlines."""
|
|
self.strip_newlines = strip_newlines
|
|
self.return_err_output = return_err_output
|
|
|
|
def run(self, commands: Union[str, List[str]]) -> str:
|
|
"""Run commands and return final output."""
|
|
if isinstance(commands, str):
|
|
commands = [commands]
|
|
commands = ";".join(commands)
|
|
try:
|
|
output = subprocess.run(
|
|
commands,
|
|
shell=True,
|
|
check=True,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
).stdout.decode()
|
|
except subprocess.CalledProcessError as error:
|
|
if self.return_err_output:
|
|
return error.stdout.decode()
|
|
return str(error)
|
|
if self.strip_newlines:
|
|
output = output.strip()
|
|
return output
|