mirror of
https://github.com/kennethreitz/langchain.git
synced 2026-06-05 23:00:18 +00:00
b7bef36ee1
Love the project, a ton of fun! I think the PR is pretty self-explanatory, happy to make any changes! I am working on using it in an `LLMBashChain` and may update as that progresses. Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
24 lines
802 B
Python
24 lines
802 B
Python
import subprocess
|
|
from typing import Dict, List, Union
|
|
|
|
class BashProcess:
|
|
"""Executes bash commands and returns the output."""
|
|
|
|
def __init__(self, strip_newlines: bool = False):
|
|
self.strip_newlines = strip_newlines
|
|
|
|
|
|
def run(self, commands: List[str]) -> Dict[str, Union[bool, list[str]]]:
|
|
outputs = []
|
|
for command in commands:
|
|
try:
|
|
output = subprocess.check_output(command, shell=True).decode()
|
|
if self.strip_newlines:
|
|
output = output.strip()
|
|
outputs.append(output)
|
|
except subprocess.CalledProcessError as error:
|
|
outputs.append(str(error))
|
|
return {"success": False, "outputs": outputs}
|
|
|
|
return {"success": True, "outputs": outputs}
|