Files
langchain/langchain/utilities/bash.py
T
coyotespike b7bef36ee1 BashChain (#260)
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>
2022-12-06 21:57:50 -08:00

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}